下面的代码有异常请改正一下#include iostream#include cstdlib#include ctimeusing namespace std;const int ROW = 15;const int COL = 15;class ChessBoard private int boardROWCOL;public ChessBoard for int i
修改后的代码如下:
#include
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) { if (x < 0 || x >= ROW || y < 0 || y >= COL) { return -1; } return board[x][y]; } void setBoard(int x, int y, int color) { if (x < 0 || x >= ROW || y < 0 || y >= COL) { return; } board[x][y] = color; } void undo(int x, int y) { if (x < 0 || x >= ROW || y < 0 || y >= COL) { return; } 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); } ~Game() { delete player1; delete player2; } 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);
if (color == -1) {
return false;
}
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;
原文地址: https://www.cveoy.top/t/topic/ho7F 著作权归作者所有。请勿转载和采集!