1

Some colleagues have written a sudoku generator.

  • The generator creates a completely solved solution with a bruteforce method.
  • The sudoku is stored in a 9x9 numpy array.
  • Then, based on a difficulty level, some cells are set to zero.
  • Then a grid is drawn using pygame.
  • Finally, all the cells of the array with a value != 0 are filled into the grid.

Here's the part of the code where the grid is filled with the not removed values.

def placeNumbers(sudoku_array):
font = pygame.font.Font(None, 36)
for row in range(9):
    for col in range(9):
        if sudoku_array[row, col] != 0:
            integer = font.render(str(sudoku_array[row, col]), 1, black)
            display.blit(integer, (15 + col * 45, 10 + row * 45))
            pygame.display.flip()

Now we would like to make the emptied cells (the ones containing zeros) editable, i.e. convert them into text fields. We don't really know how to do this; any ideas?

It could look like that:

elif sudoku_array[row, col] == 0:
    #create editable text field
    #at every position where a zero occurs

And here's an example what the array looks like:

[[7 6 0 0 0 4 2 3 5]
 [0 4 8 0 3 5 0 7 6]
 [3 9 5 0 0 2 1 0 4]
 [1 2 0 5 0 0 3 6 7]
 [0 0 9 0 1 6 8 4 2]
 [6 0 3 4 2 0 0 9 1]
 [0 0 6 8 5 3 0 2 9]
 [8 0 2 0 6 9 4 1 0]
 [9 3 7 2 4 0 0 5 0]]

EDIT:

I found a solution which partially works. Using the forms.py from the pygame website (http://www.pygame.org/project-Pygame+Forms-1042-.html), I managed to add a text box into empty cells. However, there are some more problems:

  • I can write into the text box. But if I hit backspace, nothing happens (i.e. I can't delete numbers). A number (e.g. "2" or "3") is just written over the other numbers "below".
  • I can only get to the next text field by clicking the close button in the top right of the pygame window.

Here's the next part of the code:

if sudoku_array[row,col] == 0:
    f = Form(False)
    f.add_object('input', Input(' ', ' ', position = 'absolute', top= row*45, left = col * 45, input_bg_color=(0,0,0,0)))
    r = f.run(display)
neacal
  • 47
  • 7
  • PyGame has no widgets so you have to do everything on your own - draw, handle keys, etc. – furas Dec 01 '15 at 19:09
  • btw: you don't have to `flip` after every `blit` - do it only once after all `blit` – furas Dec 01 '15 at 19:12
  • Hello @furas, thanks for the answer. I found a partial solution and edited my question. Maybe you could have a look at it - that would be great! :-) – neacal Dec 01 '15 at 19:42
  • It seems you can create only one `Form`. Maybe you have to create one Form and put all Input (and other elements) in that one Form. – furas Dec 01 '15 at 19:57
  • Other GUI elements for PyGame - but not developed for a long time - http://www.pygame.org/wiki/gui – furas Dec 01 '15 at 19:59

0 Answers0