码迷,mamicode.com
首页 > 编程语言 > 详细

2048 C++实现

时间:2017-03-20 22:24:37      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:space   clu   out   合并   put   get   sleep   tar   unsigned   

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

 

2048 C++实现

标签:space   clu   out   合并   put   get   sleep   tar   unsigned   

原文地址:http://www.cnblogs.com/yhyl/p/6591831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!