#include//使用二維數組實現 掃雷 int main() { char ui[8][8]={ '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+', '+','+','+','+','+','+','+','+' }; int map[8][8]={ 0,0,0,0,0,0,0,0, 0,0,1,0,0,1,0,0, 0,0,0,0,1,0,0,0, 0,0,0,0,0,1,0,0, 0,0,1,0,0,0,0,0, 0,0,1,0,0,0,0,0, 0,1,0,1,1,0,0,0, 1,0,0,0,0,0,0,0 }; int p[8][2]={{-1,-1} ,{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; int i=0,j=0; int h=0,l=0; int h1=0,l1=0; int n=0;//用來保存 雷的數量 計數 int win=0; while(1) { printf(" 掃雷游戲\n"); for(i=0;i<8;i++) { for(j=0;j<8;j++) { printf("%c ",ui[i][j]); } printf("\n"); } printf("請輸入你要打開的位置 行 列:"); scanf("%d%d",&h,&l); if(map[h-1][l-1]==1) { printf("踩到雷 游戲結束\n"); break; } h=h-1; l=l-1; //沒有踩到雷的情況 判斷周圍有幾個雷 並把數字顯示在 界面上 //-1 -1 -1, 0 -1,+1 0 ,-1 0 ,+1 +1 ,-1 +1,0 +1 ,+1 //n=map[h-1][l-1]+map[h-1][l]+map[h-1][l+1]+map[h][l-1]+map[h][l+1]+map[h+1][l-1]+map[h+1][l+1]+map[h+1][l]; i=0; while(i<8) { n=0; h1=h; l1=l; h1= h1+p[i][0]; l1=l1+p[i][1]; if(h1>=0&&h1<8&&l1>=0&&l1<8) { if(map[h1][l1]==1) { n++; } } i++; } //把得到的數字顯示到 界面上 ui[h][l]; //把int數字轉換成 字符 switch(n) { case 0: ui[h][l]='0'; break; case 1: ui[h][l]='1'; break; case 2: ui[h][l]='2'; break; case 3: ui[h][l]='3'; break; case 4: ui[h][l]='4'; break; case 5: ui[h][l]='5'; break; case 6: ui[h][l]='6'; break; case 7: ui[h][l]='7'; break; case 8: ui[h][l]='8'; break; } win++; if(win==54) { printf("獲勝 游戲結束\n"); break; } } return 0; }