0

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()
  • I would suggest to listen to a mouse event. Then check the position of the mouse click to get the clicked square of the board. Promt the user to input a number for that square. Then update your board with the given number (maybe add a plausibility check beforehand). That should point you towards the right direction, I can not write the whole code for you ;-) Try yourself and come back if you have specific questions. – Sparkofska Mar 25 '21 at 14:10
  • I was able to listen for the click event but I don't know how to determine what number is present at that location – Jeff24215 Mar 25 '21 at 16:27
  • In the line `screen.blit(...)` you are calculating the position of the text dependent on `row`, `col` and `offset`. The mouse event should give you a position. By inverting the calculation you can find `col` and `row` given the mouse postition. Now use `clicked_number = unsolved_board[row][col]` to find the number. Post your code in another question if you have problems working that out – Sparkofska Mar 26 '21 at 06:19

0 Answers0