五子棋游戏c编程代码是什么

不及物动词 其他 30

回复

共3条回复 我来回复
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    五子棋游戏的C编程代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 15
    
    char board[SIZE][SIZE]; // 棋盘
    
    // 初始化棋盘
    void initBoard() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = ' ';
            }
        }
    }
    
    // 打印棋盘
    void printBoard() {
        system("clear"); // 清屏(适用于Linux和MacOS)
        printf("  ");
        for (int i = 0; i < SIZE; i++) {
            printf("%2d ", i);
        }
        printf("\n");
        for (int i = 0; i < SIZE; i++) {
            printf("%2d ", i);
            for (int j = 0; j < SIZE; j++) {
                printf("%c  ", board[i][j]);
            }
            printf("\n");
        }
    }
    
    // 下子
    void makeMove(int x, int y, char player) {
        board[x][y] = player;
    }
    
    // 判断是否胜利
    int isWin(int x, int y, char player) {
        int count = 1; // 连续棋子的个数
        int i, j;
    
        // 水平方向
        i = x;
        j = y - 1;
        while (j >= 0 && board[i][j] == player) {
            count++;
            j--;
        }
        j = y + 1;
        while (j < SIZE && board[i][j] == player) {
            count++;
            j++;
        }
        if (count >= 5) {
            return 1;
        }
    
        // 竖直方向
        count = 1;
        i = x - 1;
        j = y;
        while (i >= 0 && board[i][j] == player) {
            count++;
            i--;
        }
        i = x + 1;
        while (i < SIZE && board[i][j] == player) {
            count++;
            i++;
        }
        if (count >= 5) {
            return 1;
        }
    
        // 左上到右下方向
        count = 1;
        i = x - 1;
        j = y - 1;
        while (i >= 0 && j >= 0 && board[i][j] == player) {
            count++;
            i--;
            j--;
        }
        i = x + 1;
        j = y + 1;
        while (i < SIZE && j < SIZE && board[i][j] == player) {
            count++;
            i++;
            j++;
        }
        if (count >= 5) {
            return 1;
        }
    
        // 左下到右上方向
        count = 1;
        i = x + 1;
        j = y - 1;
        while (i < SIZE && j >= 0 && board[i][j] == player) {
            count++;
            i++;
            j--;
        }
        i = x - 1;
        j = y + 1;
        while (i >= 0 && j < SIZE && board[i][j] == player) {
            count++;
            i--;
            j++;
        }
        if (count >= 5) {
            return 1;
        }
    
        return 0;
    }
    
    int main() {
        int x, y; // 下子的坐标
        int player = 1; // 当前玩家,1表示黑子,2表示白子
    
        initBoard(); // 初始化棋盘
    
        while (1) {
            printBoard(); // 打印棋盘
    
            // 提示玩家下子
            if (player == 1) {
                printf("轮到黑子下棋,请输入坐标(x y):");
            } else {
                printf("轮到白子下棋,请输入坐标(x y):");
            }
            scanf("%d %d", &x, &y);
    
            // 检查输入是否合法
            if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || board[x][y] != ' ') {
                printf("输入不合法,请重新输入!\n");
                continue;
            }
    
            // 下子
            if (player == 1) {
                makeMove(x, y, 'X');
            } else {
                makeMove(x, y, 'O');
            }
    
            // 判断是否胜利
            if (isWin(x, y, player == 1 ? 'X' : 'O')) {
                printBoard();
                if (player == 1) {
                    printf("黑子胜利!\n");
                } else {
                    printf("白子胜利!\n");
                }
                break;
            }
    
            // 切换玩家
            player = (player == 1) ? 2 : 1;
        }
    
        return 0;
    }
    

    以上是一个简单的五子棋游戏的C编程代码。其中包括了初始化棋盘、打印棋盘、下子、判断是否胜利等功能。通过循环交替下子,直到出现胜利条件,游戏结束。玩家可以通过输入坐标来下子,程序会判断输入的合法性,并在胜利时显示胜利信息。

    1年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    五子棋是一种非常古老且经典的棋类游戏,它在许多国家和地区都非常受欢迎。下面是一个使用C语言编写的五子棋游戏代码示例。

    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 15
    
    char board[SIZE][SIZE];
    char player = 'X';
    
    // 初始化棋盘
    void initBoard() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                board[i][j] = '.';
            }
        }
    }
    
    // 打印棋盘
    void printBoard() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                printf("%c ", board[i][j]);
            }
            printf("\n");
        }
    }
    
    // 判断是否胜利
    int checkWin(int row, int col) {
        int count;
    
        // 水平方向
        count = 1;
        for (int i = col - 1; i >= 0 && board[row][i] == player; i--) {
            count++;
        }
        for (int i = col + 1; i < SIZE && board[row][i] == player; i++) {
            count++;
        }
        if (count >= 5) {
            return 1;
        }
    
        // 垂直方向
        count = 1;
        for (int i = row - 1; i >= 0 && board[i][col] == player; i--) {
            count++;
        }
        for (int i = row + 1; i < SIZE && board[i][col] == player; i++) {
            count++;
        }
        if (count >= 5) {
            return 1;
        }
    
        // 左上到右下方向
        count = 1;
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) {
            count++;
        }
        for (int i = row + 1, j = col + 1; i < SIZE && j < SIZE && board[i][j] == player; i++, j++) {
            count++;
        }
        if (count >= 5) {
            return 1;
        }
    
        // 右上到左下方向
        count = 1;
        for (int i = row - 1, j = col + 1; i >= 0 && j < SIZE && board[i][j] == player; i--, j++) {
            count++;
        }
        for (int i = row + 1, j = col - 1; i < SIZE && j >= 0 && board[i][j] == player; i++, j--) {
            count++;
        }
        if (count >= 5) {
            return 1;
        }
    
        return 0;
    }
    
    int main() {
        int row, col;
    
        initBoard();
    
        while (1) {
            printf("Player %c's turn\n", player);
            printf("Enter row and column: ");
            scanf("%d %d", &row, &col);
    
            if (row < 0 || row >= SIZE || col < 0 || col >= SIZE) {
                printf("Invalid move\n");
                continue;
            }
    
            if (board[row][col] != '.') {
                printf("Invalid move\n");
                continue;
            }
    
            board[row][col] = player;
            printBoard();
    
            if (checkWin(row, col)) {
                printf("Player %c wins\n", player);
                break;
            }
    
            player = (player == 'X') ? 'O' : 'X';
        }
    
        return 0;
    }
    

    这段代码实现了一个简单的五子棋游戏。它使用一个二维数组来表示棋盘,'.'表示空位,'X'表示玩家1的棋子,'O'表示玩家2的棋子。每个玩家轮流输入行和列,然后在棋盘上放置自己的棋子。每次放置完棋子后,程序会检查是否有五子连线,如果有则宣布该玩家胜利。游戏会一直进行,直到有玩家获胜或者棋盘已满。

    这段代码只是一个简单的示例,可能还有一些缺陷和改进的空间。但它可以作为你编写自己五子棋游戏的起点,你可以根据需要进行修改和扩展。

    1年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    下面是一个简单的五子棋游戏的C语言编程代码示例:

    #include <stdio.h>
    
    #define SIZE 15
    
    char chessboard[SIZE][SIZE]; // 保存棋盘的二维数组
    
    // 初始化棋盘
    void initChessboard() {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                chessboard[i][j] = '+';
            }
        }
    }
    
    // 打印棋盘
    void printChessboard() {
        printf("   ");
        for (int i = 0; i < SIZE; i++) {
            printf("%2d ", i);
        }
        printf("\n");
        for (int i = 0; i < SIZE; i++) {
            printf("%2d ", i);
            for (int j = 0; j < SIZE; j++) {
                printf("%2c ", chessboard[i][j]);
            }
            printf("\n");
        }
    }
    
    // 判断是否胜利
    int isWin(int x, int y, char player) {
        int count = 0;
        int dx[] = {-1, 0, 1, 1}; // 横向、纵向、斜向(左上到右下)、斜向(右上到左下)的增量
        int dy[] = {0, 1, 1, -1};
        for (int i = 0; i < 4; i++) {
            int tx = x, ty = y;
            while (tx >= 0 && tx < SIZE && ty >= 0 && ty < SIZE && chessboard[tx][ty] == player) {
                count++;
                tx += dx[i];
                ty += dy[i];
            }
            tx = x - dx[i];
            ty = y - dy[i];
            while (tx >= 0 && tx < SIZE && ty >= 0 && ty < SIZE && chessboard[tx][ty] == player) {
                count++;
                tx -= dx[i];
                ty -= dy[i];
            }
            if (count >= 5) {
                return 1;
            }
            count = 0;
        }
        return 0;
    }
    
    int main() {
        initChessboard();
        printChessboard();
        int x, y;
        char player = 'X';
        while (1) {
            printf("Player %c's turn, please enter the coordinates (x y): ", player);
            scanf("%d %d", &x, &y);
            if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || chessboard[x][y] != '+') {
                printf("Invalid input, please try again.\n");
                continue;
            }
            chessboard[x][y] = player;
            printChessboard();
            if (isWin(x, y, player)) {
                printf("Player %c wins!\n", player);
                break;
            }
            player = (player == 'X') ? 'O' : 'X';
        }
        return 0;
    }
    

    该代码实现了一个简单的五子棋游戏。通过命令行输入坐标,玩家轮流下棋,当有五颗连续的棋子在横、竖、斜线上时,判定该玩家获胜。游戏的棋盘使用一个15×15的二维数组表示,初始值为'+',玩家下棋后改为'X'或'O'。在每次下棋后,都会打印当前棋盘状态,并判断是否有玩家获胜。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部