| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 970 人关注过本帖
标题:写了个井字游戏,大家可以给些意见
只看楼主 加入收藏
captain2050
Rank: 2
等 级:论坛游民
帖 子:57
专家分:43
注 册:2016-7-15
结帖率:92.86%
收藏
 问题点数:0 回复次数:0 
写了个井字游戏,大家可以给些意见
程序代码:
#include<stdio.h>

int main()
{
    char chess[3][3]={'1','2','3','4','5','6','7','8','9',};
    int player1=0,player2=0,winner=0,column,line,turn=0;
    int check(char chess[3][3]);
   

    printf("Game start!\nplayer1 *\nplayer2 O\n\n");
   

    printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
    while(1)
    {
       

       

        printf("player1's turn:");
        scanf("%d",&player1);
        if(!(player1>=1)&&(player1<=9))
        {
            printf("input error\n");
            return 0;
        }
        turn++;
        column=(int)(player1-1)/3;
        line=(int)(player1-1)%3;
        chess[column][line]='*';
        printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
        if(check(chess)==1)
        {
            printf("player1 is the winner\n");
            return 1;
        }
        if(turn==9)
        {
            printf("onbody win\n");
            return 1;
        }
            printf("player2's turn:");
        scanf("%d",&player2);
        if(!(player2>=1)&&(player2<=9))
        {
            printf("input error\n");
            return 0;
        }
        column=(int)(player2-1)/3;
        line=(int)(player2-1)%3;
        chess[column][line]='O';
        turn++;
        printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
        printf("---+---+---\n");
        printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
        if(check(chess)==1)
        {
            printf("player2 is the winner\n");
            return 1;
        }
       

       

       

    }
}

int check(char chess[3][3])
{
    for(int i=0;i<=2;i++)
        if(chess[i][0]==chess[i][1]&&chess[i][1]==chess[i][2])
            return 1;
        else if(chess[0][i]==chess[1][i]&&chess[1][i]==chess[2][i])
            return 1;
     if(chess[0][0]==chess[1][1]&&chess[1][1]==chess[2][2])
            return 1;
        else if(chess[0][2]==chess[1][1]&&chess[1][1]==chess[2][0])
            return 1;
        else

            return 0;   

}







搜索更多相关主题的帖子: 游戏 color 
2016-11-23 21:50
快速回复:写了个井字游戏,大家可以给些意见
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028205 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved