How to return data to a different method and obtaining that data in java?

103 Views Asked by At

I am making a tic tac toe program. The program needs to be able to determine a winner or a tie. I am using a "check" method and calling it after someone inputs their moves. I'm sure it's just syntax that I don't remember and can't find in the internet but I don't know how to return data from one method to another. Also is my ,logic right for finding a tie?

 import javax.swing.JOptionPane;
    public class tictactoe
    {
        public static void main(String args[])//coordinates = row:column
        {        
            String grid[][] = new String [4][4];
            int w = 0;
            int j = 0;
            int z = 1;
            for(w = 1; w <= 3; w++)
            {
                for(j = 1; j <= 3; j++)
                {
                    grid[w][j] = "[]";
                }
            }
            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 you symbol." + "\n" +"DO NOT MAKE A MOVE YET!");
           Board_Game(grid, w, j);
        }    

        public static boolean Board_Game(String grid[][], int w, int j)
        {
            boolean test = false;
            boolean done = false;
            int player = 1;
            while(!done)
            {
                String a = grid[1][1]; String b = grid[1][2]; String c = grid[1][3]; 
                String d = grid[2][1]; String e = grid[2][2]; String f = grid[2][3];
                String g = grid[3][1]; String h = grid[3][2]; String i = grid[3][3];
                String 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++;
                    JOptionPane.showMessageDialog(null, board);
                    String r=JOptionPane.showInputDialog("                    PLAYER 1" + "\n" +"Enter the row of your desired move.");
                    int row = Integer.parseInt(r);
                    String cm=JOptionPane.showInputDialog("                    PLAYER 1" + "\n" +"Enter the column of your desired move.");
                    int column = Integer.parseInt(cm);
                    grid[row][column] = "X";
                    test = true;                
                    check(done, grid, a, b, c, d, e, f, g, h, i);
                    boolean x = done;
                }
                else if(player % 2 == 0)//player 2
                {
                    player++;
                    JOptionPane.showMessageDialog(null, board);
                    String rr=JOptionPane.showInputDialog("                    PLAYER 2" + "\n" +"Enter the row of your desired move.");
                    int row2 = Integer.parseInt(rr);
                    String cc=JOptionPane.showInputDialog("                    PLAYER 2" + "\n" +"Enter the column of your desired move.");
                    int column2 = Integer.parseInt(cc);
                    grid[row2][column2] = "O";
                    check(done, grid, a, b, c, d, e, f, g, h, i);
                    boolean x = done;
                }            

                else if(test == true)
                {
                    JOptionPane.showMessageDialog(null, "Player 1 is the winner!");
                    JOptionPane.showMessageDialog(null, board);
                }
                else if (test == false)
                {
                    JOptionPane.showMessageDialog(null, "Player 2 is the winner!");
                    JOptionPane.showMessageDialog(null, 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]) && b.equals(grid[1][3]))
            {
                done = true;
            }
            else if(grid[1][1].equals(grid[1][2]) && b.equals(grid[1][3]))
            {
                done = true;
            }
             else if(grid[1][1].equals(grid[1][2]) && b.equals(grid[1][3]))
            {
                done = true;
            }
             else if(grid[1][1].equals(grid[1][2]) && b.equals(grid[1][3]))
            {
                done = true;
            }
             else if(grid[1][1].equals(grid[1][2]) && b.equals(grid[1][3]))
            {
                done = true;
            }
             else if(grid[1][1].equals(grid[1][2]) && b.equals(grid[1][3]))
            {
                done = true;
            }
             else if(grid[1][1].equals(grid[1][2]) && b.equals(grid[1][3]))
            {
                done = true;
            }
             else if(grid[1][1].equals(grid[1][2]) && b.equals(grid[1][3]))
            {
                done = true;
            }
            int y = 0;
            for(int x = 1; x <= grid.length-1; x++)//FINDING A TIE
            {
                for(int z = 1; z <= grid.length-1; z++)
                {
                    if(grid[x][z] .equals("[]"))
                    {
                        y++;
                    }
                }
             }
            if(y == 0)
            {
                done = true;
            }
            else
            {
                done = false;
            }
            return Board_Game(done);//THIS IS WHERE I RETURN AND I DONT KNOW IF THE              //SYNTAX IS RIGHT
        }
    }
1

There are 1 best solutions below

1
On

To fix the issue with returning values, you could do this:

Change your method signature and let it return a boolean:

public static boolean check(String grid[][], String a, String b, String c,String d, String e, String f, String g, String h, String i) {
    boolean done = false;

    // ....
    // do the checking now, and set done to true if its done ;-)

    return done;

And then call your method like this:

done = check(grid, a, b, c, d, e, f, g, h, i);