0

Running into the age old problem of the infinite loop...

Writing a text adventure game and have the game mechanics down; map and movement around it. Running into an issue with an infinite loop when I try to implement the other functionality - take items etc.

Below are 2 snippets of the code including the initializing variables, movement code, and the next stage stuff. Below the "......" are where the issue is.

All indentation is aligned as seen.


print("Welcome to this most intrepid" + game['name']
+ " your goal; should you dare to continue... is to" + game['goal'])
star_loc = int(game['start'])
curr_loc = star_loc 
character = {'inventory': [], 'location':curr_loc}
x_loc = 1
y_loc = 2
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')

while True:
    command_parts = command.split()
    cmd = command_parts[0]
    print(cmd)
    obj = command_parts[-1]
    print(obj)
    #move around map
    if cmd == 'move' or cmd == 'go' or 'hiddenpath':
        #handles movement on the X plane - East, West
        elif obj == 'east':
            if x_loc == int(game['xsize']): # use the x size the game dict instead, compute the x and y 
                curr_loc = curr_loc - 2
                x_loc = 1
                y_loc = y_loc
                command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
            else:
                curr_loc = curr_loc + 1
                x_loc = x_loc + 1
                y_loc = y_loc
                command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
        elif obj == 'west':
            if x_loc == int(game['xsize']) - 2:
                curr_loc = curr_loc + 2
                x_loc = x_loc = 3
                y_loc = y_loc
                command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
            else:
                curr_loc = curr_loc - 1
                x_loc = x_loc - 1
                y_loc = y_loc
                command = input("You are " + map[curr_loc]["desc"] + ' what next? ')

.................

    if cmd == 'exit':
        print('Goodbye')
        break

    if cmd == 'inv':
        print('You are carrying')
        for item in character['inventory']:
            print('', item)
            continue 

    if cmd == 'goal':
        print(game['goal'])
    else:
        continue
    
    if cmd == 'take':
        if obj in map[curr_loc]['obj']:
            print("You take the " + str(map[curr_loc]['obj'][0]))
            map[curr_loc]['obj'].remove(obj)
            character['inventory'].append(obj)
        continue

Major drag... └[∵┌]└[ ∵ ]┘[┐∵]┘

2 Answers 2

1

I went through your code and have the following suggestions:

  • Make sure your sscce is runnable. Specifically, you don't tell us what game looks like.

  • instead of multiple equal conditions, which get very cumbersome as you get more clauses, I would use "cmd in ['move', 'go', 'hiddenpath']".

  • Instead of putting print statements all over your code, the logging module is your friend.

  • y_loc = y_loc is a no-op. Get rid of it.

  • command = input("You are " + map[curr_loc]["desc"] + ' what next? ') does not need to be in every clause, have it after your chain of if/elif statements.

Sign up to request clarification or add additional context in comments.

Comments

0

On the line

if cmd == 'move' or cmd == 'go' or 'hiddenpath':

Change it to

if cmd == 'move' or cmd == 'go' or cmd == 'hiddenpath':

1 Comment

or you can change it to if cmd in [ 'move','go' ,'hiddenpath']:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.