Comp Sci How to navigate the Wumpus Map?

  • Thread starter Thread starter Kiff
  • Start date Start date
  • Tags Tags
    Map
AI Thread Summary
The discussion focuses on implementing movement logic for the Wumpus game, where players navigate through rooms based on a map file structure. Participants clarify that the map consists of room numbers and their adjacent rooms, and they suggest using a dictionary for better organization of room connections. Key issues include defining the room variable before the while loop and correcting the syntax for checking adjacent rooms. Suggestions for improving the code include ensuring proper input handling and refining the movement function to accurately reflect room transitions. Overall, the conversation emphasizes refining the code structure for effective navigation within the game.
Kiff
Messages
6
Reaction score
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()<-----This turns all map elements into a list
start_room = everything[i]<---room player is in
close_rooms = everything[i+1]<-----adjacent rooms to start_room


def movement(i):
    if room != item in list(close_rooms):<-----Whats the correct syntax for this?
        return room + "is not an adjacent room"
    else:
        i = room + 2
     
    
while room != "quit":<----This is the loop that runs until user inputs "quit"
    
    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
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
 
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.
 
Hmmmm... I still get a error after the changes.

it says room is not defined on the line
Code:
while room!= "q"
 
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.
 
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()<-----This turns all map elements into a list
start_room = everything[i]<---room player is in
close_rooms = everything[i+1]<-----adjacent rooms to start_roomdef movement(i):
    if room != item in list(close_rooms):<-----Whats the correct syntax for this?
        return room + "is not an adjacent room"
    else:
        i = room + 2
     
    
while room != "quit":<----This is the loop that runs until user inputs "quit"
    
    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:

Similar threads

Replies
6
Views
3K
Replies
2
Views
3K
Replies
9
Views
3K
Replies
11
Views
3K
Replies
5
Views
2K
Back
Top