过了样例就算队(test4)

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <iomanip>

using namespace std;

const int BOARD_SIZE = 4; // 游戏板的大小
int score = 0; // 游戏得分

// 定义游戏板
vector<vector<int>> board(BOARD_SIZE, vector<int>(BOARD_SIZE, 0));

// 初始化游戏板,添加两个随机数字
void initBoard() {
    srand(time(NULL)); // 仅在这里初始化随机数种子
    for (int i = 0; i < 2; ++i) {
        vector<pair<int, int>> emptyCells;
        for (int i = 0; i < BOARD_SIZE; ++i) {
            for (int j = 0; j < BOARD_SIZE; ++j) {
                if (board[i][j] == 0) {
                    emptyCells.push_back(make_pair(i, j));
                }
            }
        }
        if (!emptyCells.empty()) {
            int index = rand() % emptyCells.size();
            pair<int, int> pos = emptyCells[index];
            board[pos.first][pos.second] = (rand() % 10 == 0) ? 4 : 2;
        }
    }
}

// 打印棋盘状态
void printBoard() {
    for (int i = 0; i < BOARD_SIZE; ++i) {
        for (int j = 0; j < BOARD_SIZE; ++j) {
            cout << board[i][j] << "\t";
        }
        cout << endl;
    }
    cout << "Score: " << score << endl;
}


// 棋盘向左移动
bool moveLeft() {
    bool moved = false;
    for (int i = 0; i < BOARD_SIZE; ++i) {
        vector<int> tempRow;
        int mergeIndex = 0;
        // 移动非零元素至左侧
        for (int j = 0; j < BOARD_SIZE; ++j) {
            if (board[i][j] != 0) {
                tempRow.push_back(board[i][j]);
            }
        }
        // 合并相同的元素
        for (size_t j = 0; j < tempRow.size(); ++j) {
            if (mergeIndex < j && tempRow[mergeIndex] == tempRow[j]) {
                tempRow[mergeIndex] *= 2;
                score += tempRow[mergeIndex];
                tempRow.erase(tempRow.begin() + j);
                --j;
                ++mergeIndex;
                moved = true;
            } else if (tempRow[mergeIndex] != tempRow[j]) {
                ++mergeIndex;
            }
        }
        // 将合并后的结果放回原位置
        for (int j = 0; j < BOARD_SIZE; ++j) {
            if (j < tempRow.size()) {
                board[i][j] = tempRow[j];
            } else {
                board[i][j] = 0;
            }
        }
    }
    return moved;
}

// 棋盘向右移动
bool moveRight() {
    bool moved = false;
    for (int i = 0; i < BOARD_SIZE; ++i) {
        vector<int> tempRow;
        int mergeIndex = 0;
        // 移动非零元素至右侧
        for (int j = BOARD_SIZE - 1; j >= 0; --j) {
            if (board[i][j] != 0) {
                tempRow.insert(tempRow.begin(), board[i][j]);
            }
        }
        // 合并相同的元素
        for (size_t j = 0; j < tempRow.size(); ++j) {
            if (mergeIndex < j && tempRow[mergeIndex] == tempRow[j]) {
                tempRow[mergeIndex] *= 2;
                score += tempRow[mergeIndex];
                tempRow.erase(tempRow.begin() + j);
                --j;
                ++mergeIndex;
                moved = true;
            } else if (tempRow[mergeIndex] != tempRow[j]) {
                ++mergeIndex;
            }
        }
        // 将合并后的结果放回原位置
        for (int j = BOARD_SIZE - 1; j >= 0; --j) {
            if (j < BOARD_SIZE - tempRow.size()) {
                board[i][j] = 0;
            } else {
                board[i][j] = tempRow[BOARD_SIZE - 1 - j];
            }
        }
    }
    return moved;
}

