Forum Archive

Sudoku crashes my ipad!

amharder

Hello everyone, I've been told that the reason for these crashes is how much data is being processed. Here is my code:

import random
class game(object):
    def __init__(self):
        self.found = False
        self.board = list([[0] for i in range(9)] for i in range(9))
        for column in range(0,9):
            for row in range(0,9):
                run = True
                while run:
                    x = random.randint(1,9)
                    n = column
                    o = x
                    p = row
                    self.found = False
                    if x not in self.board[column]:
                        for y in range(9):
                            self.check = True
                            if y != column:
                                if self.board[y][row] == x:
                                    self.check = False
                    else:
                        self.check = False
                    if self.check == True:
                        self.board[column][row] = x
                        run = False
        print(self.board)
game()

What do you think? How can I fix this to make a working sudoku generator. If you also know how to make the 3 by 3 squares, that would help out too. Thank you.

cvp

No crash for me

Output:
[[3, 2, 9, 6, 1, 5, 7, 4, 8], [7, 9, 8, 5, 1, 3, 2, 6, 4], [2, 6, 3, 1, 4, 7, 9, 8, 5], [2, 8, 9, 4, 3, 5, 6, 7, 1], [9, 3, 8, 1, 4, 5, 6, 2, 7], [8, 6, 3, 4, 9, 7, 5, 1, 2], [8, 7, 1, 4, 2, 6, 5, 3, 9], [3, 8, 6, 1, 7, 5, 9, 4, 2], [9, 2, 8, 5, 6, 1, 3, 7, 4]]

amharder

Sorry I didn't explain more, it was crashing, and I tried to fix the code, now it doesnt crash, but there are the same numbers horizontal to each other, contrary to rules of sudoku. If you need more info please write back, but if you can help please help!

lukaskollmer

Can you post the code that crashed?

ccc

Check out https://forum.omz-software.com/topic/3050/snapsudoku Norvig's random_puzzle() might be what you are looking for.

The part about sudoku as a denial of service attack on the human intellect is a favorite of mine.

amharder

WARNING: This will crash your pythonista on execution. But if you might know why that would be helpful.

from scene import *
import random
import numpy as np
class game(Scene):
    def setup(self):
        self.found = False
        self.board = list([[0] for i in range(9)] for i in range(9))
        for column in range(0,9):
            for row in range(0,9):
                run = True
                while run:
                    x = random.randint(1,9)
                    self.find(column,x,row)
                    if self.found == False:
                        self.board[column][row] = x
                        run = False
        print(self.board)
    def find(self,list,arg,orig):
        n = list
        o = arg
        p = orig
        self.found = False
        if o not in self.board[n]:
            for y in range(9):
                if y != n:
                    if self.board[y][p] == o:
                        self.found = True
        else:
            self.found = True       

run(game())


ccc

I suspect that you have an infinite loop. If you comment out the fourth last line and replace it with a line that just says pass then you will not get caught in that loop.

You should avoid using list as the name of a parameter in find() because list is the name of a builtin datatype. I don't think this is a problem here but it could bite you in similar code.

Another recommendation would be to have find() return True or False. You could then eliminate the self.found variable and make your code easier to follow.

amharder

@ccc Thank you very much!

ccc

https://github.com/cclauss/Ten-lines-or-less/blob/master/sudoku_print.py and https://github.com/cclauss/Ten-lines-or-less/blob/master/sudoku_revisited.py