Chapter 15. Dice of Doom, a Game Written in the Functional Style

Now we’re finally ready to create a more sophisticated (and fun) computer program in the functional style. As we expand this program throughout the rest of this book, you’ll learn about techniques for writing elegant functional code, while at the same time maintaining strong performance in your programs.

image with no caption

The Rules of Dice of Doom

Dice of Doom is a game in the same family as Risk, Dice Wars (http://www.gamedesign.jp/flash/dice/dice.html), and KDice (http://kdice.com/). In the beginning, we’re going to keep the rules of Dice of Doom mind-numbingly simple. In later chapters, we’ll expand the rules, until eventually we’ll have a game very similar to Dice Wars.

Here are the simplified rules we’ll start with:

  • Two players (named A and B) occupy spaces on a hexagonal grid. Each hexagon in the grid will have some six-sided dice on it, owned by the occupant.

  • During a turn, a player can perform any number of moves, but must perform at least one move. If the player cannot move, the game ends.

  • A move consists of attacking a neighboring hexagon owned by the opponent. The player must have more dice in her hexagon than the neighboring hexagon in order to attack. For now, all attacks will automatically lead to a win. In future variants, we’ll actually roll the dice for a battle. But for now, the player with more dice just wins automatically.

  • After winning a battle, the losing player’s dice are removed from the board, and all but one of the winning player’s dice are moved onto the newly won hexagon.

  • After a player is finished making her moves, reinforcements are added to that player’s dice armies. Reinforcements to the player’s occupied hexagons are added one die at a time, starting from the upper-left corner, moving across and down. The maximum number of dice added as reinforcements is one less than the player took from the opponent in her completed turn.

  • When a player can no longer take her turn, the game has ended. The player who occupies the most hexagons at this point is the winner. (A tie is also possible.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.71.28