用c语言写一个五子棋游戏
时间: 2023-11-16 21:06:15 浏览: 129
C语言五子棋,没有界面
好的,下面是一个简单的五子棋游戏的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 15 // 棋盘行数
#define COLS 15 // 棋盘列数
#define PLAYER1 'X' // 玩家1使用的棋子
#define PLAYER2 'O' // 玩家2使用的棋子
char board[ROWS][COLS]; // 棋盘
// 初始化棋盘
void init_board()
{
memset(board, ' ', sizeof(board));
}
// 打印棋盘
void print_board()
{
int i, j;
for (i = 0; i < ROWS; i++) {
printf(" %d ", i + 1);
for (j = 0; j < COLS; j++) {
printf("|%c", board[i][j]);
}
printf("|\n");
}
printf(" ");
for (i = 0; i < COLS; i++) {
printf(" %c", 'a' + i);
}
printf("\n");
}
// 判断某一行是否有五子连珠
int check_row(int row, char player)
{
int i, count = 0;
for (i = 0; i < COLS; i++) {
if (board[row][i] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
return 0;
}
// 判断某一列是否有五子连珠
int check_col(int col, char player)
{
int i, count = 0;
for (i = 0; i < ROWS; i++) {
if (board[i][col] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
return 0;
}
// 判断从左上到右下的斜线是否有五子连珠
int check_diag1(int row, int col, char player)
{
int i, count = 0;
for (i = 0; i < 5; i++) {
if (row + i >= ROWS || col + i >= COLS) {
return 0;
}
if (board[row + i][col + i] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
return 0;
}
// 判断从右上到左下的斜线是否有五子连珠
int check_diag2(int row, int col, char player)
{
int i, count = 0;
for (i = 0; i < 5; i++) {
if (row - i < 0 || col + i >= COLS) {
return 0;
}
if (board[row - i][col + i] == player) {
count++;
if (count == 5) {
return 1;
}
} else {
count = 0;
}
}
return 0;
}
// 判断当前玩家是否胜利
int check_win(char player)
{
int i, j;
for (i = 0; i < ROWS; i++) {
// 检查行
if (check_row(i, player)) {
return 1;
}
// 检查列
if (check_col(i, player)) {
return 1;
}
}
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
// 检查左上到右下的斜线
if (check_diag1(i, j, player)) {
return 1;
}
// 检查右上到左下的斜线
if (check_diag2(i, j, player)) {
return 1;
}
}
}
return 0;
}
// 判断棋盘是否已满
int is_full()
{
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == ' ') {
return 0;
}
}
}
return 1;
}
// 玩家下棋
void player_move(char player)
{
int row, col;
while (1) {
printf("Player %c's turn:\n", player);
printf("Enter row number (1-15): ");
scanf("%d", &row);
printf("Enter column letter (a-o): ");
scanf(" %c", &col);
if (row < 1 || row > ROWS || col < 'a' || col > 'o') {
printf("Invalid input, please try again.\n");
continue;
}
if (board[row-1][col-'a'] != ' ') {
printf("This position is already occupied, please try again.\n");
continue;
}
board[row-1][col-'a'] = player;
break;
}
}
// 游戏主循环
void game_loop()
{
char player = PLAYER1;
while (!is_full()) {
print_board();
player_move(player);
if (check_win(player)) {
printf("Player %c wins!\n", player);
print_board();
return;
}
player = player == PLAYER1 ? PLAYER2 : PLAYER1;
}
printf("Game over, it's a tie!\n");
print_board();
}
int main()
{
init_board();
game_loop();
return 0;
}
```
这个代码实现了基本的五子棋游戏功能,但是还有很多可以改进的地方,比如可以加上落子提示、悔棋功能、AI对战等等。
阅读全文