The adventure generator featured at the beginning of this chapter uses a combination of MySQL and PHP code. You'll learn more about the PHP part in Chapter 8, "Connecting to Databases Within PHP." For now though, you have enough information to start building the data structure that forms the core of the game.
The adventure game is entirely about data. It has an incredibly repetitive structure. The same code operates over and over, but it operates on different parts of the database. I started the program by sketching out the primary play screen and thinking about what data elements I would need for each screen of the game. I ended up building a table that looks like Table 7.3.
id |
name |
description |
north |
east |
south |
west |
---|---|---|---|---|---|---|
0 |
-nothing- |
You cannot go that way! |
1 |
0 |
0 |
0 |
1 |
start over |
You are at a submarine yard, looking for the famous Enigma code machine |
0 |
3 |
0 |
2 |
2 |
sub deck |
As you step on the submarine deck, a guard approaches you. Your only choice is to jump off the sub before you are caught. |
15 |
15 |
15 |
15 |
3 |
warehouse |
You wait inside the warehouse. You see a doorway to the east and a box to the south. |
0 |
4 |
5 |
0 |
4 |
doorway |
You walked right into a group of guards. It does not look good... |
0 |
19 |
0 |
15 |
5 |
box |
You crawl inside the box and wait. Suddenly, you feel the box being picked up and carried across the wharf! |
6 |
0 |
0 |
7 |
6 |
wait |
..You wait until the box settles in a dark space. You can move forward or aft... |
8 |
0 |
9 |
0 |
7 |
jump out |
You decide to jump out of the box, but you are cornered at the end of the wharf. |
15 |
19 |
15 |
15 |
8 |
forward |
As you move forward, two rough sailors grab you and hurl you out of the conning tower. |
15 |
15 |
15 |
15 |
9 |
aft |
In a darkened room, you see the enigma device. How will you get it out of the sub? |
13 |
11 |
10 |
12 |
10 |
signal on enigma |
You use the enigma device to send a signal. Allied forces recognize your signal and surround the ship when it surfaces. |
14 |
0 |
0 |
0 |
11 |
shoot your way out |
A gunfight on a submerged sub is a bad idea... |
19 |
0 |
0 |
0 |
12 |
wait with enigma |
You wait, but the sailors discover that enigma is missing and scour the sub for it. You are discovered and cast out in the torpedo tube. |
15 |
0 |
0 |
0 |
13 |
replace enigma and wait |
You put the enigma back in place and wait patiently, but you never get another chance. You are discovered when the sub pulls in to harbor. |
19 |
0 |
0 |
0 |
14 |
Win |
Congratulations! You have captured the device and shortened the war! |
1 |
0 |
0 |
0 |
15 |
Water |
You are in the water. The sub moves away. It looks bad... |
19 |
0 |
0 |
0 |
16 |
0 |
0 |
0 |
0 |
||
17 |
0 |
0 |
0 |
0 |
||
18 |
0 |
0 |
0 |
0 |
||
19 |
Game Over |
The game is over. You lose. |
1 |
0 |
0 |
0 |
As you examine the chart, you can see that I simplified the game so that each possible choice in the game boils down to seven elements. Each node (or decision point) in the game consists of an id (or room number), a room name, and a description of the current circumstances. Each node also has pointers that describe what happens when the user chooses to go in various directions from that node. For example, if the user is in the warehouse (node 3) and chooses to go east, he will go to node 4, which represents the doorway. Going south from node three takes the user to node 5, which is the box. I carefully thought about the game so the data structure represents all the places the user can go in this game. I chose to think of winning and losing as nodes, so everything in the game can be encapsulated in the table.
It's critical to understand that creating the table on paper is the first step. Once you've decided what kind of data your program needs, you can think about how you will put that data together. As you'll see, choosing a database gives me an incredible amount of control, and makes it pretty easy to work with the data. Perhaps the most amazing thing is this program can handle an entirely different game simply by changing the database. I don't have to change a single line of code to make the game entirely different. All I have to do is point to a different database or change the database.
Once I decided on the data structure, I built an SQL script to create the first draft of the database. That script is shown here.
## build Adventure SQL File ## for MySQL ## Andy Harris DROP TABLE IF EXISTS adventure; CREATE TABLE ADVENTURE ( id int PRIMARY KEY, name varchar(20), description varchar(200), north int, east int, south int, west int ); INSERT INTO adventure values( 0, 'lost', 'You cannot go that way!', 1, 0, 0, 0 ); INSERT INTO adventure values( 1, 'start', 'You are at a submarine yard, looking for the famous Enigma code machine', 0, 3, 0, 2 ); INSERT INTO adventure values( 2, 'sub deck', 'As you step on the submarine deck, a guard approaches you. Your only choice is to jump off the sub before you are caught.', 15, 15, 15, 15 ); INSERT INTO adventure values( 3, 'warehouse', 'You wait inside the warehouse. You see a doorway to the south and a box to the east.', 0, 4, 5, 0 ); INSERT INTO adventure values( 4, 'doorway', 'You walked right into a group of guards. It does not look good...', 0, 19, 0, 15 ); INSERT INTO adventure values( 5, 'box', 'You crawl inside the box and wait. Suddenly, you feel the box being picked up and carried across the wharf!', 6, 0, 0, 7 ); INSERT INTO adventure values( 6, 'wait', '..You wait until the box settles in a dark space. You can move forward or aft...', 8, 0, 9, 0 ); INSERT INTO adventure values( 7, 'jump out', 'You decide to jump out of the box, but you are cornered at the end of the wharf.', 15 ,19, 15, 15 ); INSERT INTO adventure values( 8, 'forward', 'As you move forward, two rough sailors grab you and hurl you out of the conning tower.', 15 ,15, 15, 15 ); INSERT INTO adventure values( 9, 'aft', 'In a darkened room, you see the enigma device. How will you get it out of the sub?', 13 ,11, 10, 12 ); INSERT INTO adventure values( 10, 'signal on enigma', 'You use the enigma device to send a signal. Allied forces recognize your signal and surround the ship when it surfaces', 14 ,0, 0, 0 ); INSERT INTO adventure values( 11, 'shoot your way out', 'A gunfight on a submerged sub is a bad idea...', 19 ,0, 0, 0 ); INSERT INTO adventure values( 12, 'wait with enigma' ,'You wait, but the sailors discover that enigma is missing and scour the sub for it. You are discovered and cast out in the torpedo tube.', 15 ,0, 0, 0 ); INSERT INTO adventure values( 13, 'replace enigma and wait','You put the enigma back in place and wait patiently, but you never get another chance. You are discovered when the sub pulls in to harbor.', 19 ,0, 0, 0 ); INSERT INTO adventure values( 14, 'Win', 'Congratulations! You have captured the device and shortened the war!', 1, 0, 0, 0 ); INSERT INTO adventure values( 15, 'Water', 'You are in the water. The sub moves away. It looks bad...', 19 ,0, 0, 0 ); INSERT INTO adventure values( 16,'','', 0, 0, 0, 0 ); INSERT INTO adventure values( 17,'','', 0, 0, 0, 0 ); INSERT INTO adventure values( 18,'','', 0, 0, 0, 0 ); INSERT INTO adventure values( 19, 'Game Over' ,'The game is over. You lose.', 1, 0, 0, 0 ); SELECT id, name, north, east, south, west FROM adventure; SELECT id, description FROM adventure;
I actually wrote this code by hand, but I could have designed it with SQLyog just as well. Note that I created the table, inserted values into it, and wrote a couple of SELECT statements to check the values. I like to have a script for creating a database even if I built it in a tool like SQLyog, because I managed to mess up this database several times as I was writing the code for this chapter. It was very handy to have a script that could instantly rebuild the database without any tears.