Hunt the Wumpus: Difference between revisions

Content deleted Content added
Wherrera (talk | contribs)
Wherrera (talk | contribs)
 
Line 550:
 
#define NUM_ROOMS 20
#define NUM_ARROWSARROWS 5
 
typedef enum { false, true } bool;
int cave[NUM_ROOMS][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}
};
 
typedef struct {
int wumpus, pits[2], bats[2], player, arrows;
int connected[3];
bool has_wumpus;
bool has_bat;
bool has_pit;
} Room;
 
Room cave[NUM_ROOMS + 1]; // Cave rooms are numbered from 1 to 20
int random_room() {
 
return rand() % NUM_ROOMS;
void initialize_cave() {
int i, j;
// Initialize cave rooms
for (i = 1; i <= NUM_ROOMS; i++) {
for (j = 0; j < 3; j++) {
cave[i].connected[j] = (i + j) % NUM_ROOMS + 1; // Circular connections
}
cave[i].has_wumpus = false;
cave[i].has_bat = false;
cave[i].has_pit = false;
}
// Randomly place wumpus, bats, and pits
cave[rand() % NUM_ROOMS + 1].has_wumpus = true;
for (i = 0; i < 2; i++) {
cave[rand() % NUM_ROOMS + 1].has_bat = true;
cave[rand() % NUM_ROOMS + 1].has_pit = true;
}
}
 
void initialize_gamesense(int room) {
printf("You are in room %d.\n", room);
srand(time(NULL));
int i, adjacent_room;
 
for (i = 0; i < 3; i++) {
wumpus = random_room();
adjacent_room = cave[room].connected[i];
pits[0] = random_room();
if (cave[adjacent_room].has_wumpus)
do {
printf("You smell something terrible nearby.\n");
pits[1] = random_room();
if (cave[adjacent_room].has_bat)
} while (pits[1] == pits[0]);
printf("You hear a rustling.\n");
bats[0] = random_room();
if (cave[adjacent_room].has_pit)
do {
printf("You feel a cold wind blowing from a nearby cavern.\n");
bats[1] = random_room();
}
} while (bats[1] == bats[0]);
}
 
void move(int *room) {
player = random_room();
arrowsint = NUM_ARROWSchoice;
printf("Choose an adjacent room to move into: ");
scanf("%d", &choice);
if (choice < 1 || choice > 3) {
printf("Invalid choice. Please choose a number between 1 and 3.\n");
move(room);
return;
}
*room = cave[*room].connected[choice - 1];
}
 
void sense_roomshoot(int room, int *arrows, bool *game_over) {
int choice, adjacent_room;
printf("You are in room %d.\n", room);
if printf("Choose an adjacent room ==to wumpus)shoot {into: ");
scanf("%d", &choice);
printf("You smell something terrible nearby.\n");
if (choice < 1 || choice > 3) {
printf("Invalid choice. Please choose a number between 1 and 3.\n");
shoot(room, arrows, game_over);
return;
}
adjacent_room = cave[room].connected[choice - 1];
if (room == pits[0] || room == pits[1]) {
if (cave[adjacent_room].has_wumpus) {
printf("You feel a cold wind blowing from a nearby cavern.\n");
printf("Congratulations! You've killed the Wumpus!\n");
*game_over = true;
return;
}
else {
if (room == bats[0] || room == bats[1]) {
if (rand() % 4 != 0) { // 75% chance of waking up the wumpus
printf("You hear a rustling.\n");
if (cave[adjacent_room].has_wumpus) {
printf("The Wumpus has woken up and eaten you!\n");
*game_over = true;
return;
}
}
printf("You missed! The Wumpus is still asleep.\n");
}
(*arrows)--;
}
 
int main() {
char actionsrand(time(NULL));
int next_roominitialize_cave();
int current_room = 1;
int arrows = ARROWS;
bool game_over = false;
 
initialize_game();
printf("Welcome to Hunt the Wumpus!\n");
 
while (1!game_over) {
sense_roomsense(playercurrent_room);
printf("Choose your action:\n");
printf("1. Move to an adjacent room\n");
printf("2. Shoot into an adjacent room\n");
 
ifint (player == wumpus) {choice;
printf("OhEnter no!your You'vechoice been eaten(1 byor the2): Wumpus!\n");
scanf("%d", break&choice);
}
if (player == pits[0] || player == pits[1]) {
printf("You fell into a bottomless pit!\n");
break;
}
 
printfswitch("Do you want to (mchoice)ove or (s)hoot? ");{
scanf(" %c", &action); case 1:
move(&current_room);
 
if (action == 'm') {
printf("Which room do you want to move to? ");
scanf("%d", &next_room);
if (next_room < 0 || next_room >= NUM_ROOMS || next_room == player ||
!(next_room == cave[player][0] || next_room == cave[player][1] || next_room == cave[player][2])) {
printf("Invalid room!\n");
continue;
}
player = next_room;
} else if (action == 's') {
printf("Which room do you want to shoot into? ");
scanf("%d", &next_room);
if (next_room < 0 || next_room >= NUM_ROOMS || next_room == player ||
!(next_room == cave[player][0] || next_room == cave[player][1] || next_room == cave[player][2])) {
printf("Invalid room!\n");
continue;
}
arrows--;
if (next_room == wumpus) {
printf("Congratulations! You've killed the Wumpus!\n");
break;
}case else {2:
if (rand()arrows % 4 !=> 0) {
wumpusshoot(current_room, =&arrows, cave[wumpus][rand(&game_over) % 3];
} if (wumpus == player)else {
printf("OhYou're no!out Theof Wumpusarrows! hasYou foundlost youthe and eaten you!game.\n");
game_over = breaktrue;
}
}
printf("You missed! The Wumpus has moved to another room.\n")break;
}default:
printf("Invalid choice. Please choose 1 or 2.\n");
}
 
if (arrows == 0) {
printf("You're out of arrows! Game over.\n");
break;
}
}
Line 659 ⟶ 675:
}
</syntaxhighlight>
 
 
=={{header|C sharp|C#}}==