## Beginning Programming Series - Part 5

### Developing the Game

So now we have the general display worked out, lets work out some of the rules and goals of this game. First the primary goal is to make it from your starting position at star position 1,1 through the stargate at star position 8,12. This implies movement of your ship. This is done by entering the coordinates that you wish to move to. It would be simple just to jump straight to the stargate (coordinates 8,12) except you must first have the energy reserve to make this move. With that introduction out of the way, here are the rules:

1. Energy reserves start at just 20 gigajoules.

2. The movement of one star position (a unit of movement) to the next either the x or y direction requires 100 gigajoules.

3. Diagonal movements are permitted, but the energy consumption is greater. It is equal to 1.25 times the calculated distance of the hypotenuse formed by the horizontal and vertical distances. The graphic below demonstrates:

In this image we are moving a vertical distance of 2 and a horizontal distance of 3. The actual distance calculated (using simple geometry) is about 3.6 units. We would multiply that times 1.25 to make a total movement of 4.5 units, requiring 450 gigajoules to make the jump.

4. Energy is increased by sitting still. Each move that you sit still your energy will increase by anywhere from 65 to 105 gigajoules. Since the energy is harvested from distant stars, and their energy is variable, the actual amount of energy stored is somewhat random in nature. Storage cells on board our ship prevent us from storing any more than 485 gigajoules of energy.

5. Did I mention that this is hostile space? Each turn, whether you move or sit still the computer will randomly place a "K" mine where there once was empty space. If you land on one, or if one is placed on top of you then you loose all your energy and must start over at star position 1,1. The game is over the third time you are hit!

So there are the rules. Are you ready to develop the game? Well here we go...

### The pseudo code

This is the largest project yet, representing a huge a significant step in skill and knowledge. In order to get from our basic set of game rules to a working program we will need to develop a better model of our program. For this we will turn to pseudo coding, a technique discussed in the second installment of this series. We will develop the pseudo code structure in several passes, refining it as we go. This works well for big projects, allowing us to continually break down the problems into smaller, more manageable chucks.

Initial program structure:

1. initialize variables and arrays
2. show the star map
3. get the users new coordinates to move to
4. if they don't move, add energy
5. if they do move, move the ship
6. place a k bomb
7. check for explosion
8. update arrays and variables and go to step 2

Well, that is not a giant list, but some of those steps are fairly complex and will need to be broken down more. Of course there are a few things missing. For instance, how does the game end? What happens if there is an explosion? Also, maybe some instructions would be nice! Lets see if we can't work these things into our model.

1. initialize variables and arrays

>> See if the user wants instructions, and show them if so

2. show the star map
3. get the users new coordinates to move to
4. if they don't move, add energy
5. if they do move, move the ship

>> If we move into stargate - show end game

6. place a k bomb
7. check for explosion

>> if all lives are used, show end game

8. update arrays and variables and go to step 2

Now we need to expand a few of these steps a little to make them more explanatory.

1. initialize variables and arrays
2. See if the user wants instructions, and show them if so
3. show the star map

3.a. show users current location

3.b. show lives remaining

3.c. show energy level

4. get the users new coordinates to move to

4.a. verify the users move

4.b. if they are moving goto 6

4.c. if they are not moving goto 5

5. they don't want to move, add energy

5.a. choose a random amount of energy

5.b. add the energy to the users total

5.c. if energy exceeds 485 gJ do cap energy at 485 gJ

5.d. goto 8

6. they do want move...

6.a. verify the user has selected a valid move

6.b. calculate energy requirements for move

6.c. verify user has enough energy to make the move

6.d. if the user can not make the move go back to 3

6.e. move the ship, deduct the energy.

6.f. if this is the stargate - you win - show winner

7. If we move into stargate - show end game

7.a. if our destination is occupied by a k bomb goto 9

8. place a k bomb randomly

8.a. choose random location x and y

8.b. if there is a k bomb there already choose again

8.c. if this is the stargate or the origin (1,1) choose again

8.d if the ship is at this location goto 9

8.e place the bomb - goto 11

9. explosion - you hit a mine, lose a life
10. if all lives are used, you lose! - show end game
11. update arrays and variables and go to step 3

There is more we could do to further explode the pseudo code out and detail the steps more, but if we add too much detail, then we are really writing the program. The purpose of the pseudo code is to give us a good, clear road map explaining where we are going. It does not need to solve all of our problems for us though.