Accessing returned data in java(used in tic tac toe)

97 Views Asked by At

Now how do i use the return to determine a winner. I use 8 of these statements for each of the possible winning possibilities. This ends the game after the first move, why and what would work?

 if(grid[1][1].equals(grid[1][2]) && grid[1][2].equals(grid[1][3]))
        {
            done = true;//makes game over by making done true
        }
        //later returns done
3

There are 3 best solutions below

3
On
int z = 1;
test(x,y);

Throws away the result, the z inside of test is in a local scope. You want

z = test(x,y);
System.out.println(z);
0
On

This code will compile even in this shape, as you wrote. But you dont passed value of method test anywhere, so it is redundant code. You have to declare variable and passed a value of test method to it. for example int veryImportanatValue = test(x,y);

0
On

you need a clearer understanding of global and local variables.

here, z is a local variable in main method, and another z is another local variable in your test method. they are totally different expect that they share the same name.

what you can do is to assign the return value to some local variable in your main method (the method form where you call the test method) and then do your computation with the variable you have assigned the value to.