Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with method

 
John Daniels
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a simple program for tictactoe and have everything working except a method to check and see if a player has won the game diagonally. I am not sure how to write the for loop to check the board.

Could someone please help.
 
Trevor Cole
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,

I wrote a TicTacToe in J2ME, I simply use an if statement to check the value of the three cells in the diagonal...

My code looks like this...

// check first diagonal
if ((cellValues[0] == cellValues[4]) && (cellValues[4] == cellValues[8])) {
tileType = cellValues[0];
}

// check second diagonal
if ((cellValues[2] == cellValues[4]) && (cellValues[4] == cellValues[6])) {
tileType = cellValues[2];
}

cellValues[] holds the current tileType (X, O or null).

Hope this helps,
 
John Daniels
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic