CS1101C Lab 3 (Odd Week)

The Warlock of Firetop Mountain #1

The deadline for this lab question is Friday 21 March 2008, 23:59:59 hours.

The name of your C program file must be called firetop1.c, files with any other name will not be marked.

Preliminary

The Warlock of Firetop Mountain was the first in a series of Role Playing Games. It uses a very simple combat system. We will simulate the (simplified) combat system.

Each battle is between two combatants: the human player and one creature. Each combatant has two attributes; the skill level, and the stamina. The skill level for each combatant is fixed at a value of between 1 to 12 inclusive; the higher the skill level, the better. The stamina is the life of the combatant, and always starts from between 1 to 24 inclusive.

During combat, combatants attack each other until one is dead, or both are dead. During each attack, the human player and the creature will roll 2d6 (two six-sided dice), and add to that their skill. The combatant with the higher total (skill + roll of two six-sided dice) will deduct two stamina points from the opponent. If the totals are the same, each combatant will have one stamina point deducted. When the stamina of a combatant reaches zero or less, the combatant is dead. If the staminas of both combatants reach zero simultaneously, both combatants are dead and the result is a draw.

Suppose the human has skill 1 and stamina 5, and that the creature has the same skill and stamina. Here is an example. (Note that this is not sample output.)

Human rolls 12, human's attack is 1 + 12 = 13
Creature rolls 5, creature's attack is 1 + 5 = 6
Human attacks successfully, creature's stamina is now 3.

Human rolls 5, human's attack is 1 + 5 = 6
Creature rolls 8, creature's attack is 1 + 8 = 9
Creature attacks successfully, human's stamina is now 3.

Human rolls 2, human's attack is 1 + 2 = 3
Creature rolls 8, creature's attack is 1 + 8 = 9
Creature attacks successfully, human's stamina is now 1.

Human rolls 7, human's attack is 1 + 7 = 8
Creature rolls 3, creature's attack is 1 + 3 = 4
Human attacks successfully, creature's stamina is now 1.

Human rolls 9, human's attack is 1 + 9 = 10
Creature rolls 6, creature's attack is 1 + 6 = 7
Human attacks successfully, creature's stamina is now -1.

Human wins!

Text File

You will be provided with an input text file called firetop1.txt consisting of one or more lines. Each line contains the parameters for each experiment. Each line consists of a random number seed, the number of simulations, the human's skill, the human's stamina, the creature's skill, and the creature's stamina. A typical line from the input file consist of six integers separated by blank spaces as shown below:
6094 100 1 5 1 5
The explanation for each number as well as their valid ranges are given as follows:

6094Random number seed (1-32767)
100Number of simulations (1-100000)
1Human's skill (1-12)
5Human's stamina (1-24)
1Creature's skill (1-12)
5Creature's stamina (1-24)

The random number seed should be initialized to the value shown (by using the srand function) at the beginning of each experiment. Remember to include stdlib.h in your preprocessor directives.

Each experiment consists of several simulations. Each simulation consists of a new battle to the death between the human player and the creature. There are three possible outcomes for each simulation; either the player wins, the creature wins, or it is a draw. At the end of each experiment, we wish to know the number of times the human wins, the number of times the creature wins, the number of times it is a draw, as well as the percentage outcomes for each case.

Here is the contents of a sample text file:

6094 100 1 5 1 5
6095 1000 1 5 1 5
6096 10000 1 5 1 5
6097 100000 1 5 1 5

7094 100 12 24 12 24
7095 1000 12 24 12 24
7096 10000 12 24 12 24
7097 100000 12 24 12 24

8094 100 12 12 11 12
8095 1000 12 12 11 12
8096 10000 12 12 11 12
8097 100000 12 12 11 12

9094 100 12 12 10 12
9095 1000 12 12 10 12
9096 10000 12 12 10 12
9097 100000 12 12 10 12
We will of course test your programs with different test data.

Sample Run

Assuming that the executable is firetop1, a sample run of the program is shown below. Follow the sample output precisely. User input is denoted in bold.

$ gcc -Wall firetop1.c -o firetop1

$ ./firetop1

Seed: 6094, human skill: 1, human stamina: 5
    creature skill: 1, creature stamina: 5
Number of simulations: 100, human victories: 45
    creature victories: 53, draws: 2
Percentage of human victories: 45.000%
Percentage of creature victories: 53.000%
Percentage of draws: 2.000%

Seed: 6095, human skill: 1, human stamina: 5
    creature skill: 1, creature stamina: 5
Number of simulations: 1000, human victories: 465
    creature victories: 508, draws: 27
Percentage of human victories: 46.500%
Percentage of creature victories: 50.800%
Percentage of draws: 2.700%

