1. Computing

Programming Games in C - Tutorial 1 Star Empires


3 of 5

About Systems and Random Numbers

Each of the neutral systems (1-8) starts with 15 ships (a number I picked out of the air!) to start with and the other two (yours: system 0 and your computer opponent at system 9) have 50 ships each. Each turn the number of ships at a system is increased by 10% rounded down. So after one turn if you don't move them, your 50 will become 55 and each of the neutral systems will have 16 (15 + 1.5 rounded down). Note that fleets moving to another system don't increase in numbers.

Increasing the number of ships this way may seem a little odd, but I've done it to keep the game moving along. Rather than clutter this tutorial with too much on design decisions, I wrote a separate article about the design decisions of Star Empires.

Implementing Systems

At the start we need to generate all the systems and put them on the map, with a maximum of one system in each location, As there are 25 locations on our 5 x 5 grid, we will have ten systems and 15 empty locations. We generate them using the function GenMapSystems() which we'll look at on the next page.

A system is stored in a struct, with the following 4 fields that are all int.

struct system {
    int x,y;
    int numfleets;
    int owner;

The galaxy (all 10 systems) is stored in another array just like with fleets except we have 10 systems.

struct system galaxy[10];

Random Numbers

All games need random numbers. C has a built in function rand() that returns a random int. We can force this into a range by passing the maximum number in and using the % operator. (Modulus). This is like clock arithemetic except instead of 12 or 24 we pass in an int number called max.

/* returns a number between 1 and max */
int Random(int max) {
 return (rand() % max)+1;

This is an example of a function which is a piece of code wrapped up inside a container. The first line here that starts /* and end */ is a comment. It says what the code does but is ignored by the compiler which reads the C instructions and converts them into instructions that the computer understands and can execute very fast.

A function is like a mathematical function such as Sin(x). There are three parts to this function:

int Random(int max)

The int says what type of number it returns (usually int or float). Random is the name of the function and (int max) says that we're passing in an int number. We might use it like this:

int dice;
dice = Random(6); /* returns a random number between 1 and 6 */

The line:

return (rand() % max)+1;
This calls that built in function rand() which returns a large number. % max does clock arithmetic reducing it to the range 0 to max-1. Then the +1 adds 1 making it return a value in the range 1 to max.

On the next page: Generating a Random Start Map

  1. About.com
  2. Computing
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. About Systems and Random Numbers

©2014 About.com. All rights reserved.