C++五子棋人机对战的代码
时间: 2023-09-02 14:14:02 浏览: 206
以下是一个简单的C++五子棋人机对战代码,仅供参考:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
const int BOARD_SIZE = 15;
enum class ChessType {
Empty,
Black,
White
};
struct ChessBoard {
ChessType board[BOARD_SIZE][BOARD_SIZE];
ChessBoard() {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
board[i][j] = ChessType::Empty;
}
}
}
bool inBound(int x, int y) const {
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
}
bool isEmpty(int x, int y) const {
return inBound(x, y) && board[x][y] == ChessType::Empty;
}
bool isFive(int x, int y, ChessType chessType) const {
int dx[] = { -1, 0, 1, 1 };
int dy[] = { 1, 1, 1, 0 };
for (int i = 0; i < 4; ++i) {
int cnt = 1;
int tx = x + dx[i];
int ty = y + dy[i];
while (inBound(tx, ty) && board[tx][ty] == chessType) {
++cnt;
tx += dx[i];
ty += dy[i];
}
tx = x - dx[i];
ty = y - dy[i];
while (inBound(tx, ty) && board[tx][ty] == chessType) {
++cnt;
tx -= dx[i];
ty -= dy[i];
}
if (cnt >= 5) {
return true;
}
}
return false;
}
};
struct ChessPos {
int x, y;
int score;
ChessPos(int xx, int yy, int ss) : x(xx), y(yy), score(ss) {}
};
class AI {
public:
ChessPos getBestMove(const ChessBoard& board, ChessType chessType) {
vector<ChessPos> allMoves = generateMoves(board);
int maxScore = INT_MIN;
ChessPos bestMove(-1, -1, 0);
for (auto& move : allMoves) {
ChessBoard newBoard = board;
newBoard.board[move.x][move.y] = chessType;
int score = evaluateBoard(newBoard, chessType);
move.score = score;
if (score > maxScore) {
maxScore = score;
bestMove = move;
}
}
return bestMove;
}
private:
vector<ChessPos> generateMoves(const ChessBoard& board) {
vector<ChessPos> allMoves;
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board.isEmpty(i, j)) {
allMoves.emplace_back(i, j, 0);
}
}
}
return allMoves;
}
int evaluateBoard(const ChessBoard& board, ChessType chessType) {
int score = 0;
int dx[] = { -1, 0, 1, 1 };
int dy[] = { 1, 1, 1, 0 };
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board.board[i][j] == chessType) {
for (int k = 0; k < 4; ++k) {
int cnt = 1;
int tx = i + dx[k];
int ty = j + dy[k];
while (board.inBound(tx, ty) && board.board[tx][ty] == chessType) {
++cnt;
tx += dx[k];
ty += dy[k];
}
tx = i - dx[k];
ty = j - dy[k];
while (board.inBound(tx, ty) && board.board[tx][ty] == chessType) {
++cnt;
tx -= dx[k];
ty -= dy[k];
}
if (cnt >= 5) {
return INT_MAX;
}
score += cnt * cnt;
}
}
}
}
return score;
}
};
void printBoard(const ChessBoard& board) {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
switch (board.board[i][j]) {
case ChessType::Empty:
cout << "+";
break;
case ChessType::Black:
cout << "X";
break;
case ChessType::White:
cout << "O";
break;
}
}
cout << endl;
}
}
int main() {
srand(time(NULL));
ChessBoard board;
AI ai;
while (true) {
printBoard(board);
cout << "Your turn (row col): ";
int x, y;
cin >> x >> y;
if (board.isEmpty(x, y)) {
board.board[x][y] = ChessType::Black;
}
else {
cout << "Invalid move!" << endl;
continue;
}
if (board.isFive(x, y, ChessType::Black)) {
cout << "You win!" << endl;
break;
}
ChessPos aiMove = ai.getBestMove(board, ChessType::White);
board.board[aiMove.x][aiMove.y] = ChessType::White;
cout << "AI's move: " << aiMove.x << " " << aiMove.y << endl;
if (board.isFive(aiMove.x, aiMove.y, ChessType::White)) {
cout << "AI wins!" << endl;
break;
}
}
return 0;
}
```
这个代码使用了极小化极大算法和评估函数来进行人机对战。玩家使用X表示,AI使用O表示。玩家先手,如果玩家或AI先五子连珠,则游戏结束。