• c语言实现:三子棋


    问题描述:两个游戏者在3*3棋盘里轮流作标记,如果一个人在行,列或者两个对角线可以作三个标记,则为获胜。

    我们首先得打印菜单供玩家选择(可以选择玩游戏或者退出游戏)

    1 void menu()
    2 {
    3     printf("********************
    ");
    4     printf("*****  1.play  *****
    ");
    5     printf("*****  0.exit  *****
    ");
    6     printf("********************
    ");
    7 }

    接下来得有个test函数测试游戏的可玩性:

    srand()函数随机生成生成数起点,为rand()函数服务,注意包含头文件为<Time.h>

    void test()
    {
        int input = 0;
        srand((unsigned int)time(NULL));
    do { menu(); printf("请输入选项:"); scanf("%d",&input); switch(input) { case 1: game(); break; case 0: printf("退出游戏"); break; default: printf("输入有误,请重新输入"); break; } }while(input); }

    接下来就到了玩游戏环节:

    用'#'作电脑标记,用'*'作玩家标记,用变量ret接受输赢信息,'*'为玩家赢,’#‘为电脑赢,’C'继续玩游戏,‘P’平局;

    void game()
    {
        char ret = '0';
        char board[ROW][COL]={0};
        InitBoard(board,ROW,COL);
        DisplatBoard(board,ROW,COL);
        while(1)
        {
            PlayMove(board,ROW,COL);
            DisplatBoard(board,ROW,COL);
            ret=IsWin(board,ROW,COL);
            if(ret=='*')
            {
                printf("玩家赢
    ");
                break;
            }
            else if(ret=='#')
            {
                printf("电脑赢
    ");
                break;
            }
            else if(ret=='Q')
            {
                printf("平局
    ");
                break;
            }
            ComputerMove(board,ROW,COL);
            DisplatBoard(board,ROW,COL);
            ret=IsWin(board,ROW,COL);
            if(ret=='*')
            {
                printf("玩家赢
    ");
                break;
            }
            else if(ret=='#')
            {
                printf("电脑赢
    ");
                break;
            }
            else if(ret=='Q')
            {
                printf("平局
    ");
                break;
            }
        }
    }

    最后就是game.c的游戏完善部分:

    初始化棋盘:

    使用memset()将棋盘全部初始化为空格,头文件为<string.h>

    void InitBoard(char board[ROW][COL],int row,int col)
    {
        memset(board,' ',row*col*sizeof(board[0][0]));
    }

    打印棋盘:

    void DisplatBoard(char board[ROW][COL],int row,int col)
    {
        int i=0;
        int j=0;
        for(i = 0; i < row; i++)
        {
            for(j = 0; j < col; j++)
            {
                printf(" %c ", board[i][j]);
                if (j < col - 1)
                printf("|");
            }
            printf("
    ");
            for(j = 0; j < col ; j++)
            {
                if (i < row - 1)
                printf("___");
                else
                printf("   ");
                if (j < col - 1)
                printf("|");
            }
            printf("
    ");
        }
    }

    玩家下:

    void PlayMove(char board[ROW][COL],int row,int col)
    {
        int x=1;
        int y=1;
        do
        {
            printf("请玩家输入坐标:");
            scanf("%d %d",&x,&y);
            if ((x>0 && x<=row) && (y>0 && y<=col)) 
            {
                if (board[x-1][y-1]==' ')
                {
                    board[x-1][y-1]='*';
                    break;
                }
                else
                    printf("这个坐标被占了,请重新输入:
    ");
            }
            else
                printf("输入错误,请重新输入
    ");
        }while(1);
    }

    电脑下:

    使用rand()函数随机生成坐标

    void ComputerMove(char board[ROW][COL],int row,int col)
    {
        int x = 0;        
        int y = 0;        
        printf("电脑走:
    ");        
        while (1)        
        {            
            x = rand()%row;   
            y = rand()%col;            
            if (board[x][y] == ' ')            
            {                
                board[x][y] = '#';                
                break;
            }
        }
    }

    判断输赢:

    char IsWin(char board[ROW][COL],int row,int col)
    {
        int i=0;
        int j=0;
        int count=0;
        char temp[ROW] = {'0'};
        for (i = 0; i < row; i++)    
        {
            count = 0;
            for(j = 0; j < col; j++)
                temp[j]=board[i][j];
            for(j = 0; j < col - 1; j++)
            {
                if(temp[j]==temp[j+1])
                    count++;
                else 
                    break;
            }
            if(count==(row-1))
                return board[i][0];
        }    
        count = 0;
        for (i = 0; i < col; i++)    
        {
            count=0;
            for(j = 0; j < row; j++)
                temp[j]=board[j][i];
            for(j = 0; j < row - 1; j++)
            {
                if(temp[j]==temp[j+1])
                    count++;
                else 
                    break;
            }
            if(count==(row-1))
                return board[0][i];
        }    
        count = 0;
        for (i = 0; i < row; i++)    
            temp[i]=board[i][i];
        for(j = 0; j < col - 1; j++)
            {
                if(temp[j]==temp[j+1])
                    count++;
                else 
                    break;
            }
        if(count==(row-1))
            return board[0][0];
        count = 0;
        for (i = 0; i < row; i++)    
            temp[i]=board[i][row-1-i];
        for(j = 0; j < col - 1; j++)
            {
                if(temp[j]==temp[j+1])
                    count++;
                else 
                    break;
            }
        if(count==(row-1))
            return board[0][row-1];
        if (IsFull(board,ROW,COL))        
            return 'Q';
    }

    判断棋盘是否满了:

    static把函数封装在game.c内部

    static int IsFull(char board[ROW][COL],int row,int col)
    {
        int i=0;
        int j=0;    
        int count=0;    
        for (i = 0; i < row; i++)    
        {        
            for (j = 0; j < col; j++)        
            {            
                if (board[i][j] != ' ')                
                    count++;        
            }    
        }    
        if (count==row*col)        
            return 1;        
        else        
            return 0;
    }

    部分调试界面展示:

    因为我写代码时考虑到想让三子棋改为五子棋、八子棋等方便点,就在声明部分声明行和列,方便修改棋盘,这是调试过程调试五子棋显示的棋盘。

    最后,我的游戏还很蠢想要打败玩家除非玩家想让它赢,存在很多不足,希望各位可以借鉴的同时不要吐槽,也期待进一步的优化和改进。

  • 相关阅读:
    SAP 锁对象
    smartforms取消word为默认编辑器
    abap 配置 zconfig
    Ant步步为营(1)解压本地的zip包
    点击页面出现文字动画
    js简单实现累加
    github发布线上项目
    jsonp的实现
    js操作class
    js开发实用技巧
  • 原文地址:https://www.cnblogs.com/nbsun/p/10594718.html
Copyright © 2020-2023  润新知