#🔒 Unable to animate the change

169 messages · Page 1 of 1 (latest)

regal oasis
#

I am trying to make a soduko game complete in all its glory with different sizes and solver but right not the pygame part along with soduko.py are somewhat hardcoded for size=9

Main Problem:
● I was trying to animate the solver i.e display the changes it makes tho i am unable and have been stuck at this problem for well over 40 minutes.

● The soduko.py contains two classes i.e the Generator which generates the soduko itself and the Solver class which is a copy of the prior one with some essential changes i thought were necessary to animate the solving process.

Code:
https://paste.pythondiscord.com/SCAQ

cerulean duneBOT
#

@regal oasis

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

regal oasis
#

Have i not explained the problem correctly?

rough lake
#

Not really. You’ve basically said, “here’s a lot of code. Something is wrong.” You need to narrow down the problem and say “I think here is where something is not working correctly.”

spare heart
#

When I wrote a sudoku creator/solver, I used Numpy arrays. 9x9x9 boolean. I ended up using tkinter, but PyGame would do.

regal oasis
#

Problem:

● About Solver class:
-> Its supposed to solve the grid and return it everytime a change is made into it.

Thats what it was supposed to do

● Problem:
-> when solver.solve() is called in th while loop tje grid is solved in an instance which is exactly what i dont want .

-> its obvious i have to modify the solve() function but also keep it recursive so it solves correctly but i tried changing it but it got progressively worse and worse and i dont feel lile using ai to cheat so please guide me

spare heart
#

Would writing a stack help you manage things better, vs recursive calls?

regal oasis
spare heart
#

I'm not fond of tkinter, either, its primary advantage being that it's a standard library module, thus readily available.

#

Using AI is liable to get you deeper in trouble.

regal oasis
spare heart
#

Eventually, you'll find a solution if a solution exists.

regal oasis
spare heart
#

Yes. The removing and discarding states with no legal moves is the backtracking.

regal oasis
regal oasis
spare heart
#

Each thing added to the stack is a whole state.

#

So, fill in a number? That's a new state added.

#

Something to be built upon later when that state gets popped from the stack.

#

All you need to do is write a routine to display a state.

#

Reading the data and writing the numbers at the positions.

regal oasis
#

So lets say the function puts a num in grid[x][y] and its wrong then it should access the last grid&change made while also removing the current one and change the num entered in the last state?
So in theory i can get away by just storing last change?

spare heart
#

Fair warning: My solution only worked some of the time and I could never figure out quite what I was doing wrong at the time.

#

It would usually work.

#

It should have always worked, in theory, for legal starting states.

regal oasis
spare heart
#

Who knows?

#

Maybe the starting states were subtly illegal.

regal oasis
#

hm.... for me it was checking if a num exists in a 3x3 box was hard which was causing the grid generated to be wrong hence illegal positions when removing_n digits

#

mb it was the onr for u too

spare heart
regal oasis
spare heart
#

!e py import numpy as np arr = np.ones((9, 9, 9), bool) print(arr)This was my representation of a blank grid. The first two dimensions being the 9x9 grid, the rows of Trues as the last dimension representing the remaining possible values from 0-9.

regal oasis
cerulean duneBOT
# spare heart !e ```py import numpy as np arr = np.ones((9, 9, 9), bool) print(arr)```This was...

:white_check_mark: Your 3.14 eval job has completed with return code 0.

