Creating the Ludo Board view
TL;DR
We implement a visualization of the board state, which will later also serve as a GUI for the game, as well as the basis for other views.
The Board view
Before we implement the game logic, it would be nice to have already a visualization of the Board state. We can lay out 40 squares on an 11x11 grid.
NB: As before we continue to experiment and prototype our ideas as Notebook snippets, while documenting our steps.
Here is a simple experiment showing how we can place squares on arbitrary locations on a board:
container := (BlElement new) size: 100 @ 100; background: Color blue. container addChild: ((BlElement new) background: Color yellow; size: 10 @ 10; relocate: 20 @ 30; when: BlClickEvent do: [ :event | event currentTarget phlow spawnObject: 42 ]). container asScalableElement
We start building the GtLudoBoardElement
class.
We encode magic numbers such as the grid size and the default dimensions as constant methods, GtLudoBoardElement>>#gridSize
and GtLudoBoardElement>>#dimensions
.
We hard-code the positions of the first ten squares as GtLudoBoardElement>>#playerAPositions
, and then compute the other positions by rotation in GtLudoBoardElement>>#playPositions
.
Here is the visualization of the earlier example:
GtLudoBoardElement for: GtLudoBoardExamples new boardWith2PlayingPlayers
Adding start and goal positions
We also want to display the tokens that are in the start position, or have reached their goal.
We add slots for startSquares and goalSquares to the Board.
We add a kind
slot to Squares so we know what kind a given square is.
We extend the BoardElement to display the start and goal squares, and we assign colors to them based on their kind.
Now we can programmatically move a token around the board.
example := GtLudoBoardExamples new boardWith2PlayingPlayers . board := GtLudoBoardElement for: example
Now by playing with the following snippet we can move the token A around the board:
position := 9. example players first tokens first goToSquare: (example squares at: position)