用c语言设计一个五子棋程序
时间: 2024-04-30 11:18:20 浏览: 130
好的,以下是一个简单的五子棋程序的C语言代码实现,使用了基于控制台的界面和简单的随机AI,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_SIZE 15 // 棋盘大小
#define EMPTY 0 // 空格
#define PLAYER 1 // 玩家棋子
#define AI 2 // AI棋子
int board[BOARD_SIZE][BOARD_SIZE]; // 棋盘
int turn; // 当前回合,1为玩家回合,2为AI回合
int ai_level; // AI难度级别,1为简单,2为中等,3为困难
// 初始化棋盘
void init_board() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
// 打印棋盘
void print_board() {
printf(" ");
for (int i = 1; i <= BOARD_SIZE; i++) {
printf("%d ", i);
}
printf("\n");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%c ", i + 'A');
for (int j = 0; j < BOARD_SIZE; j++) {
switch (board[i][j]) {
case EMPTY:
printf(". ");
break;
case PLAYER:
printf("O ");
break;
case AI:
printf("X ");
break;
}
}
printf("%c\n", i + 'A');
}
printf(" ");
for (int i = 1; i <= BOARD_SIZE; i++) {
printf("%d ", i);
}
printf("\n");
}
// 判断是否有五子连珠
int has_won(int x, int y, int player) {
int i, j, count;
// 横向
count = 0;
for (i = x; i < BOARD_SIZE && board[i][y] == player; i++) count++;
for (i = x - 1; i >= 0 && board[i][y] == player; i--) count++;
if (count >= 5) return 1;
// 纵向
count = 0;
for (j = y; j < BOARD_SIZE && board[x][j] == player; j++) count++;
for (j = y - 1; j >= 0 && board[x][j] == player; j--) count++;
if (count >= 5) return 1;
// 斜向(左上到右下)
count = 0;
for (i = x, j = y; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == player; i++, j++) count++;
for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) count++;
if (count >= 5) return 1;
// 斜向(右上到左下)
count = 0;
for (i = x, j = y; i >= 0 && j < BOARD_SIZE && board[i][j] == player; i--, j++) count++;
for (i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == player; i++, j--) count++;
if (count >= 5) return 1;
return 0;
}
// 玩家下棋
void player_turn() {
int x, y;
printf("请输入下棋位置(如 A1):");
scanf("%c%d", &x, &y);
getchar(); // 消耗掉换行符
x -= 'A';
y--;
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || board[x][y] != EMPTY) {
printf("下棋位置无效,请重新输入!\n");
player_turn();
return;
}
board[x][y] = PLAYER;
}
// AI下棋(简单难度)
void ai_turn_easy() {
int x, y;
srand(time(NULL));
do {
x = rand() % BOARD_SIZE;
y = rand() % BOARD_SIZE;
} while (board[x][y] != EMPTY);
board[x][y] = AI;
printf("AI下棋位置:%c%d\n", x + 'A', y + 1);
}
// AI下棋(中等难度)
void ai_turn_medium() {
int x, y;
int i, j, count;
// 检查是否有自己的四子连珠
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = AI;
if (has_won(i, j, AI)) {
printf("AI下棋位置:%c%d\n", i + 'A', j + 1);
return;
}
board[i][j] = EMPTY;
}
}
}
// 检查是否有玩家的四子连珠,如果有则防守
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = PLAYER;
if (has_won(i, j, PLAYER)) {
board[i][j] = AI;
printf("AI下棋位置:%c%d\n", i + 'A', j + 1);
return;
}
board[i][j] = EMPTY;
}
}
}
// 随机落子
ai_turn_easy();
}
// AI下棋(困难难度)
void ai_turn_hard() {
// TODO: 实现更高级的AI算法
ai_turn_medium();
}
// 判断游戏是否结束
int is_game_over() {
int i, j;
// 检查是否有五子连珠
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] != EMPTY) {
if (has_won(i, j, board[i][j])) {
return 1;
}
}
}
}
// 检查是否棋盘已满
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
return 0;
}
}
}
return 1;
}
int main() {
int level;
// 初始化棋盘
init_board();
// 选择难度
printf("请选择AI难度级别(1为简单,2为中等,3为困难):");
scanf("%d", &level);
ai_level = level;
// 游戏循环
while (!is_game_over()) {
// 换回合
turn = 3 - turn;
// 打印棋盘
printf("\n");
print_board();
// 玩家或AI下棋
if (turn == PLAYER) {
player_turn();
} else {
switch (ai_level) {
case 1:
ai_turn_easy();
break;
case 2:
ai_turn_medium();
break;
case 3:
ai_turn_hard();
break;
}
}
}
// 游戏结束,显示结果
printf("\n");
print_board();
if (has_won(0, 0, PLAYER)) {
printf("玩家获胜!\n");
} else if (has_won(0, 0, AI)) {
printf("AI获胜!\n");
} else {
printf("平局!\n");
}
return 0;
}
```
该程序使用了基本的C语言语法,包括循环、条件语句、数组等。实现了简单的五子棋游戏逻辑,包括初始化棋盘、打印棋盘、判断胜负、下棋等功能。其中AI部分使用了简单的随机落子和中等难度的算法,困难难度的算法还待实现。
阅读全文