标签:style blog http color os io strong for ar
手动转一下田神的2048 :http://blog.csdn.net/tc_to_top/article/details/38793869
1 #include <iostream> 2 #include <ctime> 3 #include <cstdlib> 4 #include <cstring> 5 #include <conio.h> 6 #include <cstdlib> 7 #include <windows.h> 8 #define Coord_x 15 9 #define Coord_y 15 10 #define Height 20 11 #define Width 16 12 13 using namespace std; 14 15 class Game 16 { 17 public: 18 int score; 19 int num[5][5]; 20 21 public: 22 Game(int s = 0); 23 bool control(char ch); 24 void init(); 25 void make(); 26 }G; 27 28 class Console 29 { 30 public: 31 void gotoxy(HANDLE hOut, int x, int y); 32 void enter_game(); 33 void window(); 34 void show(); 35 void start_game(); 36 void end_game(); 37 38 friend class Game; 39 }C; 40 41 Game::Game(int s) 42 { 43 score = s; 44 memset(num,0,sizeof(num)); 45 } 46 47 void Console::gotoxy(HANDLE hOut, int x, int y) 48 { 49 COORD pos; 50 pos.X = x; 51 pos.Y = y; 52 SetConsoleCursorPosition(hOut, pos); 53 } 54 55 void Console::enter_game() 56 { 57 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 58 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); 59 system("title 2048 by Tc"); 60 gotoxy(hOut,Coord_x+Width-1,Coord_y-12); 61 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); 62 cout<<" 欢迎进入 2 0 4 8"; 63 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_GREEN); 64 gotoxy(hOut,Coord_x+Width-25,Coord_y-8); 65 cout << " * * * * * * * * " << endl; 66 gotoxy(hOut,Coord_x+Width-25,Coord_y-7); 67 cout << " * * * * * * * * " << endl; 68 gotoxy(hOut,Coord_x+Width-25,Coord_y-6); 69 cout << " * * * * * * * * " << endl; 70 gotoxy(hOut,Coord_x+Width-25,Coord_y-5); 71 cout << " * * * * * * " << endl; 72 gotoxy(hOut,Coord_x+Width-25,Coord_y-4); 73 cout << " * * * * * * * * * * * " << endl; 74 gotoxy(hOut,Coord_x+Width-25,Coord_y-3); 75 cout << " * * * * * * " << endl; 76 gotoxy(hOut,Coord_x+Width-25,Coord_y-2); 77 cout << " * * * * * * " << endl; 78 gotoxy(hOut,Coord_x+Width-25,Coord_y-1); 79 cout << " * * * * * * * * * * " << endl; 80 gotoxy(hOut,Coord_x+Width+1,Coord_y+3); 81 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED); 82 cout << "按回车键进入游戏\n"<< endl;; 83 while(1) 84 { 85 char c; 86 if(kbhit()) 87 { 88 c = getch(); 89 if(c == 13) 90 { 91 system("CLS"); 92 C.start_game(); 93 } 94 } 95 } 96 if(getch() == 27) 97 { 98 C.end_game(); 99 } 100 } 101 102 103 void Console::end_game() 104 { 105 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); 106 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 107 system("CLS"); 108 gotoxy(hOut,Coord_x+Width+5,Coord_y-10); 109 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED); 110 cout << "游戏结束"; 111 gotoxy(hOut,Coord_x+Width+5,Coord_y-7); 112 cout << "最终得分 : " << G.score << endl; 113 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); 114 exit(0); 115 } 116 117 void Console::window() 118 { 119 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 120 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); 121 system("title 2 0 4 8 by Tc"); 122 system("color 0C"); 123 gotoxy(hOut,Coord_x+Width-2,Coord_y-13); 124 cout << " *******"; 125 gotoxy(hOut,Coord_x+Width-2,Coord_y-12); 126 cout << " 2 0 4 8"; 127 gotoxy(hOut,Coord_x+Width-2,Coord_y-11); 128 cout << " *******"; 129 gotoxy(hOut,Coord_x+2*Width+3,Coord_y+6); 130 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); 131 gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 8); 132 cout << "w键 : 向上 " << "s键 : 向下"; 133 gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 6); 134 cout << "a键 : 向左 " << "d键 : 向右"; 135 gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 4); 136 cout << "Esc :退出"; 137 } 138 139 void Console :: show() 140 { 141 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); 142 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); 143 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED); 144 gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 10); 145 cout << " "; 146 gotoxy(hOut,Coord_x+2*Width+3 + 2,Coord_y - 10); 147 cout << "分数 : " << G.score; 148 for(int i = 0; i < 4; i++) 149 { 150 for(int j = 0; j < 4; j++) 151 { 152 if(G.num[i][j] != 0) 153 { 154 cout << " "; 155 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_RED); 156 gotoxy(hOut, Coord_x + 6 + j * 8,Coord_x - 8 + i * 4); 157 cout << G.num[i][j]; 158 } 159 else 160 { 161 cout << " "; 162 SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY | FOREGROUND_GREEN); 163 gotoxy(hOut, Coord_x + 6 + j * 8,Coord_x - 8 + i * 4); 164 cout << "■"; 165 } 166 } 167 cout << endl; 168 } 169 170 } 171 172 bool Game::control(char dir) 173 { 174 int num2[4][4]; 175 for(int n = 0; n < 4; n++) 176 for(int m = 0; m < 4; m++) 177 num2[n][m] = num[n][m]; 178 bool IsMove = false; 179 if(dir == ‘w‘ || dir == ‘W‘) 180 { 181 for(int j = 0; j < 4; j++) 182 { 183 int ii = 0, temp = 5; 184 for(int i = 0; i < 4; i++) 185 { 186 if(num[i][j] == 0) 187 continue; 188 else 189 { 190 temp = i; 191 break; 192 } 193 } 194 if(temp == 5) 195 continue; 196 num[ii][j] = num[temp][j]; 197 for(int i = temp + 1; i < 4; i++) 198 { 199 if(num[i][j] == 0) 200 continue; 201 else if(num[i][j] != 0 && num[i][j] == num[ii][j]) 202 { 203 num[ii][j] *= 2; 204 G.score += num[ii][j]; 205 num[i][j] = 0; 206 } 207 else if(num[i][j] != 0 && num[i][j] != num[ii][j]) 208 num[++ii][j] = num[i][j]; 209 } 210 while(ii < 4) 211 num[++ii][j] = 0; 212 for(int n = 0; n < 4; n++) 213 { 214 for(int m = 0; m < 4; m++) 215 { 216 if(num2[n][m] != num[n][m]) 217 { 218 IsMove = true; 219 break; 220 } 221 } 222 if(IsMove) 223 break; 224 } 225 } 226 return IsMove; 227 } 228 else if(dir == ‘s‘ || dir == ‘S‘) 229 { 230 for(int j = 0; j < 4; j++) 231 { 232 int ii = 3, temp = 5; 233 for(int i = 3; i >= 0; i--) 234 { 235 if(num[i][j] == 0) 236 continue; 237 else 238 { 239 temp = i; 240 break; 241 } 242 } 243 if(temp == 5) 244 continue; 245 num[ii][j] = num[temp][j]; 246 for(int i = temp - 1; i >= 0; i--) 247 { 248 if(num[i][j] == 0) 249 continue; 250 else if(num[i][j] != 0 && num[i][j] == num[ii][j]) 251 { 252 num[ii][j] *= 2; 253 G.score += num[ii][j]; 254 num[i][j] = 0; 255 } 256 else if(num[i][j] != 0 && num[i][j] != num[ii][j]) 257 num[--ii][j] = num[i][j]; 258 } 259 while(ii >= 0) 260 num[--ii][j] = 0; 261 for(int n = 0; n < 4; n++) 262 { 263 for(int m = 0; m < 4; m++) 264 { 265 if(num2[n][m] != num[n][m]) 266 { 267 IsMove = true; 268 break; 269 } 270 } 271 if(IsMove) 272 break; 273 } 274 } 275 return IsMove; 276 } 277 else if(dir == ‘a‘ || dir == ‘A‘) 278 { 279 for(int i = 0; i < 4; i++) 280 { 281 int jj = 0, temp = 5; 282 for(int j = 0; j < 4; j++) 283 { 284 if(num[i][j] == 0) 285 continue; 286 else 287 { 288 temp = j; 289 break; 290 } 291 } 292 if(temp == 5) 293 continue; 294 num[i][jj] = num[i][temp]; 295 for(int j = temp + 1; j < 4; j++) 296 { 297 if(num[i][j] == 0) 298 continue; 299 else if(num[i][j] != 0 && num[i][j] == num[i][jj]) 300 { 301 302 num[i][jj] *= 2; 303 G.score += num[i][jj]; 304 num[i][j] = 0; 305 } 306 else if(num[i][j] != 0 && num[i][j] != num[i][jj]) 307 { 308 309 num[i][++jj] = num[i][j]; 310 } 311 } 312 while(jj < 4) 313 { 314 num[i][++jj] = 0; 315 } 316 for(int n = 0; n < 4; n++) 317 { 318 for(int m = 0; m < 4; m++) 319 { 320 if(num2[n][m] != num[n][m]) 321 { 322 IsMove = true; 323 break; 324 } 325 } 326 if(IsMove) 327 break; 328 } 329 } 330 return IsMove; 331 } 332 else if(dir == ‘d‘ || dir == ‘D‘) 333 { 334 for(int i = 0; i < 4; i++) 335 { 336 int jj = 3, temp = 5; 337 for(int j = 3; j >= 0; j--) 338 { 339 if(num[i][j] == 0) 340 continue; 341 else 342 { 343 temp = j; 344 break; 345 } 346 } 347 if(temp == 5) 348 continue; 349 num[i][jj] = num[i][temp]; 350 for(int j = temp - 1; j >= 0 && jj > 0; j--) 351 { 352 if(num[i][j] == 0) 353 continue; 354 else if(num[i][j] != 0 && num[i][j] == num[i][jj]) 355 { 356 num[i][jj] *= 2; 357 G.score += num[i][jj]; 358 num[i][j] = 0; 359 } 360 else if(num[i][j] != 0 && num[i][j] != num[i][jj]) 361 num[i][--jj] = num[i][j]; 362 } 363 while(jj > 0) 364 num[i][--jj] = 0; 365 for(int n = 0; n < 4; n++) 366 { 367 for(int m = 0; m < 4; m++) 368 { 369 if(num2[n][m] != num[n][m]) 370 { 371 IsMove = true; 372 break; 373 } 374 } 375 if(IsMove) 376 break; 377 } 378 } 379 return IsMove; 380 } 381 return false; 382 } 383 384 void Game :: init() 385 { 386 srand((unsigned int) time(NULL)); 387 int x = rand() % 4; 388 int y = rand() % 4; 389 G.num[x][y] = 2; 390 int xx = rand() % 4; 391 int yy = rand() % 4; 392 while(1) 393 { 394 if(xx != x && yy != y) 395 { 396 G.num[xx][yy] = 2; 397 break; 398 } 399 xx = rand() % 4; 400 yy = rand() % 4; 401 } 402 } 403 404 void Game :: make() 405 { 406 int count = 0; 407 for(int i = 0; i < 4; i++) 408 { 409 for(int j = 0; j < 4; j++) 410 { 411 if(num[i][j]) 412 count ++; 413 } 414 } 415 if(count == 16) 416 C.end_game(); 417 int xx = rand() % 4; 418 int yy = rand() % 4; 419 while(1) 420 { 421 if(num[xx][yy] == 0) 422 { 423 num[xx][yy] = 2; 424 break; 425 } 426 xx = rand() % 4; 427 yy = rand() % 4; 428 } 429 } 430 431 void Console :: start_game() 432 { 433 G.init(); 434 window(); 435 show(); 436 char dir; 437 while(true) 438 { 439 bool flag = false; 440 dir = getch(); 441 if(dir == 27) 442 end_game(); 443 if(dir == ‘w‘ || dir == ‘W‘ || dir == ‘a‘ || dir == ‘A‘ || dir == ‘s‘ || dir == ‘S‘ || dir == ‘D‘ || dir == ‘d‘) 444 { 445 flag = G.control(dir); 446 window(); 447 if(flag) 448 G.make(); 449 show(); 450 } 451 } 452 } 453 454 int main() 455 { 456 C.enter_game(); 457 }
标签:style blog http color os io strong for ar
原文地址:http://www.cnblogs.com/njczy2010/p/3932587.html