Python Forum

Full Version: Naughts and Crosses win check
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was wondering how I would make a Win checker for my Naughts and Crosses game (Tic Tac Toe).

I am using lists
Row1 = ["-","-","-"] 
Row2 = ["-","-","-"] 
Row3 = ["-","-","-"]

Loop = True #Loop
Turn = 0 #Flips between Player 1 and Player 2
Pinput = 0 #Player input

def check():
    print("checkerhere")
the def check(): is where I want the checker to be.
If any of you could point me in the right direction that would be great!
You only need to check if the last move created a win. So, assume last move was by X
Check to see if all in that row are X
Check to see if item in each row in that position are X (check columns)
If the position is a corner or the middle, check diagonals.
It's also easier if you use a list of lists:

board = [['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]
That way you can use incremental indexing to go through the rows, the columns, and the diagonals.
The 'check' function should return True or False regarding win.

Some ideas that might be useful: use set() or all() to determine whether there is 'all-spam' situation:

>>> m = ['spam spam spam'.split(), 'spam ham spam'.split()]
>>> m
[['spam', 'spam', 'spam'], ['spam', 'ham', 'spam']]
>>> for row in m:
...     print(set(row) == {'spam'})
... 
True
False
>>> for row in m:
...     print(all(mark == 'spam' for mark in row))
... 
True
False
To make it more useful any() could be added - to determine is there at least one row with 'all-spam':

>>> any(set(row) == {'spam'} for row in m)
True
>>> any(all(mark == 'spam' for mark in row) for row in m)
True