I was wondering how to solve this Sudoku board in Python using Human input and Computer solving. I have a Human and Computer solve def. I tried using rows and columns to solve and it just wouldn't print the results on the board every time it would work. I tried doing these using things such as for 'i in range'. But when I would run the program it would be working but not printing the results each time it was solving it. Thank you!
import time, sys, random
def print_board(sudoku):
for i in range(9):
print(sudoku[i][0:3],'|',sudoku[i][3:6],'|',sudoku[i][6:9])
if i==5 or i==2:
print('-'*51)
if __name__ == '__main__':
# Don't change the layout of the following sudoku examples
sudoku1 = [
[' ', '1', '5', '4', '7', ' ', '2', '6', '9'],
[' ', '4', '2', '3', '5', '6', '7', ' ', '8'],
[' ', '8', '6', ' ', ' ', ' ', ' ', '3', ' '],
['2', ' ', '3', '7', '8', ' ', ' ', ' ', ' '],
[' ', ' ', '7', ' ', ' ', ' ', ' ', '9', ' '],
['4', ' ', ' ', ' ', '6', '1', ' ', ' ', '2'],
['6', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', '4', ' ', ' ', ' ', '1', ' ', '7'],
[' ', ' ', ' ', ' ', '3', '7', '9', '4', ' '],
]
sudoku2 = [
[' ', ' ', ' ', '3', ' ', ' ', ' ', '7', ' '],
['7', '3', '4', ' ', '8', ' ', '1', '6', '2'],
['2', ' ', ' ', ' ', ' ', ' ', ' ', '3', '8'],
['5', '6', '8', ' ', ' ', '4', ' ', '1', ' '],
[' ', ' ', '2', '1', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', '7', '8', ' ', ' ', '2', '5', '4'],
[' ', '7', ' ', ' ', ' ', '2', '8', '9', ' '],
[' ', '5', '1', '4', ' ', ' ', '7', '2', '6'],
['9', ' ', '6', ' ', ' ', ' ', ' ', '4', '5'],
]
sudoku3 = [
['8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', '3', '6', ' ', ' ', ' ', ' ', ' '],
[' ', '7', ' ', ' ', '9', ' ', '2', ' ', ' '],
[' ', '5', ' ', ' ', ' ', '7', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', '4', '5', '7', ' ', ' '],
[' ', ' ', ' ', '1', ' ', ' ', ' ', '3', ' '],
[' ', ' ', '1', ' ', ' ', ' ', ' ', '6', '8'],
[' ', ' ', '8', '5', ' ', ' ', ' ', '1', ' '],
[' ', '9', ' ', ' ', ' ', ' ', '4', ' ', ' '],
]
sudoku4 = [
[' ', '4', '1', ' ', ' ', '8', ' ', ' ', ' '],
['3', ' ', '6', '2', '4', '9', ' ', '8', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', '7', ' '],
[' ', ' ', ' ', '4', '7', ' ', '2', '1', ' '],
['7', ' ', ' ', '3', ' ', ' ', '4', ' ', '6'],
[' ', '2', ' ', ' ', ' ', ' ', ' ', '5', '3'],
[' ', ' ', '7', ' ', '9', ' ', '5', ' ', ' '],
[' ', ' ', '3', ' ', '2', ' ', ' ', ' ', ' '],
[' ', '5', '4', ' ', '6', '3', ' ', ' ', ' '],
]
option = 2
if option == 1:
sudoku = sudoku1
elif option == 2:
sudoku = sudoku2
elif option == 3:
sudoku = sudoku3
elif option == 4:
sudoku = sudoku4
else:
raise ValueError('Invalid choice!')
# Player Types | Computer or Player depending on the input below!
def Human_play():
print('The Human Plays.')
def Computer_play():
print('The Computer Plays')
# Chooses Player Type
Player_Type = ''
while Player_Type != 'Human' or 'Computer':
Player_Type = input('"Human" or "Computer" solve?: ')
if Player_Type == 'Human' or 'human' or 'HUMAN':
Human_play()
print_board(sudoku)
break
elif Player_Type == 'Computer' or 'computer' or 'COMPUTER':
Computer_play()
print_board(sudoku)
break
else:
print('Invalid Player Type!\nMake sure you use "Human" or "Computer"')