1. 程式人生 > 其它 >基於C語言控制檯輸出的掃雷遊戲2.0版

基於C語言控制檯輸出的掃雷遊戲2.0版

1.修改了開局踩雷的情況

2.修改了16*16和16*30棋盤無法對齊情況

game.h

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #define ROW 9 //9*9  16*16  16*30
 3 #define COL 9
 4 #define ROWS ROW+2
 5 #define COLS COL+2
 6 #define MINE 10  //10   40   99
 7 
 8 #include <stdio.h>
 9 #include <time.h>
10 #include <stdlib.h>
11 //初始化  引數(陣列 ;行;列;字元)
12 void InItBoard(char Board[ROWS][COLS],int row,int col,char set); 13 //列印 引數(陣列 ;行;列) 14 void PrintBoard(char Board[ROWS][COLS], int row, int col); 15 //埋雷 引數(陣列 ;行;列) 16 void SetMine(char Mine[ROWS][COLS],int row,int col); 17 //掃雷 引數(陣列 ;行;列) 18 void ClearMine(char Board[ROWS][COLS], char Mine[ROWS][COLS],int
row, int col); 19 //計算周圍的雷的個數 20 int SumMine(char Mine[ROWS][COLS], int x, int y); 21 //自動開啟 22 void AutoDisplay(char Board[ROWS][COLS], char Mine[ROWS][COLS], int x, int y);

