C++ 井字棋游戏实现:功能详解及测试截图
该程序是一个井字棋游戏,具有以下功能:
-
构造函数
TicTacToe(int s):根据用户输入的尺寸s创建一个井字棋游戏的实例,初始化游戏板board为大小为s的二维向量,初始值为'*'。 -
void startGame():开始游戏,根据用户输入选择游戏符号 ('X' 或 'O'),选择是否先手,然后根据选择调用playGame()函数开始游戏。 -
void playGame():游戏进行的主要逻辑,轮流玩家和计算机进行移动,直至游戏结束。 -
void playerMove():玩家移动,根据玩家输入的行和列,在游戏板上放置相应的符号。 -
void computerMove():计算机移动,根据当前游戏板的状态计算最佳移动位置,并在游戏板上放置相应的符号。 -
int calculateScore(int row, int col):计算某个位置的得分,根据当前游戏板的状态判断该位置在胜利条件下的得分。 -
bool isGameOver():判断游戏是否结束,检查所有行、列和对角线上的符号是否相同。 -
void printBoard():打印当前游戏板的状态。
下面是测试截图:
- 启动游戏并选择符号和先手:

- 玩家和计算机轮流进行移动:

- 游戏结束并显示结果:

#include
public:
TicTacToe(int s) {
size = s;
board.resize(size, vector
void startGame() {
cout << 'Welcome to Tic Tac Toe!' << endl;
cout << 'Choose your symbol (X/O): ';
cin >> playerSymbol;
while (playerSymbol != 'X' && playerSymbol != 'O') {
cout << 'Invalid symbol. Please choose again: ';
cin >> playerSymbol;
}
cout << 'Do you want to play first? (Y/N): ';
char choice;
cin >> choice;
while (choice != 'Y' && choice != 'N') {
cout << 'Invalid choice. Please choose again: ';
cin >> choice;
}
if (choice == 'Y') {
cout << 'You play first.' << endl;
computerSymbol = (playerSymbol == 'X') ? 'O' : 'X';
printBoard();
playGame();
} else {
cout << 'Computer plays first.' << endl;
computerSymbol = (playerSymbol == 'X') ? 'X' : 'O';
playGame();
}
}
void playGame() {
bool playerTurn = (playerSymbol == 'X');
bool gameOver = false;
while (!gameOver) {
if (playerTurn) {
playerMove();
printBoard();
if (isGameOver()) {
cout << 'You win!' << endl;
gameOver = true;
}
playerTurn = false;
} else {
computerMove();
printBoard();
if (isGameOver()) {
cout << 'Computer wins!' << endl;
gameOver = true;
}
playerTurn = true;
}
}
}
void playerMove() {
int row, col;
cout << 'Enter the row and column to place your symbol (1-' << size << '): ';
cin >> row >> col;
while (row < 1 || row > size || col < 1 || col > size || board[row-1][col-1] != '*') {
cout << 'Invalid move. Please enter again: ';
cin >> row >> col;
}
board[row-1][col-1] = playerSymbol;
}
void computerMove() {
int maxScore = 0;
int maxRow, maxCol;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == '*') {
int score = calculateScore(i, j);
if (score > maxScore) {
maxScore = score;
maxRow = i;
maxCol = j;
}
}
}
}
board[maxRow][maxCol] = computerSymbol;
cout << 'Computer placed symbol at row ' << maxRow+1 << ', column ' << maxCol+1 << endl;
}
int calculateScore(int row, int col) {
int score = 0;
// Check row
int xCount = 0, oCount = 0, emptyCount = 0;
for (int j = 0; j < size; j++) {
if (board[row][j] == 'X') {
xCount++;
} else if (board[row][j] == 'O') {
oCount++;
} else {
emptyCount++;
}
}
score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
score += (emptyCount == size) ? 4 : 0;
// Check column
xCount = 0, oCount = 0, emptyCount = 0;
for (int i = 0; i < size; i++) {
if (board[i][col] == 'X') {
xCount++;
} else if (board[i][col] == 'O') {
oCount++;
} else {
emptyCount++;
}
}
score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
score += (emptyCount == size) ? 4 : 0;
// Check diagonal (top-left to bottom-right)
if (row == col) {
xCount = 0, oCount = 0, emptyCount = 0;
for (int i = 0; i < size; i++) {
if (board[i][i] == 'X') {
xCount++;
} else if (board[i][i] == 'O') {
oCount++;
} else {
emptyCount++;
}
}
score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
score += (emptyCount == size) ? 4 : 0;
}
// Check diagonal (top-right to bottom-left)
if (row + col == size - 1) {
xCount = 0, oCount = 0, emptyCount = 0;
for (int i = 0; i < size; i++) {
if (board[i][size-i-1] == 'X') {
xCount++;
} else if (board[i][size-i-1] == 'O') {
oCount++;
} else {
emptyCount++;
}
}
score += (xCount == size-1 && emptyCount == 1) ? 50 : 0;
score += (oCount == size-1 && emptyCount == 1) ? 25 : 0;
score += (xCount == size-2 && emptyCount == 2) ? 10 : 0;
score += (oCount == size-2 && emptyCount == 2) ? 8 : 0;
score += (emptyCount == size) ? 4 : 0;
}
return score;
}
bool isGameOver() {
// Check rows
for (int i = 0; i < size; i++) {
bool rowWin = true;
for (int j = 0; j < size-1; j++) {
if (board[i][j] != board[i][j+1]) {
rowWin = false;
break;
}
}
if (rowWin && board[i][0] != '*') {
return true;
}
}
// Check columns
for (int j = 0; j < size; j++) {
bool colWin = true;
for (int i = 0; i < size-1; i++) {
if (board[i][j] != board[i+1][j]) {
colWin = false;
break;
}
}
if (colWin && board[0][j] != '*') {
return true;
}
}
// Check diagonal (top-left to bottom-right)
bool diag1Win = true;
for (int i = 0; i < size-1; i++) {
if (board[i][i] != board[i+1][i+1]) {
diag1Win = false;
break;
}
}
if (diag1Win && board[0][0] != '*') {
return true;
}
// Check diagonal (top-right to bottom-left)
bool diag2Win = true;
for (int i = 0; i < size-1; i++) {
if (board[i][size-i-1] != board[i+1][size-i-2]) {
diag2Win = false;
break;
}
}
if (diag2Win && board[0][size-1] != '*') {
return true;
}
return false;
}
void printBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << board[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
};
int main() { int size; cout << 'Enter the size of the board: '; cin >> size; TicTacToe game(size); game.startGame(); return 0;
原文地址: https://www.cveoy.top/t/topic/qjMp 著作权归作者所有。请勿转载和采集!