• 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

problem with new game button in tic tac toe game

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 204 is board[x][y] = noughtsTurn ? 'O' : 'X'; from this function:
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which values do x and y have when things go wrong.? They should each be 0, 1 or 2.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic