I have created a sudoku solver in python using pygame. Currently this solves the board when the user hits the spacebar. I want to upgrade the game so the user can input numbers onto the board. I am not sure how to get the input and update the board display. Also are there any other features I should add to this?
unsolved_board = [
[8, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 3, 6, 0, 0, 0, 0, 0],
[0, 7, 0, 0, 9, 0, 2, 0, 0],
[0, 5, 0, 0, 0, 7, 0, 0, 0],
[0, 0, 0, 0, 4, 5, 7, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 3, 0],
[0, 0, 1, 0, 0, 0, 0, 6, 8],
[0, 0, 8, 5, 0, 0, 0, 1, 0],
[0, 9, 0, 0, 0, 0, 4, 0, 0]
]
def draw_numbers():
row = 0
offset = 37
while row < 9:
col = 0
while col < 9:
output = unsolved_board[row][col]
n_text = font.render(str(output), True, pg.Color("black"))
screen.blit(n_text, pg.Vector2((col*80)+offset, (row*80)+offset))
col += 1
row += 1
def draw_background():
screen.fill(pg.Color("white"))
pg.draw.rect(screen, pg.Color("black"), pg.Rect(15, 15, 720, 720), 10)
i = 1
while (i*80) < 720:
if i%3 == 0:
width = 7
else:
width = 3
pg.draw.line(screen, pg.Color("black"), pg.Vector2((i*80)+15, 15), pg.Vector2((i*80)+15, 735), width)
pg.draw.line(screen, pg.Color("black"), pg.Vector2(15, (i*80)+15), pg.Vector2(735, (i*80)+15), width)
i+=1
def game_loop():
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
new_board = solve_sudoku(unsolved_board)
if event.type == pg.MOUSEBUTTONUP:
pos = pg.mouse.get_pos()
print(pos)
draw_background()
draw_numbers()
pg.display.flip()
def main():
clock = pg.time.Clock()
while True:
clock.tick(FPS)
game_loop()
if __name__ == "__main__":
main()