c++用easyX五子棋人机对战代码
时间: 2023-07-24 14:16:25 浏览: 175
以下是一个简单的C++ EasyX五子棋人机对战代码,使用了基本的minimax算法:
```c++
#include <graphics.h>
#include <conio.h>
#include <iostream>
#include <ctime>
using namespace std;
const int ROWS = 15;
const int COLS = 15;
const int BLOCK_SIZE = 40;
const int CHESS_RADIUS = 15;
int chessBoard[ROWS][COLS] = {0}; // 0表示空,1表示玩家,-1表示电脑
void init();
void drawChessBoard();
void playerMove();
void computerMove();
int evaluate();
int minimax(int depth, int alpha, int beta, bool isMaxPlayer);
int main()
{
init();
while (true)
{
playerMove();
if (evaluate() == 10000)
{
cout << "You win!" << endl;
break;
}
computerMove();
if (evaluate() == -10000)
{
cout << "You lose!" << endl;
break;
}
}
getch();
closegraph();
return 0;
}
void init()
{
initgraph(COLS * BLOCK_SIZE, ROWS * BLOCK_SIZE);
drawChessBoard();
srand(time(NULL));
}
void drawChessBoard()
{
setbkcolor(WHITE);
cleardevice();
setlinecolor(BLACK);
for (int i = 1; i < ROWS; i++)
{
line(BLOCK_SIZE, i * BLOCK_SIZE, COLS * BLOCK_SIZE - BLOCK_SIZE, i * BLOCK_SIZE);
}
for (int i = 1; i < COLS; i++)
{
line(i * BLOCK_SIZE, BLOCK_SIZE, i * BLOCK_SIZE, ROWS * BLOCK_SIZE - BLOCK_SIZE);
}
}
void playerMove()
{
int x, y;
while (true)
{
cout << "Enter the row and column you want to place your chess: ";
cin >> x >> y;
if (x >= 1 && x <= ROWS && y >= 1 && y <= COLS && chessBoard[x - 1][y - 1] == 0)
{
break;
}
else
{
cout << "Invalid move, please try again." << endl;
}
}
chessBoard[x - 1][y - 1] = 1;
setfillcolor(BLACK);
solidcircle(y * BLOCK_SIZE, x * BLOCK_SIZE, CHESS_RADIUS);
}
void computerMove()
{
int bestScore = INT_MIN;
int bestRow = -1;
int bestCol = -1;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (chessBoard[i][j] == 0)
{
chessBoard[i][j] = -1;
int score = minimax(0, INT_MIN, INT_MAX, false);
chessBoard[i][j] = 0;
if (score > bestScore)
{
bestScore = score;
bestRow = i;
bestCol = j;
}
}
}
}
chessBoard[bestRow][bestCol] = -1;
setfillcolor(RED);
solidcircle((bestCol + 1) * BLOCK_SIZE, (bestRow + 1) * BLOCK_SIZE, CHESS_RADIUS);
}
int evaluate()
{
// 检查行
for (int i = 0; i < ROWS; i++)
{
int sum = 0;
for (int j = 0; j < COLS; j++)
{
sum += chessBoard[i][j];
}
if (sum == 5)
{
return 10000;
}
else if (sum == -5)
{
return -10000;
}
}
// 检查列
for (int j = 0; j < COLS; j++)
{
int sum = 0;
for (int i = 0; i < ROWS; i++)
{
sum += chessBoard[i][j];
}
if (sum == 5)
{
return 10000;
}
else if (sum == -5)
{
return -10000;
}
}
// 检查对角线
int sum1 = 0, sum2 = 0;
for (int i = 0; i < ROWS; i++)
{
sum1 += chessBoard[i][i];
sum2 += chessBoard[i][COLS - 1 - i];
}
if (sum1 == 5 || sum2 == 5)
{
return 10000;
}
else if (sum1 == -5 || sum2 == -5)
{
return -10000;
}
return 0;
}
int minimax(int depth, int alpha, int beta, bool isMaxPlayer)
{
int score = evaluate();
if (score != 0)
{
return score;
}
if (depth == 3)
{
return 0;
}
if (isMaxPlayer)
{
int bestScore = INT_MIN;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (chessBoard[i][j] == 0)
{
chessBoard[i][j] = -1;
int score = minimax(depth + 1, alpha, beta, false);
chessBoard[i][j] = 0;
bestScore = max(bestScore, score);
alpha = max(alpha, bestScore);
if (beta <= alpha)
{
return bestScore;
}
}
}
}
return bestScore;
}
else
{
int bestScore = INT_MAX;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (chessBoard[i][j] == 0)
{
chessBoard[i][j] = 1;
int score = minimax(depth + 1, alpha, beta, true);
chessBoard[i][j] = 0;
bestScore = min(bestScore, score);
beta = min(beta, bestScore);
if (beta <= alpha)
{
return bestScore;
}
}
}
}
return bestScore;
}
}
```
注意:该代码只是一个简单的实现,并没有考虑很多复杂的情况,可以根据自己的需求进行修改和优化。
阅读全文