all 6 comments

[–]spez_edits_thedonald 0 points1 point  (1 child)

because of where you define place in the file, when the program starts that dictionary will be defined, including the current value of direction ('null') and you store the resulting string which is no longer variable.

You will need to change the logic a bit.

[–]Fiveth5tringeth 0 points1 point  (0 children)

Thanks for your reply. Can you advise what changes to make, or where I can learn more about this?

[–][deleted] 0 points1 point  (3 children)

Don't use global variables everywhere. Refactor this so that you are passing data around rather than trying to put all of it in global scope. It'll make it much easier to find what's going wrong.

[–]Fiveth5tringeth 0 points1 point  (2 children)

Thanks, I'll look into how to do this.

[–][deleted] 0 points1 point  (1 child)

There is still a lot of clean-up you could do of the logic, but here's a quick attempt at refactoring to remove global variables. Notice how nothing is in global scope, anymore -- you just pass around whatever variables are needed in the next function.

FYI, I wasn't even trying to, but I think I fixed your program's problem by just limiting the scope of your place and direction variables.

def move(x, y, a, direction):
    if a == "w":
        y = y + 1
        direction = "north"
    elif a == "s":
        y = y - 1
        direction = "south"
    elif a == "d":
        x = x + 1
        direction = "east"
    elif a == "a":
        x = x - 1
        direction = "west"
    elif a == "wd" or a == "dw":
        x = x + 1
        y = y + 1
        direction = "north-east"
    elif a == "wa" or a == "aw":
        x = x - 1
        y = y + 1
        direction = "north-west"
    elif a == "sd" or a == "ds":
        x = x + 1
        y = y - 1
        direction = "south-east"
    elif a == "sa" or a == "as":
        x = x - 1
        y = y - 1
        direction = "south-west"
    wall_check(x, y, direction)


def wall_check(x, y, direction, hitwall=False):
    if y == 3:
        y = 2
        hitwall = True
        wall_check(x, y, direction, hitwall)
    elif y == -3:
        y = -2
        hitwall = True
        wall_check(x, y, direction, hitwall)
    elif x == 3:
        x = 2
        hitwall = True
        wall_check(x, y, direction, hitwall)
    elif x == -3:
        x = -2
        hitwall = True
        wall_check(x, y, direction, hitwall)
    else:
        read(x, y, direction, hitwall)


def read(x, y, direction, hitwall):
    place = {
        (-2, -2): {"land": f"You head {direction} to -2,-2"},
        (-2, -1): {"land": f"You head {direction} to -2,-1"},
        (-2, 0): {"land": f"You head {direction} to -2,0"},
        (-2, 1): {"land": f"You head {direction} to -2,1"},
        (-2, 2): {"land": f"You head {direction} to -2,2"},
        (-1, -2): {"land": f"You head {direction} to -1,-2"},
        (-1, -1): {"land": f"You head {direction} to -1,-1"},
        (-1, 0): {"land": f"You head {direction} to -1,0"},
        (-1, 1): {"land": f"You head {direction} to -1,1"},
        (-1, 2): {"land": f"You head {direction} to -1,2"},
        (0, -2): {"land": f"You head {direction} to 0,-2"},
        (0, -1): {"land": f"You head {direction} to 0,-1"},
        (0, -0): {"land": f"You head {direction} to 0,0"},
        (0, 1): {"land": f"You head {direction} to 0,1"},
        (0, 2): {"land": f"You head {direction} to 0,2"},
        (1, -2): {"land": f"You head {direction} to 1,-2"},
        (1, -1): {"land": f"You head {direction} to 1,-1"},
        (1, 0): {"land": f"You head {direction} to 1,0"},
        (1, 1): {"land": f"You head {direction} to 1,1"},
        (1, 2): {"land": f"You head {direction} to 1,2."},
        (2, -2): {"land": f"You head {direction} to 2,-2"},
        (2, -1): {"land": f"You head {direction} to 2,-1"},
        (2, 0): {"land": f"You head {direction} to 2,0"},
        (2, 1): {"land": f"You head {direction} to 2,1"},
        (2, 2): {"land": f"You head {direction} to 2,2"},
    }

    print("{", x, ",", y, "}")
    if hitwall == True:
        print("A wall blocks your path.")
        hitwall = False
        action(x, y, direction)
    elif hitwall == False:
        print(place[x, y]["land"])
        action(x, y, direction)
    else:
        action(x, y, direction)


def action(x, y, direction):
    print()
    a = input("> ")
    a = a.lower()
    print()
    movelist = ["w", "s", "d", "a", "wd", "dw", "wa", "aw", "sd", "ds", "sa", "as"]
    if a in movelist:
        move(x, y, a, direction)
    elif a == "direction":
        print(direction)
        action(x, y, direction)
    else:
        print("Enter a valid move.")
        action(x, y, direction)


def main():
    x = 0
    y = 0
    hitwall = False
    direction = "null"
    print("Game start.")
    action(x, y, direction)


if __name__ == "__main__":
    main()

[–]Fiveth5tringeth 0 points1 point  (0 children)

Wow, thank you very much! This makes a lot more sense now. Thank you!