It’s May 1st soon, let’s take everyone to play backgammon——C language

May Day blessing

Because this blog post was completed on May 1st, before introducing backgammon, I would like to wish everyone who supports Xiaobai a happy May Day!
It took me a while to play a "Happy May Day" backgammon, hahahahahaha, I'm not very skilled yet, so the writing is a bit ugly, just pass it by force, I don't know if you guys can see it, or be honest It doesn't look like it, but it doesn't matter. After reading it, I hope you can correct it for me and play. The word "乐" is indeed not corrected, hahaha, it looks super awkward,
as shown in the picture below:
insert image description here
After the blessing is given to the fight, let's talk about the text.

Realization of backgammon

The implementation of backgammon is divided into the following functions:

  1. The first is to build the chessboard. Here we use a two-dimensional array chessboard to initialize our chessboard and place chess pieces.
  2. Realize the function of displaying the chessboard void ShowChessBoard(int board[ROW][COL])
  3. Player 1 plays chess, we use the function void PlayerMove(int board[ROW][COL], int who) to realize, the latter parameter who passes whoever is who will play chess
  4. After player 1 plays chess, the chessboard will change. We need to judge whether the chess game has reached Wuzilianju, or if the chessboard is full and the chessboard is full, it is a draw. Otherwise, the game will continue. We use the function int IsOver(int board[ROW][COL]) to judge, the judgment method of this function is to judge the position after each chess move by calling the function int ChessCount(int board[ROW][COL], enum Dir d) All eight directions are judged to see if at least one of the directions satisfies five sons in a row to judge the current situation of the chess game
  5. Player 2 plays chess, player 1 in the 3rd and 4th becomes player 2 and repeats the above 3rd and 4th steps.

1 menu

Code:

void menu()
{
    
    
    printf("****************************\n");
    printf("******1. 开始   0.退出******\n");
    printf("****************************\n");
    printf("请选择 :> ");

}

2. Display the chessboard

Code:

void ShowChessBoard(int board[ROW][COL])
{
    
    
    printf("\033c");
    printf("玩家1——●\n");
    printf("玩家2——○\n");
    printf("\n\n  ");
    //打印y坐标轴
    for (int i = 0; i < COL; i++) {
    
    
        printf("%3d", i);
    }
    printf("\n");
    for (int i = 0; i < ROW; i++) {
    
    
        printf("%3d", i);//打印x坐标轴
        for (int j = 0; j < COL; j++) {
    
    
            if (board[i][j] == Player1) {
    
    
                //player1
                printf(" ●");//用黑棋表示玩家1的棋子
            }
            else if (board[i][j] == Player2) {
    
    
                //player2
                printf(" ○");//用白棋表示玩家2的棋子
            } 
            else {
    
    
                //Space
                printf(" + ");//空余部分默认为'+'
            }
        }
        printf("\n");
    }
}

3. Players play chess

Code:

void PlayerMove(int board[ROW][COL], int who)
{
    
    
    while (1) {
    
    
        printf("\n玩家[%d] 请输入你需要下棋的位置:> ", who);
        scanf("%d %d", &x, &y);//输入一个下棋的坐标
        if (x<0 || y > COL) {
    
    
            printf("非法坐标,重新输入!\n");
        }
        else if (board[x][y] != 0) {
    
    
            printf("该位置已被占用!\n");
        }
        else {
    
    
            board[x][y] = who;//把下棋的玩家对应的常量值赋值给该位置对应的值
            break;
        }
    }
}

4. Count the number of pieces of the same kind corresponding to the chess position

Code:

int ChessCount(int board[ROW][COL], enum Dir d)
{
    
    
    int _x = x;
    int _y = y;

    int count = 0;
    while (1) {
    
    
        switch (d) {
    
    
        case LEFT:
            _y--;
            break;
        case RIGHT:
            _y++;
            break;
        case UP:
            _x--;
            break;
        case DOWN:
            _x++;
            break;
        case LEFT_UP:
            _x--, _y--;
            break;
        case RIGHT_DOWN:
            _x++, _y++;
            break;
        case RIGHT_UP:
            _x--, _y++;
            break;
        case LEFT_DOWN:
            _x++, _y--;
            break;
        }
        if (_x < 0 || _x > ROW - 1 || _y < 0 || _y > COL - 1) {
    
    
            break;
        }//对下的棋子位置进行八个方向位置的连续的同种棋子个数进行统计
        if (board[x][y] == board[_x][_y]) {
    
    
            count++;
        }
        else {
    
    
            break;
        }
    }
    return count;
}