Seed: 6096, human skill: 1, human stamina: 5
    creature skill: 1, creature stamina: 5
Number of simulations: 10000, human victories: 4915
    creature victories: 4794, draws: 291
Percentage of human victories: 49.150%
Percentage of creature victories: 47.940%
Percentage of draws: 2.910%

Seed: 6097, human skill: 1, human stamina: 5
    creature skill: 1, creature stamina: 5
Number of simulations: 100000, human victories: 48818
    creature victories: 48217, draws: 2965
Percentage of human victories: 48.818%
Percentage of creature victories: 48.217%
Percentage of draws: 2.965%

Seed: 7094, human skill: 12, human stamina: 24
    creature skill: 12, creature stamina: 24
Number of simulations: 100, human victories: 55
    creature victories: 45, draws: 0
Percentage of human victories: 55.000%
Percentage of creature victories: 45.000%
Percentage of draws: 0.000%

Seed: 7095, human skill: 12, human stamina: 24
    creature skill: 12, creature stamina: 24
Number of simulations: 1000, human victories: 510
    creature victories: 483, draws: 7
Percentage of human victories: 51.000%
Percentage of creature victories: 48.300%
Percentage of draws: 0.700%

Seed: 7096, human skill: 12, human stamina: 24
    creature skill: 12, creature stamina: 24
Number of simulations: 10000, human victories: 4933
    creature victories: 4957, draws: 110
Percentage of human victories: 49.330%
Percentage of creature victories: 49.570%
Percentage of draws: 1.100%

Seed: 7097, human skill: 12, human stamina: 24
    creature skill: 12, creature stamina: 24
Number of simulations: 100000, human victories: 49595
    creature victories: 49426, draws: 979
Percentage of human victories: 49.595%
Percentage of creature victories: 49.426%
Percentage of draws: 0.979%

Seed: 8094, human skill: 12, human stamina: 12
    creature skill: 11, creature stamina: 12
Number of simulations: 100, human victories: 86
    creature victories: 14, draws: 0
Percentage of human victories: 86.000%
Percentage of creature victories: 14.000%
Percentage of draws: 0.000%

Seed: 8095, human skill: 12, human stamina: 12
    creature skill: 11, creature stamina: 12
Number of simulations: 1000, human victories: 788
    creature victories: 202, draws: 10
Percentage of human victories: 78.800%
Percentage of creature victories: 20.200%
Percentage of draws: 1.000%

Seed: 8096, human skill: 12, human stamina: 12
    creature skill: 11, creature stamina: 12
Number of simulations: 10000, human victories: 7861
    creature victories: 2050, draws: 89
Percentage of human victories: 78.610%
Percentage of creature victories: 20.500%
Percentage of draws: 0.890%

Seed: 8097, human skill: 12, human stamina: 12
    creature skill: 11, creature stamina: 12
Number of simulations: 100000, human victories: 78709
    creature victories: 20373, draws: 918
Percentage of human victories: 78.709%
Percentage of creature victories: 20.373%
Percentage of draws: 0.918%

Seed: 9094, human skill: 12, human stamina: 12
    creature skill: 10, creature stamina: 12
Number of simulations: 100, human victories: 97
    creature victories: 3, draws: 0
Percentage of human victories: 97.000%
Percentage of creature victories: 3.000%
Percentage of draws: 0.000%

Seed: 9095, human skill: 12, human stamina: 12
    creature skill: 10, creature stamina: 12
Number of simulations: 1000, human victories: 957
    creature victories: 43, draws: 0
Percentage of human victories: 95.700%
Percentage of creature victories: 4.300%
Percentage of draws: 0.000%

Seed: 9096, human skill: 12, human stamina: 12
    creature skill: 10, creature stamina: 12
Number of simulations: 10000, human victories: 9517
    creature victories: 439, draws: 44
Percentage of human victories: 95.170%
Percentage of creature victories: 4.390%
Percentage of draws: 0.440%

Seed: 9097, human skill: 12, human stamina: 12
    creature skill: 10, creature stamina: 12
Number of simulations: 100000, human victories: 94816
    creature victories: 4838, draws: 346
Percentage of human victories: 94.816%
Percentage of creature victories: 4.838%
Percentage of draws: 0.346%

$

Note and Ponder

  1. Assume that all user input is valid.

  2. To print the % sign, use %% in your printf format specifier.

  3. To edit the text file, you may use the vim editor by typing "vim firetop1.txt".

  4. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  5. How do you know if your program is working perfectly? Did you test your programs with your own test data? What constitutes as good test data?

  6. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 9 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 5 different hosts have accessed this document in the last 389 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 4 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.