2048(有VS的帮忙给点建议)
  • 板块灌水区
  • 楼主__KevinMZ__
  • 当前回复6
  • 已保存回复6
  • 发布时间2024/12/13 13:06
  • 上次更新2024/12/13 19:07:50
查看原帖
2048(有VS的帮忙给点建议)
1358215
__KevinMZ__楼主2024/12/13 13:06
#include<stdlib.h>
#include<time.h>
#include<easyx.h>
#include<iostream>

class _2048 {

private:

	int array[4][4];

	int nullCount;

	int nullpos[2][16];

	int GetNullPos() {
		nullCount = 0;
		for (int row = 0;row < 4;row++) {
			for (int column = 0;column < 4;column++) {
				if (array[row][column] == 0) {
					nullpos[0][nullCount] = row;
					nullpos[1][nullCount] = column;
					nullCount++;
				}
			}
		}
		return nullCount;
	}

	void CreateRandData(int count) {
		for (int c = 0;c < count;c++) {
			if (GetNullPos() == 0) return;
			int pos = rand() % nullCount;
			int row = nullpos[0][pos];
			int column = nullpos[1][pos];
			int rnd = rand() % 4 < 3 ? 2 : 4;
			array[row][column] = rnd;
		}

	}

	void Left()
	{
		for (int row = 0;row < 4;row++) {
			for (int column = 0;column < 4;column++) {
				if (array[row][column] == 0) {
					for (int t = column + 1;t < 4;t++) {
						if (array[row][t] != 0) {
							array[row][column] = array[row][t];
							array[row][t] = 0;
							break;
						}
					}
				}
				if (array[row][column] != 0) {
					for (int t = column + 1;t < 4;t++) {
						if (array[row][t] == array[row][column]) {
							array[row][column] *= 2;
							array[row][t] = 0;
						}
						else if (array[row][t] == 0) {
							continue;
						}
						else {
							break;
						}
					}
				}
			}
		}
		CreateRandData(1);
	}

	void Right() {
		for (int row = 0;row < 4;row++) {
			for (int column = 3;column >= 0;column--) {
				if (array[row][column] == 0) {
					for (int t = column - 1;t >= 0;t--) {
						if (array[row][t] != 0) {
							array[row][column] = array[row][t];
							array[row][t] = 0;
							break;
						}
					}
				}
				if (array[row][column] != 0) {
					for (int t = column - 1;t >= 0;t--) {
						if (array[row][t] == array[row][column]) {
							array[row][column] *= 2;
							array[row][t] = 0;
						}
						else if (array[row][t] == 0) {
							continue;
						}
						else {
							break;
						}
					}
				}
			}
		}
		CreateRandData(1);
	}

	void Up() {
		for (int column = 0;column < 4;column++) {
			for (int row = 0;row < 4;row++) {
				if (array[row][column] == 0) {
					for (int t = row + 1;t < 4;t++) {
						if (array[t][column] != 0) {
							array[row][column] = array[t][column];
							array[t][column] = 0;
							break;
						}
					}
				}
				if (array[row][column] != 0) {
					for (int t = row + 1;t < 4;t++) {
						if (array[t][column] == array[row][column]) {
							array[row][column] *= 2;
							array[t][column] = 0;
						}
						else if (array[t][column] == 0) {
							continue;
						}
						else {
							break;
						}
					}
				}
			}
		}
		CreateRandData(1);
	}

	void Down() {
		for (int column = 0;column < 4;column++) {
			for (int row = 3;row >= 0;row--) {
				if (array[row][column] == 0) {
					for (int t = row - 1;t >= 0;t--) {
						if (array[t][column] != 0) {
							array[row][column] = array[t][column];
							array[t][column] = 0;
							break;
						}
					}
				}
				if (array[row][column] != 0) {
					for (int t = row - 1;t >= 0;t--) {
						if (array[t][column] == array[row][column]) {
							array[row][column] *= 2;
							array[t][column] = 0;
						}
						else if (array[t][column] == 0) {
							continue;
						}
						else {
							break;
						}
					}
				}
			}
		}
		CreateRandData(1);
	}

