五子棋c++人机对战
时间: 2024-04-27 16:18:29 浏览: 203
五子棋是一种非常经典的棋类游戏,它的规则简单易懂,但是却有很高的策略性。在C++中实现五子棋的人机对战可以通过以下步骤来完成:
1. 创建棋盘:首先需要创建一个二维数组来表示棋盘,可以使用一个大小为15x15的数组来表示。每个数组元素可以用来表示棋盘上的一个位置,初始值可以设为0表示空位。
2. 绘制棋盘:可以使用C++的图形库或者控制台输出来绘制棋盘,将棋盘的状态以图形化的方式展示出来。
3. 下棋操作:通过交替输入玩家和电脑的下棋位置来进行游戏。玩家可以通过鼠标点击或者键盘输入来选择下棋位置,而电脑则需要通过算法来选择最佳下棋位置。
4. 判断胜负:在每次下棋后,需要判断是否有一方获胜。可以通过检查横向、纵向、斜向是否有连续的五个相同颜色的棋子来判断胜负。
5. AI算法:实现电脑的下棋算法是实现人机对战的关键。常用的算法包括极大极小搜索算法、Alpha-Beta剪枝算法等。这些算法可以评估当前棋盘状态的价值,并选择最佳的下棋位置。
6. 游戏结束:当有一方获胜或者棋盘填满时,游戏结束。可以根据游戏结果输出相应的提示信息,并询问玩家是否继续游戏。
相关问题
用c++做个五子棋,人机对战
好的,以下是一个简单的五子棋人机对战的 C++ 代码,供您参考:
```c++
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int ROW = 15;
const int COL = 15;
char board[ROW][COL]; // 棋盘
char player = 'X'; // 玩家棋子
char computer = 'O'; // 电脑棋子
// 初始化棋盘
void InitBoard()
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
board[i][j] = '+';
}
}
}
// 显示棋盘
void DisplayBoard()
{
cout << " ";
for (int i = 0; i < COL; i++)
{
cout << " " << i + 1;
}
cout << endl;
for (int i = 0; i < ROW; i++)
{
cout << i + 1 << " ";
for (int j = 0; j < COL; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
// 玩家下棋
void PlayerMove()
{
int x, y;
cout << "请输入你要下棋的位置(行 列):";
cin >> x >> y;
while (x < 1 || x > ROW || y < 1 || y > COL || board[x - 1][y - 1] != '+')
{
cout << "输入有误,请重新输入:";
cin >> x >> y;
}
board[x - 1][y - 1] = player;
}
// 电脑下棋
void ComputerMove()
{
int x, y;
srand((unsigned)time(NULL));
do
{
x = rand() % ROW;
y = rand() % COL;
} while (board[x][y] != '+');
board[x][y] = computer;
}
// 判断游戏是否结束
bool IsGameOver()
{
// 横向判断
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL - 4; j++)
{
if (board[i][j] != '+' && board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] == board[i][j + 3] && board[i][j] == board[i][j + 4])
{
return true;
}
}
}
// 纵向判断
for (int i = 0; i < ROW - 4; i++)
{
for (int j = 0; j < COL; j++)
{
if (board[i][j] != '+' && board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] == board[i + 3][j] && board[i][j] == board[i + 4][j])
{
return true;
}
}
}
// 左上到右下斜向判断
for (int i = 0; i < ROW - 4; i++)
{
for (int j = 0; j < COL - 4; j++)
{
if (board[i][j] != '+' && board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4])
{
return true;
}
}
}
// 右上到左下斜向判断
for (int i = 0; i < ROW - 4; i++)
{
for (int j = 4; j < COL; j++)
{
if (board[i][j] != '+' && board[i][j] == board[i + 1][j - 1] && board[i][j] == board[i + 2][j - 2] && board[i][j] == board[i + 3][j - 3] && board[i][j] == board[i + 4][j - 4])
{
return true;
}
}
}
// 棋盘已满,平局
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (board[i][j] == '+')
{
return false;
}
}
}
return true;
}
int main()
{
InitBoard(); // 初始化棋盘
while (!IsGameOver())
{
DisplayBoard(); // 显示棋盘
PlayerMove(); // 玩家下棋
if (IsGameOver())
{
break;
}
ComputerMove(); // 电脑下棋
}
DisplayBoard(); // 显示最终棋盘
if (IsGameOver())
{
cout << "游戏结束!";
if (player == board[0][0])
{
cout << "玩家胜利!";
}
else if (computer == board[0][0])
{
cout << "电脑胜利!";
}
else
{
cout << "平局!";
}
}
return 0;
}
```
以上是一个简单的五子棋人机对战的 C++ 代码,其中包括棋盘的初始化、显示、玩家和电脑下棋、游戏是否结束的判断等功能。您可以根据需要进行修改和扩展。
c++五子棋人机对战代码
以下是一个简单的五子棋人机对战代码,使用了简单的极大极小算法和alpha-beta剪枝优化:
```c
#include <stdio.h>
#include <stdlib.h>
#define SIZE 15
int board[SIZE][SIZE] = {0}; // 棋盘
int player = 1; // 玩家编号
int computer = -1; // 电脑编号
// 初始化棋盘
void init_board()
{
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
board[i][j] = 0;
}
}
}
// 打印棋盘
void print_board()
{
int i, j;
printf(" ");
for (i = 0; i < SIZE; i++) {
printf("%2d ", i+1);
}
printf("\n");
for (i = 0; i < SIZE; i++) {
printf("%2d ", i+1);
for (j = 0; j < SIZE; j++) {
if (board[i][j] == player) {
printf("● ");
} else if (board[i][j] == computer) {
printf("○ ");
} else {
printf(" ");
}
}
printf("\n");
}
}
// 判断是否有五子连珠
int check_win(int player)
{
int i, j, k;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (board[i][j] == player) {
for (k = 0; k < 5; k++) {
if (i+k < SIZE && board[i+k][j] == player && k == 4) {
return 1;
}
if (j+k < SIZE && board[i][j+k] == player && k == 4) {
return 1;
}
if (i+k < SIZE && j+k < SIZE && board[i+k][j+k] == player && k == 4) {
return 1;
}
if (i+k < SIZE && j-k >= 0 && board[i+k][j-k] == player && k == 4) {
return 1;
}
}
}
}
}
return 0;
}
// 评估棋局价值
int evaluate()
{
int value = 0;
int i, j, k;
// 横向
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE-4; j++) {
int count = 0;
for (k = 0; k < 5; k++) {
if (board[i][j+k] == computer) {
count++;
} else if (board[i][j+k] == player) {
count = 0;
break;
}
}
if (count == 1) {
value += 10;
} else if (count == 2) {
value += 100;
} else if (count == 3) {
value += 1000;
} else if (count == 4) {
value += 10000;
}
}
}
// 纵向
for (j = 0; j < SIZE; j++) {
for (i = 0; i < SIZE-4; i++) {
int count = 0;
for (k = 0; k < 5; k++) {
if (board[i+k][j] == computer) {
count++;
} else if (board[i+k][j] == player) {
count = 0;
break;
}
}
if (count == 1) {
value += 10;
} else if (count == 2) {
value += 100;
} else if (count == 3) {
value += 1000;
} else if (count == 4) {
value += 10000;
}
}
}
// 斜向
for (i = 0; i < SIZE-4; i++) {
for (j = 0; j < SIZE-4; j++) {
int count = 0;
for (k = 0; k < 5; k++) {
if (board[i+k][j+k] == computer) {
count++;
} else if (board[i+k][j+k] == player) {
count = 0;
break;
}
}
if (count == 1) {
value += 10;
} else if (count == 2) {
value += 100;
} else if (count == 3) {
value += 1000;
} else if (count == 4) {
value += 10000;
}
}
}
// 反斜向
for (i = 4; i < SIZE; i++) {
for (j = 0; j < SIZE-4; j++) {
int count = 0;
for (k = 0; k < 5; k++) {
if (board[i-k][j+k] == computer) {
count++;
} else if (board[i-k][j+k] == player) {
count = 0;
break;
}
}
if (count == 1) {
value += 10;
} else if (count == 2) {
value += 100;
} else if (count == 3) {
value += 1000;
} else if (count == 4) {
value += 10000;
}
}
}
return value;
}
// 判断是否是合法落子
int is_valid(int x, int y)
{
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
return 0;
}
if (board[x][y] != 0) {
return 0;
}
return 1;
}
// 极大极小算法
int minmax(int depth, int alpha, int beta)
{
if (depth == 0) {
return evaluate();
}
int i, j, value;
if (player == computer) { // 电脑落子
value = -99999;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (is_valid(i, j)) {
board[i][j] = computer;
player = player * -1;
int tmp = minmax(depth-1, alpha, beta);
board[i][j] = 0;
player = player * -1;
if (tmp > value) {
value = tmp;
}
if (value > alpha) {
alpha = value;
}
if (alpha >= beta) {
return value;
}
}
}
}
return value;
} else { // 玩家落子
value = 99999;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (is_valid(i, j)) {
board[i][j] = player;
computer = computer * -1;
int tmp = minmax(depth-1, alpha, beta);
board[i][j] = 0;
computer = computer * -1;
if (tmp < value) {
value = tmp;
}
if (value < beta) {
beta = value;
}
if (alpha >= beta) {
return value;
}
}
}
}
return value;
}
}
// 电脑落子
void computer_play()
{
int x = -1, y = -1;
int i, j;
int value = -99999;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (is_valid(i, j)) {
board[i][j] = computer;
player = player * -1;
int tmp = minmax(4, -99999, 99999);
board[i][j] = 0;
player = player * -1;
if (tmp > value) {
value = tmp;
x = i;
y = j;
}
}
}
}
board[x][y] = computer;
}
int main()
{
init_board();
print_board();
while (1) {
printf("请玩家输入坐标(x,y):");
int x, y;
scanf("%d,%d", &x, &y);
if (is_valid(x-1, y-1)) {
board[x-1][y-1] = player;
if (check_win(player)) {
printf("玩家胜利!\n");
break;
}
computer_play();
if (check_win(computer)) {
printf("电脑胜利!\n");
break;
}
print_board();
} else {
printf("非法坐标!\n");
}
}
return 0;
}
```
这个代码只是简单实现了五子棋的人机对战,如果要实现更高级的AI,需要使用更复杂的算法和优化。
阅读全文