001 | [[[ True  True  True  True  True  True  True  True  True]
002 |   [ True  True  True  True  True  True  True  True  True]
003 |   [ True  True  True  True  True  True  True  True  True]
004 |   [ True  True  True  True  True  True  True  True  True]
005 |   [ True  True  True  True  True  True  True  True  True]
006 |   [ True  True  True  True  True  True  True  True  True]
007 |   [ True  True  True  True  True  True  True  True  True]
008 |   [ True  True  True  True  True  True  True  True  True]
009 |   [ True  True  True  True  True  True  True  True  True]]
010 | 
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/HLQF5MFOGKI3X3THWJSNKNKTIU

spare heart
#

As I "confirmed" a value for that line of inquiry in a state, I'd knock out all the remaining Trues to Falses except for that one True.

#

I would then also knock out the other Trues that would imply in the same state.

#

So in that row, column, and 3x3.

#

If at any point I had no Trues left in a row or column or 3x3, that was a dead end and discarded.

#

If that makes any sense.

#

Each state was a map of remaining potential values.

regal oasis
#

I dont mean to be judgy but the approach you took, sounds so confusing to me

First of all , how did u kept track of what num was their at place of a True or False?

spare heart
#

As potentials were confirmed, that would knock out other possibilities.

#

True, False, False, False, False, False, False, False, False was 1 at that position.

#

But they all started off as True.

#

A blank square.

#

As numbers get filled in, the possibilities of other positions are eliminated.

spare heart
#

When only one True remains, that's that square's value, assuming it doesn't get backtracked for some other reason.

#

Sorry.

#

123
456
789 Imagine a 9 x 9 replication of this.

regal oasis
# spare heart Sorry.

No problem man, ur approach was for you i am not that genius and its not that i am

regal oasis
#

So thats how u get the third 9 in np.ones((9,9,9), bool)?

spare heart
#

Yes.

regal oasis
#

Ahh.... i see what you mean now

#

So technically all the nums are prefilled in some sense then you just go round eliminating what u can, cool

spare heart
#

As you go, if you prospect on a square having a value, you work on a copy of the state, cross out all numbers but that one, then cross out every other value that needs to get crossed out for the row, column and 3x3

#

Then if that state causes more values to be confirmed, then, still working on the copy, you cross out and potentially confirm other squares

#

and so on until it settles

#

Then you add that to the stack, because at that point you're back to prospective confirming of squares.

#

So if you wanted, you might have a set of {*range(1, 10)}

#

9x9

regal oasis
#

Hm.... to be fair thats a very unique approach me on the other hand just relied on making an empty canvas all positions init as 0s then 1st approach was going through all cells taking a random num filling and checking if that didnt violated any rules if it did changing it and moving then i learned backtracking was needed to generate a valid grid so i factored that in

spare heart
#

copy.deepcopy getting a workout.

#

Sometimes confirming squares will snowball

#

Confirming other squares

regal oasis
regal oasis
spare heart
#

But it shouldn't be random.

regal oasis
regal oasis
spare heart
#

If you print out solutions as you go, you can still discard them and keep working to find other solutions.

spare heart
#

When you backtrack...what do you try next if the thing you're backtracking didn't work?

#

The next legal thing along in a definite sequence.

regal oasis
regal oasis
spare heart
regal oasis
#

As each box can multiple nums to be filled based on starting stat but all are eliminated except one

spare heart
regal oasis
spare heart
regal oasis
#

Is their a way to cross-check the validity of the generator?

spare heart
#

Start with solved sudokus and knock out a bunch of numbers to be filled in.

#

That way you know there's at least one valid solution.

regal oasis
spare heart
#

The backtracking isn't about filling so much as discarding unsolvable states.

#

I'm not sure how to make a good generator without attempting to solve it, beyond a naive legal selection of numbers at positions.

#

But not all "legal" initial states can be solved legally, maybe.

#

The starting state might not break rules in and of itself, but the numbers they force you to use, and the numbers they force you to use and so on might not result in legal states.

regal oasis
#

Damn

#

Its just get more and more difficult/useless to make one

spare heart
#

It's not straightforward, no.

regal oasis
regal oasis
#

I dont understand this error:

Traceback (most recent call last):
  File "/storage/emulated/0/Sudoku/main.py", line 41, in <module>
    puzzle, solver_changes = solver.solve(changes=solver_changes)
    ^^^^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 2)

@spare heart

#

Solve function

def solve(self, changes):
        empty_cell = self.find_empty()
        
        if not empty_cell:
            return self.puzzle # Means grid is full
        
        row, col = empty_cell
        
        nums = list(range(1,self.size+1))
        random.shuffle(nums)
        
        for num in nums:
            if self.check_box(row, col, num) and self.check_row(row, num) and self.check_column(col, num):
                self.puzzle[row][col] = num
                return self.puzzle, [self.puzzle, [row,col,num]]
        
        '''
        If above return statement is executed means filling is correct else we need
        to reset the last changes
        '''
        
        
        last_row, last_col, last_num = changes[len(changes)-1][1]
        self.puzzle = changes[len(changes)-1][0]
        for num in nums:
            if num == last_num:
                continue # skip to next num
            if self.check_box(last_row, last_col, num) and self.check_row(last_row, num) and self.check_column(last_col, num):
                self.puzzle[last_row][last_col] = num
                return self.puzzle, [self.puzzle, [last_row, last_col, num]]