// 棋盘向上移动
bool moveUp() {
    bool moved = false;
    for (int j = 0; j < BOARD_SIZE; ++j) {
        vector<int> tempCol;
        int mergeIndex = 0;
        // 移动非零元素至上侧
        for (int i = 0; i < BOARD_SIZE; ++i) {
            if (board[i][j] != 0) {
                tempCol.push_back(board[i][j]);
            }
        }
        // 合并相同的元素
        for (size_t i = 0; i < tempCol.size(); ++i) {
            if (mergeIndex < i && tempCol[mergeIndex] == tempCol[i]) {
                tempCol[mergeIndex] *= 2;
                score += tempCol[mergeIndex];
                tempCol.erase(tempCol.begin() + i);
                --i;
                ++mergeIndex;
                moved = true;
            } else if (tempCol[mergeIndex] != tempCol[i]) {
                ++mergeIndex;
            }
        }
        // 将合并后的结果放回原位置
        for (int i = 0; i < BOARD_SIZE; ++i) {
            if (i < tempCol.size()) {
                board[i][j] = tempCol[i];
            } else {
                board[i][j] = 0;
            }
        }
    }
    return moved;
}

// 棋盘向下移动
bool moveDown() {
    bool moved = false;
    for (int j = 0; j < BOARD_SIZE; ++j) {
        vector<int> tempCol;
        int mergeIndex = 0;
        // 移动非零元素至下侧
        for (int i = BOARD_SIZE - 1; i >= 0; --i) {
            if (board[i][j] != 0) {
                tempCol.insert(tempCol.begin(), board[i][j]);
            }
        }
        // 合并相同的元素
        for (size_t i = 0; i < tempCol.size(); ++i) {
            if (mergeIndex < i && tempCol[mergeIndex] == tempCol[i]) {
                tempCol[mergeIndex] *= 2;
                score += tempCol[mergeIndex];
                tempCol.erase(tempCol.begin() + i);
                --i;
                ++mergeIndex;
                moved = true;
            } else if (tempCol[mergeIndex] != tempCol[i]) {
                ++mergeIndex;
            }
        }
        // 将合并后的结果放回原位置
        for (int i = BOARD_SIZE - 1; i >= 0; --i) {
            if (i < BOARD_SIZE - tempCol.size()) {
                board[i][j] = 0;
            } else {
                board[i][j] = tempCol[BOARD_SIZE - 1 - i];
            }
        }
    }
    return moved;
}

// 检查游戏是否结束
bool isGameOver() {
    // 检查是否有空位
    for (int i = 0; i < BOARD_SIZE; ++i) {
        for (int j = 0; j < BOARD_SIZE; ++j) {
            if (board[i][j] == 0) {
                return false;
            }
        }
    }
    // 检查是否有相邻的相同数字
    for (int i = 0; i < BOARD_SIZE; ++i) {
        for (int j = 0; j < BOARD_SIZE - 1; ++j) {
            if (board[i][j] == board[i][j + 1] || (j < BOARD_SIZE - 1 && board[j][i] == board[j + 1][i])) {
                return false;
            }
        }
    }
    return true;
}


// 主函数
int main() {
    initBoard();

    bool gameEnded = false;
    while (!gameEnded) {
        printBoard();
        cout << "Enter your move (w/a/s/d): ";
        char move;
        cin >> move;

        switch (move) {
        case 'a':
            moveLeft();
            break;
        case 'd':
            moveRight();
            break;
        case 'w':
            moveUp();
            break;
        case 's':
            moveDown();
            break;
        default:
            cout << "Invalid input. Try again." << endl;
            continue;
        }

        // 确保在每次有效移动后添加一个新的随机数字
        vector<pair<int, int>> emptyCells;
        for (int i = 0; i < BOARD_SIZE; ++i) {
            for (int j = 0; j < BOARD_SIZE; ++j) {
                if (board[i][j] == 0) {
                    emptyCells.push_back(make_pair(i, j));
                }
            }
        }
        if (!emptyCells.empty()) {
            int index = rand() % emptyCells.size();
            pair<int, int> pos = emptyCells[index];
            board[pos.first][pos.second] = (rand() % 10 == 0) ? 4 : 2;
        }

        gameEnded = isGameOver();
    }

    printBoard();
    cout << "Game Over! Your score is: " << score << endl;
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Charles Coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值