版本比较
LegendaryMiracle's小游戏合集
正文
增加 521 行删除 30 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286156 行未改动全部展开44344444544644744844945045145245345445545645745845946046146246346446546646773 行未改动5415425435442535455465475485498 行未改动55855956056127056256356456527456656756856957057157228157357457557628557757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660729360860961061129761261361461530161661761861930562062162262330962462562662731362862963063131763263363463532163663763863932564064164264332964464564664733364864965065133765265365465534165665765865934566066166266334966466566666766837 行未改动70670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685739885885986086186286386486586686786886930 行未改动90090190290344390490590690790816 行未改动92592692792892993093193293393493593693793893994094194294347494494594694794815 行未改动96496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210035065075085095105115121004# 迷宫```cpp line-numbers#include<bits/stdc++.h>using namespace std;int board[101][101],n,mode,x,y,field = 100;bool came[101][101],dark;stack<pair<int,int>>r;vector<pair<int,int>>route;void jump(){ for(int i = 0;i < 100;i++) { cout << "\n"; }}void print(bool cheat){ int a,b,c,d; if(field == 100) { a = c = 50 - (n - 1) / 2 - 1; b = d = 50 + (n - 1) / 2 + 1; } else { a = x - field; b = x + field; c = y - field; d = y + field; } for(int i = a;i <= b;i++) { for(int j = c;j <= d;j++) { if(i == x && j == y) { cout << "\033[32m你\033[0m"; continue; } if(board[i][j] == 3) { cout << "\033[34m!!\033[0m"; continue; } cout << (board[i][j] == 1?"墙":(cheat && board[i][j] == 100?"\033[32m路\033[0m":" ")); } cout << "\n"; }}int white(int x,int y){ return (board[x - 1][y] != 1 ? 1 : 0) + (board[x + 1][y] != 1 ? 1 : 0) + (board[x][y - 1] != 1 ? 1 : 0) + (board[x][y + 1] != 1 ? 1 : 0);}bool in_range(int x,int y){ return (abs(x - 50) <= (n - 1) / 2 && abs(y - 50) <= (n - 1) / 2);}void dfs(int x,int y,bool spj){ r.push({x,y}); if(x == 50 && y == 50) { came[x][y] = 1; return; } else if(!in_range(x,y) || came[x][y] && !spj || white(x,y) > 1) { came[x][y] = 1; do { r.pop(); x = r.top().first; y = r.top().second; board[x][y] = 1; } while((!in_range(x,y + 1) || came[x][y + 1]) && (!in_range(x,y - 1) || came[x][y - 1]) && (!in_range(x + 1,y) || came[x + 1][y]) && (!in_range(x - 1,y) || came[x - 1][y])); dfs(x,y,1); return; } board[x][y] = 100; came[x][y] = 1; int r = rand() % 100 + 1; if(r <= 25) { dfs(x,y + 1,0); } else if(r <= 50) { dfs(x + 1,y,0); } else if(r <= 75) { dfs(x - 1,y,0); } else { dfs(x,y - 1,0); }}void dfs2(int x,int y,bool spj){ r.push({x,y}); if(white(x,y)) { board[x][y] = 2; came[x][y] = 1; return; } else if(!in_range(x,y) || came[x][y] && !spj) { came[x][y] = 1; do { r.pop(); x = r.top().first; y = r.top().second; board[x][y] = 1; } while((!in_range(x,y + 1) || came[x][y + 1]) && (!in_range(x,y - 1) || came[x][y - 1]) && (!in_range(x + 1,y) || came[x + 1][y]) && (!in_range(x - 1,y) || came[x - 1][y])); dfs2(x,y,1); return; } board[x][y] = 2; came[x][y] = 1; int r = rand() % 100 + 1; if(r <= 25) { dfs2(x,y + 1,0); } else if(r <= 50) { dfs2(x + 1,y,0); } else if(r <= 75) { dfs2(x - 1,y,0); } else { dfs2(x,y - 1,0); }}int main(){ srand(time(0)); cout << "欢迎游玩迷宫小游戏!\n请选择迷宫大小:\n1:11*11 2:33*33 3:55*55 其他:退出\n"; cin >> n; if(n == 1 || n == 2 || n == 3) { n = (n * 2 - 1) * 11; } else { return 0; } cout << "请选择模式:\n1:大地图模式 2:范围模式 3:视野模式 4:黑夜模式\n"; cin >> mode; while(mode < 1 || mode > 4) { cout << "输入不合法,请重新输入:\n"; cin >> mode; } if(mode == 2 || mode == 4) { cout << "请选择视野:\n1:1格 2:2格 3:3格 4:5格\n"; cin >> field; while(field < 1 || field > 4) { cout << "输入不合法,请重新输入:\n"; cin >> field; } if(field == 4) { field++; } } else { field = 100; } if(mode >= 3) { dark = 1; } for(int i = 0;i < 101;i++) { for(int j = 0;j < 101;j++) { board[i][j] = 1; } } dfs(50 - (n - 1) / 2,50 - (n - 1) / 2,0); while(!r.empty()) { route.push_back(r.top()); r.pop(); } while(1) { int x,y,t = 0; do { x = rand() % n + (50 - (n - 1) / 2); y = rand() % n + (50 - (n - 1) / 2); t++; if(t > 1000) { break; } } while(board[x][y] == 0 || white(x,y)); if(t > 1000) { break; } memset(came,0,sizeof(came)); dfs2(x,y,0); for(int i = 50 - (n - 1) / 2;i <= 50 + (n - 1) / 2;i++) { for(int j = 50 - (n - 1) / 2;j <= 50 + (n - 1) / 2;j++) { if(board[i][j] == 2) { board[i][j] = 0; } } } } board[50 - (n - 1) / 2][50 - (n - 1) / 2] = 3; board[50][50] = 3; x = 50 - (n - 1) / 2; y = 50 - (n - 1) / 2; char move; int xd,yd; while(!(x == 50 && y == 50)) { xd = x; yd = y; jump(); print(0); cin >> move; if(move == 'w') { x--; } else if(move == 'a') { y--; } else if(move == 's') { x++; } else if(move == 'd') { y++; } else if(move == 'q') { field = 100; dark = 0; jump(); cout << "你输了!\n完整地图:\n"; print(1); return 0; } if(board[x][y] == 1) { x = xd; y = yd; } } jump(); cout << "赢辣!\n完整地图:\n"; x = 0; y = 0; field = 100; dark = 0; print(1); return 0;}```# 扫雷```cpp line-numbers#include<bits/stdc++.h>//扫雷小游戏#define Made using156 行未改动全部展开 return 0;}```# 中国象棋$\tiny\color{FFFFFF}注:此文作者为洛谷UID为1557225的用户(LegendaryMiracle)。本文仅仅出现在洛谷的我的主页上,若发现出现在其他地方,欢迎告诉我$```cpp line-numbers#include<bits/stdc++.h>//中国象棋小游戏#define Made using#define by namespace#define LegendaryMiracle stdMade by LegendaryMiracle;int board[10][9] = {};//空为0,将为±1,士为±2,象为±3,马为±4,炮为±5,车为±6,卒为±7,其中我方棋子为正数,对方棋子为负数int reach[10][9];vector<pair<int,int>>dissed;void jump(){ for(int i = 0;i < 100;i++) { cout << "\n"; }}bool moveable(int x01,int y01,int x02,int y02){ if(x01 == x02 && y01 == y02 || x01 < 0 || y01 < 0 || x01 >= 10 || y01 >= 9 || x02 < 0 || y02 < 0 || x02 >= 10 || y02 >= 9 || board[x01][y01] == 0 || (board[x02][y02] != 0 && (board[x01][y01] > 0) == (board[x02][y02] > 0))) {73 行未改动 } } else if(abs(board[x01][y01]) == 4) { return (x01 + 2 < 0 && y01 + 1 < 0 && x01 + 2 >= 10 && y01 + 1 >= 9 && x02 == x01 + 2 && y02 == y01 + 1 && board[x01 + 1][x02] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0 || x01 + 1 < 0 && y01 + 2 < 0 && x01 + 1 >= 10 && y01 + 2 >= 9 && x02 == x01 + 1 && y02 == y01 + 2 && board[x01][x02 + 1] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0 || x01 - 2 < 0 && y01 + 1 < 0 && x01 - 2 >= 10 && y01 + 1 >= 9 && x02 == x01 - 2 && y02 == y01 + 1 && board[x01 - 1][x02] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0 || x01 - 1 < 0 && y01 + 2 < 0 && x01 - 1 >= 10 && y01 + 2 >= 9 && x02 == x01 - 1 && y02 == y01 + 2 && board[x01][x02 + 1] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0 || x01 + 2 < 0 && y01 - 1 < 0 && x01 + 2 >= 10 && y01 - 1 >= 9 && x02 == x01 + 2 && y02 == y01 - 1 && board[x01 + 1][x02] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0 || x01 + 1 < 0 && y01 - 2 < 0 && x01 + 1 >= 10 && y01 - 2 >= 9 && x02 == x01 + 1 && y02 == y01 - 2 && board[x01][x02 - 1] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0 || x01 - 2 < 0 && y01 - 1 < 0 && x01 - 2 >= 10 && y01 - 1 >= 9 && x02 == x01 - 2 && y02 == y01 - 1 && board[x01 - 1][x02] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0 || x01 - 1 < 0 && y01 - 2 < 0 && x01 - 1 >= 10 && y01 - 2 >= 9 && x02 == x01 - 1 && y02 == y01 - 2 && board[x01][x02 - 1] && x02 < 0 && y02 < 0 && x02 >= 10 && y02 >= 9 == 0); return !(!(abs(x02 - x01) == 2 && abs(y02 - y01) == 1 || abs(x02 - x01) == 1 && abs(y02 - y01) == 2) || (abs(x02 - x01) == 2 && board[(x01 + x02) / 2][y01] != 0) || (abs(y02 - y01) == 2 && board[x01][(y01 + y02) / 2] != 0)); } else if(abs(board[x01][y01]) == 3) { if(board[x01][y01] > 0)8 行未改动 else if(abs(board[x01][y01]) == 2) { if(board[x01][y01] > 0) { return (x02 <= 2 && y02 <= 5 && y02 >= 3 && (y02 - 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 + 1 == x01 || y02 - 1 == y01 && x02 + 1 == x01)); return (x02 >= 7 && y02 <= 5 && y02 >= 3 && (y02 - 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 + 1 == x01 || y02 - 1 == y01 && x02 + 1 == x01)); } else { return (x02 >= 7 && y02 <= 5 && y02 >= 3 && (y02 - 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 + 1 == x01 || y02 - 1 == y01 && x02 + 1 == x01)); return (x02 <= 2 && y02 <= 5 && y02 >= 3 && (y02 - 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 - 1 == x01 || y02 + 1 == y01 && x02 + 1 == x01 || y02 - 1 == y01 && x02 + 1 == x01)); } } else { if(board[x01][y01] > 0) { return (x02 <= 2 && y02 <= 5 && y02 >= 3 && (y02 - 1 == y01 && x02 == x01 || y02 + 1 == y01 && x02 == x01 || y02 == y01 && x02 - 1 == x01 || y02 == y01 && x02 + 1 == x01)); return (x02 >= 7 && y02 <= 5 && y02 >= 3 && (y02 - 1 == y01 && x02 == x01 || y02 + 1 == y01 && x02 == x01 || y02 == y01 && x02 - 1 == x01 || y02 == y01 && x02 + 1 == x01)); } else { return (x02 >= 7 && y02 <= 5 && y02 >= 3 && (y02 - 1 == y01 && x02 == x01 || y02 + 1 == y01 && x02 == x01 || y02 == y01 && x02 - 1 == x01 || y02 == y01 && x02 + 1 == x01)); return (x02 <= 2 && y02 <= 5 && y02 >= 3 && (y02 == y01 - 1 && x02 == x01 || y02 == y01 + 1 && x02 == x01 || y02 == y01 && x02 == x01 - 1 || y02 == y01 && x02 == x01 + 1)); } }}void gen(){ memset(reach,0,sizeof(reach)); for(int i = 0;i < 10;i++) { for(int j = 0;j < 9;j++) { if(board[i][j] > 0) { for(int i1 = 0;i1 < 10;i1++) { for(int j1 = 0;j1 < 9;j1++) { if(moveable(i,j,i1,j1)) { reach[i1][j1]++; } } } } } }}void character(int n){ if(n == 0) { cout << " "; cout << "\033[37m "; } else if(n == 1) { cout << "\033[34m" << "将" << "\033[37m"; cout << "\033[34m将\033[37m"; } else if(n == 2) { cout << "\033[34m" << "士" << "\033[37m"; cout << "\033[34m士\033[37m"; } else if(n == 3) { cout << "\033[34m" << "象" << "\033[37m"; cout << "\033[34m象\033[37m"; } else if(n == 4) { cout << "\033[34m" << "马" << "\033[37m"; cout << "\033[34m马\033[37m"; } else if(n == 5) { cout << "\033[34m" << "炮" << "\033[37m"; cout << "\033[34m炮\033[37m"; } else if(n == 6) { cout << "\033[34m" << "车" << "\033[37m"; cout << "\033[34m车\033[37m"; } else if(n == 7) { cout << "\033[34m" << "卒" << "\033[37m"; cout << "\033[34m卒\033[37m"; } else if(n == -1) { cout << "\033[31m" << "帅" << "\033[37m"; cout << "\033[31m帅\033[37m"; } else if(n == -2) { cout << "\033[31m" << "仕" << "\033[37m"; cout << "\033[31m仕\033[37m"; } else if(n == -3) { cout << "\033[31m" << "相" << "\033[37m"; cout << "\033[31m相\033[37m"; } else if(n == -4) { cout << "\033[31m" << "马" << "\033[37m"; cout << "\033[31m马\033[37m"; } else if(n == -5) { cout << "\033[31m" << "炮" << "\033[37m"; cout << "\033[31m炮\033[37m"; } else if(n == -6) { cout << "\033[31m" << "车" << "\033[37m"; cout << "\033[31m车\033[37m"; } else if(n == -7) { cout << "\033[31m" << "兵" << "\033[37m"; cout << "\033[31m兵\033[37m"; }}void print(){37 行未改动 cout << " " << i; } cout << "\n";}int ai_x01,ai_y01,ai_x02,ai_y02,mx,sc,mxmn;void ai(){ gen(); mx = -2147483648; for(int i = 0;i < 10;i++) { for(int j = 0;j < 9;j++) { if(board[i][j] < 0) { for(int i1 = 0;i1 < 10;i1++) { for(int j1 = 0;j1 < 9;j1++) { if(moveable(i,j,i1,j1)) { //因为board的数值和分值不同,要进行一些调整 if(board[i1][j1] == 1) { sc = 114514; } else if(board[i1][j1] == 2) { sc = 1; } else if(board[i1][j1] == 7) { sc = 2; } else { sc = board[i1][j1]; } if(board[i1][j1] > 0) { for(int i2 = 0;i2 < 10;i2++) { for(int j2 = 0;j2 < 9;j2++) { if(moveable(i1,j1,i2,j2)) { reach[i2][j2]--; dissed.push_back({i2,j2}); } } } } mxmn = 0; for(int i2 = 0;i2 < 10;i2++) { for(int j2 = 0;j2 < 9;j2++) { if(reach[i2][j2] > 0 && board[i2][j2] < 0) { //因为board的数值和分值不同,要进行一些调整 if(board[i2][j2] == -1) { mxmn = max(mxmn,114514); } else if(board[i2][j2] == -2) { mxmn = max(mxmn,1); } else if(board[i2][j2] == -7) { mxmn = max(mxmn,2); } else { mxmn = max(mxmn,abs(board[i2][j2])); } } } } sc -= mxmn; if(sc > mx) { mx = sc; ai_x01 = i; ai_y01 = j; ai_x02 = i1; ai_y02 = j1; } for(int ii = 0;ii < dissed.size();ii++) { reach[dissed[ii].first][dissed[ii].second]++; } dissed.clear(); } } } } } } if(mx <= 0) { int tx = -1,ty = -1,jx,jy,fx = -1,fy = -1; for(int i = 0;i < 10;i++) { for(int j = 0;j < 9;j++) { if(board[i][j] == -6 || board[i][j] == -5 || board[i][j] == -4) { tx = i; ty = j; break; } } if(tx != -1) { break; } } for(int i = 0;i < 10;i++) { for(int j = 0;j < 9;j++) { if(board[i][j] == 1) { jx = i; jy = j; } } } for(int i = 0;i < 10;i++) { for(int j = 0;j < 9;j++) { if(moveable(tx,ty,i,j) && !reach[i][j] && abs(i - jx) + abs(j - jy) < abs(fx - jx) + abs(fy - jy)) { fx = i; fy = j; } } } if(fx != -1) { ai_x01 = tx; ai_y01 = ty; ai_x02 = fx; ai_y02 = fy; } }}int main(){ int mode; cout << "\033[37m" << "欢迎游玩中国象棋1.0!\n请选择模式,双人对战输入1,AI对手输入2:"; cout << "\033[37m欢迎游玩中国象棋1.0!\n请选择模式,双人对战输入1,AI对手输入2:"; cin >> mode; while(mode < 1 || mode > 2) { cout << "输入不合法,请重新输入:"; cin >> mode; } jump(); cout << "初始棋盘:\n"; //这里设置初始状态 board[9][0] = 6; board[9][1] = 4;30 行未改动 print(); int x01,y01,x02,y02; while(1) { cout << "\033[34m" << "蓝方" << "\033[37m" << ":\n请输入你要移动的棋子的x,y坐标:"; cout << "\033[34m蓝方\033[37m:\n请输入你要移动的棋子的x,y坐标:"; cin >> x01 >> y01; x01--; y01--; while(x01 < 0 || y01 < 0 || x01 >= 10 || y01 >= 9 || board[x01][y01] <= 0)16 行未改动 cin >> x02 >> y02; x02--; y02--; } jump(); if(board[x02][y02] == -1) { board[x02][y02] = board[x01][y01]; board[x01][y01] = 0; cout << "恭喜\033[34m蓝\033[37m方胜利!\n最终棋盘:\n"; print(); return 0; } board[x02][y02] = board[x01][y01]; board[x01][y01] = 0; cout << "移动成功!\n目前棋盘:\n"; print(); if(mode == 1) { cout << "\033[31m" << "红方" << "\033[37m" << ":\n请输入你要移动的棋子的x,y坐标:"; cout << "\033[31m红方\033[37m:\n请输入你要移动的棋子的x,y坐标:"; cin >> x01 >> y01; x01--; y01--; while(x01 < 0 || y01 < 0 || x01 >= 10 || y01 >= 9 || board[x01][y01] >= 0)15 行未改动 cin >> x02 >> y02; x02--; y02--; } jump(); if(board[x02][y02] == 1) { board[x02][y02] = board[x01][y01]; board[x01][y01] = 0; cout << "恭喜\033[31m红\033[37m方胜利!\n最终棋盘:\n"; print(); return 0; } board[x02][y02] = board[x01][y01]; board[x01][y01] = 0; cout << "移动成功!\n目前棋盘:\n"; print(); } else { ai(); jump(); if(board[ai_x02][ai_y02] == 1) { board[ai_x02][ai_y02] = board[ai_x01][ai_y01]; board[ai_x01][ai_y01] = 0; cout << "恭喜\033[31m红\033[37m方胜利!\n最终棋盘:\n"; print(); return 0; } cout << "\033[31m红方\033[37m移动完毕,把(" << ai_x01 + 1 << "," << ai_y01 + 1 << ")的\033[31m"; character(board[ai_x01][ai_y01]); cout << "\033[37m挪到了(" << ai_x02 + 1 << "," << ai_y02 + 1 << ")\n目前棋盘:\n"; board[ai_x02][ai_y02] = board[ai_x01][ai_y01]; board[ai_x01][ai_y01] = 0; print(); } } return 0;}/*todo:完整基础移动兵的左右移动AI功能对将*/```