spare heart
#

!e py a, b, c = 1, 2, 3, 4

cerulean duneBOT
# spare heart !e ```py a, b, c = 1, 2, 3, 4```

:x: Your 3.14 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     a, b, c = 1, 2, 3, 4
004 |     ^^^^^^^
005 | ValueError: too many values to unpack (expected 3, got 4)
spare heart
#

!e py a, b, c = 1, 2, 3 print(a) print(b) print(c)

cerulean duneBOT
regal oasis
#

Fixed it

#

I had reduced the cells to be filled = 10
So it wasnt returning changes in the case all cells with 0s were filled

#

New error

Traceback (most recent call last):
  File "/storage/emulated/0/Sudoku/main.py", line 41, in <module>
    puzzle, solver_changes = solver.solve(changes=solver_changes)
                             ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/storage/emulated/0/Sudoku/soduko.py", line 119, in solve
    last_row, last_col, last_num = changes[len(changes)-1][1]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot unpack non-iterable int object
#

@spare heart

spare heart
#

!e py a, b = 1

cerulean duneBOT
# spare heart !e ```py a, b = 1```

:x: Your 3.14 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     a, b = 1
004 |     ^^^^
005 | TypeError: cannot unpack non-iterable int object
regal oasis
#

Hm... i tried try-except TypeError and then print(solver_changes)
It did have those 3 vals lrow, lcol, lnum

spare heart
#

119

regal oasis
#

Line 119:

last_row, last_col, last_num = changes[len(changes)-2][1]
spare heart
#

You're running this in on your phone? Well done. I hope you're not writing this on your phone. That would suuuuck.

regal oasis
#

Yes, i am running this on my phone

spare heart
regal oasis
#

Soo for some reasons the errors are popping occasionally

#

It solved this

spare heart
#

If it helps, indices may be negative.

regal oasis
spare heart
#

The error message is telling you that it doesn't.

regal oasis
regal oasis
spare heart
#

Are you printing that, or something else?

regal oasis
#

And also its not getting TypeError anymore instead its ValueError

regal oasis
#
try:
        puzzle, solver_changes = solver.solve(changes=solver_changes)
    except ValueError:
        print(solver_changes[1])
        break
spare heart
#

No, as in you are printing, that or something else.

#

Damn it...

#

um....

#

You are printing either that or printing something else.

#

Be sure you know which.

regal oasis
#
Traceback (most recent call last):
  File "/storage/emulated/0/Sudoku/main.py", line 43, in <module>
    sudoku_surface = GenSurface(surface_x,surface_y,size).make_surface(puzzle)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/storage/emulated/0/Sudoku/soduko.py", line 119, in solve
    last_row, last_col, last_num = changes[len(changes)-2][1]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: too many values to unpack (expected 3)
regal oasis
#

See its line 119 and the same index for change

#

Oh..fcm

#

Fck

spare heart
#

Mhm...

regal oasis
#

I am printibg wrong

#

Okay somehow its indexing the puzzle and not the rows,col,nums part

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

Oh.... see this

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

it doesnt have the second list containing last change

#

@opalmist

spare heart
#

I'm here. I just didn't have anything to say.

regal oasis
#

Same

regal oasis
#

Yessss

#

Its working

#

@spare heart

spare heart
#

Neato burrito.

regal oasis
#

You like it?

#

Now one last thing i guess

#

Thank you @spare heart

spare heart
#

I think if you've made it work reliably, good.

#

Try to trip it up.

regal oasis
#

Find errors u mean?

#

But look at its time complexity

regal oasis
#

Lol my implementation beat someone elses on internet and the other one is still solving it py_strong

#

@spare heart

cerulean duneBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.