5. Determine whether the game is over

Code:

int IsOver(int board[ROW][COL])
{
    
    
    //将八个方向对应的同种棋子转变为统计四条直线上对应的同种棋子总数
    int count1 = ChessCount(board, LEFT) + ChessCount(board, RIGHT) + 1;
    int count2 = ChessCount(board, UP) + ChessCount(board, DOWN) + 1;
    int count3 = ChessCount(board, LEFT_UP) + ChessCount(board, RIGHT_DOWN) + 1;
    int count4 = ChessCount(board, LEFT_DOWN) + ChessCount(board, RIGHT_UP) + 1;
    //只要有一个方向连续的同种棋子数满足大于等于5个,那么相对应的玩家获得胜利
    if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5) {
    
    
        if (board[x][y] == Player1) {
    
    
            return PLAYER1_WIN;
        }
        else {
    
    
            return PLAYER2_WIN;
        }
    }
    //否则如果棋盘还没下满,也就是只要还有位置没放棋子,则对局继续
    for (int i = 0; i < ROW; i++) {
    
    
        for (int j = 0; j < COL; j++) {
    
    
            if (board[i][j] == 0) {
    
    
                return CONTINUE;
            }
        }
    }
    //以上情况都不符合那就是平局
    return DROW;
}

6. The game function that combines the functions of the above functions

Code:

void game()
{
    
    
	 //初始化
    int chessboard[ROW][COL];
    memset(chessboard, '\0', sizeof(chessboard));
    //判断结果的变量,默认初始化为对局继续
    int result = CONTINUE;
    do {
    
    
        ShowChessBoard(chessboard);
        PlayerMove(chessboard, Player1);
        result = IsOver(chessboard);
        if (CONTINUE != result) {
    
    
            break;
        }
        ShowChessBoard(chessboard);
        PlayerMove(chessboard, Player2);
        result = IsOver(chessboard);
        if (CONTINUE != result) {
    
    
            break;
        }
    } while (1);

    ShowChessBoard(chessboard);
    switch (result) {
    
    
    case PLAYER1_WIN:
        printf("恭喜玩家1获胜!\n");
        break;
    case PLAYER2_WIN:
        printf("恭喜玩家2获胜了!\n");
        break;
    case DROW:
        printf("平局,不服可以再战!\n");
        break;
    }
}

Full code :

game.h
#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROW 30               
#define COL 30
#define Player1 1
#define Player2 2
#define CONTINUE 0
#define PLAYER1_WIN 1
#define PLAYER2_WIN 2
#define DROW   3

enum dir
{
    
    
	LEFT,
	RIGHT,
	UP,
	DOWN,
	LEFT_UP,
	RIGHT_UP,
	LEFT_DOWN,
	RIGHT_DOWN
};


void menu();

void game();

void ShowChessBoard(int chessboard[ROW][COL]);

int IsOver(int chessboard[ROW][COL]);

void PlayerMove(int chessboard[ROW][COL], int who);

int ChessCount(int chessboard[ROW][COL], enum dir d);




game.c
#include "game.h"

int x = 0;
int y = 0;

void menu()
{
    
    
    printf("****************************\n");
    printf("******1. 开始   0.退出******\n");
    printf("****************************\n");
    printf("请选择 :> ");

}

void ShowChessBoard(int board[ROW][COL])
{
    
    
    printf("\033c");
    printf("玩家1——●\n");
    printf("玩家2——○\n");
    printf("\n\n  ");
    //打印y坐标轴
    for (int i = 0; i < COL; i++) {
    
    
        printf("%3d", i);
    }
    printf("\n");
    for (int i = 0; i < ROW; i++) {
    
    
        printf("%3d", i);//打印x坐标轴
        for (int j = 0; j < COL; j++) {
    
    
            if (board[i][j] == Player1) {
    
    
                //player1
                printf(" ●");//用黑棋表示玩家1的棋子
            }
            else if (board[i][j] == Player2) {
    
    
                //player2
                printf(" ○");//用白棋表示玩家2的棋子
            } 
            else {
    
    
                //Space
                printf(" + ");//空余部分默认为'+'
            }
        }
        printf("\n");
    }
}
void PlayerMove(int board[ROW][COL], int who)
{
    
    
    while (1) {
    
    
        printf("\n玩家[%d] 请输入你需要下棋的位置:> ", who);
        scanf("%d %d", &x, &y);//输入一个下棋的坐标
        if (x<0 || y > COL) {
    
    
            printf("非法坐标,重新输入!\n");
        }
        else if (board[x][y] != 0) {
    
    
            printf("该位置已被占用!\n");
        }
        else {
    
    
            board[x][y] = who;//把下棋的玩家对应的常量值赋值给该位置对应的值
            break;
        }
    }
}

