You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在已实现胜负判断的Connect4代码中添加合法移动检查与玩家切换

Connect4游戏:移动合法性检查与玩家切换实现

一、移动合法性检查实现

实现IsValidMove成员函数,只需判断两个核心条件:

  • 输入的列号是否在0到COLS-1的有效区间内(数组为0索引)
  • 目标列是否还有空位(通过piecesInCol[col] < ROWS判断,该数组记录每列已放置的棋子数量)

代码实现:

bool Connect4::IsValidMove(int col) const {
    // 验证列号是否合法
    if (col < 0 || col >= COLS) {
        return false;
    }
    // 验证列是否还有空位
    return piecesInCol[col] < ROWS;
}

二、合法移动后的玩家切换逻辑

在MakeMove函数中,先通过IsValidMove验证移动合法性:

  • 若移动不合法,直接返回false
  • 若合法,计算棋子落点(从列的最底部开始填充),放置当前玩家的棋子,更新列棋子计数,最后切换玩家。

同时需要在默认构造函数中初始化currentPlayer为Player1,确保游戏从玩家1开始。

1. 完善默认构造函数

Connect4::Connect4() {
    for(int i=0; i<ROWS; i++) {
        for(int j=0; j<COLS; j++) {
            board[i][j] = BLANK;
        }
    }
    for (int i = 0; i < COLS; i++) {
        piecesInCol[i] = 0;
    }
    // 初始化先手玩家为Player1
    currentPlayer = Player1;
}

2. 实现MakeMove函数

bool Connect4::MakeMove(int col) {
    // 先检查移动合法性
    if (!IsValidMove(col)) {
        return false;
    }
    // 计算棋子落点:从列的最底部开始,已放piecesInCol[col]个棋子,所以行号为ROWS-1 - piecesInCol[col]
    int targetRow = ROWS - 1 - piecesInCol[col];
    board[targetRow][col] = currentPlayer;
    // 更新该列的棋子计数
    piecesInCol[col]++;
    
    // 切换当前玩家:X和O交替
    currentPlayer = (currentPlayer == Player1) ? Player2 : Player1;
    
    return true;
}

额外修复:原有胜负判断函数的bug

原代码中的胜负判断存在越界和逻辑错误,需同步修复以保证游戏功能正常:

1. 修复横向胜负判断

bool Connect4::HasHorizontalWinner() const {
    for(int row=0; row < ROWS; row++) {
        // 列最多遍历到COLS-4,避免col+3越界
        for(int col = 0; col <= COLS - 4; col++) {
            char token = board[row][col];
            if (token == BLANK) {
                continue;
            }
            if (token == board[row][col+1] &&
                token == board[row][col+2] &&
                token == board[row][col+3]) {
                return true;
            }
        }
    }
    return false;
}

2. 修复纵向胜负判断

bool Connect4::HasVerticalWinner() const {
    // 行最多遍历到ROWS-4,避免row+3越界
    for(int row=0; row <= ROWS - 4; row++) {
        for(int col = 0; col < COLS; col++) {
            char token = board[row][col];
            if (token == BLANK) {
                continue;
            }
            if (token == board[row+1][col] &&
                token == board[row+2][col] &&
                token == board[row+3][col]) {
                return true;
            }
        }
    }
    return false;
}

3. 修复对角线胜负判断

bool Connect4::HasDiagonalWinner() const {
    // 左上到右下对角线
    for (int row = 0; row <= ROWS - 4; row++) {
        for (int col = 0; col <= COLS - 4; col++) {
            char token = board[row][col];
            if (token == BLANK) {
                continue;
            }
            // 修复原代码的笔误:col=3改为col+3,补充完整相等判断
            if (token == board[row+1][col+1] && 
                token == board[row+2][col+2] && 
                token == board[row+3][col+3]) {
                return true;
            }
        }
    }

    // 右上到左下对角线
    for(int row=0; row <= ROWS -4; row++) {
        for (int col = COLS - 1; col >= 3; --col) {
            char token = board[row][col];
            if (token == BLANK) {
                continue;
            }
            // 补充完整相等判断
            if (token == board[row+1][col-1] &&
                token == board[row+2][col-2] &&
                token == board[row+3][col-3]) {
                return true;
            }
        }
    }
    return false;
}

内容的提问来源于stack exchange,提问作者BundeeTM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 16:45:07