Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any patterns for modelling board games? [closed]

For fun, I'm trying to write one of my son's favorite board games as a piece of software. Eventually I expect to build a WPF UI on top of it, but right now I'm building the machine that models the games and its rules.

As I do this, I keep seeing problems that I think are common to many board games, and perhaps others have already solved them better than I will.

(Note that AI to play the game, and patterns around high performance are not interesting to me.)

So far my patterns are:

  • Several immutable types representing entities in the game box, e.g. dice, checkers, cards, a board, spaces on the board, money, etc.

  • An object for each player, which contains the players resources (e.g. money, score), their name, etc.

  • An object that represents the state of the game: the players, who's turn it is, the layout of the peices on the board, etc.

  • A state machine that manages the turn sequence. For example, many games have a small pre-game where each player rolls to see who goes first; that's the start state. When a player's turn starts, first they roll, then they move, then they have to dance in place, then other players guess what breed of chicken they are, then they receive points.

Is there some prior art I can take advantage of?

EDIT: One thing I realized recently is that game state can be split in to two categories:

  • Game artifact state. "I have $10" or "my left hand is on blue".

  • Game sequence state. "I have rolled doubles twice; the next one puts me in jail". A state machine may make sense here.

EDIT: What I'm really looking for here is the best way to implement multiplayer turn-based games like Chess or Scrabble or Monopoly. I'm sure I could create such a game by just working through it start to finish, but, like other Design Patterns, there are probably some ways to make things go much more smoothly that aren't obvious without careful study. That's what I'm hoping for.

like image 882
Jay Bazuzi Avatar asked Dec 11 '08 21:12

Jay Bazuzi


People also ask

What is the most complicated board game in the world?

Google artificial intelligence beats champion at world's most complicated board game. An artificial intelligence program developed by researchers at Google can beat a human at the board game Go, which some consider to be the most complicated board game in existence.

Do they still make board games?

Every year, thousands of new board games are published—more than our guide to the best beginner board games for adults could possibly accommodate. Here we list a few Wirecutter staff favorites. And though these may not be as approachable for new gamers, they have other traits we think you'll love.

What is an OOP board game?

and OOP stands for out of print. 20.


1 Answers

it seems this is a 2 month old thread that I've just noticed now, but what the heck. I've designed and developed the gameplay framework for a commercial, networked board game before. We had a very pleasant experience working with it.

Your game can probably be in a (close to) infinite amount of states because of the permutations of things like how much money player A has, how much money player B has, and etc... Therefore, I'm pretty sure you want to stay away from state machines.

The idea behind our framework was to represent the game state as structure with all the data fields that together, provide the complete game state (ie: if you wanted to save the game to disk, you write that structure out).

We used the Command Pattern to represent all of the valid game actions a player could make. Here would be an example action:

class RollDice : public Action {   public:   RollDice(int player);    virtual void Apply(GameState& gameState) const; // Apply the action to the gamestate, modifying the gamestate   virtual bool IsLegal(const GameState& gameState) const; // Returns true if this is a legal action }; 

So you see that to decide whether a move is valid, you can construct that action and then call its IsLegal function, passing in the current game state. If it is valid, and the player confirms the action, you can call the Apply function to actually modify the game state. By ensuring that your gameplay code can only modify the game state by creating and submitting legal Actions (so in other words, the Action::Apply family of methods are the only thing that directly modifies the game state), then you ensure that your game state will never be invalid. Furthermore, by using the command pattern, you make it possible to serialize your player's desired moves and send them over a network to be executed on other player's game states.

There ended up being one gotcha with this system that turned out to have a fairly elegant solution. Sometimes actions would have two or more phases. For example, the player may land on a property in Monopoly and must now make a new decision. What is the game state between when the player rolled the dice, and before they decide to purchase a property or not? We managed situations like this by featuring an "Action Context" member of our game state. The action context would normally be null, indicating that the game is not currently in any special state. When the player rolls the dice and the dice rolling action is applied to the game state, it will realize that the player has landed on an un-owned property, and can create a new "PlayerDecideToPurchaseProperty" action context that contains the index of the player we are waiting for a decision from. By the time the RollDice action has completed, our game state represents that it is currently waiting for the specified player to decide whether to buy a property is not. It is now easy for all other actions' IsLegal method to return false, except for the "BuyProperty" and "PassPropertyPurchaseOpportunity" actions, which are only legal when the game state has the "PlayerDecideToPurchaseProperty" action context.

Through the use of action contexts, there is never a single point in the life-time of the board game where the game state structure does not completely represent EXACTLY what is happening in the game at that point in time. This is a very desirable property of your board game system. It will it much easier for you to write code when you can find everything you ever want to know about what's happening in the game by examining only one structure.

Furthermore, it extends very nicely to networked environments, where clients can submit their actions over a network to a host machine, which can apply the action to the host's "official" game state, and then echo that action back to all the other clients to have them apply it to their replicated game states.

I hope this was concise and helpful.

like image 194
Andrew Top Avatar answered Sep 22 '22 21:09

Andrew Top