C++ 人机对战 3x3 棋盘游戏:Tic Tac Toe 实现
以下是一个使用 C++ 实现的人机下棋的代码示例,它包含了 3x3 棋盘的 Tic Tac Toe 游戏,用户可以选择是先下还是后下,并选择棋子是 'X' 还是 'O'。
#include <iostream>
#include <vector>
using namespace std;
class TicTacToe {
private:
int boardSize;
vector<vector<char>> board;
char playerPiece;
char computerPiece;
int playerScore;
int computerScore;
public:
TicTacToe(int size) {
boardSize = size;
board.resize(size, vector<char>(size, '*'));
playerScore = 0;
computerScore = 0;
}
void printBoard() {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void setPlayerPiece(char piece) {
playerPiece = piece;
computerPiece = (piece == 'X') ? 'O' : 'X';
}
void makeMove() {
int row, col;
cout << "Enter row and column to make a move (1-" << boardSize << "): ";
cin >> row >> col;
row--;
col--;
if (isValidMove(row, col)) {
board[row][col] = playerPiece;
playerScore += calculateScore(row, col, playerPiece);
printBoard();
if (checkWin(row, col, playerPiece)) {
cout << "Congratulations! You won!" << endl;
cout << "Player Score: " << playerScore << endl;
cout << "Computer Score: " << computerScore << endl;
return;
}
if (isBoardFull()) {
cout << "It's a draw!" << endl;
cout << "Player Score: " << playerScore << endl;
cout << "Computer Score: " << computerScore << endl;
return;
}
makeComputerMove();
} else {
cout << "Invalid move! Try again." << endl;
makeMove();
}
}
void makeComputerMove() {
int bestScore = INT_MIN;
int bestRow, bestCol;
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (board[i][j] == '*') {
int score = calculateScore(i, j, computerPiece);
if (score > bestScore) {
bestScore = score;
bestRow = i;
bestCol = j;
}
}
}
}
board[bestRow][bestCol] = computerPiece;
computerScore += bestScore;
printBoard();
if (checkWin(bestRow, bestCol, computerPiece)) {
cout << "Oh no! Computer won!" << endl;
cout << "Player Score: " << playerScore << endl;
cout << "Computer Score: " << computerScore << endl;
return;
}
if (isBoardFull()) {
cout << "It's a draw!" << endl;
cout << "Player Score: " << playerScore << endl;
cout << "Computer Score: " << computerScore << endl;
return;
}
makeMove();
}
bool isValidMove(int row, int col) {
return (row >= 0 && row < boardSize && col >= 0 && col < boardSize && board[row][col] == '*');
}
int calculateScore(int row, int col, char piece) {
int score = 0;
// Check row
int countX = 0, countO = 0;
for (int i = 0; i < boardSize; i++) {
if (board[row][i] == 'X') {
countX++;
} else if (board[row][i] == 'O') {
countO++;
}
}
if (piece == 'X') {
if (countX == boardSize - 1 && countO == 0) {
score += 50;
} else if (countO == boardSize - 1 && countX == 0) {
score += 25;
} else if (countX == boardSize - 2 && countO == 0) {
score += 10;
} else if (countO == boardSize - 2 && countX == 0) {
score += 8;
} else if (countX == boardSize - 2 && countO == 1) {
score += 4;
}
} else {
if (countO == boardSize - 1 && countX == 0) {
score += 50;
} else if (countX == boardSize - 1 && countO == 0) {
score += 25;
} else if (countO == boardSize - 2 && countX == 0) {
score += 10;
} else if (countX == boardSize - 2 && countO == 0) {
score += 8;
} else if (countO == boardSize - 2 && countX == 1) {
score += 4;
}
}
// Check column
countX = 0;
countO = 0;
for (int i = 0; i < boardSize; i++) {
if (board[i][col] == 'X') {
countX++;
} else if (board[i][col] == 'O') {
countO++;
}
}
if (piece == 'X') {
if (countX == boardSize - 1 && countO == 0) {
score += 50;
} else if (countO == boardSize - 1 && countX == 0) {
score += 25;
} else if (countX == boardSize - 2 && countO == 0) {
score += 10;
} else if (countO == boardSize - 2 && countX == 0) {
score += 8;
} else if (countX == boardSize - 2 && countO == 1) {
score += 4;
}
} else {
if (countO == boardSize - 1 && countX == 0) {
score += 50;
} else if (countX == boardSize - 1 && countO == 0) {
score += 25;
} else if (countO == boardSize - 2 && countX == 0) {
score += 10;
} else if (countX == boardSize - 2 && countO == 0) {
score += 8;
} else if (countO == boardSize - 2 && countX == 1) {
score += 4;
}
}
// Check diagonal
if (row == col) {
countX = 0;
countO = 0;
for (int i = 0; i < boardSize; i++) {
if (board[i][i] == 'X') {
countX++;
} else if (board[i][i] == 'O') {
countO++;
}
}
if (piece == 'X') {
if (countX == boardSize - 1 && countO == 0) {
score += 50;
} else if (countO == boardSize - 1 && countX == 0) {
score += 25;
} else if (countX == boardSize - 2 && countO == 0) {
score += 10;
} else if (countO == boardSize - 2 && countX == 0) {
score += 8;
} else if (countX == boardSize - 2 && countO == 1) {
score += 4;
}
} else {
if (countO == boardSize - 1 && countX == 0) {
score += 50;
} else if (countX == boardSize - 1 && countO == 0) {
score += 25;
} else if (countO == boardSize - 2 && countX == 0) {
score += 10;
} else if (countX == boardSize - 2 && countO == 0) {
score += 8;
} else if (countO == boardSize - 2 && countX == 1) {
score += 4;
}
}
}
// Check anti-diagonal
if (row + col == boardSize - 1) {
countX = 0;
countO = 0;
for (int i = 0; i < boardSize; i++) {
if (board[i][boardSize - 1 - i] == 'X') {
countX++;
} else if (board[i][boardSize - 1 - i] == 'O') {
countO++;
}
}
if (piece == 'X') {
if (countX == boardSize - 1 && countO == 0) {
score += 50;
} else if (countO == boardSize - 1 && countX == 0) {
score += 25;
} else if (countX == boardSize - 2 && countO == 0) {
score += 10;
} else if (countO == boardSize - 2 && countX == 0) {
score += 8;
} else if (countX == boardSize - 2 && countO == 1) {
score += 4;
}
} else {
if (countO == boardSize - 1 && countX == 0) {
score += 50;
} else if (countX == boardSize - 1 && countO == 0) {
score += 25;
} else if (countO == boardSize - 2 && countX == 0) {
score += 10;
} else if (countX == boardSize - 2 && countO == 0) {
score += 8;
} else if (countO == boardSize - 2 && countX == 1) {
score += 4;
}
}
}
return score;
}
bool checkWin(int row, int col, char piece) {
// Check row
bool isWin = true;
for (int i = 0; i < boardSize; i++) {
if (board[row][i] != piece) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
// Check column
isWin = true;
for (int i = 0; i < boardSize; i++) {
if (board[i][col] != piece) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
// Check diagonal
if (row == col) {
isWin = true;
for (int i = 0; i < boardSize; i++) {
if (board[i][i] != piece) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
}
// Check anti-diagonal
if (row + col == boardSize - 1) {
isWin = true;
for (int i = 0; i < boardSize; i++) {
if (board[i][boardSize - 1 - i] != piece) {
isWin = false;
break;
}
}
if (isWin) {
return true;
}
}
return false;
}
bool isBoardFull() {
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (board[i][j] == '*') {
return false;
}
}
}
return true;
}
};
int main() {
int boardSize;
char playerPiece;
char startPiece;
cout << "Enter board size (3 or greater): ";
cin >> boardSize;
TicTacToe game(boardSize);
cout << "Choose your piece (X or O): ";
cin >> playerPiece;
game.setPlayerPiece(playerPiece);
cout << "Do you want to start? (Y/N): ";
cin >> startPiece;
if (startPiece == 'Y' || startPiece == 'y') {
game.makeMove();
} else {
game.makeComputerMove();
}
return 0;
}
注意:这只是一个简单的实现示例,可能还有很多优化的空间,例如更智能的计算机下棋策略、更灵活的棋盘大小设置等。
原文地址: https://www.cveoy.top/t/topic/qp3c 著作权归作者所有。请勿转载和采集!