[C++项目]2048控制台游戏

  1 #include <iostream>
  2 #include <windows.h>
  3 #include <ctime>
  4 using namespace std;
  5  
  6 int const ROW = 4;
  7 int const COL = 4;
  8 int game[ROW][COL] = {0};
  9  
 10 //上下左右
 11 int const UP = 1;
 12 int const DOWN = 2;
 13 int const LEFT = 3;
 14 int const RIGHT = 4;
 15  
 16 //游戏所处的状态
 17 int const GAME_OVER = 1;
 18 int const GAME_WIN = 2;
 19 int const GAME_CONTINUE = 3;
 20  
 21 enum GameNum
 22 {
 23     Game_2 = 2,
 24     Game_4 = 4,
 25     Game_8 = 8,
 26     Game_16 = 16,
 27     Game_32 = 32,
 28     Game_64 = 64,
 29     Game_128 = 128,
 30     Game_256 = 256,
 31     Game_512 = 512,
 32     Game_1024 = 1024,
 33     Game_2048 = 2048,
 34 };
 35  
 36 //打印所得的数组
 37 void Print()
 38 {
 39     system("cls");
 40     cout << "*****************  2048 控 制 台 版 ******************" << endl;
 41     cout << "*****************  By Linxu ******************" << endl << endl;
 42     for (int i = 0; i < ROW; ++i)
 43     {
 44         cout << "================================"<< endl;
 45         for (int j = 0; j < COL; ++j)
 46         {
 47             if (game[i][j] == 0)
 48             {
 49                 cout <<"|   \t";
 50             }
 51             else 
 52             {
 53                 cout <<"|   " << game[i][j] << "\t";
 54             }
 55         }
 56         cout << "|" << endl;
 57     }
 58     cout << "================================"<< endl;
 59     cout << "【操作说明】:"<< endl;
 60     cout << "请使用按照↑↓←→控制移动方向" << endl << endl;
 61 }
 62  
 63  
 64 bool CreateNumber()
 65 {
 66     int x = -1;
 67     int y = -1;
 68     int times = 0;
 69     int maxTimes = ROW * COL;
 70     //三分之二的概率生成2,三分之一的概率生成4
 71     int whitch = rand() % 3;
 72     do 
 73     {
 74         x = rand() % ROW;
 75         y = rand() % COL;
 76         ++times;
 77     } while (game[x][y] != 0 && times <= maxTimes);
 78  
 79     //说明格子已经满了
 80     if(times >= maxTimes)
 81     {
 82         return false;
 83     }
 84     else
 85     {
 86         GameNum num;
 87         if(whitch == 0)
 88         {
 89             num = Game_4;
 90         }
 91         else if(whitch)
 92         {
 93             num = Game_2;
 94         }
 95         game[x][y] = num;
 96     }
 97  
 98     return true;
 99 }
