Add threads to a Sudoku checker
<p>I have just created a simple function in Python which checks whether or not a Sudoku board (input is given as a list) is valid or not. The way I did it is pretty straight forward:</p>

<ul>
<li><p>check if the sudoku board is 9x9</p></li>
<li><p>check that each number appears only once per row</p></li>
<li>check that each number appears only once per column</li>
<li>check that each number appears exactly once per 3x3 grid </li>
</ul>

<p>Now, once I started this, I wanted to take advantage and learn a bit about Python threads. I read the docs, and also some awesome general multithreading related posts here on SO, but I just couldn't think of a way of implementing them in my checker.</p>

<p>Now, the way I'd like the threads works (as I thought) is one thread to check that each column contains 1-9 digits, another one to check the lines for the same thing, and another nine threads to check each 3x3 sub-grid. Could you guys please tell me (eventually with some explanations) how I could achieve this ? Thanks</p>

<p>So to give some general pointers on how to achieve this, without taking away any challenge. Lets start by import Threading:</p>

<pre><code>import threading
</code></pre>

<p>Which will let us use thread objects! Also, in order to know if the Sudoku grid will be valid after the fact, we need a variable to store the True/False condition in. You can either go for a single variable, and use Thread Locks to ensure no other threads access it at the same time, or go with three separate. For simplicity, I'll use three separate variables in this example</p>

<pre><code>LinesValid = False
ColumnsValid = False
GridsValid = False
</code></pre>

<p>Then, since threads require a function or another callable to run as their target, and you desire a thread for columns, rows and for each 3x3 grid, we need three functions for each thread. However, since there are 9 columns, 9 rows and also 9 grids I believe it would be a lot better to just do a single thread for the grids as well, but for the purpose of the exercise I suppose it is fine to do one for each.</p>

<pre><code>def CheckLines():
    # Line Checking Code

def CheckColumns():
    # ColumnCheckingCode

def CheckGrid(UpperLeft, BottomRight):
    # GridCheckingCode
</code></pre>

<p>Anyway, here we define our three functions, with their appropriate line checking code. Like the lines would check the X axis, and the Columns the Y axis, but the idea is to split it up. For the CheckGrid you would need to specify corners if you want it to have a thread for each tile, or if you decide on a single thread you would just define it as:</p>

<pre><code>def CheckGrid():
    # GridCheckingCode
</code></pre>

<p>After that we need to make our Threads:</p>

<pre><code>LineThread = threading.Thread(target=CheckLines)
ColumnThread = threading.Thread(target=CheckLines)
GridThread = threading.Thread(target=CheckLines, args=([0, 0], [2, 2]))
</code></pre>

<p>You can ignore the arguments from the GridThread if you do not need the grids. Otherwise you need to specify corners, and find a way to loop through the specified tiles.</p>

<p>After that it is a matter of starting the threads, and joining them with the main thread, before showing the data to the user:</p>

<pre><code>LineThread.start()
ColumnThread.start()
GridThread.start()

LineThread.join()
ColumnThread.join()
GridThread.join()

if sum(LinesValid, ColumnsValid, GridsValid) == 3:
    print("The grid is valid!")
else:
    print("The grid is invalid!")
</code></pre>

<p>Here we check if all our Bools are True: <code>( 1 + 1 + 1 ) == 3</code> if they are, and print data to the user based on this. These bools would be set to True/False within their respective Check** functions!</p>

<p>If you desire some more direct solutions, rather than a general direction with explanations, please let me know and I'll throw something together! The final piece of code looks something like this:</p>

<pre><code>import threading

def CheckLines(self):
    # Line Checking Code

def CheckColumns(self):
    # ColumnCheckingCode

def CheckGrid(self, UpperLeft, BottomRight):
    # GridCheckingCode

LinesValid = False
ColumnsValid = False
GridsValid = False

LineThread = threading.Thread(target=CheckLines)
ColumnThread = threading.Thread(target=CheckLines)
GridThread = threading.Thread(target=CheckLines, args=([0, 0], [2, 2]))

LineThread.start()
ColumnThread.start()
GridThread.start()

LineThread.join()
ColumnThread.join()
GridThread.join()

if sum(LinesValid, ColumnsValid, GridsValid) == 3:
    print("The grid is valid!")
else:
    print("The grid is invalid!")
</code></pre>

python multithreading python-3.x