1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| #include<bits/stdc++.h> #include<iostream> #include<stdlib.h> #include<conio.h> #include<windows.h> #include<time.h>
using namespace std; const int N = 100;
int n, m, k, cnt, mp[N][N]; bool boom[N][N], vis[N][N]; int q[8][2] = {{1, 1},{1, 0},{1, -1},{-1, 1},{-1, 0},{-1, -1},{0, 1},{0, -1}};
void err() { printf("What are you nong sha lie?????\n"); system("pause"); exit(0); }
bool mineChecker(int x, int y, int sx, int sy) { if (n * m - k < 9) return true; for (int i = 0; i < 8; i++) { int nx = sx + q[i][0]; int ny = sy + q[i][1]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) { if (x == nx && y == ny) return false; } } return true; }
void mineCreater(int sx, int sy) { for (int i = 1; i <= k; i++) { sta:; int x = rand() % n + 1; int y = rand() % m + 1; if (boom[x][y] || !mineChecker(x, y, sx, sy) || (x == sx && y == sy)) goto sta; boom[x][y] = true; } }
void findBorder(int x, int y) { if (!vis[x][y]) cnt++; vis[x][y] = 1; if (mp[x][y] != 0) return; for (int i = 0; i < 8; i++) { int nx = x + q[i][0]; int ny = y + q[i][1]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) { if (!boom[nx][ny]) { if (!vis[nx][ny]) findBorder(nx, ny); } } } }
void mapOutput() { system("cls"); printf("bomb number: %d\n\n", k); cout << " "; for (int i = 1; i <= m; i++) { if (i >= 10) printf("%d ", i); else printf("%d ", i); } cout << endl; cout << endl; for (int i = 1; i <= n; i++) { if (i >= 10) printf("%d ", i); else printf("%d ", i); for (int j = 1; j <= m; j++) { if (vis[i][j]) { if (boom[i][j]) printf("* "); else if (mp[i][j] != 0) printf("%d ", mp[i][j]); else printf(" "); } else { printf("# "); } } printf("\n"); } }
void mapInit() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (boom[i][j]) continue; for (int l = 0; l < 8; l++) { int nx = i + q[l][0]; int ny = j + q[l][1]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) { mp[i][j] += boom[nx][ny]; } } } } }
void play() { memset(mp,0,sizeof(mp)); memset(vis,0,sizeof(vis)); cout << "请输入行数:" << endl; cin >> n; cout << "请输入列数:" << endl; cin >> m; cout << "请输入雷数:" << endl; cin >> k; if (k + 8 >= n * m) err();
mapOutput();
int x, y; cin >> x >> y; if (!(x >= 1 && x <= n && y >= 1 && y <= m)) err(); mineCreater(x, y); mapInit(); while (true) { findBorder(x, y); mapOutput(); if (cnt >= n * m - k) { printf("You win\n"); system("pause"); break; } cin >> x >> y; if (!(x >= 1 && x <= n && y >= 1 && y <= m)) err(); if (boom[x][y]) { system("cls"); printf("bomb number: %d\n\n", k); cout << " "; for (int i = 1; i <= m; i++) printf("%d ", i); cout << endl; cout << endl; for (int i = 1; i <= n; i++) { if (i >= 10) cout << i << " "; else cout << i << " "; for (int j = 1; j <= m; j++) { if (boom[i][j]) printf("* "); else printf(" "); } printf("\n"); } printf("boom!!!\n"); system("pause"); break; } } }
void rule() { cout << "这是一个扫雷游戏。\n\n你的任务是在地图内找出所有的地雷。\n\n"; cout<< "游戏的基本玩法是输入坐标。\n\n"; cout << "在游戏中,地图会显示成一个由数字、空格或井号(#)组成的点阵,每行每列旁边有序号方便确定坐标。\n\n"; cout<<"例:\n\n"; cout<<" 1 2 3 4 5 6 7"<<endl; cout<<" "<<endl; cout<<"1 # # # 2 1 #"<<endl; cout<<"2 # # # 2 1 1"<<endl; cout<<"3 # # # 2 "<<endl; cout<<"4 # # # 1 "<<endl; cout<<"5 # # # 1 1 2 2"<<endl; cout<<"6 # # # # # # #"<<endl; cout<<"7 # # # # # # #"<<endl; cout<<"\n其中各字符含义如下:\n\n"; cout << "1.\t数字代表这个格子周围(即上下左右、左上左下、右上右下八个格子)地雷的数量;\n\n"; cout << "2.\t空格代表这个格子周围没有地雷;\n\n"; cout << "3.\t星号代表这个格子的情况未知。\n\n"; cout << "开始时"; cout << "如果你认为一个格子安全,输入它的坐标。先输输入纵坐标,再输入横坐标,中间用空格分开,最后按回车确认。\n\n"; system("pause"); }
void dataExport(){
}
void dataImport(){
}
void logoOutput(){ cout<<" __ __ _ ____"<<endl; cout<<"| \\/ (_)_ __ ___/ ___|_ _____ ___ _ __ ___ _ __"<<endl; cout<<"| |\\/| | | '_ \\ / _ \\___ \\ \\ /\\ / / _ \\/ _ \\ '_ \\ / _ \\ '__|"<<endl; cout<<"| | | | | | | | __/___) \\ V V / __/ __/ |_) | __/ |"<<endl; cout<<"|_| |_|_|_| |_|\\___|____/ \\_/\\_/ \\___|\\___| .__/ \\___|_|"<<endl; cout<<" |_|"<<endl; } void about(){
} int main() { logoOutput(); cout << "请将本程序放大,以获得最佳体验。\n\n"; cout << "请切换到英文输入法。\n\n"; cout << "按r快速开始游戏,按其他键进入主菜单"; if(getch()=='r'){ play(); } srand(time(0)); system("cls"); srand((int) time(0)); while (true) { logoOutput(); cout << "<0>\t开始游戏\n\n<1>\t游戏规则\n\n<2>\t导入存档\n\n<3>\t退出游戏\n\n<4>\t关于游戏"; cout << "\n\n若要进入某一菜单,请键入该菜单对应的数字。\n\n注意!!!第一次玩前请务必查看游戏规则!!!"; switch (getch()) { case '0': system("cls"); play(); break; case '1': system("cls"); rule(); break; case '2': system("cls"); dataImport(); break; case '3': system("cls"); cout << "真的要退出吗?(y/其他按键)"; if(getch()=='y'){ goto L1; } break; case '4': system("cls"); about(); break; default: cout << "\n\n\n无效输入。"; Sleep(1000); break; } system("cls"); } L1: cout << "\n\n下次再来!"; return 0; }
|