1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <iostream>
#include <string>
#include "Map.h"
#include "Random.h"
using namespace std;
Map::Map()
{
//Player starts at origin
mPlayerXPos = 0;
mPlayerYPos = 0;
}
int Map::getPlayerXPos()
{
return mPlayerXPos;
}
int Map::getPlayerYPos()
{
return mPlayerYPos;
}
void Map::movePlayer()
{
int selection = 0;
cout << "1)North 2)East 3)South 4)West : ";
cin >> selection;
//Update coordinates based on selection
switch (selection)
{
case 1://North
++mPlayerYPos;
break;
case 2://East
++mPlayerXPos;
break;
case 3://South
--mPlayerYPos;
break;
default://West
--mPlayerXPos;
break;
}
cout << endl;
}
/*
*This function is the key function of the Map class. It generates a random number in the range [0, 20].
*Depending upon which sub-range in which the generated number falls, a different encounter takes place:
* Range [0, 5] – The player encounters no enemy.
* Range [6, 10] – The player encounters an Orc.
* Range [11, 15] – The player encounters a Goblin.
* Range [15, 19] – The player encounters an Ogre.
* Range [20] – The player encounters an Orc Lord.
*The bulk of this method code consists of testing in which range the random number falls and then
*creating the appropriate kind of monster.
*/
Monster* Map::checkRandomEncounter()
{
int roll = Random(0, 20);
Monster* monster = 0;
if (roll <= 5)
{
//No encounter return a null pointer
return 0;
}
else if (roll >= 6 && roll <= 10)
{
monster = new Monster("Orc", 10, 8, 200, 2, "War Axe", 2, 7);
cout << "You encountered an Orc!" << endl;
cout << "Prepare for battle!" << endl;
cout << endl;
}
else if (roll >= 11 && roll <= 15)
{
monster = new Monster("Goblin", 12, 8, 350, 3, "Twin Axes", 3, 7);
cout << "You encountered a Goblin!" << endl;
cout << "Prepare for battle!" << endl;
cout << endl;
}
else if (roll >= 16 && roll <= 19)
{
monster = new Monster("Ogre", 13, 10, 450, 4, "Battle Axe", 3, 8);
cout << "You encountered an Ogre!" << endl;
cout << "Prepare for battle!" << endl;
cout << endl;
}
else if (roll == 20)
{
monster = new Monster("Orc Warlord", 25, 15, 2000, 7, "Dark Axe of Brutel Dismemberment", 6, 10);
cout << "You encountered an Orc Warlord!" << endl;
cout << "You better run away!" << endl;
cout << endl;
}
return monster;
}
void Map::printPlayerPos()
{
cout << "Player position = (" << mPlayerXPos << ", " << mPlayerYPos << ")" << endl << endl;
}
|