The command line window of Gobang backgammon game implements C language/C++ (human-to-human) graduation project

The graduation project is going to engage in "Design and Implementation of an Invincible Backgammon AI System Based on Deep Learning".
Let's realize the backgammon game first (human-to-human)
to occupy a pit. In the future, I will put the backgammon AI implementation link here: empty link

I feel that the programming level has been greatly improved compared to when I first started learning computers a year or two ago, and the programming ideas are clearer

The complete code is placed below, just copy and run

examples

insert image description here
insert image description here

the code

If you are lucky enough to help you, don't forget to give me a thumbs up~

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

#define BSIZE 15 //实际有效棋盘是13*13,棋盘外围多一圈
#define ASIGN '*'
#define BSIGN '@'
#define SIGN '.'


struct Piece
{
    
    
    int x,y;//x是第几列,y是第几行
};

char board[BSIZE][BSIZE];//列,行
struct Piece POfA[100];//piece_of_player_A
struct Piece POfB[100];
int ALen=0,BLen=0;


void boardInitialize()//初始化棋盘
{
    
    
    int i,j;
    for(i=0;i<BSIZE;i++)
        for(j=0;j<BSIZE;j++)
            board[i][j]=SIGN;
    ALen=0,BLen=0;
}

void boardPrint()//输出棋盘
{
    
    
    int i,j;
    printf("   ");
    for(i=1;i<BSIZE-1;i++)
        printf("%3d",i);
    putchar('\n');
    for(i=BSIZE-2;i>0;i--)
    {
    
    
        printf("%3d",i);
        for(j=1;j<BSIZE-1;j++)
        {
    
    
            printf("  ");
            putchar(board[j][i]);
        }
        printf("%3d\n",i);
    }
    for(i=0;i<BSIZE-1;i++)
        printf("%3d",i);
    putchar('\n');
}

int judge(const struct Piece POf[],const int len,const char sign)//判断该选手是否获胜
{
    
    
    int i,j;
    const int xAdd[8]={
    
    1,1,0,-1,-1,-1,0,1};//偏移量数组
    const int yAdd[8]={
    
    0,1,1,1,0,-1,-1,-1};

    for(i=0;i<len;i++)//每个棋
    {
    
    
        for(j=0;j<8;j++)//八个方向
        {
    
    
            int count=1;
            int x = POf[i].x, y = POf[i].y;
            while(1)//朝着这个方向继续判断
            {
    
    
                x += xAdd[j], y += yAdd[j];
                if(x>13||x<1||y<1||y>13)
                    break;
                if(board[x][y]==sign)
                {
    
    
                    count++;
                    if(count==5)
                        return 1;//赢了
                }else
                    break;
            }
        }
    }
    return 0;
}

void game()//一次游戏
{
    
    
    int i,j,step=1;//result=0代表还未产生结果
    char result = 0;

    boardInitialize();
    while(!result)//每一步
    {
    
    
        struct Piece tmp;
        system("cls");
        boardPrint();
        if(step&1)//奇数步为A玩家
        {
    
    
            printf("playerA 黑棋 请输入坐标:x y\n");
            scanf("%d %d",&tmp.x,&tmp.y);
            if(board[tmp.x][tmp.y]==SIGN)
                board[tmp.x][tmp.y]=ASIGN;
            else
                continue;//输入错误重新输入
            POfA[ALen++]=tmp;
            if(judge(POfA,ALen,ASIGN))
                result = 'A';
        }else
        {
    
    
            printf("playerB 白棋 请输入坐标:x y\n");
            scanf("%d %d",&tmp.x,&tmp.y);
            if(board[tmp.x][tmp.y]==SIGN)
                board[tmp.x][tmp.y]=BSIGN;
            else
                continue;
            POfB[BLen++]=tmp;
            if(judge(POfB,BLen,BSIGN))
                result = 'B';
        }
        step++;
    }
    system("cls");
    boardPrint();
    printf("玩家 %c 获胜!!!\n",result);
}

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

おすすめ

転載: blog.csdn.net/weixin_47964723/article/details/123473473