五子棋人机对战c++代码
时间: 2023-08-24 21:09:58 浏览: 81
C++五子棋人机对战代码
4星 · 用户满意度95%
以下是一个简单的五子棋人机对战的C代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define ROW 15
#define COL 15
#define PLAYER 1
#define COMPUTER 2
int board[ROW][COL] = {0};
void init_board()
{
int i, j;
for (i = 0; i < ROW; ++i) {
for (j = 0; j < COL; ++j) {
board[i][j] = 0;
}
}
}
void print_board()
{
int i, j;
for (i = 0; i < ROW; ++i) {
for (j = 0; j < COL; ++j) {
if (board[i][j] == 0) {
printf("+ ");
} else if (board[i][j] == PLAYER) {
printf("X ");
} else if (board[i][j] == COMPUTER) {
printf("O ");
}
}
printf("\n");
}
}
int is_win(int player)
{
int i, j, k;
for (i = 0; i < ROW; ++i) {
for (j = 0; j < COL; ++j) {
if (board[i][j] == player) {
for (k = 0; k < 5; ++k) {
if (i+k < ROW && board[i+k][j] == player &&
i+k-4 >= 0 && board[i+k-4][j] == player) {
return 1;
}
if (j+k < COL && board[i][j+k] == player &&
j+k-4 >= 0 && board[i][j+k-4] == player) {
return 1;
}
if (i+k < ROW && j+k < COL && board[i+k][j+k] == player &&
i+k-4 >= 0 && j+k-4 >= 0 && board[i+k-4][j+k-4] == player) {
return 1;
}
if (i+k < ROW && j-k >= 0 && board[i+k][j-k] == player &&
i+k-4 >= 0 && j-k+4 < COL && board[i+k-4][j-k+4] == player) {
return 1;
}
}
}
}
}
return 0;
}
int get_score(int player)
{
int i, j, k;
int score = 0;
for (i = 0; i < ROW; ++i) {
for (j = 0; j < COL; ++j) {
if (board[i][j] == player) {
for (k = 0; k < 5; ++k) {
if (i+k < ROW && board[i+k][j] == player &&
i+k-1 >= 0 && i+k+1 < ROW && board[i+k-1][j] == 0 && board[i+k+1][j] == 0) {
score += 100;
}
if (j+k < COL && board[i][j+k] == player &&
j+k-1 >= 0 && j+k+1 < COL && board[i][j+k-1] == 0 && board[i][j+k+1] == 0) {
score += 100;
}
if (i+k < ROW && j+k < COL && board[i+k][j+k] == player &&
i+k-1 >= 0 && j+k-1 >= 0 && i+k+1 < ROW && j+k+1 < COL &&
board[i+k-1][j+k-1] == 0 && board[i+k+1][j+k+1] == 0) {
score += 100;
}
if (i+k < ROW && j-k >= 0 && board[i+k][j-k] == player &&
i+k-1 >= 0 && j-k+1 < COL && i+k+1 < ROW && j-k-1 >= 0 &&
board[i+k-1][j-k+1] == 0 && board[i+k+1][j-k-1] == 0) {
score += 100;
}
}
}
}
}
return score;
}
int get_position(int *x, int *y)
{
int i, j;
int max_score = 0;
int score;
for (i = 0; i < ROW; ++i) {
for (j = 0; j < COL; ++j) {
if (board[i][j] == 0) {
score = get_score(COMPUTER);
if (score > max_score) {
max_score = score;
*x = i;
*y = j;
}
}
}
}
return max_score;
}
void player_move()
{
int x, y;
do {
printf("Your turn (x,y): ");
scanf("%d,%d", &x, &y);
} while (x < 0 || x >= ROW || y < 0 || y >= COL || board[x][y] != 0);
board[x][y] = PLAYER;
}
void computer_move()
{
int x, y;
get_position(&x, &y);
board[x][y] = COMPUTER;
printf("Computer move: (%d,%d)\n", x, y);
}
int main()
{
init_board();
print_board();
int turn = PLAYER;
while (1) {
if (turn == PLAYER) {
player_move();
print_board();
if (is_win(PLAYER)) {
printf("You win!\n");
break;
}
turn = COMPUTER;
} else {
computer_move();
print_board();
if (is_win(COMPUTER)) {
printf("Computer win!\n");
break;
}
turn = PLAYER;
}
}
return 0;
}
```
这个代码实现了五子棋的基本功能,包括初始化棋盘、打印棋盘、判断胜负、评估落子位置和人机轮流下棋等。您可以在此基础上进行更复杂的优化和扩展。
阅读全文