版本比较
LegendaryMiracle's小游戏合集
正文
增加 282 行删除 0 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286718 行未改动全部展开# 迷宫```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 using718 行未改动全部展开