• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tic Tac Toe Game query

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds correct, AC
 
Sucheta Shrivastava
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Adam & Campbell

thanks i understood
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic