在tic tac toe java中寻找胜利者(返回)

时间:2013-11-30 20:40:30

标签: java return return-value tic-tac-toe

此tic tac toe代码适用于除确定获胜者之外的所有内容。它可以确定一个平局,并很好地展示董事会。 我觉得问题在于从'check'方法返回变量'done'但我不知道是什么。我的返回语法生锈了所以我不确定我是否可以'记得如何访问返回的变量或者我被困住了什么。任何帮助是极大的赞赏。提前谢谢!

新代码

     import javax.swing.JOptionPane;
public class tictactoe
{
    public static void main(String args[])//coordinates = row:column
    {        
        String grid[][] = new String [4][4];//creates 2D array
        int w = 0;//initiates counter
        int j = 0;//initiates counter
        for(w = 1; w <= 3; w++)//counter
        {
            for(j = 1; j <= 3; j++)///counter
            {
                grid[w][j] = "[]";//fills the array with "[]" (empty symbol)
            }
        }
        JOptionPane.showMessageDialog(null,"WELCOME to Matt's Fun World of TIC TAC TOE");
        JOptionPane.showMessageDialog(null, "Find the number of the box " + "\n" + "that you would like to enter your coordinates.");
       board_game(grid, w, j);//sends grid, w and j to the board game method
    }    

    public static void board_game(String grid[][], int w, int j)
    {
        boolean test = false;//checks which player won the game
        boolean done = false;//used to test if the game is done
        int player = 1;//used to switch between player 1 and 2
        String a = grid[1][1]; String b = grid[1][2]; String c = grid[1][3]; //makes playing board
            String d = grid[2][1]; String e = grid[2][2]; String f = grid[2][3];//makes playing board
            String g = grid[3][1]; String h = grid[3][2]; String i = grid[3][3];//makes playing board
            String board = 
                     ("       "+a+"       |          "+b+"        |       "+c+"          "      + "\n" +//makes board
                      "-------------|---------------|-------------       "      + "\n" +
                      "        "+d+"       |         "+e+"         |       "+f+"    "    + "\n" +
                      "-------------|---------------|-------------       "      + "\n" +
                      "        "+g+"      |          "+h+"        |       "+i+"    "  );

        while(!done)
        {
            a = grid[1][1]; b = grid[1][2]; c = grid[1][3];//makes board be able to use in loop 
            d = grid[2][1]; e = grid[2][2]; f = grid[2][3];
            g = grid[3][1]; h = grid[3][2]; i = grid[3][3];                                                           
            board = 
                     ("       "+a+"       |          "+b+"        |       "+c+"          "      + "\n" +
                      "-------------|---------------|-------------       "      + "\n" +
                      "        "+d+"       |         "+e+"         |       "+f+"    "    + "\n" +
                      "-------------|---------------|-------------       "      + "\n" +
                      "        "+g+"      |          "+h+"        |       "+i+"    "  );

            check(done, grid, a, b, c, d, e, f, g, h, i);
            if(player % 2 != 0)//player 1 
            {
                player++;//moves it to next player
                JOptionPane.showMessageDialog(null, board);//prints board
                String r=JOptionPane.showInputDialog("                    PLAYER 1" + "\n" +"Enter the row of your desired move.");
                int row = Integer.parseInt(r);//parses which row in desired
                String cm=JOptionPane.showInputDialog("                    PLAYER 1" + "\n" +"Enter the column of your desired move.");
                int column = Integer.parseInt(cm);//parses which column is desired
                grid[row][column] = "X";//puts an X in the desired location
                test = true;//says player 1 wins if a winner is determined                
                done = check(done, grid, a, b, c, d, e, f, g, h, i);//sends done, grid, a, b, c, d, e, f, g, h, i
            }
            else if(player % 2 == 0)//player 2
            {
                player++;//moves it to next player
                JOptionPane.showMessageDialog(null, board);//prints board
                String rr=JOptionPane.showInputDialog("                    PLAYER 2" + "\n" +"Enter the row of your desired move.");
                int row2 = Integer.parseInt(rr);//parses which row is desired
                String cc=JOptionPane.showInputDialog("                    PLAYER 2" + "\n" +"Enter the column of your desired move.");
                int column2 = Integer.parseInt(cc);//parses which column is desired
                grid[row2][column2] = "O";//puts an X in the desired location
                done = check(done, grid, a, b, c, d, e, f, g, h, i);//sends done, grid, a, b, c, d, e, f, g, h, i
            }            
        }
        if(test == true)
        {
            JOptionPane.showMessageDialog(null, "Player 1 is the winner!");
            JOptionPane.showMessageDialog(null, board);//prints the board
        }
        else if (test == false)
        {
            JOptionPane.showMessageDialog(null, "Player 2 is the winner!");
            JOptionPane.showMessageDialog(null, board);//prints the board
        }
    }

    public static boolean check(boolean done, String grid[][], String a, String b, String c,String d, String e, String f, String g, String h, String i)
    {
        if(grid[1][1].equals(grid[1][2]) && grid[1][2].equals(grid[1][3]))
        {
            done = true;//makes game over by making done true
        }
        else if(grid[2][1].equals(grid[2][2]) && grid[2][2].equals(grid[2][3]))
        {
            done = true;//makes game over by making done true
        }
         else if(grid[3][1].equals(grid[3][2]) && grid[3][2].equals(grid[3][3]))
        {
            done = true;//makes game over by making done true
        }
         else if(grid[1][1].equals(grid[2][1]) && grid[2][1].equals(grid[3][1]))
        {
            done = true;//makes game over by making done true
        }
         else if(grid[1][2].equals(grid[2][2]) && grid[2][2].equals(grid[3][2]))
        {
            done = true;//makes game over by making done true
        }
         else if(grid[1][3].equals(grid[2][3]) && grid[2][3].equals(grid[3][3]))
        {
            done = true;//makes game over by making done true
        }
         else if(grid[1][1].equals(grid[2][2]) && grid[2][2].equals(grid[3][3]))
        {
            done = true;//makes game over by making done true
        }
         else if(grid[1][3].equals(grid[2][2]) && grid[3][1].equals(grid[1][3]))
        {
            done = true;//makes game over by making done true
        }
        else
        {
            int y = 0;//initiates the empty space counter at zero
            for(int x = 1; x <= grid.length-1; x++)//counter
            {
                for(int z = 1; z <= grid.length-1; z++)//counter
                {
                    if(grid[x][z] .equals("[]"))//if space is equal to "[]"
                    {
                        y++;//adds one to y(counter)
                    }
                }
            }
            if(y == 0)//if there are no empty spaces 
            {
                done = true;//game over
                JOptionPane.showMessageDialog(null, "IT'S A DRAW!");                
            }
            else
            {
                done = false;//game isn't over
            }
        }
        return done;//return done
    }
}

2 个答案:

答案 0 :(得分:0)

永远无法联系到显示代码的获胜者,因为player总是奇数或偶数 - 如果它不奇怪,那就是偶数。

答案 1 :(得分:0)

您的方法check()无法正常工作。就像我一样,如果玩家赢了并观察check()方法,请逐步调试代码。那些逻辑错误的东西。

尽管如此,就像Robin Green解释的那样:你的其他if(test ...)案例将永远不会达到,因为前两个if语句之一总是正确的。