跳到正文

剪贴板

剪贴板 g2dzjkof

原文
剪贴板信息

剪贴板信息

收录
更新
C++
//2048

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <conio.h>
#include <windows.h>

using namespace std;

class Game2048 {
private:
	vector<vector<int>> board;
	int score;
	bool moved;

public:
	Game2048() : board(4, vector<int>(4, 0)), score(0) {
		srand(time(0));
		addRandomTile();
		addRandomTile();
	}

	void addRandomTile() {
		vector<pair<int, int>> emptyCells;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (board[i][j] == 0) {
					emptyCells.push_back({i, j});
				}
			}
		}

		if (!emptyCells.empty()) {
			int randomIndex = rand() % emptyCells.size();
			int value = (rand() % 10 < 9) ? 2 : 4;
			board[emptyCells[randomIndex].first][emptyCells[randomIndex].second] = value;
		}
	}

	void displayBoard() {
		cout << "\033[H";
		cout << "=== 2048 GAME ===" << endl;
		cout << "Score: " << score << endl;
		cout << "Use W/A/S/D to move, Q to quit" << endl << endl;

		for (int i = 0; i < 4; i++) {
			cout << "+------+------+------+------+" << endl;
			for (int j = 0; j < 4; j++) {
				if (board[i][j] == 0) {
					cout << "|      ";
				} else {
					cout << "| " << setw(4) << board[i][j] << " ";
				}
			}
			cout << "|" << endl;
		}
		cout << "+------+------+------+------+" << endl;
	}

	void rotateBoard() {
		vector<vector<int>> newBoard(4, vector<int>(4));
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				newBoard[i][j] = board[3-j][i];
			}
		}
		board = newBoard;
	}

	void reverseRows() {
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 2; j++) {
				swap(board[i][j], board[i][3-j]);
			}
		}
	}

	bool moveLeft() {
		moved = false;
		for (int i = 0; i < 4; i++) {
			vector<int> row;
			for (int j = 0; j < 4; j++) {
				if (board[i][j] != 0) {
					row.push_back(board[i][j]);
				}
			}

			vector<int> newRow;
			for (int j = 0; j < row.size(); j++) {
				if (j < row.size() - 1 && row[j] == row[j+1]) {
					int mergedValue = row[j] * 2;
					newRow.push_back(mergedValue);
					score += mergedValue;
					j++;
					moved = true;
				} else {
					newRow.push_back(row[j]);
				}
			}

			while (newRow.size() < 4) {
				newRow.push_back(0);
			}

			for (int j = 0; j < 4; j++) {
				if (board[i][j] != newRow[j]) {
					moved = true;
				}
				board[i][j] = newRow[j];
			}
		}
		return moved;
	}

	bool move(char direction) {
		switch(direction) {
		case 'w':
		case 'W':
			rotateBoard();
			rotateBoard();
			rotateBoard();
			moveLeft();
			rotateBoard();
			break;
		case 's':
		case 'S':
			rotateBoard();
			moveLeft();
			rotateBoard();
			rotateBoard();
			rotateBoard();
			break;
		case 'a':
		case 'A':
			moveLeft();
			break;
		case 'd':
		case 'D':
			reverseRows();
			moveLeft();
			reverseRows();
			break;
		default:
			return false;
		}
		return moved;
	}

	bool isGameOver() {
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (board[i][j] == 0) {
					return false;
				}
			}
		}

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 3; j++) {
				if (board[i][j] == board[i][j+1]) {
					return false;
				}
			}
		}

		for (int j = 0; j < 4; j++) {
			for (int i = 0; i < 3; i++) {
				if (board[i][j] == board[i+1][j]) {
					return false;
				}
			}
		}

		return true;
	}

	bool hasWon() {
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (board[i][j] == 2048) {
					return true;
				}
			}
		}
		return false;
	}

	void playGame() {
		// 修复SetConsoleTitle报错问题:使用ASCII字符串替代宽字符串
		SetConsoleTitleA("2048 Game - No Flicker Version");

		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		DWORD dwMode = 0;
		GetConsoleMode(hOut, &dwMode);
		dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
		SetConsoleMode(hOut, dwMode);

		CONSOLE_CURSOR_INFO cursorInfo;
		GetConsoleCursorInfo(hOut, &cursorInfo);
		cursorInfo.bVisible = false;
		SetConsoleCursorInfo(hOut, &cursorInfo);

		char input;
		while (true) {
			displayBoard();

			if (hasWon()) {
				cout << "Congratulations! You reached 2048!" << endl;
				cout << "Continue playing? (Y/N): ";
				char choice;
				cin >> choice;
				if (choice == 'n' || choice == 'N') {
					break;
				}
			}

			if (isGameOver()) {
				displayBoard();
				cout << "Game Over! Final Score: " << score << endl;
				break;
			}

			cout << "Enter move (WASD), Q to quit: ";
			input = _getch();

			if (input == 'q' || input == 'Q') {
				cout << "Thanks for playing!" << endl;
				break;
			}

			if (move(input)) {
				addRandomTile();
			}
		}

		cursorInfo.bVisible = true;
		SetConsoleCursorInfo(hOut, &cursorInfo);
	}
};

int main() {
	system("chcp 65001 > nul");

	Game2048 game;
	game.playGame();
	return 0;
}