九宫棋小游戏(Java自学第二天之数组)

final int SIZE = 3;
//            int[][] a = {
//                        {1,2,1},
//                        {1,2,2},
//                        {2,1,1}};
            int[][] a = new int[SIZE][SIZE];
            boolean gotResult = false;
            int numofX = 0;
            int numofO = 0;
            boolean count=true;
            for(int i=0;i<SIZE*SIZE;i++)
            {
                System.out.println("3X3规格:O/X轮流输入坐标:");
                int x,y;
                x = in.nextInt();
                y = in.nextInt();
                if(count)
                {
                    a[x][y] = 1;    //O赢
                    count=!count;
                }
                else
                {
                    a[x][y] = 2;    //X赢
                    count=!count;
                }
                //行
                if(gotResult != true)
                {
                    hang:
                    for(int q=0;q<a.length;q++)
                    {
                        numofO = 0;
                        numofX = 0;
                        for(int j=0;j<a.length;j++)
                        {
                            
                            if(a[q][j] == 1)
                            {
                                numofO++;
                            }
                            else if(a[q][j] == 2)
                            {
                                numofX++;
                            }
                        }
                        if(numofX == SIZE || numofO == SIZE)
                        {
                            gotResult = true;
                            break hang;
                        }
                    }
                }
                //列
                if(gotResult != true)
                {
                    lie:
                    for(int q=0;q<a.length;q++)
                    {
                        numofO = 0;
                        numofX = 0;
                        for(int j=0;j<a.length;j++)
                        {
                            
                            if(a[j][q] == 1)
                            {
                                numofO++;
                            }
                            else if(a[j][q] == 2)
                            {
                                numofX++;
                            }
                        }
                        if(numofX == SIZE || numofO == SIZE)
                        {
                            gotResult = true;
                            break lie;
                        }
                    }
                }
                
                //反对角线
                if(gotResult != true)
                {
                    numofO = 0;
                    numofX = 0;
                    for(int q=0,j=2;q<SIZE&&j>=0;q++,j--)
                    {
                        if(a[q][j] == 1)
                        {
                            numofO++;
                        }
                        else if(a[q][j] == 2)
                        {
                            numofX++;
                        }
                    }
                    if(numofX == SIZE || numofO == SIZE)
                    {
                        gotResult = true;
                    
                    }
                }
                    //对角线
                if(gotResult != true)
                {
                    numofO = 0;
                    numofX = 0;
                    for(int q=0;q<SIZE;q++)
                    {
                        if(a[q][q] == 1)
                        {
                            numofO++;
                        }
                        else if(a[q][q] == 2)
                        {
                            numofX++;
                        }
                    }
                    if(numofX == SIZE || numofO == SIZE)
                    {
                        gotResult = true;
                    }
                }
                
                if(gotResult)
                {
                    if(numofO == SIZE) 
                    {
                        System.out.print(" O 赢了!");
                        break;
                    }
                    else
                    {
                        System.out.print(" X 赢了!");
                        break;
                    }
                }
                else if(i == SIZE*SIZE)
                {
                    System.out.print("平局");
                    break;
                }
            }
            

程序的内容是双方依次输入九宫格的坐标 (0,0)到(2,2)中的一个,每次输入完后进行判断,若某一方满足条件就结束输入,并打印哪一方赢,若输入结束后还没得出胜负,则输出平局。

缺点:输入过的坐标,如果重复输入的话,会占用输入次数,因为比较懒这个没在开头限制。

转载于:https://my.oschina.net/u/4069817/blog/3003780

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值