生成一个简单的c语言五子棋对战代码
时间: 2023-08-07 10:05:03 浏览: 155
好的,这是一个简单的 C 语言五子棋对战代码,希望能帮助到你:
```c
#include <stdio.h>
#include <stdlib.h>
#define ROWS 15
#define COLS 15
#define BLANK ' '
#define PLAYER 'X'
#define COMPUTER 'O'
char chessboard[ROWS][COLS]; // 棋盘
int player_row, player_col; // 玩家下棋的行列坐标
int computer_row, computer_col; // 电脑下棋的行列坐标
// 初始化棋盘
void init_chessboard() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
chessboard[i][j] = BLANK;
}
}
}
// 显示棋盘
void display_chessboard() {
int i, j;
printf(" ");
for (i = 0; i < ROWS; i++) {
printf("%d ", i + 1);
}
printf("\n");
for (i = 0; i < ROWS; i++) {
printf("%d ", i + 1);
for (j = 0; j < COLS; j++) {
printf("%c ", chessboard[i][j]);
}
printf("\n");
}
}
// 判断落子是否合法
int is_valid(int row, int col) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
return 0;
}
if (chessboard[row][col] != BLANK) {
return 0;
}
return 1;
}
// 玩家下棋
void player_move() {
printf("请输入您要下棋的位置(行 列):\n");
scanf("%d %d", &player_row, &player_col);
while (!is_valid(player_row - 1, player_col - 1)) {
printf("您输入的位置不合法,请重新输入:\n");
scanf("%d %d", &player_row, &player_col);
}
chessboard[player_row - 1][player_col - 1] = PLAYER;
}
// 电脑下棋
void computer_move() {
int i, j, flag = 0;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (is_valid(i, j)) {
chessboard[i][j] = COMPUTER;
if (check_win(COMPUTER)) {
computer_row = i;
computer_col = j;
flag = 1;
}
chessboard[i][j] = BLANK;
}
if (flag) {
break;
}
}
if (flag) {
break;
}
}
if (!flag) {
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (is_valid(i, j)) {
chessboard[i][j] = PLAYER;
if (check_win(PLAYER)) {
computer_row = i;
computer_col = j;
flag = 1;
}
chessboard[i][j] = BLANK;
}
if (flag) {
break;
}
}
if (flag) {
break;
}
}
}
if (!flag) {
do {
computer_row = rand() % ROWS;
computer_col = rand() % COLS;
} while (!is_valid(computer_row, computer_col));
}
printf("电脑下棋(%d %d)\n", computer_row + 1, computer_col + 1);
chessboard[computer_row][computer_col] = COMPUTER;
}
// 检查是否获胜
int check_win(char c) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (chessboard[i][j] == c) {
if (j + 4 < COLS && chessboard[i][j + 1] == c && chessboard[i][j + 2] == c && chessboard[i][j + 3] == c && chessboard[i][j + 4] == c) {
return 1;
}
if (i + 4 < ROWS && chessboard[i + 1][j] == c && chessboard[i + 2][j] == c && chessboard[i + 3][j] == c && chessboard[i + 4][j] == c) {
return 1;
}
if (j + 4 < COLS && i + 4 < ROWS && chessboard[i + 1][j + 1] == c && chessboard[i + 2][j + 2] == c && chessboard[i + 3][j + 3] == c && chessboard[i + 4][j + 4] == c) {
return 1;
}
if (j - 4 >= 0 && i + 4 < ROWS && chessboard[i + 1][j - 1] == c && chessboard[i + 2][j - 2] == c && chessboard[i + 3][j - 3] == c && chessboard[i + 4][j - 4] == c) {
return 1;
}
}
}
}
return 0;
}
// 判断游戏是否结束
int is_gameover() {
if (check_win(PLAYER)) {
printf("恭喜您,获胜了!\n");
return 1;
}
if (check_win(COMPUTER)) {
printf("很遗憾,您输了!\n");
return 1;
}
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (chessboard[i][j] == BLANK) {
return 0;
}
}
}
printf("平局!\n");
return 1;
}
int main() {
srand(time(0));
init_chessboard();
display_chessboard();
while (!is_gameover()) {
player_move();
display_chessboard();
if (!is_gameover()) {
computer_move();
display_chessboard();
}
}
return 0;
}
```
这个程序实现了人机对战,玩家执黑先行,输入行列坐标下棋,电脑执白后行,采用简单的 AI 算法进行下棋,直到出现获胜或平局为止。
阅读全文