2048游戏设计
#if _MSC_VER>=1900 #include "stdio.h" _ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned); #ifdef __cplusplus extern "C" #endif FILE* __cdecl __iob_func(unsigned i) { return __acrt_iob_func(i); } #endif /* _MSC_VER>=1900 */ #include<graphics.h> #include<stdio.h> #include<time.h> #include<stdlib.h> #include<conio.h> #include<vector> #include<string.h> #include<windows.h> using namespace std; int map[5][5], score = 0; char score_s[15],c; const char* name = "2048"; const char* gg = "Game over.Your score is:"; const char*goon = "Continue?(y/n)"; void init() { initgraph(480, 480); setbkcolor(WHITE); setcolor(BLACK); cleardevice(); settextstyle(15, 15, _T("幼圆")); for (int i = 1; i <= 4; i++) for (int j = 1; j <= 4; j++) map[i][j] = 0; srand((unsigned)time(NULL)); } void create() { vector<int>blank; blank.clear(); for (int i = 1; i <= 4; i++) for (int j = 1; j <= 4; j++) { if (map[i][j] == 0) blank.push_back((i - 1) * 4 + j); } if (blank.size() != 0) { int pos = rand() % blank.size(); int x = (blank[pos] - 1) / 4 + 1; int y = blank[pos] - (x - 1) * 4; map[x][y] = 2; } } bool judge_w(int j) { bool flag = false; for (int i = 1; i <= 3; i++) { if (map[i][j] != 0 && map[i][j] == map[i + 1][j]) { flag = true; break; } } for (int i = 2; i <= 4; i++) { if (map[i][j] != 0 && map[i - 1][j] == 0) { flag = true; break; } } return flag; } bool judge_s(int j) { bool flag = false; for (int i = 4; i >= 2; i--) { if (map[i][j] != 0 && map[i][j] == map[i - 1][j]) { flag = true; break; } } for (int i = 1; i <= 3; i++) { if (map[i][j] != 0 && map[i + 1][j] == 0) { flag = true; break; } } return flag; } bool judge_a(int i) { bool flag = false; for (int j = 2; j <= 4; j++) { if (map[i][j] != 0 && map[i][j] == map[i][j - 1]) { flag = true; break; } } for (int j = 3; j >= 1; j--) { if (map[i][j + 1] != 0 && map[i][j] == 0) { flag = true; break; } } return flag; } bool judge_d(int i) { bool flag = false; for (int j = 4; j >= 2; j--) { if (map[i][j] != 0 && map[i][j] == map[i][j - 1]) { flag = true; break; } } for (int j = 1; j <= 3; j++) { if (map[i][j + 1] == 0 && map[i][j] != 0) { flag = true; break; } } return flag; } void move_w(int j) { for (int i = 1; i <= 3; i++) if (map[i][j] == map[i + 1][j] && map[i][j] != 0) { map[i][j] *= 2; score += map[i][j]; map[i + 1][j] = 0; } for (int i = 2; i <= 4; i++) if (map[i][j] != 0 && map[i - 1][j] == 0) { map[i - 1][j] = map[i][j]; map[i][j] = 0; } } void move_s(int j) { for (int i = 4; i >= 2; i--) if (map[i][j] != 0 && map[i][j] == map[i - 1][j]) { map[i][j] *= 2; score += map[i][j]; map[i - 1][j] = 0; } for (int i = 1; i <= 3; i++) if (map[i][j] != 0 && map[i + 1][j] == 0) { map[i + 1][j] = map[i][j]; map[i][j] = 0; } } void move_a(int i) { for (int j = 2; j <= 4; j++) { if (map[i][j] != 0 && map[i][j] == map[i][j - 1]) { map[i][j - 1] *= 2; score += map[i][j]; map[i][j] = 0; } } for (int j = 3; j >= 1; j--) { if (map[i][j + 1] != 0 && map[i][j] == 0) { map[i][j] = map[i][j + 1]; map[i][j + 1] = 0; } } } void move_d(int i) { for (int j = 4; j >= 2; j--) { if (map[i][j] != 0 && map[i][j] == map[i][j - 1]) { map[i][j] *= 2; score += map[i][j]; map[i][j - 1] = 0; } } for (int j = 1; j <= 3; j++) { if (map[i][j + 1] == 0 && map[i][j] != 0) { map[i][j + 1] = map[i][j]; map[i][j] = 0; } } } bool gameover() { bool final[5]; for (int i = 1; i <= 4; i++) if (judge_a(i) == false && judge_d(i) == false && judge_w(i) == false && judge_s(i) == false) final[i] = false; return final[1] == false && final[2] == false && final[3] == false && final[4] == false; } void nextgame() { cleardevice(); Sleep(1000); outtextxy(80, 160, _T(gg)); sprintf_s(score_s, "%d", score); outtextxy(240, 215, _T(score_s)); outtextxy(140, 270, _T(goon)); while (1) { c = _getch(); if (c == 'y') { score = 0; init(); break; } else if (c == 'n') { closegraph(); exit(0); } } } void print() { cleardevice(); for (int i = 1; i <= 4; i++) for (int j = 1; j <= 4; j++) { outtextxy(210, 0, _T(name)); sprintf_s(score_s, "%d", score); outtextxy(400, 0, _T(score_s)); rectangle(115 * i - 95, 115 * j - 95, 115 * i, 115 * j); char N[6]; sprintf_s(N, "%d", map[i][j]); if (map[i][j] != 0) outtextxy(115 * j - 55, 115 * i - 55, _T(N)); } } void game() { c = _getch(); switch (c) { case 'w': for (int j = 1; j <= 4; j++) while (1) { if (judge_w(j) == false) break; move_w(j); }; break; case 's': for (int j = 1; j <= 4; j++) while (1) { if (judge_s(j) == false) break; move_s(j); }; break; case 'a': for (int i = 1; i <= 4; i++) while (1) { if (judge_a(i) == false) break; move_a(i); }; break; case 'd': for (int i = 1; i <= 4; i++) while (1) { if (judge_d(i) == false) break; move_d(i); }; break; default:break; } } void main() { init(); while (1) { create(); if (gameover()) nextgame(); print(); game(); } }