#include<stdio.h>//#include<conio.h>#include<graphics.h>#include<Windows.h>#include<time.h>int _back[4][4] = {};void draw();//绘图void play();void init();//初始化数字void _up();//向上移动void _down();//向下移动void _left();//像左移动void _right();//向右移动void add_number();//增加一个数字int a[4][4] = { 0 };int emtpy;//空格的数量void draw(){ int i, j; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { _back[i][j] = a[i][j]; PIMAGE img; img = newimage(); switch (a[i][j]) { case 0: { getimage(img, "2048//0.png"); putimage(j * 180, i * 180, img); break; } case 2: { getimage(img, "2048//2.png"); putimage(j * 180, i * 180, img); break; } case 4: { getimage(img, "2048//4.png"); putimage(j * 180, i * 180, img); break; } case 8: { getimage(img, "2048//8.png"); putimage(j * 180, i * 180, img); break; } case 16: { getimage(img, "2048//16.png"); putimage(j * 180, i * 180, img); break; } case 32: { getimage(img, "2048//32.png"); putimage(j * 180, i * 180, img); break; } case 64: { getimage(img, "2048//64.png"); putimage(j * 180, i * 180, img); break; } case 128: { getimage(img, "2048//128.png"); putimage(j * 180, i * 180, img); break; } case 256: { getimage(img, "2048//256.png"); putimage(j * 180, i * 180, img); break; } case 512: { getimage(img, "2048//512.png"); putimage(j * 180, i * 180, img); break; } case 1024: { getimage(img, "2048//1024.png"); putimage(j * 180, i * 180, img); break; } case 2048: { getimage(img, "2048//2048.png"); putimage(j * 180, i * 180, img); break; } } } }}void init(){ int x, y; srand(time(0)); x = rand() % 4; y = rand() % 4; a[x][y] = 2; emtpy = 15;}void _up(){ int x, y, i; for (y = 0; y < 4; ++y) { // 从上向下合并相同的方块 for (x = 0; x < 4; ++x) { if (a[x][y] == 0) ; else { for (i = x + 1; i < 4; ++i) { if (a[i][y] == 0) ; else if (a[x][y] == a[i][y]) { a[x][y] += a[i][y]; a[i][y] = 0; ++emtpy; x = i; break; } else { break; } } } } } for (y = 0; y < 4; ++y) // 向上移动箱子 for (x = 0; x < 4; ++x) { if (a[x][y] == 0) ; else { for (i = x; (i > 0) && (a[i - 1][y] == 0); --i) { a[i - 1][y] = a[i][y]; a[i][y] = 0; } } }}void _down() { int x, y, i; for (y = 0; y < 4; ++y) // 向下合并相同的方格 for (x = 3; x >= 0; --x) { if (a[x][y] == 0) ; else { for (i = x - 1; i >= 0; --i) { if (a[i][y] == 0) ; else if (a[x][y] == a[i][y]) { a[x][y] += a[i][y]; a[i][y] = 0; ++emtpy; x = i; break; } else break; } } } for (y = 0; y < 4; ++y) // 向下移动方格 for (x = 3; x >= 0; --x) { if (a[x][y] == 0) ; else { for (i = x; (i < 3) && (a[i + 1][y] == 0); ++i) { a[i + 1][y] = a[i][y]; a[i][y] = 0; } } }}void _left(){ int x, y, i; for (x = 0; x < 4; ++x) // 向左合并相同的方格 for (y = 0; y < 4; ++y) { if (a[x][y] == 0) ; else { for (i = y + 1; i < 4; ++i) { if (a[x][i] == 0) ; else if (a[x][y] == a[x][i]) { a[x][y] += a[x][i]; a[x][i] = 0; emtpy++; y = i; break; } else break; } } } for (x = 0; x < 4; ++x) // 向左移动方格 for (y = 0; y < 4; ++y) { if (a[x][y] == 0) ; else { for (i = y; (i > 0) && (a[x][i - 1] == 0); --i) { a[x][i - 1] = a[x][i]; a[x][i] = 0; } } }}void _right() { int x, y, i; for (x = 0; x < 4; ++x) // 向右合并相同的方格 for (y = 3; y >= 0; --y) { if (a[x][y] == 0) ; else { for (i = y - 1; i >= 0; --i) { if (a[x][i] == 0) ; else if (a[x][y] == a[x][i]) { a[x][y] += a[x][i]; a[x][i] = 0; ++emtpy; y = i; break; } else break; } } } for (x = 0; x < 4; ++x) // 向右移动方格 for (y = 3; y >= 0; --y) { if (a[x][y] == 0) ; else { for (i = y; (i < 3) && (a[x][i + 1] == 0); ++i) { a[x][i + 1] = a[x][i]; a[x][i] = 0; } } }}void add_number(){ srand(time(0)); if (emtpy > 0) { int x, y, temp; do { x = rand() % 4; y = rand() % 4; } while (a[x][y] != 0); temp = rand(); int i = temp % 2; if (i == 1) { a[x][y] = 2; emtpy--; } else { a[x][y] = 4; emtpy--; } }}void play(){ char c = getch(); switch (c) { case 'w': case'W': { _up(); add_number(); draw(); break; } case 's': case'S': { _down(); add_number(); draw(); break; } case 'a': case'A': { _left(); add_number(); draw(); break; }case 'd': case'D': { _right(); add_number(); draw(); break; } case 'q': case 'Q': { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { a[i][j] = _back[i][j]; draw(); } } } }}int main(){ init(); int i, j; initgraph(724, 724);//初始化 PIMAGE img; img = newimage(); getimage(img, "2048/背景.jpg"); putimage(0, 0, img); draw(); for (; is_run(); delay_fps(30)) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { _back[i][j] = a[i][j]; } } play(); }}