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
|
#include <iostream>
#include <string>
enum { NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3 } ;
const std::string dir[4] = { "north", "east", "south", "west" } ;
struct room_info
{
// location after moves to NORTH, EAST, SOUTH, WEST
int location_after_move[4] ; // -1 if move is not possible
std::string description ;
};
const int NUM_ROOMS = 25 ;
const room_info room_details[ NUM_ROOMS ] =
{
// room 1 - no doors towards N,W; E => 1, S => 5
{ { -1, 1, 5, -1 }, "whatever ..." }, // room 0
// room 1 - no doors towards N,S; E => 14, W => 2
{ { -1, 14, -1, 2 }, "You're in a stone room, torches lit around you." },
// ... room 2
// ... room 3
// ...
// room 14 - no doors towards E,W; N => 16, S => 13
{ { 16, -1, 13, -1 }, "There are torches on the wall\nand the body..." }
// etc..
};
int playerloc = 0; //will store a numerical reference to the player's location
bool running = true ; //Allows for easy termination of the program
int main()
{
// ....
while( running )
{
const room_info& info = room_details[playerloc] ;
std::cout << info.description << '\n' ;
std::cout << "possible moves are: " ;
for( int i = NORTH ; i <= WEST ; ++i )
if( info.location_after_move[i] != -1 ) std::cout << dir[i] << ' ' ;
std::cout << "\n0. go north 1. go east ... ? " ;
int user_input ;
std::cin >> user_input ;
if( user_input<NORTH || user_input>WEST || info.location_after_move[user_input] == -1 )
{
// error in input
// handle it
}
playerloc = info.location_after_move[user_input] ;
}
// ...
}
|