How to navigate the Wumpus Map?

  • Comp Sci
  • Thread starter Kiff
  • Start date
  • Tags
    Map
In summary: This is the loop that runs until user inputs "quit" print "You are in room: " + start_room print "Adjoining rooms: " + close_roomsIn summary, the player moves through the map using a for-loop and a local variable to keep track of the current room. If an adjacent room isn't found, an error is generated.
  • #1
Kiff
6
0
I'm doing the movement part of the wumpus game. I'm sure many of you are familiar with it, but if not; basically the player starts out in room 1 and s allowed to move into any adjacent room, then from that room into any adjacent room and so on .

The map files that are organized as such:
1
275
2
567
and so on

so lines 1 and 3 are the room numbers and lines 2,4 are adjacent rooms
This is what I have so far. btw the map files are text format.

The attempt at a solution
Code:
from wumpglobal import *
f = open("map1", "r")
    
i = 0
everything = f.readlines()[COLOR="DarkRed"]<-----This turns all map elements into a list[/COLOR]
start_room = everything[i][COLOR="DarkRed"]<---room player is in[/COLOR]
close_rooms = everything[i+1][COLOR="DarkRed"]<-----adjacent rooms to start_room[/COLOR]


def movement(i):
    if room != item in list(close_rooms):[COLOR="DarkRed"]<-----Whats the correct syntax for this?[/COLOR]
        return room + "is not an adjacent room"
    else:
        i = room + 2
     
    
while room != "quit":[COLOR="DarkRed"]<----This is the loop that runs until user inputs "quit"[/COLOR]
    
    print "You are in room: " + start_room
    print "Adjoining rooms: " + close_rooms

    room = raw_input("Enter a room number to move, or q) quit")
    print movement(i)


Am I on the right track here? I worked out the problem on paper, and it seems like I have all the right concepts in code. I'm just confused about the arrangement. (integrating the loop and movement function)

Thanks in advance
 
Physics news on Phys.org
  • #2
The correct method for finding an item in Python:

Code:
if needle in haystack {
  do something
} else {
  do something else
}

In your code:

Code:
if i in close_rooms:
At least, I take it that i in def movement is a local variable. My Python ain't good.

Shouldn't you also say 'print movement(room)' instead of 'print movement(i)'? You don't set i anywhere in your while loop.

The everything variable seems to be an array. Correct me if I'm wrong, but I believe that every even element is a room and every odd element are the rooms that are adjacent to that room. In short, every line in your maps file has its own cell in the array, in the order in which they've been noted.

Following that assumption: I don't think that you ever have the guarantee that room + 2 is the new room the player entered. Following your example: Say I'm in room 1 and I enter 275. It's a valid room, but won't I end up in room 2 according to your i = room + 2 assignment?

You're on the right track, though.

You'll want to add an if-statement to your while-loop, though. You can't move to the room labeled 'quit'. :p
 
  • #3
wow, thanks that was quite helpful.

Ya, I get what you are saying about the room+2 thing.

i think it should be i + room.
room being the input(room you want to go to) and i bring the room currently in.

My mistake, the map file is actually like this:
1
2,7,5
2
5,6,7

but that shouldn't make a difference.
 
  • #4
Hmmmm... I still get a error after the changes.

it says room is not defined on the line
Code:
while room!= "q"
 
  • #5
Kiff said:
Hmmmm... I still get a error after the changes.

it says room is not defined on the line
Code:
while room!= "q"

Right, that's because on the first iteration of the while loop, Python doesn't yet know what the heck the room variable is. To fix this, just add this line of code to your program a second time, but this time add it before the while loop:

room = raw_input("Enter a room number to move, or q) quit")

That way Python will know what room is before the while loop first iterates.
 
  • #6
Kiff said:
I'm doing the movement part of the wumpus game. I'm sure many of you are familiar with it, but if not; basically the player starts out in room 1 and s allowed to move into any adjacent room, then from that room into any adjacent room and so on .

The map files that are organized as such:
1
275
2
567
and so on

so lines 1 and 3 are the room numbers and lines 2,4 are adjacent rooms
This is what I have so far. btw the map files are text format.

