九宫棋小游戏(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)中的一个,每次输入完后进行判断,若某一方知足条件就结束输入,并打印哪一方赢,若输入结束后还没得出胜负,则输出平局。next

缺点:输入过的坐标,若是重复输入的话,会占用输入次数,由于比较懒这个没在开头限制。co

相关文章
相关标签/搜索