Naughts and Crosses win check - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Naughts and Crosses win check (/thread-21682.html) |
Naughts and Crosses win check - GalaxyCoyote - Oct-09-2019 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! RE: Naughts and Crosses win check - jefsummers - Oct-10-2019 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. RE: Naughts and Crosses win check - ichabod801 - Oct-10-2019 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. RE: Naughts and Crosses win check - perfringo - Oct-10-2019 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 FalseTo 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 |