How to navigate the Wumpus Map?

  • Context: Comp Sci 
  • Thread starter Thread starter Kiff
  • Start date Start date
  • Tags Tags
    Map
Click For Summary

Discussion Overview

The discussion revolves around implementing the movement mechanics for a Wumpus game, focusing on how to navigate through a map represented in a text file format. Participants are sharing code snippets, addressing syntax issues, and clarifying the structure of the map data.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes the initial setup of the Wumpus game, explaining how the map files are organized with room numbers and their adjacent rooms.
  • Another participant suggests a correction to the syntax for checking if a room is adjacent, proposing the use of 'if room in close_rooms' instead of the original syntax.
  • A participant points out that the logic for updating the current room may not correctly reflect the player's movement, questioning the assignment of 'i = room + 2'.
  • One participant identifies an error related to the 'room' variable not being defined before its first use in the while loop, suggesting a fix by initializing 'room' before the loop starts.
  • Another participant reiterates the need to ensure that the input for room movement is valid and suggests using a dictionary to manage room connections more effectively.
  • Several participants express uncertainty about the correct implementation and structure of the code, with some acknowledging potential mistakes in their logic.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections and improvements in the code, but there are multiple competing views on the best approach to implement the movement logic and manage room adjacency. The discussion remains unresolved regarding the optimal solution.

Contextual Notes

There are limitations in the assumptions about the map file structure and the handling of input validation. Participants have not reached a consensus on the best coding practices for this implementation.

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 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K