- Reset Game
- Book
- BASIC
-
Reverse
Original Resources
Download the original BASIC code. Check out the original page the text below was taken from in the Book.
Instructions From the Book
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!
There are many ways to beat the game, but approaches tend to be either algorithmic or heuristic. The game thus offers the player a chance to play with these concepts in a practical (rather than theoretical) context.
An algorithmic approach guarantees a solution in a predictable number of moves, given the number of items in the list. For example, one method guarantees a solution in 2N -: 3 moves when the list contains N numbers. The essence of an algorithmic approach is that you know in advance what your next move will be. One could easily program a computer to do this.
A heuristic approach takes advantage of "partial orderings" in the list at any moment. Using this type of approach, your next move is dependent on the way the list currently appears. This way of solving the problem does not guarantee a solution in a predictable number of moves, but if you are lucky and clever, you may come out ahead of the algorithmic solutions. One could not so easily program this method.
In practice, many players adopt a "mixed" strategy, with both algorithmic and heuristic features. Is this better than either "pure" strategy?
The program was created by Peter Sessions of People's Computer Company and the notes above adapted from his original write-up.
Modifications For This Version
As with many of the modern adaptations, I've stayed pretty true to the original. There are, however, some minor variances. For example, in the original version, entering a 0 would end the game. In the modern version, if the player wants to end the game, they can simply click the "Reset Game" link. If anything other than 1 through 9 is enterd, an error message will be displayed. User input uses a web form rather than the command line, of course. Otherwise, things are functionally pretty much the same.
In the future, I may add some logic to give the user different feedback based on how many (or how few) tries it takes them. Maybe with fewer tries they get a more congratulatory message, but with too many tries, the computer mocks them for taking too long. If I'm feeling really ambitous I may (someday in the very distant future) write an algorithm that figures out the fewest number of tries needed to solve the puzzle. But don't count on it. Lastly, there is a small tribute to HomeStarRunner.
Technical Stuff
I've once again decided to use PHP rather than JavaScript. However, this is the kind of program that is well-suited for JavaScript because there's no secret information that is needing to be hidden from the user. In guessing games, there is a secret value that the user is trying guess. This secret value (for non-layfolk) can easily be assertained with a quick peek at the code, and a quick console query. This game, however, doesn't need to hide anything from the user. So I may, someday, build a JavaScript version of it. This will likely be the approach I take when I build a CLI Version.
The Reason I chose PHP, however, is because I thought it would be an interesting excercise to see how I would effeciently keep track of things like the number of tries, the history of the swaps, and other things that need to persist even though the page is being reloaded with each user interaction. Of course, that's what sessions are for. Did I make good use of sessions? Should I have made this in JavaScript? Who knows? Feel free to take a peek a the BASIC code and build your own version in your language of choice. Until you do, however . . .
Enjoy Reverse!
Reverse
This is the game of 'reverse'. To win, all you have to do is arrange a list of numbers (1 through 9) in numerical order from left to right. To move, you tell me 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!
1 2 3 4 5 6 7 8 9