Python Forum
Naughts and Crosses win check
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Naughts and Crosses win check
#1
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!
Reply
#2
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.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020