Gobang五子棋游戏的命令行窗口实现C语言/C++(人对人)毕业设计

毕业设计准备搞“基于深度学习的不可战胜的五子棋AI系统设计与实现”
先把五子棋游戏实现了吧(人对人)
占个坑,日后把五子棋AI的实现链接放这:空链接

感觉编程水平已经比一两年前刚开始学计算机有挺大提升了,编程思路更为清晰了

完整代码放在下方,复制即可运行

例图

在这里插入图片描述
在这里插入图片描述

代码

如果有幸能帮到你,不要忘了给我点赞哈~

#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