Home > other >  List is resetting after every update Python, any way to make not reset and still update
List is resetting after every update Python, any way to make not reset and still update

Time:02-04

I'm trying to make a Sudoku solver that takes a picture of a board in a Sudoku app and then solves it. I've made the actual program that solves it and it works. I'm just trying to get the board from the app into the code now, I'm doing so via pyautogui and using image center locations of the numbers which is working great. Yt's just the the board keeps on resetting after every update I put into the board. The starting position of the board is:

board = np.array([
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
])

but when I run the code it ends up like this:

0 0 0  | 0 0 0  | 0 0 0
0 0 0  | 0 0 0  | 0 0 0
0 0 0  | 0 0 0  | 0 0 0
- - - - - - - - - - - - - 
0 0 9  | 0 0 0  | 0 0 0
0 0 0  | 0 0 0  | 0 0 0
0 0 0  | 0 0 0  | 0 9 0
- - - - - - - - - - - - - 
0 0 0  | 0 0 9  | 0 0 0
0 0 0  | 0 0 0  | 0 0 0
0 0 0  | 0 0 0  | 0 0 0

It's only keeping the last run number(9) in the board. Are there any ways to keep this from happening and saving all the numbers in the board after every update?

Code that makes the board update:

def place_number(num, pos1, pos2):
    global board
    if num == one:
        board[pos2, pos1] = 1
    if num == two:
        board[pos2, pos1] = 2
    if num == three:
        board[pos2, pos1] = 3
    if num == four:
        board[pos2, pos1] = 4
    if num == five:
        board[pos2, pos1] = 5
    if num == six:
        board[pos2, pos1] = 6
    if num == seven:
        board[pos2, pos1] = 7
    if num == eight:
        board[pos2, pos1] = 8
    if num == nine:
        board[pos2, pos1] = 9
    else:
        board[pos2, pos1] = 0

CodePudding user response:

Solved, just needed to remove else statement from place_number function which was reseting all board_locations other than the number I was running through.

def place_number(num, pos1, pos2):
    global board
    if num == one:
        board[pos2, pos1] = 1
    if num == two:
        board[pos2, pos1] = 2
    if num == three:
        board[pos2, pos1] = 3
    if num == four:
        board[pos2, pos1] = 4
    if num == five:
        board[pos2, pos1] = 5
    if num == six:
        board[pos2, pos1] = 6
    if num == seven:
        board[pos2, pos1] = 7
    if num == eight:
        board[pos2, pos1] = 8
    if num == nine:
        board[pos2, pos1] = 9
  •  Tags:  
  • Related