跳到正文

剪贴板

剪贴板 6rdg0k8i

qiuby123456
原文
剪贴板信息

剪贴板信息

发布
收录
更新

#ifdef __cplusplus

include <cstdio>

include <cstdlib>

include <ctime>

#else

include <stdio.h>

include <stdlib.h>

include <time.h>

#endif #include <conio.h> #include <windows.h> #ifndef inline

define inline

#endif #ifndef bool

define bool int

#endif #ifndef true

define true 1

#endif #ifndef false

define false 0

#endif #ifndef nullptr

define nullptr NULL

#endif #ifndef getch

define getch _getch

#endif #ifndef kbhit

define kbhit _kbhit

#endif int num[4][4]; HANDLE handle; inline void gotoscreen(SHORT x, SHORT y){ SetConsoleCursorPosition(handle, (COORD){x, y}); } inline void setcolor(WORD color){ SetConsoleTextAttribute(handle, color); } inline void screencolor(SHORT x, SHORT y, WORD color){ SetConsoleCursorPosition(handle, (COORD){x, y}); SetConsoleTextAttribute(handle, color); } inline void init(){ int i, j; handle = GetStdHandle(STD_OUTPUT_HANDLE); system(“cls”); SetConsoleTitle(“2048 Game Version 1.0.0.1.1”); setcolor(240); for (i = 0; i < 8; ++i){ gotoscreen(0, i++); printf(” ”); for (j = 0; j < 35; j += 7){ gotoscreen(j, i); printf(” ”); } } gotoscreen(0, i); printf(” ”); setcolor(15); memset(num, 0, sizeof(num)); srand(time(nullptr)); } inline bool full(){ int j, i = 4; while (i—){ for (j = 0; j < 4; ++j){ if (!num[i][j]){ return false; } } } return true; } inline void put(){ int x = rand() & 3, y = rand() & 3; while (num[x][y]){ x = rand() & 3; y = rand() & 3; } if (rand() & 15){ num[x][y] = 2; } else{ num[x][y] = 4; } } inline void print(){ int i, j; for (i = 0; i < 4; ++i){ for (j = 0; j < 4; ++j){ gotoscreen(j * 7 + 2, i << 1 | 1); if (num[i][j]){ printf(“%5d”, num[i][j]); } else{ printf(” ”); } } } } inline void cut(int *a, int *b){ *a = *b; *b = 0; } inline void levelup(int *a, int *b){ *a <<= 1; *b = 0; } inline int check(int *a, int *b){ if (a == b || !*b){ return 1; } if (!*a){ cut(a, b); return 2; } if (*a == *b){ levelup(a, b); return 4; } return 0; } inline bool Up(){ int i, k, f, j = 4, ff = 0; while (j—){ i = 0; k = 1; while (i < 3){ ff |= f = check(num[i] + j, num[k] + j); if (f & 3){ ++k; } else if(f & 4){ ++i; ++k; } else{ ++i; } if (k > 3){ ++i; k = i + 1; } } } return ff & 6; } inline bool Left(){ int i, k, f, j = 4, ff = 0; while (j—){ i = 0; k = 1; while (i < 3){ ff |= f = check(num[j] + i, num[j] + k); if (f & 3){ ++k; } else if(f & 4){ ++i; ++k; } else{ ++i; } if (k > 3){ ++i; k = i + 1; } } } return ff & 6; } inline bool Right(){ int i, k, f, j = 4, ff = 0; while (j—){ i = 3; k = 2; while (i > 0){ ff |= f = check(num[j] + i, num[j] + k); if (f & 3){ —k; } else if(f & 4){ —i; —k; } else{ —i; } if (k < 0){ —i; k = i - 1; } } } return ff & 6; } inline bool Down(){ int i, k, f, j = 4, ff = 0; while (j—){ i = 3; k = 2; while (i > 0){ ff |= f = check(num[i] + j, num[k] + j); if (f & 3){ —k; } else if(f & 4){ —i; —k; } else{ —i; } if (k < 0){ —i; k = i - 1; } } } return ff & 6; } int main(){ int c; init(); put(); print(); while (1){ if (full()){ screencolor(0, 9, 15); puts(“Game Over!”); return 0; } while (kbhit()){ getch(); } if (getch() != 224){ continue; } c = getch(); if (c == 72 && Up()){ put(); print(); } else if(c == 75 && Left()){ put(); print(); } else if(c == 77 && Right()){ put(); print(); } else if(c == 80 && Down()){ put(); print(); } } return 0; }