	int IsFull() {
		for (int row = 0;row < 4;row++) {
			for (int column = 0;column < 4;column++) {
				if (array[row][column] == 0) {
					return 1;
				}
			}
		}
		return 0;
	}

	int CanVerge() {
		for (int row = 0;row < 4;row++) {
			for (int column = 0;column < 4;column++) {
				if ((column + 1 < 4 && array[row][column] == array[row][column + 1]) || (row + 1 < 4 && array[row][column] == array[row][column + 1])) {
					return 1;
				}
			}
		}
		return 0;
	}

	void InitData() {
		srand(time(0));
		for (int row = 0;row < 4;row++) {
			for (int column = 0;column < 4;column++) {
				array[row][column] = 0;
			}
		}
		GetNullPos();
		CreateRandData(2);
	}

	void updata() {
		if (1) {
			IMAGE img;
			loadimage(&img, _T("image/background.png"));
			putimage(0, 100, &img);
		}
		for (int row = 0;row < 4;row++) {
			for (int column = 0;column < 4;column++) {
				int pos = array[row][column];
				if (pos) {
					IMAGE img;
					switch (pos)
					{
					case 2:
						loadimage(&img, _T("image/block_2.png"));
						break;
					case 4:
						loadimage(&img, _T("image/block_4.png"));
						break;
					case 8:
						loadimage(&img, _T("image/block_8.png"));
						break;
					case 16:
						loadimage(&img, _T("image/block_16.png"));
						break;
					case 32:
						loadimage(&img, _T("image/block_32.png"));
						break;
					case 64:
						loadimage(&img, _T("image/block_64.png"));
						break;
					case 128:
						loadimage(&img, _T("image/block_128.png"));
						break;
					case 256:
						loadimage(&img, _T("image/block_256.png"));
						break;
					case 512:
						loadimage(&img, _T("image/block_512.png"));
						break;
					case 1024:
						loadimage(&img, _T("image/block_1024.png"));
						break;
					case 2048:
						loadimage(&img, _T("image/block_2048.png"));
						break;
					default:
						break;
					}
					putimage(15 + (106 + 15) * column, 115 + (106 + 15) * row, &img);
				}
			}
		}
	}

	void InitWindow() {
		InitData();
		initgraph(500, 600);
		cleardevice();
		settextcolor(WHITE);
		settextstyle(60, 0, _T("黑体"));
		outtextxy(100, 0, _T("EasyX_2048"));
	}

	int GameOver() {
		if (IsFull() == 0 && CanVerge() == 0) {
			cleardevice();
			updata();
			settextcolor(WHITE);
			settextstyle(80, 0, _T("黑体"));
			outtextxy(75, 0, _T("Game over"));
			return 1;
		}
		return 0;
	}

	int Success() {
		for (int i = 0;i < 4;i++) {
			for (int j = 0;j < 4;j++) {
				if (array[i][j] == 2048) {
					cleardevice();
					updata();
					settextcolor(WHITE);
					settextstyle(80, 0, _T("黑体"));
					outtextxy(80, 0, _T("Succeed!"));
					return 1;
				}
			}
		}
		return 0;
	}

public:

	int Main() {
		InitWindow();
		while (!GameOver()) {
			updata();
			ExMessage msg;
			getmessage(&msg);
			if (msg.message == WM_KEYDOWN) {
				switch (msg.vkcode) {
				case VK_UP:
					Up();
					break;
				case VK_DOWN:
					Down();
					break;
				case VK_LEFT:
					Left();
					break;
				case VK_RIGHT:
					Right();
					break;
				default:
					continue;
				}
			}
		}
		system("pause");
		closegraph();
		return 0;
	}
};

int main() {
	_2048 __2048;
	__2048.Main();
	return 0;
}
2024/12/13 13:06
加载中...