The attempt at a solution
Code:
from wumpglobal import *
f = open("map1", "r")
    
i = 0
everything = f.readlines()[COLOR="DarkRed"]<-----This turns all map elements into a list[/COLOR]
start_room = everything[i][COLOR="DarkRed"]<---room player is in[/COLOR]
close_rooms = everything[i+1][COLOR="DarkRed"]<-----adjacent rooms to start_room[/COLOR]def movement(i):
    if room != item in list(close_rooms):[COLOR="DarkRed"]<-----Whats the correct syntax for this?[/COLOR]
        return room + "is not an adjacent room"
    else:
        i = room + 2
     
    
while room != "quit":[COLOR="DarkRed"]<----This is the loop that runs until user inputs "quit"[/COLOR]
    
    print "You are in room: " + start_room
    print "Adjoining rooms: " + close_rooms

    room = raw_input("Enter a room number to move, or q) quit")
    print movement(i)
Am I on the right track here? I worked out the problem on paper, and it seems like I have all the right concepts in code. I'm just confused about the arrangement. (integrating the loop and movement function)

Thanks in advance
Code:
''' assumes that the initial room number is 1 ''' from wumpglobal import * 
import sys <---- optionaldef movement(room, current_room):
    text = ''
    if room in dict[current_room]:
        text =  'You are now in room number %f /n' %room
        current_room = room
        if room in dict and dict[room] != []:
           
            adjacent = str(dict[room])
            text += 'The adjacent rooms are: %s' %adajcent 

            ## this is kind of lame, the str representation of a list, you could easily have
            ## string repr's of the elements instead though 

         elif dict[room] == []:
             text += 'There is nowhere else to go'
     
     else:
         text +=  'This is room is not adjacent to your current location'

     print text 
     return current_room

if __name__ == '__main__':

    f = open("map1", "r")
    dict = {}
    line_list = []
    other_list = []
    current_room = 1    for line in f:
        line_list = line.split(,)
        if len(line_list) > 1:
            dict[room].extend(line_list)
        else:
            room = line_list[0]
            dict[room] = []

    loop = 1
    while 1 OR loop == 1:
        input = raw_input("Please enter an adjacent room number or type 'quit' to quit:")
        if input == 'quit':
            sys.exit(0) OR loop = 0 
        else:
            current_room = movement(input, current_room)
this is a rough idea of what I think should be done, you should have something that sorts out good and bad input. Also, you probably want to sort your rooms and adjacent rooms into a dictionary, since there are keys and corresponding values (rooms/adjacent roooms).
There are probably mistakes but this is rough

hope this helps
 
Last edited:

1. What is the Wumpus Map?

The Wumpus Map is a navigational tool used in the game "Hunt the Wumpus". It is a grid-like layout of connected rooms that the player must navigate through to find and defeat the Wumpus, a fearsome creature lurking within one of the rooms.

2. How do I navigate the Wumpus Map?

To navigate the Wumpus Map, you must use your sense of smell to detect the location of the Wumpus and avoid any hazards such as bottomless pits or super bats. You can move from one room to another by typing in the corresponding number or direction.

3. What is the goal of navigating the Wumpus Map?

The goal of navigating the Wumpus Map is to find and defeat the Wumpus, while also avoiding any hazards and conserving your limited supply of arrows. You must use strategy and logic to navigate the map and make it out alive.

4. Are there any tips for successfully navigating the Wumpus Map?

Yes, there are a few tips for navigating the Wumpus Map. First, try to map out the layout of the rooms as you move through them to avoid getting lost. Second, use your sense of smell to detect the location of the Wumpus and plan your moves accordingly. Lastly, pay attention to any clues or warnings given in the game to avoid hazards.

5. Is there a specific strategy for navigating the Wumpus Map?

There is no one specific strategy for navigating the Wumpus Map, as the layout and hazards are randomized with each game. However, some general tips include mapping out the rooms as you move through them, using your sense of smell to detect the Wumpus, and being cautious and strategic with your moves to avoid hazards and conserve your arrows.

Similar threads

Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
152
Views
5K
  • Programming and Computer Science
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Programming and Computer Science
Replies
1
Views
926
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
19K
  • Programming and Computer Science
2
Replies
49
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top