c++实现井字棋人工智能与人对战

实现一个井字棋(Tic-Tac-Toe)游戏,允许人工智能(AI)与人对战,需要在C++中编写一个游戏逻辑并实现AI的决策算法。下面是一个详细的实现步骤和示例代码。

实现步骤

  1. 定义游戏板和状态

    • 使用一个二维数组表示游戏板。
    • 定义常量表示玩家和AI的符号。
  2. 初始化游戏板

    • 创建一个函数初始化游戏板,将所有格子设为空。
  3. 打印游戏板

    • 创建一个函数打印当前游戏板状态。
  4. 检查胜利条件

    • 创建一个函数检查是否有玩家获胜或平局。
  5. 处理玩家移动

    • 创建一个函数处理玩家输入,更新游戏板。
  6. AI决策

    • 实现一个简单的AI算法(例如Minimax算法)来决定最佳移动。
  7. 游戏主循环

    • 在主函数中实现游戏循环,交替处理玩家和AI的移动,直到游戏结束。

示例代码

cpp
#include <iostream> #include <vector> #include <limits> using namespace std; const char PLAYER = 'X'; const char AI = 'O'; const char EMPTY = ' '; // 初始化游戏板 void initializeBoard(vector<vector<char>>& board) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = EMPTY; } } } // 打印游戏板 void printBoard(const vector<vector<char>>& board) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << board[i][j]; if (j < 2) cout << " | "; } cout << endl; if (i < 2) cout << "---------" << endl; } } // 检查胜利条件 bool checkWin(const vector<vector<char>>& board, char player) { // 检查行和列 for (int i = 0; i < 3; i++) { if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { return true; } } // 检查对角线 if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) || (board[0][2] == player && board[1][1] == player && board[2][0] == player)) { return true; } return false; } // 检查是否平局 bool checkDraw(const vector<vector<char>>& board) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == EMPTY) return false; } } return true; } // 玩家移动 void playerMove(vector<vector<char>>& board) { int row, col; while (true) { cout << "Enter your move (row and column): "; cin >> row >> col; if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == EMPTY) { board[row][col] = PLAYER; break; } else { cout << "Invalid move. Try again." << endl; } } } // Minimax算法实现AI决策 int minimax(vector<vector<char>>& board, bool isMaximizing) { if (checkWin(board, AI)) return 1; if (checkWin(board, PLAYER)) return -1; if (checkDraw(board)) return 0; if (isMaximizing) { int bestScore = numeric_limits<int>::min(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == EMPTY) { board[i][j] = AI; int score = minimax(board, false); board[i][j] = EMPTY; bestScore = max(score, bestScore); } } } return bestScore; } else { int bestScore = numeric_limits<int>::max(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == EMPTY) { board[i][j] = PLAYER; int score = minimax(board, true); board[i][j] = EMPTY; bestScore = min(score, bestScore); } } } return bestScore; } } // AI移动 void aiMove(vector<vector<char>>& board) { int bestScore = numeric_limits<int>::min(); int moveRow = -1; int moveCol = -1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == EMPTY) { board[i][j] = AI; int score = minimax(board, false); board[i][j] = EMPTY; if (score > bestScore) { bestScore = score; moveRow = i; moveCol = j; } } } } board[moveRow][moveCol] = AI; } int main() { vector<vector<char>> board(3, vector<char>(3)); initializeBoard(board); while (true) { printBoard(board); playerMove(board); if (checkWin(board, PLAYER)) { printBoard(board); cout << "Player wins!" << endl; break; } if (checkDraw(board)) { printBoard(board); cout << "It's a draw!" << endl; break; } aiMove(board); if (checkWin(board, AI)) { printBoard(board); cout << "AI wins!" << endl; break; } if (checkDraw(board)) { printBoard(board); cout << "It's a draw!" << endl; break; } } return 0; }

关键步骤解释

  1. 初始化游戏板

    cpp
    void initializeBoard(vector<vector<char>>& board) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = EMPTY; } } }
  2. 打印游戏板

    cpp
    void printBoard(const vector<vector<char>>& board) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << board[i][j]; if (j < 2) cout << " | "; } cout << endl; if (i