0

I am creating a python script in terminal which a drunk man walks around based upon the characters in a text file. Depending on n,s,e,w, he will move in that same direction. Essentially, a user must input: executing file, odd number 1(above 9/height of grid), odd number 2(above 9/width of grid ), and a filename (which contains only n,w,e,s).

I am only getting an output of 1 step. I don't know what the reason for this is.

![This is the layout of the grid][1]

The man can reach a wall and stop, reach the gate and stop, or just keep walking aimlessly and fall asleep after the characters in the file run out. I have written a pseudo-code on top, but am not sure how to have the man move, reach the wall/gate and stop, or keep walking around.


# <<user provides 3 arguments; first two are positive ints; third is file>>
# <<first number is height, second number is width>>
# <<third argument file contains only nsew without any whitespace>>
# <<if invalid input, provide user error + how to use message>>
# <<error given if NOT: 3 arguments, positive int, less than 10>>
# <<once executed, reads file>>
# <<if file not existing, readable, or containing nsew, exit, error. Assume no newline/whitespace>>
# <<if even numbers are given, it should round up to smallest odd number greater than it>>

The part where I am stuck:
# <<if he reaches origin, it must hit counter+=1 each time>>
# <<walking stops if they reach gate, wall, or no more steps>>
# <<Create an output message for output of walk: The walk was <step_count> steps long returned to the origin <counter> times. It ended because it <...> >>

#!/usr/bin/env python3

import sys
import re
import os


height = sys.argv[1]
width = sys.argv[2]
filename = sys.argv[3]
counter=0 #counter for how many times it goes to the center
x=0 #X coordinate
y=0 #Y coordinate
steps=0

if len(sys.argv) == 4:
    pass
else:
    print ("Please provide executed, followed by the height, width, and name of file")
    quit()
while len(sys.argv) == 4: # While there are 4 command-line arguments (including name of excuted file)
    arg2 = re.search("([0-9]+)", height) #Argument 1 is height
    if arg2 and int(height) > 9:
       if int(height) % 2 != 0:
           pass
       else:
            new_height = int(height)+1
    else:
        print("Please give a value for the first argument, height, greater than 9")
        sys.exit("Integer given is less than 9")
    arg3 = re.search("([0-9]+)", width) #Argument 2 is height
    if arg3 and int(width) > 9: #Argument 2 is width
        if int(width) % 2 != 0:
           pass
       else:
            new_height = int(height)+1
    else:
        print("Please give a value for the first argument, height, greater than 9")
        sys.exit("Integer given is less than 9")
    arg3 = re.search("([0-9]+)", width) #Argument 2 is height
    if arg3 and int(width) > 9: #Argument 2 is width
        if int(width) % 2 != 0:
            pass
        else:
            new_width = int(width)+1
    else:
        print("Please give a value for the second argument, height, greater than 9")
        sys.exit("Integer given is less than 9")
    file_exist=os.path.exists(filename) #File exists
    if file_exist == True:
        pass
    else:
        print ("This file does not exist. Please provide a valid filename for the third argument")
        exit()
    try: #file argument
        text = open(filename)
        lines = text.readlines()
        text.close()
    except:
        print("Error lacks reading permissions!", file=sys.stderr)
        exit()
    match=re.search("[nsew]+", filename)
    if match:
        steps+=1
        if 'n' in filename:
            y+=1
        elif 's' in filename:
            y-=1
        elif 'e' in filename:
            x+=1
        else:
            x-=1
    else:
        print("filename contains characters other than n, s, e and w. Please use a file that only contains these characters.")
        exit()
    if x == 0 and y == 0:
        counter+=1
    else:
        pass
    if x == (int(width)-1)/2 or x == (1-(int(width)))/(2):
        break
    elif y == (int(height)-1)/2 or y == (1-(int(height)))/(2):
        break
    else:
        pass
    if x == (int(width)-1)/2 and -3 < y < 3:
        print ("The walk was", steps, "long, returned to the origin", counter, " times. It ended because the drunk man reached the gate.")
        quit()
    elif x == (int(width)-1)/2 and y > 2 and y < -2 or x == (1-(int(width)))/2 or y == (int(height)-1)/2 or y == (1-(int(height)))/2:
        print ("The walk was", steps, "long, returned to the origin", counter, " times. It ended because the drunk man reached the wall.")
        quit()
    else:
        print ("The walk was", steps, "long, returned to the origin", counter, " times. It ended because the drunk man ran out of energy.")
        quit()
  [1]: https://i.stack.imgur.com/sELp4.png
Rob
  • 11
  • 4
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. What Python data structures do you know about, that might represent grid well? What do you find when you search for other grid-based games or maze programs? – Prune Nov 06 '20 at 00:31
  • This post of mine is semi-related, I think. https://stackoverflow.com/questions/55029524/how-to-create-billiard-ball-reflection-boundary-condition-in-python – bart cubrich Nov 06 '20 at 00:32

0 Answers0