Wumpus Hunt 2
Nov 11, 2010 at 5:25pm UTC
I was already thinking along Zaita'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. Again, I apologize in advance for less than ideal indentation.
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
#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;
}
Last edited on Nov 11, 2010 at 5:34pm UTC
Nov 11, 2010 at 5:59pm UTC
The reason you can't find the error is your code is a giant glob.
Did you learn about functions yet? If you break up the program into smaller parts, logic problems are much easier to spot and fix.
I've been meaning to write an article on this topic and I just haven't been able to find the time. =x
Nov 11, 2010 at 6:14pm UTC
It is a work-in-progress. I agree that collecting code relating to one facet of the program into a function is on the path to a final version.
Nov 11, 2010 at 6:21pm UTC
You should use functions to help you think about the problem, not as an afterthought. Properly named and logically concise functions allow you to abstract details away and see things more clearly.
Nov 11, 2010 at 9:12pm UTC
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.
Nov 11, 2010 at 9:36pm UTC
You should use functions to help you think about the problem, not as an afterthought. Properly named and logically concise functions allow you to abstract details away and see things more clearly.
^ this
breaking logic up into functions makes writing the program much easier, and makes bugs less likely (and easier to find/fix).
Something like this is much easier to follow than clumping everything into main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main()
{
ShowIntro();
char input;
do
{
ShowMenu();
input = GetUserInput();
switch (input)
{
case '1' : DoOption1(); break ;
case '2' : DoOption2(); break ;
case '3' : DoOption3(); break ;
}
}while (input != 'Q' );
}
Topic archived. No new replies allowed.