代码中没有明确说明异常的具体情况,因此无法确定需要修改哪一行代码。如果是因为返回了未定义的值,可以将 getBoard 函数中的 return 语句修改为:

return (x >= 0 && x < ROW && y >= 0 && y < COL) ? board[x][y] : 0;

这样,当输入的 x 和 y 不合法时,就会返回 0,避免了返回未定义的值。如果有其他异常,请在问题描述中提供更多的细节。

以下是完整的 C++ 五子棋游戏代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ROW = 15;
const int COL = 15;

class ChessBoard {
private:
    int board[ROW][COL];
public:
    ChessBoard() {
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                board[i][j] = 0;
            }
        }
    }
    void drawBoard() {
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                if (board[i][j] == 0) {
                    cout << '+ ';
                }
                else if (board[i][j] == 1) {
                    cout << 'o ';
                }
                else {
                    cout << 'x ';
                }
            }
            cout << endl;
        }
    }
    int getBoard(int x, int y) {
        return (x >= 0 && x < ROW && y >= 0 && y < COL) ? board[x][y] : 0;
    }
    void setBoard(int x, int y, int color) {
        board[x][y] = color;
    }
    void undo(int x, int y) {
        board[x][y] = 0;
    }
};

class ChessPiece {
private:
    int x, y, color;
public:
    ChessPiece(int x, int y, int color) {
        this->x = x;
        this->y = y;
        this->color = color;
    }
    int getX() {
        return x;
    }
    int getY() {
        return y;
    }
    int getColor() {
        return color;
    }
};

class Player {
private:
    string name;
    int color;
public:
    Player(string name, int color) {
        this->name = name;
        this->color = color;
    }
    string getName() {
        return name;
    }
    int getColor() {
        return color;
    }
};

class Game {
private:
    ChessBoard board;
    Player* player1;
    Player* player2;
public:
    Game() {
        player1 = new Player("Player1", 1);
        player2 = new Player("Computer", 2);
    }
    void play() {
        int turn = 1;
        int win = 0;
        int x, y;

        while (win == 0) {
            if (turn == 1) {
                cout << "Player1's turn: ";
                cin >> x >> y;
                if (board.getBoard(x, y) != 0) {
                    cout << "This position is already occupied!" << endl;
                    continue;
                }
                board.setBoard(x, y, player1->getColor());
            }
            else {
                cout << "Computer's turn: ";
                if (turn == 2) {
                    srand(time(NULL));
                    x = rand() % 15;
                    y = rand() % 15;
                }
                else {
                    // AI algorithm
                }
                if (board.getBoard(x, y) != 0) {
                    continue;
                }
                board.setBoard(x, y, player2->getColor());
            }
            board.drawBoard();

            // check win or draw
            if (checkWin(x, y)) {
                win = turn;
                if (win == 1) {
                    cout << "Player1 wins!" << endl;
                }
                else {
                    cout << "Computer wins!" << endl;
                }
            }
            else if (checkDraw()) {
                win = 3;
                cout << "Draw!" << endl;
            }

            // switch turn
            turn = (turn == 1) ? 2 : 1;
        }
    }
    bool checkWin(int x, int y) {
        int color = board.getBoard(x, y);
        int count = 1;

        // check row
        for (int i = x + 1; i < ROW && board.getBoard(i, y) == color; i++) {
            count++;
        }
        for (int i = x - 1; i >= 0 && board.getBoard(i, y) == color; i--) {
            count++;
        }
        if (count >= 5) {
            return true;
        }

        // check column
        count = 1;
        for (int i = y + 1; i < COL && board.getBoard(x, i) == color; i++) {
            count++;
        }
        for (int i = y - 1; i >= 0 && board.getBoard(x, i) == color; i--) {
            count++;
        }
        if (count >= 5) {
            return true;
        }

        // check diagonal
        count = 1;
        for (int i = x + 1, j = y + 1; i < ROW && j < COL && board.getBoard(i, j) == color; i++, j++) {
            count++;
        }
        for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board.getBoard(i, j) == color; i--, j--) {
            count++;
        }
        if (count >= 5) {
            return true;
        }

        // check anti-diagonal
        count = 1;
        for (int i = x + 1, j = y - 1; i < ROW && j >= 0 && board.getBoard(i, j) == color; i++, j--) {
            count++;
        }
        for (int i = x - 1, j = y + 1; i >= 0 && j < COL && board.getBoard(i, j) == color; i--, j++) {
            count++;
        }
        if (count >= 5) {
            return true;
        }

        return false;
    }
    bool checkDraw() {
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                if (board.getBoard(i, j) == 0) {
                    return false;
                }
            }
        }
        return true;
    }
    void reset() {
        board = ChessBoard();
    }
};

int main() {
    Game game;
    game.play();
    return 0;
}
C++ 五子棋游戏代码:解决 getBoard 函数异常问题

原文地址: https://www.cveoy.top/t/topic/oRXo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录