2

I am making a basic text adventure RPG game in Python. My problem is with the movement system, where the dungeon is broken into squares each with a coordinate. The player is asked for a coordinate of where to move which is then passed into the function move2, then the program checks a bunch of if statements, similar to the one below, and prints the according map of the floor. There is an if statement for each coordinate, so there are 40 different if statements, each with an image of the map. The problem is that nothing happens after the player is asked for a coordinate. The program ends after asking for a coordinate, but does not give any error (and I know I'm inputting a correct coordinate.)

move = input("\n To move around the room Hero, simply think the coordinates of the tile        you want to move to! However, you can only move one tile at a time Ex: You are at tile 5,4. You can move to 5,3 5,5 or 4,4")
move2(move)

I apologize for the bad code. I'm sure there is a far better method to do this, but none that I know of yet...

def move2 (move):      
    while move == "5,1" "5,2" "5,3" "5,4" "5,5" "5,6" "5,7" "5,8" "4,1" "4,2" "4,3" "4,4" "4,5" "4,6" "4,7" "4,8" "3,1" "3,2" "3,3" "3,4" "3,5" "3,6" "3,7" "3,8" "2,1" "2,2" "2,3" "2,4" "2,5" "2,6" "2,7" "2,8" "1,1" "1,2" "1,3" "1,4" "1,5" "1,6" "1,7" "1,8":
        if move == "5,3":
            move = input("""
       1  2  3  4  5  6  7  8                
       __ __ __ D_ __ __ __ __                 
    1 |_ |_ |_ |_ |_ |_ |_ |_C|                      
    2 |_ |_ |_ |_ |_ |_ |_ |_ |                             
    3 |_ |?_|_ |_ |_ |_ |_ |_ |                            
    4 |_ |_ |_ |_ |_ |_ |_ |_ |                              
    5 |_ |_ |_x|_ |_ |_ |_ |_ |                              
                D""")    
7
  • 2
    You should read some tutorial beforehand. Commented Jun 25, 2013 at 20:35
  • 4
    Please don't take this the wrong way. I like where you are going with this. But you are lacking some basic fundamental understanding with python. Read this: tutorialspoint.com/python/python_while_loop.htm and try again on your program. Commented Jun 25, 2013 at 20:36
  • 10
    while move == "5,1" "5,2" "5,3" ... "1,8": is equivalent to while move == "5,15,25,3...1,71,8", the interpreter concatenates strings that are adjacent. I doubt move will ever equal that monster string, so it will never run the loop. If it did it might be worse: if statement would always be false so it would run forever. Commented Jun 25, 2013 at 20:36
  • First, this while loop will never execute. By putting all those strings next to each other, you're concatenation them, which makes a new string that will never match their input. Secondly, even if the whole loop executed, it doesn't do anything except set one variable: it doesn't print anything or ask for anything. Commented Jun 25, 2013 at 20:36
  • 1
    If the condition in while is True, then the condition in if will never be True. Commented Jun 25, 2013 at 20:37

3 Answers 3

5

This will help a bit, but you should really read a tutorial:

while move in ("5,1", "5,2", "5,3", "5,4", ... etc):
    # body
Sign up to request clarification or add additional context in comments.

Comments

1

As others have pointed out,

while move == "5,1" "5,2" "5,3" "5,4" "5,5" "5,6" "5,7" "5,8" "4,1" "4,2" "4,3" "4,4" "4,5" "4,6" "4,7" "4,8" "3,1" "3,2" "3,3" "3,4" "3,5" "3,6" "3,7" "3,8" "2,1" "2,2" "2,3" "2,4" "2,5" "2,6" "2,7" "2,8" "1,1" "1,2" "1,3" "1,4" "1,5" "1,6" "1,7" "1,8":

concatenates (smashes together) all the strings. What you want instead is:

while move in ("5,1", "5,2", "5,3", "5,4", "5,5", "5,6", "5,7", "5,8", "4,1", "4,2", "4,3", "4,4", "4,5", "4,6", "4,7", "4,8", "3,1", "3,2", "3,3", "3,4", "3,5", "3,6", "3,7", "3,8", "2,1", "2,2", "2,3", "2,4", "2,5", "2,6", "2,7", "2,8", "1,1", "1,2", "1,3", "1,4", "1,5", "1,6", "1,7", "1,8"):

but that's not so great either. Instead I'd use better string-matching:

import re
while re.match(r'\d,\d', move):

2 Comments

Small point, but \d doesn't quite meet what op is trying to do here, it looks like they are looking for something closer to: r'([0-5]{1}), ([0-8]{1})' Assuming they should be captured.
Even better, OP should avoid magic constants and have a height and width for the grid and use r'([0-%d]{1}), ([0-%d]{1})' % (height, width).
0

For good coding practices, you should move any long declaration into a separate part of your code. Some simple re-factoring:

allowed_moves =["5,1", "5,2", "5,3", "5,4",...]
while move in allowed_moves:
    etc...

Also, if you're looking for a good resource to learn Python, which will help you with this as well as countless other thing sin your programming career I suggest the following book:

http://www.greenteapress.com/thinkpython/

Free online, and written by a professor of mine.

Comments

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.