Volume 10, Issue 2
In and Out
Download This Issue
Staff and Contributors
Exploring Board Game Strategies
"Your task is to build lines of balls of the same color on the checkerboard. Every time you move a ball, 3 new balls appear. When you build a line of 5 or more balls, these balls are removed from the board. Easy? And exciting!!!" 5star Free Lines--How to play (www.atomax.com/lines.stm)
Figure 7. The board of the five-star version of the game Lines.
Once again, the design pattern will help us quickly implement this game (Figure 7).
Configuration of the Board
The following values define the physical parameters of the board.
Initially, the board is empty.
But before the first play, the computer makes three balls appear.
The colors of the next three balls to be added are selected in advance and must be shown to the player to help him play. They are selected randomly by the Next function and kept in a global variable, NextBall.
The positions at which to add the balls are also selected randomly in the list of the empty positions.
RandomElement is a miscellaneous function randomly selecting an element of a list.
Visualizing the Board
Let us initialize the game and visualize it.
With some colors and a few graphics primitives, we are able to display the board and the next three balls.
In this game, a play consists of three possible successive actions: selecting and adding three balls, moving a ball, and deleting a line of balls. When a player selects a ball and a new location for it, this ball is moved. If possible a line is deleted and the computer adds three balls, possibly deleting a line.
To decide whether a motion is valid or not, we again use a fixed-point algorithm applied to a neighbourhood extension function: the set of reachable positions is incrementally built from the starting position by exploring empty locations.
We reuse the function BoardValue to simplify the computations of the neighbourhood.
Neighbour is slightly different than FaceNeighbour from the previous example.
Possible searches for empty locations in the neighbourhood.
The fixed-point algorithm is performed by IsPlayable, which returns a Boolean.
PlayBoard updates the board, checks for lines of five balls to delete, and adds three balls.
Checking for Lines to Delete
To check if there is a line of five balls to delete, we try to extend the position in four (nonoriented) directions as long as the color of the ball remains the same. This is performed by Extend.
DeleteBall suppresses the balls from the line.
And NewLine is the top-level function, returning a Boolean.
The game continues until fewer than three slots are available for the balls.
Once again, the interface with the player is extremely basic.
But we can play!
A Notebook Interface
To cope with the display of the "balls to come", it is necessary to adapt the NBView function and develop the dedicated function NBNext, which is similar to NBBoard.
To select two locations, the player clicks twice in a play on the board. This is taken into account through the variable FromLoc and by a small difference in the implementation of NBPlayThere.
With the notebook interface, playing becomes easier, even if the graphical design is not so nice.
About Mathematica | Download Mathematica Player
© 2006 Wolfram Media, Inc. All rights reserved.