Sei sulla pagina 1di 6

In this assignment you will be implementing a simple Role Playing Game.

The user will be controlling an


explorer who must navigate around a map to find the treasure.

Part 1
We will begin by implementing a function to read in a map from a file. The map is a 10 by 10 array of
integers that is to be stored in a file: map.txt
A sample file has been provided.

The first line of the file corresponds to the starting position of the player and consists of two numbers. In
the example file it is 0 0 - this means that the player is at the top left corner.

Then the file has a 10 by 10 matrix of integers. There are only 4 different integers used in the initial map:
 0 - this represents grass
 -1 - this represents a large rock
 9 - this represents a special item (explained shortly)
 999 – the treasure!

The function must accept a 2D array as a parameter, then fill this 2D array using what is read in from the
file.

If you read a 0, -1 or 999 then store it as a 0, -1 or 999 respectively in the array. However if a 9 is read in
then we will generate a random number between 1 and 7 to store in the array instead. So for example if
the following row is read in from the file:

0 0 -1 0 9 -1 0 9 0 0

Then it may be stored in the array like so:

0 0 -1 0 4 -1 0 2 0 0

The numbers will be generated differently each time - meaning the game will run differently each time it
is played. I will explain what the numbers mean in a later part.

Create a main file to test your function. It would be a good idea to output your map to the screen at this
stage to make sure it is working correctly.

Part 2
Let's add a player to this game! A player has a position (x and y value) and HP (health points). When we
start a new game then the player will start with 500 HP.

A player can choose from one of 5 things to input:


 'n' - player moves one space north
 's' - player moves one space south
 'e' - player moves one space east
 'w' - player moves one space west
 'q' - player brings up the quit menu

Moving one space costs the player 5 HP. A player can move to any space on the map that isn’t a large
rock (-1). A player also cannot move outside the bounds of the map – ie the position (-1, 0) is invalid.

If a player reaches a spot with 999 then they have successfully completed the level. Inform them of their
success with a message of your choice. Then the game should quit (without saving).

CSCI114/MCS9114 Assignment 4 SCSSE – Autumn 2012 1/6


If a player enters ‘q’ then the following menu should appear:

Do you want to:


1) Save your current progress?
2) Quit the game?
3) Cancel and return to the game?

For now, if a player selects 1 then output: “This has not yet been implemented”.
If a player selects 2 then the game should terminate immediately.
If a player selects 3 then the game should resume immediately.

A sample run of the program so far is as follows (assume the player starts at 0 0; user input in bold):

Which way do you want to go?


e
You have 495 HP left.

Which way do you want to go?


n
You cannot go north right now.

Which way do you want to go?


q

Do you want to:


1) Save your current progress?
2) Quit the game?
3) Cancel and return to the game?
2

Part 3
Now we will give the player a weapon. The player can carry up to 10 weapons at any one time – but may
only use one at a time. The weapon information will be stored in a file called weapon.txt like so:

Excalibur
150
Throwing Stars
15
Rapier
200
Bow and Arrow
100
Axe
200
Crossbow
100
Scimitar
250
Rusted Sword
10
Soul Slayer
500

Implement a function called LoadWeapon that reads in a weapon file. A weapon has two parts: a name

CSCI114/MCS9114 Assignment 4 SCSSE – Autumn 2012 2/6


and strength. For example there are 9 weapons in this file, the first being Excalibur which has 150
strength.

The player has the first non-destroyed weapon (explained shortly) equipped by default. When a weapon is
destroyed then the strength for that weapon will be set to -1, and the next weapon in the list will be
equipped.

Now to explain what the rest of the numbers on the map mean.

1 – the player will find 50 bonus strength to add to the current weapon. So if your weapon currently has
150 strength then it will have 200 strength if you land on ‘1’.
2-4 – monster (explained shortly).
5-6 – potion. 5 = gain 100 HP. 6 = lose 100 HP.
7 – trap. Player dies. Output an appropriate message and close the program.

When a player reaches a monster, the monster will have some random HP allocated to it (between 15 and
500). The player has no choice but to fight this monster. If their current weapon is equal to or stronger
than the monster’s HP then the monster dies, but the weapon strength is reduced (by the amount of
monster HP).
If the monster’s HP is more than the strength of the weapon then the weapon is destroyed and the
monster’s HP is reduced (by the amount of the weapon’s strength). The weapon then has its strength set
to -1 to signify that it has been destroyed. The player loses 20 HP (to indicate a loss in battle) and must
use the next weapon in the list and continue to fight until either the monster has been defeated, the player
runs out of weapons or the player runs out of HP.

If the player runs out of HP or weapons then they lose and the game is over.

When a monster is destroyed then that position on the map becomes grass (0). The same thing happens
when picking up weapon power-ups and potions.

A sample interaction is as follows. Note that you may not be able to reproduce this sample interaction due
to the randomisation used in the game.

Which way do you want to go?


