by Philippe Mougin
April 2006
Read all the articles in this series |
Paul Bissex writes about how the game of Reverse can be implemented in various languages.
The game of Reverse requires you to arrange a list of numbers in numerical order from left to right. To move, you tell the computer how many numbers (counting from the left) to reverse. For example, if the current list is 2 3 4 5 1 6 7 8 9 and you reverse 4, the result will be 5 4 3 2 1 6 7 8 9. Now if you reverse 5, you win.
Here is a version in F-Script:
|
Upon execution of this program, F-Script displays the initial list of numbers. In the spirit of F-Script, we interact with the game using F-Script itself. We play by evaluating the
flip
block, passing it the number of elements to reverse. For example, here is the end of a game:
...
|
Now that we have an implementation of the game, we can play with it interactively. We can also have fun writing programs that play automatically (and hopefully win). Below is such a program. Upon completion, it displays the detail of each step of the game and the number of steps it took to win.
|
It is based on the following process:
Copyright © 2006 Philippe Mougin