c 语言五子棋源代码
时间: 2024-05-29 13:06:49 浏览: 20
五子棋是一种非常经典的棋类游戏,以下是一份简单的C语言五子棋源代码,可以供你参考:
```
#include <stdio.h>
#include <stdlib.h>
#define SIZE 15
#define BLACK 'X'
#define WHITE 'O'
#define EMPTY '+'
char board[SIZE][SIZE];
char player = BLACK;
void init_board() {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
void display_board() {
int i, j;
printf(" ");
for (i = 0; i < SIZE; i++) {
printf("%d", i+1);
}
printf("\n");
for (i = 0; i < SIZE; i++) {
printf("%c", i+'A');
for (j = 0; j < SIZE; j++) {
printf("%c", board[i][j]);
}
printf("\n");
}
}
int get_input(int *row, int *col) {
char input;
printf("%c's turn: ", player);
if (fgets(input, 3, stdin) == NULL) {
return 0;
}
*row = input - 'A';
*col = input - '1';
return 1;
}
int check_win(int row, int col) {
int i, j;
char symbol = board[row][col];
int count = 1;
for (i = row-1; i >= 0 && board[i][col] == symbol; i--) count++;
for (i = row+1; i < SIZE && board[i][col] == symbol; i++) count++;
if (count >= 5) return 1;
count = 1;
for (j = col-1; j >= 0 && board[row][j] == symbol; j--) count++;
for (j = col+1; j < SIZE && board[row][j] == symbol; j++) count++;
if (count >= 5) return 1;
count = 1;
for (i = row-1, j = col-1; i >= 0 && j >= 0 && board[i][j] == symbol; i--, j--) count++;
for (i = row+1, j = col+1; i < SIZE && j < SIZE && board[i][j] == symbol; i++, j++) count++;
if (count >= 5) return 1;
count = 1;
for (i = row-1, j = col+1; i >= 0 && j < SIZE && board[i][j] == symbol; i--, j++) count++;
for (i = row+1, j = col-1; i < SIZE && j >= 0 && board[i][j] == symbol; i++, j--) count++;
if (count >= 5) return 1;
return 0;
}
void change_player() {
if (player == BLACK) {
player = WHITE;
} else {
player = BLACK;
}
}
int main() {
int row, col;
init_board();
display_board();
while (get_input(&row, &col)) {
if (board[row][col] != EMPTY) {
printf("This cell is already occupied!\n");
continue;
}
board[row][col] = player;
display_board();
if (check_win(row, col)) {
printf("%c wins!\n", player);
break;
}
change_player();
}
return 0;
}
```
这个程序使用了一个二维数组来表示棋盘,其中'X'表示黑棋,'O'表示白棋,'+'表示空位。游戏过程中,玩家通过输入棋子的坐标来下棋,程序会检查下的位置是否合法,如果合法则将该位置上的棋子改为当前玩家的棋子,并检查是否有玩家获胜。如果有一方获胜,则游戏结束。
相关推荐
![](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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)