tags:

views:

60

answers:

2

I'm writing a basic tic tac toe game in Java. In my Board class I have a swing window which determines which button was pressed. TttGame is the class which has a Board object.

The goal of this board is not to act as a fully functional tic-tac-toe board but to serve as a framework where the TttGame class controls all the logic. As such I need the Board to send some kind of event when the button is pressed to the TttGame class with the id of the button that has been pressed.

What is the simplest method of doing this?

edit: I found something called the Observer pattern - Is this the simplest method of doing this?

+3  A: 

Yes, the Observer pattern is what you want to apply here. Create a Listener interface (BoardListener) perhaps like this:

public interface BoardListener {
    public void squareSelected(int x, int y);
}

Then maintain a Set or List of listeners in your Board class and cycle through them, calling squareSelected on each, when a square is chosen.

I would look into the Model-View-Controller pattern however, in which the View listens to the Model for changes, and the Controller detects user actions and changes the model accordingly.

Mark Peters
I already do that in my Board class - I need to have the Board class notify the Game class
Not Important
Ah, misunderstood. Answer updated.
Mark Peters
Word of warning: For the list of listeners in your Board class use a CopyOnWriteArrayList. This prevents a ConcurrentModificationException in situations where the Board cycles through listeners and, during the iteration a listener attempts to remove itself.
Adamski
@MarkPeters: I create this interface and then have both the Board and the Game implement this interface. Then I have the board call the function??? I'm not exactly sure what you mean. The tutorial online seems to want me to extend Observable
Not Important
@Adamski: thanks
Not Important
@Not Important: The Board doesn't need to listen to itself, so it doesn't need to implement the interface. It would be responsible for *firing* the event; that is, calling `squareSelected()` on each listener in turn.
Mark Peters
@MarkPeters: I missed the Set/List part of your answer. Thanks. I'll accept it as soon as StackOverflow lets me.
Not Important
@MarkPeters: How do I pass GameBoard the listener? I tried passing "this" in the ctor as a parameter but I get noSuchMethod. the ctor takes one argument of type "BoardListener"
Not Important
@Not Important: You don't typically pass in listeners through the constructor, but rather have methods `addListener(BoardListener)` and `removeListener(BoardListener)` which just add or remove the listener from the collection. Your specific problem right now seems to be out-of-sync class files. Do a full recompile.
Mark Peters
A: 

I would suggest altering your design a bit. Having the game logic and keeping track of piece locations on the board can all be done in an "engine"(or a series of classes without a visual display).

You would then have an intermediate class for communicating all the piece locations to an interchangeable display class. It could also handle any communication from the display class to the engine(such as clicking to place an X or O)

P.S. I was thinking of checkers, not tic tac toe when typing this up, but the design is still applicable

CheesePls