Previous | Next

Implementing Choices

In the stampAlot.esp application we made a short game for two players. Although the game can be played perfectly, it does not give the players any feedback about who actually won the game. We will add that now: we will write functionality to count how many cells have been colored in red or blue. At the end of the game, we will display the winning color on the board.
bluePoints = (...) //TODO
redPoints = (...) //TODO
To make the TagTiles board show the colour of the winning player, we will have write a macro that compares the number of blue and red cells after finishing the game. Lets call this macro gameResult:
gameResult = (
  if (bluePoints > redPoints) then (
    drawAll blue
  ) else (
    drawAll red
  )

drawAll c = (...) //TODO
)
This macros decides whether to light the TagTiles board in red (red player wins), in blue (blue player wins) or in white (draw). We use the if command to make this choice.

The if command

You use if like this:
if (condition) then (command1) else (command2)
If the condition holds, command1 will be started. Otherwise, command2 will be started.

In our application, the condition is whether bluePoints (the total amount of blue cells) is greater than redPoints. If the condition holds, ,drawAll blue will be started, hence the board will light up in blue. Otherwise, drawAll red will be started, lighting the board up in red.

Conditions

In the gameResult macro, we used the ">" symbol: the condition holds when the value of bluePoints is greater than the value of redPoints. In ESPranto, We can compare values in other ways using different symbols:


value1 = value2value1 is equal to value2
value1 < value2value1 is smaller than value2
value1 <= value2value1 is smaller than or equal to value2
value1 >= value2value1 is bigger than or equal to value2

Besides setting a condition by comparing values, it can also be set by the presence of a signal. For example:

if (present Signal) then (
  command1
) else (
  command2
)


Exercise

  • Integrate gameResult with stampAlot.esp, such that gameResult runs after trap Game is finished.
  • In principe, the red and blue player can have an equal amount of cells. How does the application currently deal with this?
  • Change the application such that in case of a draw, the board is filled in white. If you are having difficulties, you can have a peek at our solution here.


Previous | Next
Serious Toys logo -  tangible inspiration without limitation