| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 702 人关注过本帖
标题:关于“井“字游戏的问题,请各位帮忙!
只看楼主 加入收藏
南山浅悠然
Rank: 2
等 级:论坛游民
威 望:1
帖 子:53
专家分:92
注 册:2014-5-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
关于“井“字游戏的问题,请各位帮忙!
有时陷入死循环且赢时无法识别

#include<stdio.h>
int main(void)
{
    int player=0;
    int winner=0;
    int choice=0;
    unsigned int row=0;
    unsigned int column=0;
    char board[3][3]={
                                      {'1','2','3'},
                                      {'4','5','6'},
                                      {'7','8','9'}
                                    };
    for(unsigned i=0;i<9;++i)
    {
        printf("\n");
        printf("%c  |  %c  | %c\n",
            board[0][0],board[0][1],
            board[0][2]);
        printf("---+-----+---\n");
        printf("%c  |  %c  | %c\n",
            board[1][0],board[1][1],
            board[1][2]);
        printf("---+-----+---\n");
        printf("%c  |  %c  | %c\n ",
            board[2][0],board[2][1],
            board[2][2]);
        player=i%2+1;
        do
           {
               printf("Player %d ,Please"
               " enter a ralid square "
               "number for where you "
               "want to place your %c: ",
               player,(player==1)?'X':'O');
              scanf("%d",&choice);
              row=--choice/3;
              column=choice%3;
           }while (choice<0||choice>8
             ||board[row][column]>'9');
           board[row][column]=(player
             ==1)?'X':'O';
           if ((board[0][0]==board[1][1]
           &&board[0][0]==board[2][2])
           ||(board[0][2]==board[1][1]
           &&board[0][2]==board[2][0]))
           winner=player;
           else
            {
                for(unsigned int line=0;
                line<=2;++line)
                {
                    if ((board[line][0]==
                    board[line][1]&&
                    board[line][0]==
                    board[line][2])||(
                    board[0][line]==board
                    [1][line]&&board[0]
                    [line]==board[2][line]))
                    winner==player;
                }
            }
    }
            printf("\n");
        printf("%c  |  %c  | %c\n",
            board[0][0],board[0][1],
            board[0][2]);
        printf("---+-----+---\n");
        printf("%c  |  %c  | %c\n",
            board[1][0],board[1][1],
            board[1][2]);
        printf("---+-----+---\n");
        printf("%c  |  %c  | %c\n ",
            board[2][0],board[2][1],
            board[2][2]);
         if (winner)
            printf("\nCongratulations,"
            "Player %d,YOU ARE THE"
            "WINNET\n",winner);
         else
            printf("\nHow boring, it is a "
                       "draw\n");
return 0;                     
}
搜索更多相关主题的帖子: include choice player 游戏 
2014-06-25 21:10
kang520
Rank: 2
来 自:青灯孤影
等 级:论坛游民
帖 子:134
专家分:41
注 册:2014-2-27
收藏
得分:14 
这个 程序是在c语言入门经典5的书上的 数组最后一个完整的程序的样本 哈

当离开了网络——谁能填满我内心的孤独与寂寞
2014-06-25 21:38
kang520
Rank: 2
来 自:青灯孤影
等 级:论坛游民
帖 子:134
专家分:41
注 册:2014-2-27
收藏
得分:0 
你程序有点错误

当离开了网络——谁能填满我内心的孤独与寂寞
2014-06-25 21:39
南山浅悠然
Rank: 2
等 级:论坛游民
威 望:1
帖 子:53
专家分:92
注 册:2014-5-21
收藏
得分:0 
哪里错了
2014-06-26 05:48
快速回复:关于“井“字游戏的问题,请各位帮忙!
数据加载中...
 
   



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

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