Question: The code posted below which is using C++ language won't compile when I use the Cloud 9 ide, i get an error saying fatal error:
The code posted below which is using C++ language won't compile when I use the Cloud 9 ide, i get an error saying
fatal error: iostream: No such file or directory #include
Process exited with code: 1
Anyway to fix this? I have to use the Cloud 9 ide, no way aroud it.
#include
#include
#include
typedef unsigned char byte;
enum gameResult { FINISH, PLAY_NEW, PLAY_SAME };
enum object : byte { NOTHING, WUMPUS = 1, BAT = 2, PIT = 4, PLAYER = 8 };
const unsigned S_PLAYER = 0, S_WUMPUS = 1, S_BAT1 = 2, S_BAT2 = 3, S_PIT1 = 4, S_PIT2 = 5,
MAX_ROOMS = 20, SAVED = 6, MAX_EXITS = 3, A_PATH_LEN = 5, MAX_ARROWS = 5;
class inOut
{
public:
int getLetter( std::string s, int a, int b )
{
int c;
do
{
msg( s );
std::string r; std::cin >> r;
std::cin.clear(); std::cin.ignore();
c = toupper( r[0] );
}
while( c != a && c != b );
return c;
}
int getNumber( std::string s )
{
int n = 0; std::string c;
while( true )
{
msg( s );
std::getline( std::cin, c );
std::stringstream strm( c );
if( strm >> n ) break;
}
return n;
}
void msg( std::string s ) { std::cout << s; }
void msg( int i ) { std::cout << i; }
void wait() { std::cin.get(); }
};
class room
{
public:
int getExit( int i ) { return exits[i]; }
byte contains() { return obj; }
void clear( object o ) { obj ^= o; }
void clearRoom() { obj = NOTHING; }
void setExit( int i, int e ) { exits[i] = e; }
void populate( object o ) { obj |= o; }
private:
int exits[MAX_EXITS];
byte obj;
};
class cave
{
public:
cave()
{
int con[] = { 1, 4, 7, 0, 2, 9, 1, 3, 11, 2, 4, 13, 0, 3, 5, 4, 6, 14, 5, 7, 16, 0, 6, 8, 7, 9, 17, 1, 8, 10, 9, 11, 18,
2, 10, 12, 11, 13, 19, 3, 12, 14, 5, 13, 15, 14, 16, 19, 6, 15, 17, 8, 16, 18, 10, 17, 19, 12, 15, 18 };
for( int x = 0, r = 0; x < MAX_ROOMS; x++, r = x * MAX_EXITS )
{
for( unsigned c = r, d = 0; c < r + MAX_EXITS; c++, d++ )
rooms[x].setExit( d, con[c] );
}
clear();
}
void clear()
{
for( int x = 0; x < MAX_ROOMS; x++ )
rooms[x].clearRoom();
}
room* getRoom( int i ) { return &rooms[i]; }
private:
room rooms[MAX_ROOMS];
};
class wumpus
{
private:
inOut inOut;
cave theCave;
unsigned playerPos, wumpusPos, pathLen, arrowsCnt, exits[MAX_EXITS], arrowPath[A_PATH_LEN], saved[SAVED];
bool gameOver, playerWins;
void look()
{
room* r = theCave.getRoom( playerPos );
inOut.msg( " ----------------------------------- " );
inOut.msg( "You are in room #" ); inOut.msg( playerPos + 1 );
inOut.msg( " Tunnels lead to rooms #: " );
for( int x = 0; x < MAX_EXITS; x++ )
{
inOut.msg( ( 1 + r->getExit( x ) ) );
inOut.msg( " " );
}
lookAround( r );
}
void shoot()
{
room* r = theCave.getRoom( playerPos );
unsigned e;
for( unsigned x = 0; x < pathLen; x++ )
{
for( e = 0; e < MAX_EXITS; e++ )
if( r->getExit( e ) == arrowPath[x] ) break;
if( e < MAX_EXITS ) r = theCave.getRoom( arrowPath[x] );
else r = theCave.getRoom( r->getExit( rand() % MAX_EXITS ) );
byte o = r->contains();
if( WUMPUS & o ) { gameOver = playerWins = true; return; }
if( PLAYER & o )
{
gameOver = true; playerWins = false;
inOut.msg( " Arrow got you! " ); return;
}
}
inOut.msg( " Missed! " );
if( --arrowsCnt == 0 )
{
inOut.msg( " You are out of arrows! " );
gameOver = true; playerWins = false;
return;
}
wumpusMove( playerPos );
}
gameResult showResult( bool pw )
{
if( pw ) inOut.msg( " At last! You got the Wumpus! HEE HEE HEE - The Wumpus'll getcha next time!! " );
else inOut.msg( " Sorry you lose! " );
if( inOut.getLetter( "Play again (Y/N)? ", 'Y', 'N' ) == 'Y' )
{
if( inOut.getLetter( "Same setup (Y/N)? ", 'Y', 'N' ) == 'Y' ) return PLAY_SAME;
return PLAY_NEW;
}
return FINISH;
}
void lookAround( room* r )
{
byte msg = 0, o;
for( int x = 0; x < MAX_EXITS; x++ )
{
o = theCave.getRoom( r->getExit( x ) )->contains();
msg += ( ( WUMPUS & o ) + ( BAT & o ) + ( PIT & o ) );
}
if( msg & WUMPUS ) inOut.msg( " Something smells terrible nearby......" );
if( msg & PIT ) inOut.msg( " There is a cold wind blowing from a nearby cavern." );
if( msg & BAT ) inOut.msg( " You hear a rustling close to you...." );
}
bool checkExits( int e )
{
for( int x = 0; x < MAX_EXITS; x++ )
if( e == exits[x] ) return true;
return false;
}
void getInput()
{
if( inOut.getLetter( " Shoot or Move (S/M)? ", 'S', 'M' ) == 'M' )
{
int e = inOut.getNumber( "Where to? " ) - 1;
if( checkExits( e ) ) setPlayer( e );
else inOut.msg( " You cannot go there! " );
}
else
{
do
pathLen = inOut.getNumber( " Number of rooms (1-5)? " );
while( pathLen < 1 || pathLen > A_PATH_LEN );
for( unsigned i = 0; i < pathLen; i++ )
{
arrowPath[i] = inOut.getNumber( "Room #" ) - 1;
if( i <= 1 || arrowPath[i] != arrowPath[i - 2]) continue;
inOut.msg( " Arrows can't bend like that! - Please, try another room. " );
i--;
}
shoot();
}
}
void setPlayer( int pos )
{
if( playerPos < MAX_ROOMS )
theCave.getRoom( playerPos )->clear( PLAYER );
if( hazards( pos ) ) return;
playerPos = pos;
room* r = theCave.getRoom( playerPos );
r->populate( PLAYER );
for( int x = 0; x < MAX_EXITS; x++ )
exits[x] = r->getExit( x );
}
bool hazards( int pos )
{
room* r = theCave.getRoom( pos );
byte o = r->contains();
if( WUMPUS & o )
{
inOut.msg( " You bumped the Wumpus! " );
if( wumpusMove( pos ) )
{
inOut.msg( " Wumpus got you! Try again! " );
gameOver = true; playerWins = false;
return true;
}
}
if( PIT & o )
{
inOut.msg( " You fell into a pit! ");
gameOver = true; playerWins = false;
return true;
}
if( BAT & o )
{
inOut.msg( " The bat snatched you right up! " );
setPlayer( rand() % MAX_ROOMS );
return true;
}
return false;
}
bool wumpusMove( int pos )
{
if( rand() % 100 < 75 )
{
room* r = theCave.getRoom( wumpusPos );
r->clear( WUMPUS );
wumpusPos = r->getExit( rand() % MAX_EXITS );
theCave.getRoom( wumpusPos )->populate( WUMPUS );
}
return ( pos == wumpusPos );
}
void initGame( gameResult gr )
{
inOut.msg( " HUNT THE WUMPUS --------------- " );
theCave.clear(); gameOver = false; arrowsCnt = MAX_ARROWS;
if( gr == PLAY_NEW )
{
saved[S_PLAYER] = rand() % MAX_ROOMS;
setPlayer( saved[S_PLAYER] );
saved[S_BAT1] = fillRoom( BAT ); saved[S_BAT2] = fillRoom( BAT );
saved[S_PIT1] = fillRoom( PIT ); saved[S_PIT2] = fillRoom( PIT );
wumpusPos = saved[S_WUMPUS] = fillRoom( WUMPUS );
}
else
{
setPlayer( saved[S_PLAYER] ); wumpusPos = saved[S_WUMPUS];
theCave.getRoom( wumpusPos )->populate( WUMPUS );
theCave.getRoom( saved[S_BAT1] )->populate( BAT );
theCave.getRoom( saved[S_BAT2] )->populate( BAT );
theCave.getRoom( saved[S_PIT1] )->populate( PIT );
theCave.getRoom( saved[S_PIT2] )->populate( PIT );
}
}
int fillRoom( object c )
{
int i; room* r;
do
{
i = rand() % MAX_ROOMS;
r = theCave.getRoom( i );
}
while( r->contains() );
r->populate( c );
return i;
}
void printInstructions()
{
if( inOut.getLetter( "Hunt The Wumpus Instructions (Y/N)? ", 'Y', 'N' ) == 'N' ) return;
inOut.msg( " Welcome to 'Hunt The Wumpus' The Wumpus lives in a cave of 20 rooms: each room has 3 tunnels leading to "
"other rooms. "
" HAZARDS: -------- Bottomless pits: ---------------- Two rooms have bottomless pits in them. "
"If you go there, you fall into the pit and lose! Super bats: ----------- Two other rooms have super bats. If you go there, "
"a bat grabs you and takes you to some other room at random, which might be troublesome. Wumpus: ------- The Wumpus is not "
"bothered by the hazards, he has sucker feet and is too big for a bat to lift. Usually he is asleep. Two things wake him up: "
"your entering his room or your shooting an arrow. If the Wumpus wakes, he has 75% chance to move one room or 25% chance to stay "
"still. After that, if he is where you are, he eats you up and you lose! You: ---- Each turn you may move or shoot a crooked arrow. "
"- Moving: you can move one room (through one tunnel) - Arrows: you have 5 arrows. You lose when you run out. Each arrow can go from "
"1 to 5 rooms, you aim by telling the computer the rooms #s you want the arrow to go to. If the arrow can't go that way (if no tunnel) "
"it moves at random to the next room. If the arrow hits the Wumpus: you win, if the arrow hits you: you lose. "
" WARNINGS: -------- When you are one room away from Wumpus or any other hazard, the computer says: Wumpus: 'Something smells "
"terrible nearby......' Bat: 'You hear a rustling close to you....' Pit: 'There is a cold wind blowing from a nearby cavern.' Press return to play..." );
inOut.wait();
}
public:
void play()
{#include
#include
#include
#include
#include
#include
#include
static int arrows;
static int debug = 0;
#define YOU 0
#define WUMPUS 1
#define PIT1 2
#define PIT2 3
#define BATS1 4
#define BATS2 5
#define LOCS 6
static int loc[LOCS];
#define NOT 0
#define WIN 1
#define LOSE -1
static int finished;
static int cave[20][3] =
{
{1,4,7},
{0,2,9},
{1,3,11},
{2,4,13},
{0,3,5},
{4,6,14},
{5,7,16},
{0,6,8},
{7,9,17},
{1,8,10},
{9,11,18},
{2,10,12},
{11,13,19},
{3,12,14},
{5,13,15},
{14,16,19},
{6,15,17},
{8,16,18},
{10,17,19},
{12,15,18},
};
int getnum(char* prompt) {
int n;
printf("%s: ", prompt);
scanf("%d", &n);
return n;
}
int getlet(char* prompt) {
char c = ' ';
printf("%s: ", prompt);
while (c == ' ') {
scanf("%c", &c);
}
return toupper(c);
}
void print_instructions() {
printf(
"WELCOME TO 'HUNT THE WUMPUS' "
"THE WUMPUS LIVES IN A CAVE OF 20 ROOMS. EACH ROOM "
"HAS 3 TUNNELS LEADING TO OTHER ROOMS. (LOOK AT A "
"DODECAHEDRON TO SEE HOW THIS WORKS-IF YOU DON'T KNOW "
"WHAT A DODECAHEDRON IS, ASK SOMEONE) "
" "
" HAZARDS: "
" BOTTOMLESS PITS: TWO ROOMS HAVE BOTTOMLESS PITS IN THEM "
" IF YOU GO THERE, YOU FALL INTO THE PIT (& LOSE!) "
" SUPER BATS : TWO OTHER ROOMS HAVE SUPER BATS. IF YOU "
" GO THERE, A BAT GRABS YOU AND TAKES YOU TO SOME OTHER "
" ROOM AT RANDOM. (WHICH MAY BE TROUBLESOME) "
" WUMPUS: "
" THE WUMPUS IS NOT BOTHERED BY HAZARDS (HE HAS SUCKER "
" FEET AND IS TOO BIG FOR A BAT TO LIFT). USUALLY "
" HE IS ASLEEP. TWO THINGS WAKE HIM UP: YOU SHOOTING AN "
" ARROW OR YOU ENTERING HIS ROOM. "
" IF THE WUMPUS WAKES HE MOVES (P=.75) ONE ROOM "
" OR STAYS STILL (P=.25). AFTER THAT, IF HE IS WHERE YOU "
" ARE, HE EATS YOU UP AND YOU LOSE! "
" "
" YOU: "
" EACH TURN YOU MAY MOVE OR SHOOT A CROOKED ARROW "
" MOVING: YOU CAN MOVE ONE ROOM (THRU ONE TUNNEL) "
" ARROWS: YOU HAVE 5 ARROWS. YOU LOSE WHEN YOU RUN OUT "
" EACH ARROW CAN GO FROM 1 TO 5 ROOMS. YOU AIM BY TELLING "
" THE COMPUTER THE ROOM#S YOU WANT THE ARROW TO GO TO. "
" IF THE ARROW CAN'T GO THAT WAY (IF NO TUNNEL) IT MOVES "
" AT RANDOM TO THE NEXT ROOM. "
" IF THE ARROW HITS THE WUMPUS, YOU WIN. "
" IF THE ARROW HITS YOU, YOU LOSE. "
" WARNINGS: "
" WHEN YOU ARE ONE ROOM AWAY FROM A WUMPUS OR HAZARD, "
" THE COMPUTER SAYS: "
" WUMPUS: 'I SMELL A WUMPUS' "
" BAT : 'BATS NEARBY' "
" PIT : 'I FEEL A DRAFT' "
" "
);
}
void show_room() {
printf(" ");
for (int k = 0; k < 3; k++) {
int room = cave[loc[YOU]][k];
if (room == loc[WUMPUS]) {
printf("I SMELL A WUMPUS! ");
} else if (room == loc[PIT1] || room == loc[PIT2]) {
printf("I FEEL A DRAFT ");
} else if (room == loc[BATS1] || room == loc[BATS2]) {
printf("BATS NEARBY! ");
}
}
printf("YOU ARE IN ROOM %d ", loc[YOU]+1);
printf("TUNNELS LEAD TO %d %d %d ",
cave[loc[YOU]][0]+1,
cave[loc[YOU]][1]+1,
cave[loc[YOU]][2]+1);
}
int move_or_shoot() {
int c = -1;
while ((c != 'S') && (c != 'M')) {
c = getlet("SHOOT OR MOVE (S-M)");
}
return (c == 'S') ? 1 : 0;
}
void move_wumpus() {
int k = rand() % 4;
if (k < 3) {
loc[WUMPUS] = cave[loc[WUMPUS]][k];
}
if (loc[WUMPUS] == loc[YOU]) {
printf("TSK TSK TSK - WUMPUS GOT YOU! ");
finished = LOSE;
}
}
void shoot() {
int path[5];
int scratchloc = -1;
finished = NOT;
int len = -1;
while (len < 1 || len > 5) {
len = getnum("NO. OF ROOMS (1-5)");
}
int k = 0;
while (k < len) {
path[k] = getnum("ROOM #") - 1;
if ((k>1) && (path[k] == path[k-2])) {
printf("ARROWS AREN'T THAT CROOKED - TRY ANOTHER ROOM ");
continue;
}
k++;
}
scratchloc = loc[YOU];
for (int k = 0; k < len; k++) {
if ((cave[scratchloc][0] == path[k]) ||
(cave[scratchloc][1] == path[k]) ||
(cave[scratchloc][2] == path[k])) {
scratchloc = path[k];
} else {
scratchloc = cave[scratchloc][rand()%3];
}
if (scratchloc == loc[WUMPUS]) {
printf("AHA! YOU GOT THE WUMPUS! ");
finished = WIN;
} else if (scratchloc == loc[YOU]) {
printf("OUCH! ARROW GOT YOU! ");
finished = LOSE;
}
if (finished != NOT) {
return;
}
}
printf("MISSED ");
move_wumpus();
if (--arrows <= 0) {
finished = LOSE;
}
}
void move() {
int scratchloc = -1;
while (scratchloc == -1) {
scratchloc = getnum("WHERE TO")-1;
if (scratchloc < 0 || scratchloc > 19) {
scratchloc = -1;
continue;
}
if ((cave[loc[YOU]][0] != scratchloc) &
(cave[loc[YOU]][1] != scratchloc) &
(cave[loc[YOU]][2] != scratchloc) &
(loc[YOU] != scratchloc)) {
printf("NOT POSSIBLE ");
scratchloc = -1;
continue;
}
}
loc[YOU] = scratchloc;
while ((scratchloc == loc[BATS1]) || (scratchloc == loc[BATS2])) {
printf("ZAP--SUPER BAT SNATCH! ELSEWHEREVILLE FOR YOU! ");
scratchloc = loc[YOU] = rand()%20;
}
if (scratchloc == loc[WUMPUS]) {
printf("... OOPS! BUMPED A WUMPUS! ");
move_wumpus();
}
if (scratchloc == loc[PIT1] || scratchloc == loc[PIT2]) {
printf("YYYYIIIIEEEE . . . FELL IN PIT ");
finished = LOSE;
}
}
void game_setup() {
for (int j = 0; j < LOCS; j++) {
loc[j] = -1;
while (loc[j] < 0) {
loc[j] = rand()%20;
for (int k=0; k if (loc[j] == loc[k]) { loc[j] = -1; } } } } } void game_play() { arrows = 5; printf("HUNT THE WUMPUS "); if (debug) { printf("Wumpus is at %d, pits at %d & %d, bats at %d & %d ", loc[WUMPUS]+1, loc[PIT1]+1, loc[PIT2]+1, loc[BATS1]+1, loc[BATS2]+1); } finished = NOT; while (finished == NOT) { show_room(); if (move_or_shoot()) { shoot(); } else { move(); } } if (finished == WIN) { printf("HEE HEE HEE - THE WUMPUS'LL GET YOU NEXT TIME!! "); } if (finished == LOSE) { printf("HA HA HA - YOU LOSE! "); } int c = getlet("NEW GAME (Y-N)"); if (c == 'N') { exit(0); } } void handle_params(int argc, char* argv[]) { int c; while ((c = getopt(argc, argv, "s:dh")) != -1) { switch (c) { case 's': srand(atoi(optarg)); break; case 'd': debug = 1; break; case 'h': default: printf("usage: ./%s [-h] [-d] [-s seed] ", argv[0]); exit(1); } } } int main(int argc, char* argv[]) { srand(time(0)); handle_params(argc, argv); int c = getlet("INSTRUCTIONS (Y-N)"); if (c == 'Y') { print_instructions(); } do { game_setup(); game_play(); } while (getlet("NEW GAME (Y-N)") != 'N'); } playerPos = MAX_ROOMS; gameResult gr = PLAY_NEW; printInstructions(); while( gr != FINISH ) { initGame( gr ); while( !gameOver ) { look(); getInput(); } gr = showResult( playerWins ); } } }; int main( int argc, char* argv[] ) { srand( static_cast wumpus hw; hw.play(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
