An else...if not working....

For some reason, for this TicTacToe assignment I've been working on, the if...else statement checking for a tie game isn't working. This is the code I have for it.

Fuctional class:

publicclass TicTacToe

{

publicchar[] gameBoard;

privateint numMarkedSquares = 0;

publicvoid setNumMarkedSquares()

{

numMarkedSquares = numMarkedSquares++;

}

publicvoid init()

{

gameBoard =newchar[9];

for(int x = 0; x < gameBoard.length; x++)

{

gameBoard[x] =' ';

}

}

publicvoid setSquare(int squareNumber,char insert)

{

if (gameBoard[squareNumber] !=' ')

{

System.out.println("Square already marked--lose turn");

return;

}

gameBoard[squareNumber] = insert;

}

publicvoid display()

{

System.out.println(" "+ gameBoard[0] +" | "+ gameBoard[1] +" | "+ gameBoard[2]);

System.out.println("||");

System.out.println(" "+ gameBoard[3] +" | "+ gameBoard[4] +" | "+ gameBoard[5]);

System.out.println("||");

System.out.println("||");

System.out.println(" "+ gameBoard[6] +" | "+ gameBoard[7] +" | "+ gameBoard[8]);

}

publicvoid checkWin(char playerOne,char playerTwo)

{

if(((gameBoard[0] == playerOne)&&(gameBoard[1] == playerOne)&&(gameBoard[2] == playerOne)) ^ ((gameBoard[3] == playerOne)&&

(gameBoard[4] == playerOne)&&(gameBoard[5] == playerOne)) ^ ((gameBoard[6] == playerOne)&&(gameBoard[7] == playerOne)&&

(gameBoard[8] == playerOne)) ^ ((gameBoard[0] == playerOne)&&(gameBoard[3] == playerOne)&&(gameBoard[6] == playerOne)) ^

((gameBoard[1] == playerOne)&&(gameBoard[4] == playerOne)&&(gameBoard[7] == playerOne)) ^ ((gameBoard[2] == playerOne)&&

(gameBoard[5] == playerOne)&&(gameBoard[8] == playerOne)) ^ ((gameBoard[0] == playerOne)&&(gameBoard[4] == playerOne)&&

(gameBoard[8] == playerOne)) ^ ((gameBoard[2] == playerOne)&&(gameBoard[4] == playerOne)&&(gameBoard[6] == playerOne)))

{

System.out.println("Congratulations "+ playerOne +", you won!");

System.exit(0);

}

else

{

if(((gameBoard[0] == playerTwo)&&(gameBoard[1] == playerTwo)&&(gameBoard[2] == playerTwo)) ^ ((gameBoard[3] == playerTwo)&&

(gameBoard[4] == playerTwo)&&(gameBoard[5] == playerTwo)) ^ ((gameBoard[6] == playerTwo)&&(gameBoard[7] == playerTwo)&&

(gameBoard[8] == playerTwo)) ^ ((gameBoard[0] == playerTwo)&&(gameBoard[3] == playerTwo)&&(gameBoard[6] == playerTwo)) ^

((gameBoard[1] == playerTwo)&&(gameBoard[4] == playerTwo)&&(gameBoard[7] == playerTwo)) ^ ((gameBoard[2] == playerTwo)&&

(gameBoard[5] == playerTwo)&&(gameBoard[8] == playerTwo)) ^ ((gameBoard[0] == playerTwo)&&(gameBoard[4] == playerTwo)&&

(gameBoard[8] == playerTwo)) ^ ((gameBoard[2] == playerTwo)&&(gameBoard[4] == playerTwo)&&(gameBoard[6] == playerTwo)))

{

System.out.println("Congratulations "+ playerTwo +", you won!");

System.exit(0);

}

else

{

if(numMarkedSquares == 9)

{

System.out.println("Tie game!");

System.exit(0);

}

}

}

}

}

Test class:

publicclass TicTacToeTest

{

publicstaticvoid main(String[] args)

{

char playerOne ='X';

char playerTwo ='O';

TicTacToe ticTac =new TicTacToe();

System.out.println("Squares are numbered from 0-8 from left to right.");

ticTac.init();

ticTac.display();

do

{

int thePlayerOne;

int thePlayerTwo;

do

{

System.out.print("Player 1, choose a square: ");

thePlayerOne = MyInput.readInt();

}

while((thePlayerOne > 8)||(thePlayerOne < 0));

{

ticTac.setSquare(thePlayerOne, playerOne);

ticTac.display();

ticTac.setNumMarkedSquares();

ticTac.checkWin(playerOne, playerTwo);

}

do

{

System.out.print("Player 2, choose a square: ");

thePlayerTwo = MyInput.readInt();

}

while((thePlayerTwo > 8)||(thePlayerTwo < 0));

{

ticTac.setSquare(thePlayerTwo, playerTwo);

ticTac.display();

ticTac.setNumMarkedSquares();

ticTac.checkWin(playerOne, playerTwo);

}

}

while(playerOne =='X');

}

}

[7880 byte] By [Sekkitea] at [2007-10-2 3:13:02]
# 1
My goodness. I can't make heads or tails (or X's or O's) of that honkin' statement. Better figure out how to write it in a much less confusing way first.
warnerjaa at 2007-7-15 21:40:07 > top of Java-index,Java Essentials,Java Programming...
# 2

> My goodness. I can't make heads or tails (or X's or

> O's) of that honkin' statement. Better figure out how

> to write it in a much less confusing way first.

warnerja is right on this one, those if statements are not readable, and very prone to error. There must be a better, more structured way to view that logic.

pkwoostera at 2007-7-15 21:40:07 > top of Java-index,Java Essentials,Java Programming...
# 3
Indeed. If tic-tac-toe were made up of not just 9 squares, but say hundreds, would you really write up an even larger by magnitudes statement? I'd sure hope not.
warnerjaa at 2007-7-15 21:40:07 > top of Java-index,Java Essentials,Java Programming...
# 4

Sorry about that. I'm still just starting out with java, and from what I've learned in my classes so far, that is the only way I could think of to write those If statements. It does work for checking if player one or player two won. Its just the last else...if for checking for a tie game doesn't work. Once all the squares are filled, it keeps prompting for whichever player is next to select a square.

Sekkitea at 2007-7-15 21:40:07 > top of Java-index,Java Essentials,Java Programming...
# 5

The main problem is that your code is an insane satanic mess that needs to be cleaned up.

Rational indentiation, naming conventions, liberal use of meaningful whitespace, and small legible blocks of code. Despite what some insane Swedes may tell you, your prof and people on these forums don't encourage these principles because we're anal-retentive big meanies. It makes your code much easier to understand, and thus easier to debug.

The part of your code that looks most insane to me are those hideous nested do/while and while loops in your main method. In particular, look at the second to last "while". Now look at the end of that line; see the semicolon. OK, now look at the block after it. Does that block loop? In the way you were thinking it would? Somehow I think it may not.

Secondly, look at your method setNumMarkedSquares. What do you think is happening? It may be what you think is happening, but maybe not. In any event there's some extraneous stuff there and the method name doesn't really expose what's happening.

Also, I strongly question the logic of putting your game loop in a test class.

I also strongly encourage reformatting the code (preferably using the 1TBS), at least adding some whitespace between methods. And please, please consider refactoring those massive if statements.

paulcwa at 2007-7-15 21:40:07 > top of Java-index,Java Essentials,Java Programming...