Home > Enterprise >  Why does my program gets stuck on the function move_down()?
Why does my program gets stuck on the function move_down()?

Time:11-25

I am making a moving "X in a grid" - learning the keyboard module.

For some reason, when debugging in vscode, my program won't leave the line:

while j < len(board[0]):
            if board[i][j] == "X":

enter image description here

Are you seeing what's going on, on the left? "Thead-8 (process) : PAUSED ON BREAKPOINT"

(next move / step-info press)

"Theard-8 (process) PAUSED ON STEP

What isVscode trying to tell me?

import keyboard
import os
import platform

def clear():
    if platform.system() == "Windows": os.system('cls')
    if platform.system() == "Darwin" or platform.system() == "Linux": os.system('clear')

board = [["-", "-", "-", "-", "-", "-", ],
        ["-", "-", "-", "-", "-", "-", ],
        ["-", "-", "-", "-", "-", "-", ],
        ["-", "X", "-", "-", "-", "-", ]]

clear()
for x in board:
    print(x)


def move_right():
    keyboard.send("Backspace") #Remove written character
    i = 0
    j = 0

    while i < len(board):
        j = 0
        while j < len(board[0]):
            if board[i][j] == "X":
                if j 1 == len(board[0]): #IF AT THE END, CONTINUE
                    break
                else: #Swap, and exit second loop - to prevent x constantly moving to the end
                    board[i][j], board[i][j 1] = board[i][j 1], board[i][j]
                    break

            j  = 1
        i  = 1

    clear()
    for x in board:
        print("%s\n" % x)

def move_left():
    keyboard.send("Backspace") #Remove written character

    i = 0
    j = 0

    while i < len(board):
        j = 0
        while j < len(board[0]):
            if board[i][j] == "X":
                if j == 0: #IF AT THE END, CONTINUE
                    break
                else: #Swap, and exit second loop - to prevent x constantly moving to the end
                    board[i][j], board[i][j-1] = board[i][j-1], board[i][j]
                    break

            j  = 1
        i  = 1

    clear()
    for x in board:
        print("%s\n" % x)

def move_up():
    keyboard.send("Backspace") #Remove written character

    i = 0
    j = 0

    while i < len(board):
        j = 0
        while j < len(board[0]):
            if board[i][j] == "X":
                if i == 0: #IF AT THE END, CONTINUE
                    break
                else: #Swap, and exit second loop - to prevent x constantly moving to the end
                    board[i][j], board[i-1][j] = board[i-1][j], board[i][j]
                    break

            j  = 1
        i  = 1

    clear()
    for x in board:
        print("%s\n" % x)

def move_down():
    keyboard.send("Backspace") #Remove written character

    i = 0
    j = 0

    while i < len(board):
        j = 0
        while j < len(board[0]):
            if board[i][j] == "X":
                if i == len(board): #IF AT THE END, CONTINUE
                    break
                else: #Swap, and exit BOTH loops, to prevent X moving to the next line, then tracking him again by mistake.
                    board[i][j], board[i 1][j] = board[i 1][j], board[i][j]
                    outer_loop_break = True
                    break
                    
        if outer_loop_break == True:
            break
        else:
            j  = 1
        i  = 1

    clear()
    for x in board:
        print("%s\n" % x)

keyboard.add_hotkey("W", lambda: move_up())
keyboard.add_hotkey("A", lambda: move_left())
keyboard.add_hotkey("S", lambda: move_down())
keyboard.add_hotkey("D", lambda: move_right())


keyboard.wait("Esc")

I am not sure if this is a bug, or something wrong in my code. for some reason, for the very similiar move_up() function, this doesn't happen.

CodePudding user response:

You are not changing the while condition:

while j < len(board[0]):
    if board[i][j] == "X":
        # (...)

This is your whole loop. j never changes and len(board[0]) never changes, and because if statement is false. It just loops indefinitely.

In move_up() function, you are changing j with this line j = 1, but this line is inside the while loop.

CodePudding user response:

j isn't incremented in your loop – Ryan Schaefer

Oops, while creating an outer-loop-breaking checker, I accidently put j =1 outside the nested-while loop... Well it was late at night anyway

Haha, thanks for everybody who answered!

  • Related