I was already thinking along ARKoenig's suggestion and revised my code to use arrays.
It was working pretty well until I made some minor changes. Now it loops non-stop without any prompt for the player to make a play. I am sure it is a small error, but I can't see it. I don't remember the changes I made just before it started looping continuously. I hope someone else's eyes can see it. Here is my new code. Thanks in advance. I apologize in advance for less than ideal indentation.

#include "../../std_lib_facilities.h"

int main() {

srand((unsigned)time(0));
double rands[3][4];      // array of random numbers; four for each of 3 adjacent rooms.
int rooms[4][4];         // array of room characterics; four for each of 3 adjacent rooms (0-2) and 1 current room (3).
    rooms[3][0] = 100;   // initialize the current room number at 100.
int flag = 0;
int play = 0;  

cout << "You are safe and in room 100!" << endl;

while (flag == 0) {

	for (int i = 0; i < 3; i++) {  // loop for the probabilities.

	    rands[i][0] = 600.0 * rand()/RAND_MAX;    // for the room number.
	    rands[i][1] = 100.0 * rand()/RAND_MAX;    // for the wumpus.
	    rands[i][2] = 100.0 * rand()/RAND_MAX;    // for the pit.
	    rands[i][3] = 100.0 * rand()/RAND_MAX;    // for the bat.
	}

	for (int i = 0; i < 3; i++) {  // loop for the rooms.

		if (rands[i][0] >=   0.0 && rands[i][0] < 100.0) {rooms[i][0] = 120;}  // randomizing of room numbers.
		if (rands[i][0] >= 100.0 && rands[i][0] < 200.0) {rooms[i][0] = 140;}
		if (rands[i][0] >= 200.0 && rands[i][0] < 300.0) {rooms[i][0] = 150;}
		if (rands[i][0] >= 300.0 && rands[i][0] < 400.0) {rooms[i][0] = 160;}
		if (rands[i][0] >= 400.0 && rands[i][0] < 500.0) {rooms[i][0] = 170;}
		if (rands[i][0] >= 500.0 && rands[i][0] < 600.0) {rooms[i][0] = 190;}

        if (rands[i][1] > 30.1 && rands[i][1] < 47.8) {     // randomizing of wumpus.  False => 0.  True => 1.
		    rooms[i][1] = 1;
	    } else {
		    rooms[i][1] = 0;
	    }

		if (rands[i][2] > 83.1 && rands[i][2] < 95.5) {     // randomizing of pit.
		    rooms[i][2] = 1;
	    } else {
		    rooms[i][2] = 0;
	    }

        if (rands[i][3] > 22.7 && rands[i][3] < 40.4) {     // randomizing of bat.
		    rooms[i][3] = 1;
	    } else {
		    rooms[i][3] = 0;
	    }
	}


	cout << "There are passage ways to rooms " << rooms[0][0] <<", " << rooms[1][0] <<", and " << rooms[2][0] <<"." << endl;

    if (rooms[0][1] == 1 || rooms[1][1] == 1 || rooms[2][1] == 1) {
		cout << "I smell the wumpus." << endl;
	}

    if (rooms[0][2] == 1 || rooms[1][2] == 1 || rooms[2][2] == 1) {
		cout << "I feel a breeze." << endl;
	}

    if (rooms[0][3] == 1 || rooms[1][3] == 1 || rooms[2][3] == 1) {
		cout << "I hear a bat." << endl;
	}

    cout << endl;
    cout << "Do you wish to move or shoot?" << endl;  // 1,000 + room number for moving.  2,000 + room number for shooting.

    cin >> play;

	double prob_wumpus = rand()/RAND_MAX;

	if (play == 2000 + rooms[0][0]) {
		if (rooms[0][1] == 1) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
			flag = 1;
		}
		if (rooms[0][1] == 0 && prob_wumpus <= 0.70) {
			cout << "You missed the wumpus.  Play again." << endl;
		} else if (rooms[0][1] == 0 && prob_wumpus > 0.70) {
			cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
			flag = 1;
	    }
	}

	if (play == 2000 + rooms[1][0]) {
		if (rooms[1][1] == 1) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
			flag = 1;
		}
		if (rooms[1][1] == 0 && prob_wumpus <= 0.70) {
			cout << "You missed the wumpus.  Play again." << endl;
		} else if (rooms[1][1] == 0 && prob_wumpus > 0.70) {
			cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
			flag = 1;
	    }
	}

	if (play == 2000 + rooms[2][0]) {
		if (rooms[2][1] == 1) {
			cout << "You shot the wumpus!  YOU WIN!!!" << endl;
			flag = 1;
		}
		if (rooms[2][1] == 0 && prob_wumpus <= 0.70) {
			cout << "You missed the wumpus.  Play again." << endl;
		} else if (rooms[2][1] == 0 && prob_wumpus > 0.70) {
			cout << "You missed the wumpus.  It has come into your room and eaten you!" << endl;
			flag = 1;
	    }
	}

	if (play == 1000 + rooms[0][0]) {
		rooms[3][0] = rooms[0][0];
		rooms[3][1] = rooms[0][1];
		rooms[3][2] = rooms[0][2];
		rooms[3][3] = rooms[0][3];
	}

	if (play == 1000 + rooms[1][0]) {
		rooms[3][0] = rooms[1][0];
		rooms[3][1] = rooms[1][1];
		rooms[3][2] = rooms[1][2];
		rooms[3][3] = rooms[1][3];
	}

	if (play == 1000 + rooms[2][0]) {
		rooms[3][0] = rooms[2][0];
		rooms[3][1] = rooms[2][1];
		rooms[3][2] = rooms[2][2];
		rooms[3][3] = rooms[2][3];
	}

	if (play == 1000 + rooms[0][0] || play == 1000 + rooms[1][0] || play == 1000 + rooms[2][0]) {

		if (rooms[3][1] == 1) {
		    cout << "The wumpus is in the room.  You are eaten!" << endl;
		    flag = 1;
	    } 
	
	    if (rooms[3][2] == 1 && rooms[3][1] == 0) {
		    cout << "You have fallen into a pit and died!" << endl;
		    flag = 1;
	    }

	    if (rooms[3][3] == 1 && rooms[3][2] == 0 && rooms[3][1] == 0) {cout << "A bat has taken you to another room!" << endl;}

		if (rooms[3][1] == 0 && rooms[3][2] == 0 && rooms[3][3] == 0) {cout << "You are safe and in room " << rooms[3][0] <<"!" << endl;}
	}
}
    cout << "Game Over." << endl;

	keep_window_open();
    return 0;
}

Recommended Answers

All 2 Replies

I have no idea what is going on there, but if it is in an infinite loop then flag is never getting set to 1.

I STRONGLY suggest breaking a lot of this code out into functions. I.e.

void GenerateRandomNumbers(int rands[][])
{
	    rands[i][0] = 600.0 * rand()/RAND_MAX;    // for the room number.
	    rands[i][1] = 100.0 * rand()/RAND_MAX;    // for the wumpus.
	    rands[i][2] = 100.0 * rand()/RAND_MAX;    // for the pit.
	    rands[i][3] = 100.0 * rand()/RAND_MAX;    // for the bat.
}

It looks like you have have at least 5 or 10 of this size functions. Then your main function should be easy for us (and you, more importantly) to follow:

main()
{
  getInput();
  while(some condition)
    {
    doSomething();
    doSomethingElse();
    if(a condition)
      {
      doAnotherThing();
      }
    }
}

David

I found my error. I am not sure what it was exactly but I suspect it was related to a conflict in types. When I made changes I think it resulted in ints mixing with doubles. After I made additional changes, the problem resolved itself.

Yes. Other have suggested using functions as well. I am working on that now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.