test.c

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include "game.h"
 4 
 5 void menu()
 6 {
 7     printf("************************\n
"); 8 printf("***** 1.開始遊戲 ******\n"); 9 printf("***** 0.退出遊戲 ******\n"); 10 printf("************************\n"); 11 printf("請選擇>>\n"); 12 } 13 14 void game() 15 { 16 //建立陣列 17 //存雷的陣列 18 char mine[ROWS][COLS] = { 0 }; 19 //顯示面板的陣列 20 char show[ROWS][COLS] = { 0 }; 21 //初始化 22 InItBoard(mine, ROWS, COLS, '0'); 23 InItBoard(show, ROWS, COLS, '*'); 24 //列印面板 25 //PrintBoard(mine, ROW, COL); 26 PrintBoard(show, ROW, COL); 27 //初始點選必是空格,避免開局遇雷 28 ClearMine(show, mine, ROW, COL); 29 //PrintBoard(mine, ROW, COL); 30 //PrintBoard(show, ROW, COL); 31 //掃雷 32 //ClearMine(show,mine,ROW,COL); 33 } 34 int main() 35 { 36 int input = 0; 37 srand((unsigned int)time(NULL)); 38 do 39 { 40 menu(); 41 scanf("%d", &input); 42 switch (input) 43 { 44 case 1: 45 game(); 46 break; 47 case 0: 48 printf("退出遊戲\n"); 49 break; 50 default: 51 printf("選擇錯誤,請重新選擇\n"); 52 break; 53 } 54 55 } while (input); 56 return 0; 57 }

game.c

  1 #include "game.h"
  2 
  3 
  4 //初始化陣列
  5 void InItBoard(char Board[ROWS][COLS], int row, int col, char set)
  6 {
  7     int i, j;
  8     for  (i = 0; i < ROWS; i++)
  9     {
 10         for (j = 0; j < COLS; j++)
 11         {
 12             Board[i][j] = set;
 13         }
 14     }
 15 }
 16 
 17 //列印
 18 void PrintBoard(char Board[ROWS][COLS], int row, int col)
 19 {
 20     int i, j;
 21     for (i = 0; i <= COL;i++)
 22     {
 23         if (i<10)
 24         {
 25             printf("%d   ", i);
 26         }
 27         else
 28         {
 29             printf("%d  ",i);
 30         }
 31         
 32     }
 33     printf("\n");
 34     for (i = 1; i < ROWS-1; i++)
 35     {
 36         if (i<10)
 37         {
 38             printf("%d   ", i);
 39         }
 40         else
 41         {
 42             printf("%d  ", i);
 43         }
 44         for (j = 1; j < COLS-1; j++)
 45         {
 46             printf("%c   ", Board[i][j]);
 47         }
 48         printf("\n\n");
 49     }
 50 }
 51 
 52 //埋雷
 53 void SetMine(char Mine[ROWS][COLS], int x, int y)
 54 {
 55     int a, b,c,d, i = 0;
 56 
 57     for (c = x - 1; c <= x + 1; c++)
 58     {
 59         for (d = y - 1; d <= y + 1; d++)
 60         {
 61             Mine[c][d] = '2';
 62         }
 63     }
 64     while (i < MINE)
 65     {
 66         a = rand() % ROW + 1;
 67         b = rand() % ROW + 1;
 68 
 69         if (Mine[a][b] == '0')
 70         {
 71             Mine[a][b] = '1';
 72             i++;    
 73         }
 74     }
 75     for (c = x - 1; c <= x + 1; c++)
 76     {
 77         for (d = y - 1; d <= y + 1; d++)
 78         {
 79             Mine[c][d] = '0';
 80         }
 81     }
 82 }
 83 
 84 //搜尋周圍8個格子有幾個雷
 85 int SumMine(char Mine[ROWS][COLS], int x, int y)
 86 {
 87     return Mine[x - 1][y - 1] +
 88     Mine[x - 1][y] +
 89     Mine[x - 1][y + 1] +
 90     Mine[x][y + 1] +
 91     Mine[x + 1][y + 1] +
 92     Mine[x + 1][y] +
 93     Mine[x + 1][y - 1] +
 94     Mine[x][y - 1] - '0'*8;
 95 }
 96 
 97 //自動開啟
 98 void AutoDisplay(char Board[ROWS][COLS], char Mine[ROWS][COLS], int x, int y)
 99 {
100     int i, j;
101     if (Board[x][y] == '0'&&x > 0 && x<ROWS-1 && y>0 && y < COLS-1)
102     {
103         if (Board[x - 1][y] == '*')
104         {
105             Board[x - 1][y] = SumMine(Mine, x - 1, y) + '0';
106             AutoDisplay(Board, Mine, x-1, y);        
107         }
108         if(Board[x][y + 1] == '*')
109         {
110             Board[x][y + 1] = SumMine(Mine, x, y + 1) + '0';
111             AutoDisplay(Board, Mine, x, y + 1);
112         }
113         if (Board[x + 1][y] =='*' )
114         {
115             Board[x + 1][y] = SumMine(Mine, x + 1, y) + '0';
116             AutoDisplay(Board, Mine, x + 1, y);
117         }
118         if (Board[x][y - 1]=='*')
119         {
120             Board[x][y - 1] = SumMine(Mine, x, y - 1) + '0';
121             AutoDisplay(Board, Mine, x , y-1);
122         }
123         if (Board[x-1][y - 1] == '*')
124         {
125             Board[x-1][y - 1] = SumMine(Mine, x-1, y - 1) + '0';
126             AutoDisplay(Board, Mine, x-1, y - 1);
127         }
128         if (Board[x+1][y - 1] == '*')
129         {
130             Board[x+1][y - 1] = SumMine(Mine, x+1, y - 1) + '0';
131             AutoDisplay(Board, Mine, x+1, y - 1);
132         }
133         if (Board[x-1][y + 1] == '*')
134         {
135             Board[x-1][y + 1] = SumMine(Mine, x-1, y + 1) + '0';
136             AutoDisplay(Board, Mine, x-1, y + 1);
137         }
138         if (Board[x + 1][y + 1] == '*')
139         {
140             Board[x + 1][y + 1] = SumMine(Mine, x + 1, y + 1) + '0';
141             AutoDisplay(Board, Mine, x + 1, y + 1);
142         }
143     }
144     for (i = 0; i < ROWS; i++)
145     {
146         for (j = 0; j < COLS; j++)
147         {
148             if (Board[i][j] == '0')
149             {
150                 Board[i][j] = ' ';
151             }
152         }
153     }
154 }
155 //判斷輸贏
156 int IsWin(char Board[ROWS][COLS], int i, int j)
157 {
158     int ret=0;
159     for (i = 1; i < ROWS-1; i++)
160     {
161         for (j = 1; j < COLS-1; j++)
162         {
163             if (Board[i][j] == '*')
164             {
165                 ret++;
166             }
167         }
168     }
169     return ret;
170 }
171 //初始掃雷避免開局遇雷
172 void ClearMine(char Board[ROWS][COLS], char Mine[ROWS][COLS])
173 {
174     int x, y, ret, win;
175     printf("請輸入座標>>\n");
176     scanf("%d %d", &x, &y);
177     if (x > 0 && x <= ROW && y > 0 && y <= COL)
178     {
179         
180         SetMine(Mine, x, y);
181         ret = SumMine(Mine, x, y);
182         Board[x][y] = ret + '0';
183         AutoDisplay(Board, Mine, x, y);
184         //PrintBoard(Mine, ROW, COL);
185         PrintBoard(Board, ROW, COL);
186         win = IsWin(Board, x, y);
187         if (win == MINE)
188         {
189             printf("恭喜你贏了!");
190             return;
191         }
192         while (1)
193         {
194             printf("請輸入座標>>\n");
195             scanf("%d %d", &x, &y);
196             if (x > 0 && x <= ROW && y > 0 && y <= COL)
197             {
198                 if (Mine[x][y] == '1')
199                 {
200                     printf("你輸了!\n");
201                     PrintBoard(Mine, ROW, COL);
202                     break;
203                 }
204                 else
205                 {
206                     ret = SumMine(Mine, x, y);
207                     Board[x][y] = ret + '0';
208                     AutoDisplay(Board, Mine, x, y);
209                     PrintBoard(Board, ROW, COL);
210                     win = IsWin(Board, x, y);
211                     if (win == MINE)
212                     {
213                         printf("恭喜你贏了!");
214                         break;
215                     }
216                 }
217             }
218             else
219             {
220                 printf("輸入的引數不合法,請重新輸入");
221             }
222         }
223 
224     }
225     else
226     {
227         printf("輸入的引數不合法,請重新輸入");
228     }
229 
230 }