0

So I'm trying to make a sudoku game and it's coming along fairly well except at the state I'm at the program gives me an error for every move I try to make. isRow checks if a move is possible on the row and so on and isPossible combines the 3 to check if a move is possible or not. Can anyone tell me what is wrong with my verifications?

board = list(range(81)) # Creates a board with 9*9 elements

board2 = board # Duplicates this board for later verfications

def showLine(start, end): # Prints each line of the sudoku puzzle
    for i in range(start, end):
        if i < end-1:
            print(str(board[i]), '|', end =" ")
        else:
            print(str(board[i]))

def showGroup(start): # Prints each 9*3 group of regions (1/3 of the puzzle)
    for i in range(3):
        end = start + 9
        showLine(start, end)
        start += 9
    if start != 81:
        print('----------+-----------+-----------')

def show(): # Prints whole puzzle
    for i in range(0, 55, 27):
        showGroup(i)

rows = []
columns = []
groups = []

for i in range(0, 80, 9): # Cretes a list of rows
    rows.append(board[i:i+9])

for i in range(9): # Creates a list of columns
    columns.append(board[i::9])

temp = 0

for x in range(0, 61, 3): # Creates a list of groups (3*3 regions)
    group = []
    for i in range(3):
        for j in range(3):
            temp = x + 9*i + j
            group.append(temp)
    groups.append(group)

#Duplicating rows columns and groups for verifications
rows2 = rows
columns2 = columns
groups2 = groups

def isRow(cell, number): # Checking if an introduces number isn't repeating in the row
    x = 0
    for row in rows2:
        if cell in row:
            x = rows2.index(row)
        if number in rows[x]:
            return False
        else:
            return True

def isColumn(cell, number):): # Checking if an introduces number isn't repeating in the column
    x = 0
    for column in columns2:
        if cell in column:
            x = columns2.index(column)
        if number in columns[x]:
            return False
        else:
            return True

def isGroup(cell, number):): # Checking if an introduces number isn't repeating in the group
    x = 0
    for group in groups2:
        if cell in group:
            x = groups2.index(group)
        if number in groups[x]:
            return False
        else:
            return True

def isPossible(cell, number): # Combines the last 3 verifications
    if isRow(cell, number) and isColumn(cell, number) and isGroup(cell, number):
        return True
    else:
        return False

for i in board: # Fills the board with 0's to not interfere with the game
    board[i] = 0

while True:
    show()


    selected_cell = int(input("Please select an available cell: "))
    number = int(input("Please select a number for that cell: "))

    if isPossible(selected_cell, number):
        board[selected_cell] = number
    else:
        print("Error, please try again")
  • Your code is difficult to understand since you apparently do not follow the obvious Sudoku conventions and you have absolutely no comments in your code. Making your code more clear with comments will get more attention for your question. Please add good comments. Also, why does your board begin with the numbers `0` through `80`? Why does `rows` not actually hold your rows and `columns` only hold the first column repeated 9 times? Why do your printouts of the board show only zeros? And so on. – Rory Daulton Dec 01 '18 at 19:07
  • Done, added comments and fixed my mistakes. My problem still persists though. –  Dec 01 '18 at 19:33
  • You could already debug your code yourself and pin down which condition is making the total condition false. – trincot Dec 01 '18 at 19:33
  • Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) –  Dec 01 '18 at 19:34
  • And how should I go about doing that? –  Dec 01 '18 at 19:35
  • https://stackoverflow.com/questions/1623039/python-debugging-tips – trincot Dec 01 '18 at 19:37
  • Still can't figure our what's wrong. –  Dec 01 '18 at 19:45
  • Its not an error in the program but the error I introduced. –  Dec 01 '18 at 20:11
  • You are comparing your `number` with the index `x` in `rows` == `if number in rows[x]`. You should compare at the index `x` in `board` instead. – stovfl Dec 01 '18 at 20:20
  • Doesn't seem to make a difference, can you clarify what I need to change. –  Dec 01 '18 at 20:25

0 Answers0