I know this is very specific question but imagine I have a board with spaces (like Monopoly) and a player that must go from one space to another after the dice are thrown.

So my workflow:

  • Player roll dice
  • After the player rolled dice
    • (from player class) space = board.getNextSpaceFromPlayer(player)
    • Move the player each step until it reach the destination
      • board.movePlayerToNextSpace(player);
      • OR
      • player.moveToSpace(space);

But how exactly do I move the player?

The board must move the player or the player move itself?

I'm trying hard to reflect how the things are in the real world but I find very hard to do so in this context.

Thanks for your help.


The board must move the player or the player move itself?

I would say neither. The board and player are 2 objects managed by a different object that runs logic depending on the information given by the board and applied onto the player. So you move the player by the "game logic". Which depends on the information returned by the board. Which in this scenario could be the cell(A space on your board) relatively from the players current position to the next calculated cell determined by the die. A Cell holds various information, like which player owns it ( if we are talking about monopoly) trough id. The condition the player must meet before progressing any further etc.

Your player could simply be a sprite, and by using an additional object you can keep track of stats like id and position. ( although you could just program the sprite and the stats into one object) This information is passed onto the board itself and processed internally to handle its own information. It then gives information back to the "game logic" and the player is moved by the conditions that are given.

So basically :

GameLogicObject ( or simply put BoardGame)
    \-->Board //contains position grid, etc.
    \-->playerList[] ( using id 0, 1 , 2 and 3 to access the right player 
        information in combination with currentPlayer++ after each 
        round depending on the amount of players and looped back to 0)
    \-->Other properties
       Game stats like current gameState, Time etc.
  • \$\begingroup\$ Game.rollDices(){dicesValue=7} -> Game.moveCurrentPlayerForward(places=7) -> for each crossing property -> {from Game} property=Board.getNextPropertyForPlayer(player) -> {from Game} Game.moveCurrentPlayerToPosition(property.getPosition()) ||| is something like this? \$\endgroup\$ – Totty.js Aug 6 '12 at 23:10
  • \$\begingroup\$ Yeah that doesn't sound too far off from what I was thinking. You might want to consider the animation ques as well to make everything go concurrent (animate to next cell, perform next check, animate to next cell, perform check, etc) However you should save this for last. Keep in mind that I'm only giving an idea. Prioritize what is crucial to the game and break them down into objects and actions ( methods and whatnot). \$\endgroup\$ – Sidar Aug 6 '12 at 23:43
  • \$\begingroup\$ I use callbacks for each methods so they when one is done, goes the next and so on, because of the animations ;) thanks for the help \$\endgroup\$ – Totty.js Aug 6 '12 at 23:58

Some times it is hard to determine who is the real actor.

Why not saving the players tokens in the player itself so that each player has a reference to it's tokens (even if you only have one) then let the board roll a dice.

The board itself (being a representation of the actual game rules) should be called to move a specific token (by players choice if there are more than one) with a specific dice roll.

So in a monopoly style game it would be the only token a player has wich is moved forward for a given ammount of fields. In a Ludo game the player would chose wich token is is, that he wants to move and submit it to the board class.

Why do I put the dice roll into the board? Because it depends on the boardgame itself what dice is it to use or if the actual player has to roll the dice more than once (because of special game rules (e.g. prison in monopoly)

So your Player would actually be a class that holds every information about a player. Name, Points, Money, game tokens that belong to him etc. . While the board says whose turn it is and what options are open to that player.

How you move your game tokens is up to the game rules. Is it important that a player cant cross specific fields without stopping, then you should have a method that moves that token only one field at a time to check if such a field has been crossed.

If it isn't that important you could just say "move my game token for 4 fields in that direction".

You could even go all the way down to writing a standard board game Interface that you can use to implement several different board games wich have a more or less linear game plan layout (monopoly, ludo, game of life).

  • \$\begingroup\$ I can't really understand what is the meaning of the tokens in this context, may you explain a little bit? thanks \$\endgroup\$ – Totty.js Aug 6 '12 at 20:46
  • \$\begingroup\$ Sorry I'm not familiar with some parts of the english vocabulary ^^ with "tokens" i mean the figures you play with \$\endgroup\$ – Aron_dc Aug 6 '12 at 21:05
  • \$\begingroup\$ Yes I will use a MVC structure where the view will be the token and the controller will be used as an API by the board to interact with the player. Do you know some interesting resources it might be useful to know before starting to make the game? I'm thinking of making a fast/small prototype of the game mechanics before beginning the development. \$\endgroup\$ – Totty.js Aug 6 '12 at 21:24
  • \$\begingroup\$ Your view would be a specific boardview. The tokens are just an object representing the game figures. You should read some basic information about MVC \$\endgroup\$ – Aron_dc Aug 6 '12 at 22:28

It is a rather unique relationship between the board and the player, isn't it?

In this case, it seems that the Board contains Spaces, and the Player can inhabit a particular Space on the Board.

I'd think it makes more sense for the Player to move itself, because the Player is the one that contains the reference to the particular Space that it inhabits.

However, that also means that the Player will need to ask either the Space it currently inhabits or the Board for a reference to the next Space on the Board.

  • \$\begingroup\$ so I would be using player.moveToSpace(space) and the player must only be aware about the board in order to get the next space? \$\endgroup\$ – Totty.js Aug 6 '12 at 19:08

In order to not limit yourself in the future, you may wish to trigger a move of the piece from the players action.

However, a player class should not contain any methods such as "movePiece", but instead they should have methods like "clickedGameSpace", or something more player-centric.

It's your board class that should be acting upon the board, not the player. However, the player would probably be best as the initiator of the move, even if that requires some abstraction to convert from a "gamespace click" to a "piece move"

You may even wish to have the Player class be completely uncoupled from the board. Instead of clicking "spaces", it simply picks options... but there are many ways to implement this which could be considered good practice.

  • \$\begingroup\$ So you are saying: player.rollDices()->player.fireEvent('moveToNextSpaceRequested')->board catches the event and then board.movePlayerToNextSpace(player)->move player visually and then set the player.setCurrentLocation(space)->space.setVisitor(player) ---- Is this correct? \$\endgroup\$ – Totty.js Aug 6 '12 at 19:04
  • \$\begingroup\$ Essentially. Though do your Spaces really need to know who's visiting the location? \$\endgroup\$ – Raceimaztion Aug 6 '12 at 19:18
  • \$\begingroup\$ the space must know who is visiting in order to the visitor to pay the rent. If the visitor == owner then no rent must be paid. \$\endgroup\$ – Totty.js Aug 6 '12 at 19:22
  • \$\begingroup\$ For example... Clicking the window would result in the ViewManager translating the click into a ViewElement that was clicked. The ViewElement might do a few additional things (e.g. confirm the click), and then it would inform the GameState that "Space X" was clicked. The GameState then makes the required calls to the Player and Space objects. \$\endgroup\$ – Wisteso Aug 7 '12 at 16:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.