• Post Reply Bookmark Topic Watch Topic
  • New Topic

TicTacToe  RSS feed

 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using model view controller and when I click a button on the board the view gets updated and works. My gameOver does not doesn't seem to check. I think both are easy fixes but I am having trouble catching it.
I get this error at my setter
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at PA7.TicTacToeModel.setBoard(TicTacToeModel.java:28)
at PA7.TicTacToeController$1.actionPerformed(TicTacToeController.java:43)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After your loops that start on lines 194 and 195, the values of r and c are both 3, and that's what is passed to setBoard(). You need to pass the current values of r, c into each ActionListener at the time of its construction, and store them as member variables there. It is actually possible to do that with anonymous inner classes like you are using, but it's a little tricky to get the syntax right. You might want to just define a CellActionHandler class instead.

Also, it's a good idea to use braces around loop bodies even when they just consist of one statement, especially if that statement is multiple lines. That section of code is hard for me to read, and I've been reading Java code for a long, long time now.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do I pass the current values of r and c into the ActionListener?
Which loop is missing braces?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which loop? Neither of the loop bodies starting after lines 194 and 195 have braces around them. That's OK with the compiler because technically each one is a single statement, but it's a better idea to just use braces each time.

Passing in the parameters to your action listener would mean defining an initialized method, which returned "this" so that you could call it as part of your chain. (I said it was a bit tricky.)

Something like this should work:



Compiling left as an exercise for the OP.
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks lol Now that I have made that change It doesn't switch from X to O
 
Ryan O'Neill
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone offer some advice as to why it doesn't switch from X to O?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan O'Neill wrote:Anyone offer some advice as to why it doesn't switch from X to O?

What do you mean by "doesn't switch from X to O"?

Since I'm no GUI expert, I can't really help you with the "V" part; but I worry a bit that your model code is very procedural, and also rather brittle. Suppose someone asked you to change this program to provide a 4x4 or 5x5 game - how much work do you think it would take?

I also worry that you're using Strings to represent your squares - and that may have something to do with your problem, since Strings are immutable (ie, you can't change them); but I honestly don't know. You may want to read the StringsAreBad page for more info.

You might also want to consider a Board class that encapsulates your grid. Such a class might, for example, be able to return a "row", "column" or "diagonal" as an array (or List) of squares. Do you see how that might make your gameover() method a bit simpler?

However, all that is perhaps for a "Mark II" version.

One thing I can see though: Your gameover() method doesn't appear to include a check for "O" winning on the diagonals. In fact, the whole method seems very redundant. Have you thought of maybe passing "X" or "O" to it as a parameter?

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!