Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with new game button in tic tac toe game

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so i created a basic tic tac toe game for android using a table layout. Each row has 3 buttons, one row with a textview that says if X or O has won and the final row has the button. I am trying to add a "new game" button so if that button is pressed at any time, the game restarts.

here is the code in MainActivity.java:


here is the code for the button in the xml file:


when I press this button:
02-28 17:43:29.920: E/AndroidRuntime(6200): FATAL EXCEPTION: main
02-28 17:43:29.920: E/AndroidRuntime(6200): java.lang.ArrayIndexOutOfBoundsException
02-28 17:43:29.920: E/AndroidRuntime(6200): at com.example.tictactoekw.MainActivity$PlayOnClick.onClick(MainActivity.java:204)
02-28 17:43:29.920: E/AndroidRuntime(6200): at android.view.View.performClick(View.java:2585)
02-28 17:43:29.920: E/AndroidRuntime(6200): at android.view.View$PerformClick.run(View.java:9299)
02-28 17:43:29.920: E/AndroidRuntime(6200): at android.os.Handler.handleCallback(Handler.java:587)
02-28 17:43:29.920: E/AndroidRuntime(6200): at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 17:43:29.920: E/AndroidRuntime(6200): at android.os.Looper.loop(Looper.java:130)
02-28 17:43:29.920: E/AndroidRuntime(6200): at android.app.ActivityThread.main(ActivityThread.java:3691)
02-28 17:43:29.920: E/AndroidRuntime(6200): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 17:43:29.920: E/AndroidRuntime(6200): at java.lang.reflect.Method.invoke(Method.java:507)
02-28 17:43:29.920: E/AndroidRuntime(6200): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
02-28 17:43:29.920: E/AndroidRuntime(6200): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
02-28 17:43:29.920: E/AndroidRuntime(6200): at dalvik.system.NativeStart.main(Native Method)
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at com.example.tictactoekw.MainActivity$PlayOnClick.onClick(MainActivity.java:204)

Line 204 of the MainActivity class seems to be the culprit. What happens there?
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 204 is board[x][y] = noughtsTurn ? 'O' : 'X'; from this function:
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which values do x and y have when things go wrong.? They should each be 0, 1 or 2.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic