C语言五子棋人机对战
时间: 2023-12-20 07:05:18 浏览: 115
以下是C语言五子棋人机对战的代码实现,其中包括了人机对战的基本逻辑和算法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_ROW 15
#define MAX_COL 15
#define CHESSBOARD_SIZE (MAX_ROW * MAX_COL)
#define PLAYER_CHESS 'X'
#define AI_CHESS 'O'
#define EMPTY_CHESS ' '
#define PLAYER_WIN 1
#define AI_WIN 2
#define DRAW 3
#define CONTINUE 4
char chessboard[MAX_ROW][MAX_COL];
void init_chessboard() {
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
chessboard[i][j] = EMPTY_CHESS;
}
}
}
void print_chessboard() {
printf(" ");
for (int i = 0; i < MAX_COL; i++) {
printf(" %d", i + 1);
}
printf("\n");
for (int i = 0; i < MAX_ROW; i++) {
printf("%c", 'A' + i);
for (int j = 0; j < MAX_COL; j++) {
printf(" %c", chessboard[i][j]);
}
printf("\n");
}
}
int check_win(char chess) {
// 检查行
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j <= MAX_COL - 5; j++) {
if (chessboard[i][j] == chess && chessboard[i][j+1] == chess && chessboard[i][j+2] == chess && chessboard[i][j+3] == chess && chessboard[i][j+4] == chess) {
return chess == PLAYER_CHESS ? PLAYER_WIN : AI_WIN;
}
}
}
// 检查列
for (int i = 0; i <= MAX_ROW - 5; i++) {
for (int j = 0; j < MAX_COL; j++) {
if (chessboard[i][j] == chess && chessboard[i+1][j] == chess && chessboard[i+2][j] == chess && chessboard[i+3][j] == chess && chessboard[i+4][j] == chess) {
return chess == PLAYER_CHESS ? PLAYER_WIN : AI_WIN;
}
}
}
// 检查正对角线
for (int i = 0; i <= MAX_ROW - 5; i++) {
for (int j = 0; j <= MAX_COL - 5; j++) {
if (chessboard[i][j] == chess && chessboard[i+1][j+1] == chess && chessboard[i+2][j+2] == chess && chessboard[i+3][j+3] == chess && chessboard[i+4][j+4] == chess) {
return chess == PLAYER_CHESS ? PLAYER_WIN : AI_WIN;
}
}
}
// 检查反对角线
for (int i = 0; i <= MAX_ROW - 5; i++) {
for (int j = 4; j < MAX_COL; j++) {
if (chessboard[i][j] == chess && chessboard[i+1][j-1] == chess && chessboard[i+2][j-2] == chess && chessboard[i+3][j-3] == chess && chessboard[i+4][j-4] == chess) {
return chess == PLAYER_CHESS ? PLAYER_WIN : AI_WIN;
}
}
}
// 检查平局
int count = 0;
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
if (chessboard[i][j] != EMPTY_CHESS) {
count++;
}
}
}
if (count == CHESSBOARD_SIZE) {
return DRAW;
}
// 游戏继续
return CONTINUE;
}
int player_move() {
char row, col;
printf("请输入您要下棋的位置(例如:A1):");
scanf(" %c%c", &row, &col);
int i = row - 'A';
int j = col - '1';
if (i < 0 || i >= MAX_ROW || j < 0 || j >= MAX_COL || chessboard[i][j] != EMPTY_CHESS) {
printf("您输入的位置不合法,请重新输入!\n");
return player_move();
}
chessboard[i][j] = PLAYER_CHESS;
return check_win(PLAYER_CHESS);
}
int ai_move() {
int max_score = 0;
int max_i = 0, max_j = 0;
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
if (chessboard[i][j] == EMPTY_CHESS) {
int score = 0;
// 检查行
for (int k = 0; k <= 4; k++) {
if (j + k < MAX_COL && chessboard[i][j+k] == AI_CHESS) {
score++;
} else {
break;
}
}
for (int k = 1; k <= 4; k++) {
if (j - k >= 0 && chessboard[i][j-k] == AI_CHESS) {
score++;
} else {
break;
}
}
if (score > max_score) {
max_score = score;
max_i = i;
max_j = j;
}
// 检查列
score = 0;
for (int k = 0; k <= 4; k++) {
if (i + k < MAX_ROW && chessboard[i+k][j] == AI_CHESS) {
score++;
} else {
break;
}
}
for (int k = 1; k <= 4; k++) {
if (i - k >= 0 && chessboard[i-k][j] == AI_CHESS) {
score++;
} else {
break;
}
}
if (score > max_score) {
max_score = score;
max_i = i;
max_j = j;
}
// 检查正对角线
score = 0;
for (int k = 0; k <= 4; k++) {
if (i + k < MAX_ROW && j + k < MAX_COL && chessboard[i+k][j+k] == AI_CHESS) {
score++;
} else {
break;
}
}
for (int k = 1; k <= 4; k++) {
if (i - k >= 0 && j - k >= 0 && chessboard[i-k][j-k] == AI_CHESS) {
score++;
} else {
break;
}
}
if (score > max_score) {
max_score = score;
max_i = i;
max_j = j;
}
// 检查反对角线
score = 0;
for (int k = 0; k <= 4; k++) {
if (i + k < MAX_ROW && j - k >= 0 && chessboard[i+k][j-k] == AI_CHESS) {
score++;
} else {
break;
}
}
for (int k = 1; k <= 4; k++) {
if (i - k >= 0 && j + k < MAX_COL && chessboard[i-k][j+k] == AI_CHESS) {
score++;
} else {
break;
}
}
if (score > max_score) {
max_score = score;
max_i = i;
max_j = j;
}
}
}
}
chessboard[max_i][max_j] = AI_CHESS;
return check_win(AI_CHESS);
}
int main() {
srand(time(NULL));
init_chessboard();
print_chessboard();
int turn = rand() % 2;
while (1) {
if (turn == 0) {
printf("轮到您下棋了!\n");
int result = player_move();
print_chessboard();
if (result == PLAYER_WIN) {
printf("恭喜您获胜了!\n");
break;
} else if (result == DRAW) {
printf("平局!\n");
break;
}
turn = 1;
} else {
printf("轮到电脑下棋了!\n");
int result = ai_move();
print_chessboard();
if (result == AI_WIN) {
printf("很遗憾,您输了!\n");
break;
} else if (result == DRAW) {
printf("平局!\n");
break;
}
turn = 0;
}
}
return 0;
}
```
阅读全文