1

I am looking for a good way to model a 'move' in a board game. I want to be able to ask a Player what their move is and have them respond, then be able to perform the correct actions on the board based on their move. The problem is, there are several types of moves, each with its own set of data. For example, in Scrabble, you can play a word, which should return the word, its position and direction (or a set of tiles with positions or whatever), but you can also swap tiles (which would involve returning the tiles you want to swap) or pass.

Similarly in chess, you normally move a piece, which should return a move with the piece and where it goes, but you could also castle, which involves indicating two pieces and their positions, or a side (king/queen), or some other piece of information besides piece/position.

I'm assuming that a Player returning a Move object is the best way to go, but I'm open to any other modeling choices or suggestions as well.

Thanks!

Risser
  • 585
  • 7
  • 20
  • I'd personally go with basic Inheritance for this one – JWiley Nov 09 '12 at 19:23
  • I don't think it's a good idea to have a common move object for all kinds of games. Or the abstraction would be just a marker. – Bhesh Gurung Nov 09 '12 at 19:24
  • To clarify, I'm not looking for a solution across multiple games. I'm looking for the solution within a particular implementation of a particular game. I don't need something that handles chess moves AND Scrabble moves, but the issue I'm trying to handle is similar for each of these games (and others). THanks! – Risser Nov 09 '12 at 20:04

5 Answers5

1

A move in a game is not really an object. It's an event.

Your chess example is the easiest. A move returns a piece and new position on the board, but it could also return a castle event, a capture event, and / or a piece promotion event.

Similarly, in a Scrabble game, a move could return a word play event, a exchange tile event, or a pass event.

Think of an event as a message to the rest of the game. You can code an event as you would a class. This event class would have to have access to the various game classes so that the model could create the message.

An event handler class processes all of the game events. This class would be the class that manipulates the other game classes. The events would just have to all be in a format that the event handler could handle.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • I like this idea, but I still have the same basic problem, in that I have three or four different events it could be. Do I compare 'eventType' and pass them to individual handlers? – Risser Nov 09 '12 at 19:57
  • Or, does the board & other game pieces subscribe to the players as listeners for player events? Again, not sure the best way to handle this. – Risser Nov 09 '12 at 19:59
  • Java Swing components have all sorts of listeners (events). You can have one event with multiple parts or separate events. Chess would lend itself to the former, while Scrabble would lend itself to the latter. It's up to you to decide what makes more sense for your game. As I said in the answer, you have an event handler. Again, it's up to you to decide in which model class the event handler method(s) make the most sense. – Gilbert Le Blanc Nov 09 '12 at 20:06
1

These may not be exactly what you are looking for, but there seems to be a lot of great ideas on this old post:

Any patterns for modelling board games?

Some of the answers include suggestions for Move implementations, and could hopefully give you some new ideas.

Community
  • 1
  • 1
Penelope The Duck
  • 656
  • 1
  • 7
  • 16
1

As a rule of thumb, if you want to have common type but different behavior for a thing - here a Move - you need to let the thing implement the behavior. So instead of asking how the board can interpret two different moves differently, you should consider how to have two different moves use the board differently when performing their move.

So: The base class (should probably be interface) is a Move, it has a perform() method, and the two kinds of moves have different implementations of the perform() method.

Anders Johansen
  • 10,165
  • 7
  • 35
  • 52
  • This was a succinct, head-slapping "of course" of an answer. Exactly what I needed. Thanks! – Risser Dec 26 '12 at 19:31
0

A good model is always based on the detailed knowledge of requirements, data flow, control flow, overall system architecture, and so on. It is not very fruitful to design an application by handwaving and philosophical argument.

Concerning your particular question, one of generally legitimate models is for the Player to return a Move, but depending on full details, its actual merit may range from a total success to a total disaster.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

If different implementations will have nothing in common other than how they present themselves to the outside world, which sort of comes across from your explanation, Move should be a loosely defined interface with each implementation across different games doing its own thing.

But my main question is whether there really is a reason to have this interface known across different games. If there is not a shared handler of some sort, which expects certain action formats across different games, I would not bother and simply define different game-specific Move classes.

amphibient
  • 29,770
  • 54
  • 146
  • 240