六子棋博弈系统实现人机对打的c语言程序代码
时间: 2024-06-09 18:11:45 浏览: 67
以下是一个简单的六子棋博弈系统实现人机对打的C语言程序代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 15
#define COL 15
#define PLAYER1 'X'
#define PLAYER2 'O'
#define EMPTY ' '
#define MAX_DEPTH 5
int board[ROW][COL];
int player = 1;
void initBoard()
{
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
board[i][j] = EMPTY;
}
}
}
void printBoard()
{
int i, j;
printf(" ");
for (i = 0; i < ROW; i++) {
printf("%d ", i + 1);
}
printf("\n");
for (i = 0; i < ROW; i++) {
printf("%d ", i + 1);
for (j = 0; j < COL; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int isFull()
{
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == EMPTY) {
return 0;
}
}
}
return 1;
}
int checkHorizontal(int row, int col, int player)
{
int count = 0;
// check left
int i;
for (i = col; i >= 0; i--) {
if (board[row][i] == player) {
count++;
} else {
break;
}
}
// check right
for (i = col + 1; i < COL; i++) {
if (board[row][i] == player) {
count++;
} else {
break;
}
}
return count;
}
int checkVertical(int row, int col, int player)
{
int count = 0;
// check up
int i;
for (i = row; i >= 0; i--) {
if (board[i][col] == player) {
count++;
} else {
break;
}
}
// check down
for (i = row + 1; i < ROW; i++) {
if (board[i][col] == player) {
count++;
} else {
break;
}
}
return count;
}
int checkDiagonal(int row, int col, int player)
{
int count = 0;
// check up-left
int i, j;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
// check down-right
for (i = row + 1, j = col + 1; i < ROW && j < COL; i++, j++) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
if (count >= 5) {
return count;
}
count = 0;
// check up-right
for (i = row, j = col; i >= 0 && j < COL; i--, j++) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
// check down-left
for (i = row + 1, j = col - 1; i < ROW && j >= 0; i++, j--) {
if (board[i][j] == player) {
count++;
} else {
break;
}
}
return count;
}
int checkWin(int row, int col, int player)
{
int count;
count = checkHorizontal(row, col, player);
if (count >= 5) {
return count;
}
count = checkVertical(row, col, player);
if (count >= 5) {
return count;
}
count = checkDiagonal(row, col, player);
if (count >= 5) {
return count;
}
return 0;
}
int evaluate(int player)
{
int i, j;
int score = 0;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == player) {
int count = checkWin(i, j, player);
if (count == 5) {
score += 10000;
} else if (count == 4) {
score += 1000;
} else if (count == 3) {
score += 100;
} else if (count == 2) {
score += 10;
} else if (count == 1) {
score += 1;
}
}
}
}
return score;
}
int minimax(int depth, int alpha, int beta, int player)
{
if (depth == MAX_DEPTH || isFull()) {
return evaluate(player);
}
int i, j;
int max = -1000000;
int min = 1000000;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = player;
int score = minimax(depth + 1, alpha, beta, (player == 1) ? 2 : 1);
board[i][j] = EMPTY;
if (player == 1) {
if (score > max) {
max = score;
}
if (max > alpha) {
alpha = max;
}
if (beta <= alpha) {
return max;
}
} else {
if (score < min) {
min = score;
}
if (min < beta) {
beta = min;
}
if (beta <= alpha) {
return min;
}
}
}
}
}
return (player == 1) ? max : min;
}
void computerMove()
{
int i, j;
int max = -1000000;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = 2;
int score = minimax(0, -1000000, 1000000, 1);
board[i][j] = EMPTY;
if (score > max) {
max = score;
player = 2;
}
}
}
}
do {
i = rand() % ROW;
j = rand() % COL;
} while (board[i][j] != EMPTY);
board[i][j] = player;
}
int main()
{
int row, col;
int count;
srand(time(NULL));
initBoard();
do {
printBoard();
if (player == 1) {
printf("Player 1's turn (X): ");
scanf("%d %d", &row, &col);
row--;
col--;
if (row < 0 || row >= ROW || col < 0 || col >= COL) {
printf("Invalid move!\n");
continue;
}
if (board[row][col] != EMPTY) {
printf("Invalid move!\n");
continue;
}
board[row][col] = PLAYER1;
} else {
printf("Computer's turn (O)...\n");
computerMove();
}
count = checkWin(row, col, player);
if (count >= 5) {
printBoard();
if (player == 1) {
printf("Player 1 wins!\n");
} else {
printf("Computer wins!\n");
}
break;
}
player = (player == 1) ? 2 : 1;
} while (!isFull());
if (count < 5) {
printBoard();
printf("Draw!\n");
}
return 0;
}
```
该代码实现了一个简单的六子棋博弈系统,其中包含了人机对打的功能。玩家使用字符'X',计算机使用字符'O'。计算机使用了alpha-beta剪枝算法来提高搜索效率。玩家先手,计算机后手。运行程序后,按照提示输入行和列的位置即可进行下棋。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)