Learn Ruby the Hard Way – Exercise 43 Gothons from Planet Percal #25

Here is my work for Exercise 43: on github.

Study Drills
1. Change it! Maybe you hate this game. Could be to violent, you aren’t into sci-fi. Get the game working, then change it to what you like. This is your computer, you make it do what you want.
I made a new game with a similar set up of class Engine and Map, to solidify my understanding of how the two work together in this example. My game code is available on github.

2. I have a bug in this code. Why is the door lock guessing 11 times?

bug in while loop
bug in while loop

The door lock guesses 11 times because the while loop executes a maximum of 10 times. Guesses can go from 0 to 9 and the second condition will still be true. Before the while loop, the user already gets one guess at the door lock, this makes a total of 11 guesses possible. To fix this, we can set guesses to 1 on line 14.

3. Explain how returning the next room works.
Each room returns a string at its conclusion, this is either a reference to the next room, death, or win.

Returning the next room as a string assigns this returned value to next_scene_name. We then call the next_scene function  and give it a parameter of next_scene_name. The next_scene function looks at the @@scenes hash with next_scene_name as the key, and gets the corresponding value. The corresponding value is a new instance of the next room. So we create a new instance of the next room, and then call the enter function on that room.

4. Add cheat codes to the game so you can get past the more difficult rooms. I can do this with two words on one line.
I added a cheat code ‘cheat’ for the keypad stage and the escape pod choice. If the user types ‘cheat’ for the input guess, then I assign the correct value to the guess variable. This way, they will pass that stage! (The condition of guess == password and guess == good_pod will be true.)

5. Go back to my description and analysis, then try to build a small combat system for the hero and the various Gothons he encounters.
I added a combat system to my new game. There is a Witch class and a Hero class. They have @health instance variables and attack/rest methods.

6. This is actually a small version of something called a “finite state machine.” Read about them. They might not make sense but try anyway.
A finite state machine is a mathematical model of computation. It is used to design computer programs and logical circuits. There are, as the name implies, a finite number of states the machine can be in. It can only be in one state at one time. In this exercise, the game play can only be in one scene at a time. Actions in each of these scenes lead to the changing of the states (changing to a different scene). These actions and triggering events are called transitions.

A simple example of a finite state machine is a turnstile. A turnstile has only two states, locked and unlocked. There are two actions we can do, we can deposit a coin (or swipe a card) and we can push the turnstile. We can look at the possibilities in the state diagram below. Each state is a node(circle), each transition is an edge (line).

state diagram for a turnstile
state diagram for a turnstile

When the turnstile is in the locked state, pushing it does not change the state. Depositing a coin changes the turnstile from locked to unlocked. When the turnstile is in the unlocked state, pushing it will change it to the locked state. When one person passes through the turnstile, the turnstile turns, and then locks again. When the turnstile is in the unlocked state, depositing a coin will not change the state. It’s already unlocked, so there is no change to the state.

Summary
This was a really interesting exercise! I learned how to make a simple game engine and map to create a game in Ruby. The map class allows us to move between different scenes/states in this finite state machine set-up. Finite state machines are mathematical computation models that have a finite number of states, and where the machine can only be in one state at a time. Triggering events and actions that change the machine’s state are called transitions. You can represent these machines with state diagrams: nodes indicate states, and edges indicate transitions.

When looking at the state diagram, we can see that the nodes and edges can make up a maze. We can use a finite state machine to solve a maze.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s