六子棋游戏源代码c语言
时间: 2023-10-19 17:03:08 浏览: 211
C语言五子棋游戏源代码
4星 · 用户满意度95%
六子棋是一种两人对弈的棋类游戏,游戏规则类似于五子棋。以下是一个简单的六子棋游戏的C语言源代码示例:
```c
#include<stdio.h>
#define SIZE 8
#define EMPTY 0
#define PLAYER1 1
#define PLAYER2 2
int board[SIZE][SIZE];
void initialize_board() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
void display_board() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == EMPTY)
printf(" - ");
else if (board[i][j] == PLAYER1)
printf(" X ");
else if (board[i][j] == PLAYER2)
printf(" O ");
}
printf("\n");
}
}
int make_move(int player, int row, int col) {
if (board[row][col] == EMPTY) {
board[row][col] = player;
return 1;
}
else
return 0;
}
int check_win(int player) {
// 省略判断获胜的代码,根据具体的游戏规则实现
return 0;
}
int main() {
int player = PLAYER1;
int row, col;
initialize_board();
while (1) {
display_board();
printf("Player %d's turn\n", player);
printf("Enter row and column to make a move: ");
scanf("%d %d", &row, &col);
if (make_move(player, row, col)) {
if (check_win(player)) {
printf("Player %d wins!\n", player);
break;
}
player = (player == PLAYER1) ? PLAYER2 : PLAYER1;
}
else {
printf("Invalid move! Try again.\n");
}
}
return 0;
}
```
这个源代码实现了一个简单的六子棋游戏,包括棋盘初始化、显示棋盘、下棋、判断获胜等功能。游戏通过循环实现,每个玩家依次输入行和列来下棋。程序会判断是否有玩家获胜并显示结果。如果玩家下出了无效的移动,程序会提示重新输入。
阅读全文