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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
//----------------------------------------------------------------------
struct Room
{
// use std::string for string data
string description;
string name;
// the following list rooms adjacent to this room
// if -1, then there is not any door in the indicated direction
int north;
int south;
int east;
int west;
// Default constructor
Room(): description(""), name(""), north(-1), south(-1), east(-1), west(-1) {};
// A convenience constructor
Room( string desc, string name, int n, int s, int e, int w ):
description(desc), name(name), north(n), south(s), east(e), west(w) {};
// Display an individual room
friend ostream& operator << (ostream&, Room&);
};
//----------------------------------------------------------------------
class Rooms: public vector<Room>
{
public:
// The index of the room that the player is in
int current;
// The constructor. It will load the rooms from file, or whatever
Rooms();
};
//----------------------------------------------------------------------
ostream& operator << ( ostream& outs, Room& room )
{
// Display an individual room for the user, and his travel options.
outs << room.description << '\n'
<< "Suggested possible routes we can take are:\n";
if (room.north != -1) outs << "--(N)orth\n";
if (room.east != -1) outs << "--(E)ast\n";
if (room.south != -1) outs << "--(S)outh\n";
if (room.west != -1) outs << "--(W)est\n";
return outs;
}
//----------------------------------------------------------------------
Rooms rooms; // uses our fancy-pants constructor
int hp=100; // this belongs to the avatar class
string player; // this belongs to the avatar class also...
// (since the avatar represents the player)
//----------------------------------------------------------------------
int main ()
{
cout << "\n\n\n"
<< "======================================================\n"
<< "EarthWars Copyright Mark Larah --- marklarah@gmail.com\n"
<< "======================================================\n\n";
cout << "Hello General. It is unfortunatley too late for the rest of them. There was nothing we could do. The people of Kai Feng have indeed been slaughtered heartlesly. We could not hold them off for long enough for our army to arrive."
<< "Oh well. Anyway, you have your army at your side, and the few people remaining in the village demand you avenge the death of your village. Go forth General, seek out the Pink Hand Rebel Forces, and eleimintae them. \n\nGood luck general!\n\n";
cout << "Sorry General, Im your Battle Coordinater, Toby. Im new here - I didn't catch your name? ";
// use getline when getting string data. See warnings below.
getline( cin, player );
cout << "\n Uh... of course, " << player << ". Anyway, hi. At anytime during the game (apart from battle), you can bring up a status update report by pressing 'U'. Now Lets go!";
cout << "\n"
<< "-----------\n"
<< "EarthWars\n"
<< "-----------\n\n";
// when HP reaches zero (or less) the player is dead
while (hp > 0)
{
// Tell the user where we are
cout << '\n' << rooms[ rooms.current ];
// Ask the user where to go
int d=0;
cout << "Enter your move sir: ";
while (d == 0) {
// Get the user's response
char move;
cin >> move;
cin.ignore( numeric_limits<streamsize>::max(), '\n' ); // (the user pressed the ENTER: get rid if it)
move = toupper(move);
int curroom; // where the user would like to go
switch (move)
{
case 'E': curroom = rooms[rooms.current].east; break;
case 'W': curroom = rooms[rooms.current].west; break;
case 'N': curroom = rooms[rooms.current].north; break;
case 'S': curroom = rooms[rooms.current].south; break;
case 'Q': return 0;
default: curroom = -1;
}
// can we go where the user would like to go?
if (curroom == -1)
{
// The response was an invalid direction.
cout << "\nSorry sir, I don't understand, do you want to go N, E, S or W? " << endl;
}
else
{
// The response was valid.
rooms.current = curroom;
d=666;
}
}
}
return 0;
}
//----------------------------------------------------------------------
Rooms::Rooms(): current(0)
{
// since we used strings, we can just assign using a string constant
// how convenient
// make sure to leave stuff like newlines and other formatting out
// of the information. it is up to the thing printing the information
// to decide how to format it.
// of course, normally you'd read all this from file...
// but for now a three-room maze will do
push_back( Room(
"Right now, were still in the village.",
"Village",
-1, 1, 2, -1
) );
push_back( Room(
"You are on the road south of the village that leads to the forest.",
"Forest road",
0, -1, -1, -1
) );
push_back( Room(
"You are in a grove east of the village.\n"
"There are spooky lights in all around you.",
"Magic Cove",
-1, -1, -1, 0
) );
}
|