C语言实现五子棋游戏

这篇文章展示了一个用C语言编写的简单五子棋游戏,并附带详细的代码解释。

代码

#include <stdio.h>
#include <stdbool.h>

#define BOARD_SIZE 15 // 棋盘大小

char board[BOARD_SIZE][BOARD_SIZE]; // 棋盘

// 初始化棋盘
void initBoard() {
    for (int i = 0; i < BOARD_SIZE; i++) {
        for (int j = 0; j < BOARD_SIZE; j++) {
            board[i][j] = ' ';
        }
    }
}

// 打印棋盘
void printBoard() {
    printf('  ');
    for (int i = 0; i < BOARD_SIZE; i++) {
        printf('%2d ', i);
    }
    printf('
');
  
    for (int i = 0; i < BOARD_SIZE; i++) {
        printf('%2d ', i);
        for (int j = 0; j < BOARD_SIZE; j++) {
            printf('%c  ', board[i][j]);
        }
        printf('
');
    }
}

// 判断落子是否合法
bool isValidMove(int row, int col) {
    if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
        return false;
    }
    if (board[row][col] != ' ') {
        return false;
    }
    return true;
}

// 判断是否获胜
bool checkWin(int row, int col, char player) {
    // 检查水平方向
    int count = 0;
    for (int i = col - 4; i <= col + 4; i++) {
        if (i < 0 || i >= BOARD_SIZE) {
            continue;
        }
        if (board[row][i] == player) {
            count++;
            if (count >= 5) {
                return true;
            }
        } else {
            count = 0;
        }
    }

    // 检查垂直方向
    count = 0;
    for (int i = row - 4; i <= row + 4; i++) {
        if (i < 0 || i >= BOARD_SIZE) {
            continue;
        }
        if (board[i][col] == player) {
            count++;
            if (count >= 5) {
                return true;
            }
        } else {
            count = 0;
        }
    }

    // 检查斜向方向(左上到右下)
    count = 0;
    for (int i = -4; i <= 4; i++) {
        int r = row + i;
        int c = col + i;
        if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) {
            continue;
        }
        if (board[r][c] == player) {
            count++;
            if (count >= 5) {
                return true;
            }
        } else {
            count = 0;
        }
    }

    // 检查斜向方向(左下到右上)
    count = 0;
    for (int i = -4; i <= 4; i++) {
        int r = row + i;
        int c = col - i;
        if (r < 0 || r >= BOARD_SIZE || c < 0 || c >= BOARD_SIZE) {
            continue;
        }
        if (board[r][c] == player) {
            count++;
            if (count >= 5) {
                return true;
            }
        } else {
            count = 0;
        }
    }

    return false;
}

// 游戏主循环
void playGame() {
    char currentPlayer = 'X';
    int row, col;

    initBoard();
    printBoard();

    while (true) {
        printf('Player %c\'s turn. Enter row and column (0-%d): ', currentPlayer, BOARD_SIZE-1);
        scanf('%d %d', &row, &col);
      
        if (!isValidMove(row, col)) {
            printf('Invalid move! Try again.\n');
            continue;
        }
      
        board[row][col] = currentPlayer;
        printBoard();
      
        if (checkWin(row, col, currentPlayer)) {
            printf('Player %c wins!\n', currentPlayer);
            break;
        }
      
        currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
    }
}

int main() {
    playGame();
    return 0;
}

代码解释

  1. 包含头文件:

    • stdio.h: 提供输入输出函数,如 printfscanf
    • stdbool.h: 提供布尔类型 bool
  2. 定义棋盘大小:

    • #define BOARD_SIZE 15: 定义棋盘大小为 15x15。
  3. 声明棋盘数组:

    • char board[BOARD_SIZE][BOARD_SIZE];: 声明一个二维字符数组表示棋盘,每个元素存储棋子的状态 ('X', 'O' 或 ' ')。
  4. initBoard() 函数:

    • 初始化棋盘,将所有元素设置为 ' ' (空格),表示空位置。
  5. printBoard() 函数:

    • 打印当前棋盘的状态,包括行号和列号。
  6. isValidMove() 函数:

    • 检查落子是否合法,即是否在棋盘范围内且该位置为空。
  7. checkWin() 函数:

    • 检查当前玩家是否获胜,检查四个方向:水平、垂直、左上到右下、左下到右上。
  8. playGame() 函数:

    • 游戏主循环,控制玩家轮流下棋,判断输赢。
  9. main() 函数:

    • 程序入口,调用 playGame() 开始游戏。

总结

这个简单的五子棋游戏代码展示了如何使用C语言编写基本的游戏逻辑,包括棋盘表示、落子判断、胜负判定等。你可以根据自己的需要扩展这个游戏,例如添加AI对手、图形界面等。

C语言实现五子棋游戏(附源码及详细注释)

原文地址: https://www.cveoy.top/t/topic/bt97 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录