#include <iostream> #include <string> #include <cstdlib> using namespace std; in
ID: 3567379 • Letter: #
Question
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
static const int rooms=5;//constant integer to represent the number of rooms
static const int exits=4;//constant integer to represent the number of exits that each room may have
int N, n = 0; //set of constant integer variables used for indexing into the possible exits
int E, e = 1;
int W, w = 2;
int S, s = 3;
char q, Q;
int current_room = 0;
bool done = false;
//initializer list for string array
const char * const room_description[ 5 ] =
{ "You are in the armory. There is a room off to the south. ",
"You are in a dusty castle room. Passages lead to the north and south. ",
"You are in the kitchen. It looks like a roast is being made for supper. A hallway is to the east. ",
"You are in a torch-lit hallway. There are rooms to the east and west. ",
"You are in a bedroom. A window overlooks the castle courtyard. A hallway is to the west. " };
//2-dimensional integer array to hold each room
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
static const int r=5;//constant integer to represent the number of rooms
static const int e=4;//constant integer to represent the number of exits that each room may have
const int NORTH = 0; //set of constant integer variables used for indexing into the possible exits
const int EAST = 1;
const int WEST = 2;
const int SOUTH = 3;
int current_room = 0;
bool done = false;
//initializer list for string array
const char * const room_description[ 5 ] =
{ "You are in the armory. There is a room off to the south. ",
"You are in a dusty castle room. Passages lead to the north and south. ",
"You are in the kitchen. It looks like a roast is being made for supper. A hallway is to the east. ",
"You are in a torch-lit hallway. There are rooms to the east and west. ",
"You are in a bedroom. A window overlooks the castle courtyard. A hallway is to the west. " };
//2-dimensional integer array to hold each room.s exits. 5 rooms and 4 exits
int a[5][4] = {
{-1, -1, -1, 1} , /* room 0 */
{0, -1, -1, 3} , /* room 1 */
{-1, 3, -1, -1}, /* room 2 */
{1, 4, 2, -1} , /* room 3 */
{-1, -1, 3, -1} /* room 4 */
}; //?????????????????
do {
cout << room_description[current_room] << endl; //hopefully works...using the value of current room variable as an index into the room_description array
char user_input;
cout<<"Which direction?: ";
cin>>user_input;
if( user_input == 'n' || user_input == 'N' )
{
// if condition is true then print the following
int next_room = a[current_room][1];
if(next_room == -1)
{
cout << "Can't go that way! " << endl;
}
else
{
current_room = next_room;
}
}
else if( user_input == 'e' || user_input=='E' )
{
// if condition is true then print the following
int next_room = a[current_room][2];
if(next_room == -1)
{
cout << "Can't go that way! " << endl;
}
else
{
current_room = next_room;
}
}
else if( user_input == 'w' || user_input == 'W' )
{
// if condition is true then print the following
int next_room = a[current_room][3];
if(next_room == -1)
{
cout << "Can't go that way! " << endl;
}
else
{
current_room = next_room;
}
}
else if( user_input == 's' || user_input =='S' )
{
// if condition is true then print the following
int next_room = a[current_room][4];
if(next_room == -1)
{
cout << "Can't go that way! " << endl;
}
else
{
current_room = next_room;
}
if( user_input == 'q'|| user_input == 'Q' )
{
// if else if condition is true
cout << ".Thanks for playing. Good bye. ." << endl;
bool done = true;
}
else
{
// if none of the conditions is true
cout << ".I don.t understand that command. " << endl;
}
}
cout<<"Do you want to try again? (0 zero to exit 1 continue) ";
cin>>done;
}
while ((done = true));
return 0; //indicates success
//end main
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.