五子棋用C语言怎么写
时间: 2023-07-05 22:11:15 浏览: 44
五子棋是一种常见的游戏,使用C语言实现也比较简单。下面是一个简单的五子棋实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW 15
#define COL 15
char board[ROW][COL]; // 棋盘
char player1 = 'X'; // 玩家1的棋子
char player2 = 'O'; // 玩家2的棋子
// 初始化棋盘
void init_board() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
board[i][j] = '+';
}
}
}
// 打印棋盘
void print_board() {
printf(" ");
for (int i = 0; i < COL; i++) {
printf("%d ", i + 1);
}
printf("\n");
for (int i = 0; i < ROW; i++) {
printf("%d ", i + 1);
for (int j = 0; j < COL; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// 判断胜负
bool is_win(char c) {
// 判断行
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL - 4; j++) {
if (board[i][j] == c && board[i][j + 1] == c && board[i][j + 2] == c && board[i][j + 3] == c && board[i][j + 4] == c) {
return true;
}
}
}
// 判断列
for (int i = 0; i < ROW - 4; i++) {
for (int j = 0; j < COL; j++) {
if (board[i][j] == c && board[i + 1][j] == c && board[i + 2][j] == c && board[i + 3][j] == c && board[i + 4][j] == c) {
return true;
}
}
}
// 判断对角线
for (int i = 0; i < ROW - 4; i++) {
for (int j = 0; j < COL - 4; j++) {
if (board[i][j] == c && board[i + 1][j + 1] == c && board[i + 2][j + 2] == c && board[i + 3][j + 3] == c && board[i + 4][j + 4] == c) {
return true;
}
}
}
// 判断反对角线
for (int i = 4; i < ROW; i++) {
for (int j = 0; j < COL - 4; j++) {
if (board[i][j] == c && board[i - 1][j + 1] == c && board[i - 2][j + 2] == c && board[i - 3][j + 3] == c && board[i - 4][j + 4] == c) {
return true;
}
}
}
return false;
}
// 下棋
void play(char c) {
int x, y;
while (true) {
printf("请输入坐标(x, y): ");
scanf("%d %d", &x, &y);
if (x < 1 || x > ROW || y < 1 || y > COL || board[x - 1][y - 1] != '+') {
printf("输入不合法,请重新输入!\n");
} else {
break;
}
}
board[x - 1][y - 1] = c;
}
int main() {
init_board();
print_board();
while (true) {
play(player1);
print_board();
if (is_win(player1)) {
printf("玩家1获胜!\n");
break;
}
play(player2);
print_board();
if (is_win(player2)) {
printf("玩家2获胜!\n");
break;
}
}
return 0;
}
```
这是一个基本的五子棋实现,其中使用了二维字符数组来表示棋盘,使用了函数来初始化棋盘、打印棋盘、下棋、判断胜负等功能。您可以根据需要进行修改和扩展。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)