int ChessCount(int board[ROW][COL], enum Dir d)
{
    
    
    int _x = x;
    int _y = y;

    int count = 0;
    while (1) {
    
    
        switch (d) {
    
    
        case LEFT:
            _y--;
            break;
        case RIGHT:
            _y++;
            break;
        case UP:
            _x--;
            break;
        case DOWN:
            _x++;
            break;
        case LEFT_UP:
            _x--, _y--;
            break;
        case RIGHT_DOWN:
            _x++, _y++;
            break;
        case RIGHT_UP:
            _x--, _y++;
            break;
        case LEFT_DOWN:
            _x++, _y--;
            break;
        }
        if (_x < 0 || _x > ROW - 1 || _y < 0 || _y > COL - 1) {
    
    
            break;
        }//对下的棋子位置进行八个方向位置的连续的同种棋子个数进行统计
        if (board[x][y] == board[_x][_y]) {
    
    
            count++;
        }
        else {
    
    
            break;
        }
    }
    return count;
}

int IsOver(int board[ROW][COL])
{
    
    
    //将八个方向对应的同种棋子转变为统计四条直线上对应的同种棋子总数
    int count1 = ChessCount(board, LEFT) + ChessCount(board, RIGHT) + 1;
    int count2 = ChessCount(board, UP) + ChessCount(board, DOWN) + 1;
    int count3 = ChessCount(board, LEFT_UP) + ChessCount(board, RIGHT_DOWN) + 1;
    int count4 = ChessCount(board, LEFT_DOWN) + ChessCount(board, RIGHT_UP) + 1;
    //只要有一个方向连续的同种棋子数满足大于等于5个,那么相对应的玩家获得胜利
    if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5) {
    
    
        if (board[x][y] == Player1) {
    
    
            return PLAYER1_WIN;
        }
        else {
    
    
            return PLAYER2_WIN;
        }
    }
    //否则如果棋盘还没下满,也就是只要还有位置没放棋子,则对局继续
    for (int i = 0; i < ROW; i++) {
    
    
        for (int j = 0; j < COL; j++) {
    
    
            if (board[i][j] == 0) {
    
    
                return CONTINUE;
            }
        }
    }
    //以上情况都不符合那就是平局
    return DROW;
}

void game()
{
    
    
	 //初始化
    int chessboard[ROW][COL];
    memset(chessboard, '\0', sizeof(chessboard));
    //判断结果的变量,默认初始化为对局继续
    int result = CONTINUE;
    do {
    
    
        ShowChessBoard(chessboard);
        PlayerMove(chessboard, Player1);
        result = IsOver(chessboard);
        if (CONTINUE != result) {
    
    
            break;
        }
        ShowChessBoard(chessboard);
        PlayerMove(chessboard, Player2);
        result = IsOver(chessboard);
        if (CONTINUE != result) {
    
    
            break;
        }
    } while (1);

    ShowChessBoard(chessboard);
    switch (result) {
    
    
    case PLAYER1_WIN:
        printf("恭喜玩家1获胜!\n");
        break;
    case PLAYER2_WIN:
        printf("恭喜玩家2获胜了!\n");
        break;
    case DROW:
        printf("平局,不服可以再战!\n");
        break;
    }
}
test.c
#include "game.h"


int main()
{
    
    
    int input = 0;
    do
    {
    
    
        menu();
        scanf("%d", &input);
        switch (input)
        {
    
    
        case 1:
            printf("欢迎来到五子棋---玩家1vs玩家2\n");
            printf("祝您游戏愉快\n");
            game();
            break;
        case 0:
            printf("游戏已退出\n");
            break;
        default:
            printf("选择错误,请重新输入!\n");
            break;
        }
    } while (input);
    return 0;
}

Well, our backgammon implementation is here, happy May Day~

Guess you like

Origin blog.csdn.net/weixin_63181097/article/details/130453107