e
You have 495 HP left.
You encounter a monster with 200 HP.
You fight the monster with Excalibur.
Excalibur’s strength is 150.
Excalibur is destroyed.
The monster loses 150 HP
The monster has 50 HP left.
You lose 20 HP.
You have 475 HP left.
You are now equipped with Throwing Stars.
Throwing Star’s strength is 15.
You fight the monster with Throwing Stars.
Throwing Stars is destroyed.
The monster loses 15 HP.
The monster has 35 HP left.
You lose 20 HP.
You have 455 HP left.
You are now equipped with Rapier.
Rapier’s strength is 200.
You fight the monster with Rapier.

CSCI114/MCS9114 Assignment 4 SCSSE – Autumn 2012 3/6


Rapier’s strength decreases by 35.
Rapier’s strength is 165.
The monster dies.

Which way do you want to go?


s
You find a potion and drink it.
You lose 100 HP.
You have 355 HP left.

Which way do you want to go?


s
Bonus! You add 50 strength to Rapier.
Rapier’s strength is 215.

Which way do you want to go?

Part 4
We will now implement the save function. Now, when a player chooses option 1 in the quit menu, the
following will happen:
1) The file currentWeapon.txt is created. Every weapon will be written in there as well as its
current strength (meaning that we will save destroyed weapons as having -1 strength).
2) The file currentMap.txt will be created. The first line of this file will contain the current position
of the player together with the player’s HP. The rest of the file will contain the current map. An
example of currentMap.txt is as follows:

5 1 150
0 2 0 4 0 0 0 0 0 0
0 -1 0 0 0 0 0 3 0 0
2 -1 0 0 -1 3 -1 0 0 0
0 1 0 -1 0 0 0 -1 0 0
0 0 1 0 0 3 0 -1 0 0
0 3 -1 2 0 0 2 2 0 -1
1 0 0 0 -1 0 0 0 0 -1
0 -1 0 0 4 0 0 4 0 0
2 0 0 0 1 0 5 6 0 0
-1 0 2 0 2 0 7 0 0 999

In this example the current position of the player is (5, 1) and their HP is 150.

Now when the program begins it will first look to see if there is a current save file. If currentMap.txt and
currentWeapon.txt exist then the following menu will appear:

Do you want to:


1) Restart the game?
2) Continue the game?

If the player selects option 1 then we read in map.txt as normal and begin the game again.
If the player selects option 2 then we read in the current position of the player, their HP, and then the
map. We also read in the current weapons. When this option is selected we do not open map.txt or
weapon.txt.

Part 5
Now we will practice multi-file compilation. You should have three files: main.cpp, functions.cpp and
functions.h
CSCI114/MCS9114 Assignment 4 SCSSE – Autumn 2012 4/6
main.cpp will hold your main function.
function.cpp will hold the definitions for all your other functions.
function.h will hold the prototypes for these definitions

Bonus Part
Here is a chance to make the game a bit more interesting.
Firstly – let’s add some colour to the game. You can change the colour of the terminal text in the
following way:

cout << “\033[22;31m” << “Hello!” << endl;

Which will print: Hello!


The list of codes is as follows:

\033[22;30m - black
\033[22;31m - red
\033[22;32m - green
\033[22;33m - brown
\033[22;34m - blue
\033[22;35m - magenta
\033[22;36m - cyan
\033[22;37m - gray
\033[01;30m - dark gray
\033[01;31m - light red
\033[01;32m - light green
\033[01;33m - yellow
\033[01;34m - light blue
\033[01;35m - light magenta
\033[01;36m - light cyan
\033[01;37m – white
\033[0m – default terminal text colour

Display a loss in HP in red, a gain in HP in green and general HP output in blue. Display keywords
(weapon names and the word monster) in light magenta. Here is an example (this will look better on a
terminal with black background and white text):

You have 495 HP left.


You encounter a monster with 200 HP.
You fight the monster with Excalibur.
Excalibur’s strength is 150.
Excalibur is destroyed.
The monster loses 150 HP
The monster has 50 HP left.
You lose 20 HP.
You have 475 HP left.

Next, change the game so that there are now two players. Each turn consists of the player entering either
n, s, e, w or q at the “Which way do you want to go?” menu and ends just before that same string is output
once more. Change map.txt so that the first line looks something like this: 0 0 0 9. The first pair is the
initial position of the first player, the second pair is the initial position of the second player.

To make the game more interesting, after reading in weapon.txt, randomly sort it for both players (so the
weapons are in a different order for player 1 than they are for player 2).
CSCI114/MCS9114 Assignment 4 SCSSE – Autumn 2012 5/6
Now when you create currentMap.txt you will need to store both players’ information in the first line. For
example:

2 7 430 6 4 140

Meaning player 1 has the location (2, 7) and HP 430 and player 2 has the location (6, 4) and HP 140.
You will also need to save 2 weapon files – one for each player. Name them p1currentWeapon.txt and
p2currentWeapon.txt. Make sure to change the loading functions appropriately.

If players appear on the same space then they must fight each other to the death! This will occur in the
same way that monster fights do.

Make sure to change the word “You” in the output text to the appropriate player. Give each player a
different colour.

CSCI114/MCS9114 Assignment 4 SCSSE – Autumn 2012 6/6

Potrebbero piacerti anche