• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tic Tac Toe Game query  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi , i have built a tic tac toe game where in i have to enter to mark within row-0 to 2 and column 0 to 2 . If i mark it outside say i mark it 3,3 i am getting ArrayIndexOutOfBoundException  though i have written a method to check is the move legal - isLegalMove(). It still throws an exception of arrayindex ... y so.




main class -


 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output


---------------
|     |    |    |
---------------
|     |    |    |
---------------
|     |    |    |
---------------
Next player -enter your mark
3

3
Next player -enter your mark
java.lang.ArrayIndexOutOfBoundsException: 3
at com.techlabs.sucheta.tictactoe.TicTacToe.isLegalMove(TicTacToe.java:97)
at com.techlabs.sucheta.tictactoe.TicTacToeUI.<init>(TicTacToeUI.java:22)
at com.techlabs.sucheta.tictactoe.TicTacToeMain.main(TicTacToeMain.java:7)
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could be wrong here and if I am please someone correct me

you are going outside the array because yous start counting at 0,so the array contains 3 arrays all with 3 values the 3 arrays are board[0] board[1]  board[2]

so if you want to access the third value you should enter 2

I could be wrong but check if that works
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds correct, AC
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Adam & Campbell

thanks i understood
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use == and != to check the values of String objects. Use equals() and !equals() instead. The former will result in checking for reference equality and in the context you're using it, that is incorrect.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:I could be wrong here and if I am please someone correct me

you are going outside the array because yous start counting at 0,so the array contains 3 arrays all with 3 values the 3 arrays are board[0] board[1]  board[2]

so if you want to access the third value you should enter 2

I could be wrong but check if that works

This is correct. It also shows that expecting players to mentally deal with zero based indexes is unreasonable. Allowing row/col entries that are one-based is trivial.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!