100  
101 void Process(int direction)
102 {
103     switch (direction)
104     {
105     case UP:
106         //最上面一行不动
107         for(int row = 1; row < ROW; ++row)
108         {
109             for(int crow = row; crow >= 1; --crow)
110             {
111                 for(int col = 0; col < COL; ++col)
112                 {
113                     //上一个格子为空
114                     if(game[crow-1][col] == 0)
115                     {
116                         game[crow-1][col] = game[crow][col];
117                         game[crow][col] = 0;
118                     }
119                     else 
120                     {
121                         //合并
122                         if(game[crow-1][col] == game[crow][col])
123                         {
124                             game[crow - 1][col] *= 2;
125                             game[crow][col] = 0;
126                         }
127  
128                     }
129                 }
130             }
131         }
132         break;
133     case DOWN:
134         //最下面一行不动
135         for(int row = ROW - 2; row >= 0; --row)
136         {
137             for(int crow = row; crow < ROW - 1; ++crow)
138             {
139                 for(int col = 0; col < COL; ++col)
140                 {
141                     //上一个格子为空
142                     if(game[crow + 1][col] == 0)
143                     {
144                         game[crow + 1][col] = game[crow][col];
145                         game[crow][col] = 0;
146                     }
147                     else 
148                     {
149                         //合并
150                         if(game[crow + 1][col] == game[crow][col])
151                         {
152                             game[crow + 1][col] *= 2;
153                             game[crow][col] = 0;
154                         }
155  
156                     }
157                 }
158             }
159         }
160         break;
161     case LEFT:
162         //最左边一列不动
163         for(int  col = 1; col < COL; ++col)
164         {
165             for(int ccol = col; ccol >= 1; --ccol)
166             {
167                 for(int row = 0; row < ROW; ++row)
168                 {
169                     //上一个格子为空
170                     if(game[row][ccol-1] == 0)
171                     {
172                         game[row][ccol - 1] = game[row][ccol];
173                         game[row][ccol] = 0;
174                     }
175                     else 
176                     {
177                         //合并
178                         if(game[row][ccol - 1] == game[row][ccol])
179                         {
180                             game[row][ccol - 1] *= 2;
181                             game[row][ccol] = 0;
182                         }
183  
184                     }
185                 }
186             }
187         }
188         break;
189     case RIGHT:
190         //最右边一列不动
191         for(int  col = COL - 2; col >= 0; --col)
192         {
193             for(int ccol = col; ccol <= COL - 2; ++ccol)
194             {
195                 for(int row = 0; row < ROW; ++row)
196                 {
197                     //上一个格子为空
198                     if(game[row][ccol + 1] == 0)
199                     {
200                         game[row][ccol + 1] = game[row][ccol];
201                         game[row][ccol] = 0;
202                     }
203                     else 
204                     {
205                         //合并
206                         if(game[row][ccol + 1] == game[row][ccol])
207                         {
208                             game[row][ccol + 1] *= 2;
209                             game[row][ccol] = 0;
210                         }
211  
212                     }
213                 }
214             }
215         }
216         break;
217     }
218  
219 }
220  
221 //处理输入输出,返回上下左右
222 int Input()
223 {
224     //读取上下左右四个方向键
225     int upArrow = 0;
226     int downArrow = 0;
227     int leftArrow = 0;
228     int rightArrow = 0;
229     int direction = 0;
230     while (true)
231     {
232         upArrow = GetAsyncKeyState(VK_UP);
233         downArrow = GetAsyncKeyState(VK_DOWN);
234         leftArrow = GetAsyncKeyState(VK_LEFT);
235         rightArrow = GetAsyncKeyState(VK_RIGHT);
236  
237         if(upArrow)
238         {
239             direction = UP;
240             break;
241         }
242         else if(downArrow)
243         {
244             direction = DOWN;
245             break;
246         }
247         else if(leftArrow)
248         {
249             direction = LEFT;
250             break;
251         }
252         else if(rightArrow)
253         {
254             direction = RIGHT;
255             break;
256         }
257  
258         Sleep(100);
259     }
260  
261     return direction;
262 }
263  
264 //判断游戏状态
265 int Judge()
266 {
267     //赢得游戏
268     for(int i = 0; i < ROW; ++i)
269     {
270         for(int j = 0; j < COL; ++j)
271         {
272             if(game[i][j] == 2048)
273             {
274                 return GAME_WIN;
275                 break;
276             }
277         }
278     }
279  
280     //横向检查
281     for(int i = 0 ; i < ROW; ++i)
282     {
283         for(int j = 0; j < COL - 1; ++j)
284         {
285             if(!game[i][j] || (game[i][j] == game[i][j+1]))
286             {
287                 return GAME_CONTINUE;
288                 break;
289             }
290         }
291     }
292     //纵向检查
293     for(int j = 0; j< COL; ++j)
294     {
295         for(int i = 0; i < ROW -1; ++i)
296         {
297             if(!game[i][j] || (game[i][j] == game[i+1][j]))
298             {
299                 return GAME_CONTINUE;
300                 break;
301             }
302         }
303     }
304  
305     //不符合上述两种状况,游戏结束
306     return GAME_OVER;
307  
308 }
309  
310 int main()
311 {
312     //设置一个随机数种子
313     srand((unsigned int)time(0));
314     CreateNumber();
315     CreateNumber();
316     Print();
317     int direction = 0;
318     int gameState = -1;
319     while(true)
320     {
321         direction = Input();
322  
323         gameState = Judge();
324         if(direction && gameState == GAME_CONTINUE)
325         {
326             Process(direction);
327             CreateNumber();
328             Print();
329             Sleep(100);
330         }
331         else if(gameState == GAME_WIN)
332         {
333             Print();
334             cout << "You Win!" << endl;
335             break;
336         }
337         else if(gameState == GAME_OVER)
338         {
339             Print();
340             cout <<"You lose!" << endl;
341             break;
342         }
343     }
344  
345     return 0;
346 }

猜你喜欢

转载自www.cnblogs.com/lx17746071609/p/10572362.html
今日推荐