C语言实现五子棋游戏

五子棋是一种双人对战的纯策略型棋类游戏,通常在15×15或19×19的棋盘上进行,游戏的目标是在棋盘上形成连续的五个棋子,无论是横线、竖线还是斜线,在这篇文章中,我们将使用C语言来实现一个简单的五子棋游戏。

我们需要定义棋盘的大小和棋子的类型,在C语言中,我们可以使用二维数组来表示棋盘,其中0表示空位,1表示黑棋,2表示白棋,我们还需要一个函数来打印棋盘的当前状态。

#include <stdio.h>
#define BOARD_SIZE 15
void print_board(int board[BOARD_SIZE][BOARD_SIZE]) {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            printf("%d ", board[i][j]);
        }
        printf("
");
    }
}

c语言五子棋 c语言五子棋代码

接下来,我们需要实现玩家输入和游戏逻辑,我们可以使用一个循环来不断获取玩家的输入,并根据输入更新棋盘,当有一方获胜或者棋盘满了时,游戏结束。

int main() {
    int board[BOARD_SIZE][BOARD_SIZE] = {0};
    int current_player = 1;
    int move_x, move_y;
    while (1) {
        print_board(board);
        printf("Player %d, please enter your move (row and column): ", current_player);
        scanf("%d %d", &move_x, &move_y);
        if (board[move_x][move_y] != 0) {
            printf("Invalid move, try again.
");
            continue;
        }
        board[move_x][move_y] = current_player;
        if (check_win(board, move_x, move_y)) {
            print_board(board);
            printf("Player %d wins!
", current_player);
            break;
        } else if (is_full(board)) {
            print_board(board);
            printf("It's a draw!
");
            break;
        } else {
            current_player = 3 - current_player; // Switch player
        }
    }
    return 0;
}

现在我们需要实现check_winis_full函数。check_win函数用于检查给定位置的棋子是否形成了连续的五个棋子,而is_full函数用于检查棋盘是否已满。

int check_win(int board[BOARD_SIZE][BOARD_SIZE], int x, int y) {
    int player = board[x][y];
    int directions[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
    int count = 1;
    int temp_x, temp_y;
    for (int i = 0; i < 4; i++) {
        temp_x = x + directions[i][0];
        temp_y = y + directions[i][1];
        while (temp_x >= 0 && temp_x < BOARD_SIZE && temp_y >= 0 && temp_y < BOARD_SIZE && board[temp_x][temp_y] == player) {
            count++;
            temp_x += directions[i][0];
            temp_y += directions[i][1];
        }
    }
    for (int i = 0; i < 4; i++) {
        temp_x = x - directions[i][0];
        temp_y = y - directions[i][1];
        while (temp_x >= 0 && temp_x < BOARD_SIZE && temp_y >= 0 && temp_y < BOARD_SIZE && board[temp_x][temp_y] == player) {
            count++;
            temp_x -= directions[i][0];
            temp_y -= directions[i][1];
        }
    }
    return count >= 5;
}
int is_full(int board[BOARD_SIZE][BOARD_SIZE]) {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[i][j] == 0) {
                return 0;
            }
        }
    }
    return 1;
}

我们需要编译并运行我们的程序,在命令行中,我们可以使用以下命令来编译和运行我们的五子棋游戏:

gcc -o gobang gobang.c -lm # Compile the program with math library for abs function in check_win function. Run: ./gobang # Run the program. The game will start automatically. You can input your moves to play against the computer. The game will end when one of you wins or the board is full. To quit the game, press Ctrl+C. Note that this implementation only supports two players playing on the same computer. If you want to play against another person, you need to implement a network version of the game using socket programming or a similar technology.