Welcome to part 11 of our Python 3 basics series, in this part we're going to continue working on validating the winners of TicTacToe, specifically on vertical wins. Code up to this point (just for verifying winners):
game = [[1, 1, 1], [2, 2, 0], [1, 2, 0]] def win(current_game): for row in game: print(row) if row.count(row[0]) == len(row) and row[0] != 0: print(f"Player {row[0]} is the winner!") win(game)
For now, let's just consider how we might validate vertical rows, so I am going to just start with just the game. Let's say our game is:
game = [[1, 0, 1], [1, 2, 0], [1, 2, 0]]
How might we validate the vertical win here? We can check my the row's index. For example:
game = [[1, 0, 1], [1, 2, 0], [1, 2, 0]] for row in game: print(row[0])
1 1 1 >>>
Now, if all of these are equal, we know we have a winner, and then we just need to validate those per index that's available. We can append
them to some sort of list, like so:
check = [] for row in game: print(row) check.append(row[0])
With a list, being mutable, you can add new elements to it with the .append() method. This will add the new elements to the end of the list.
Then, just use the same validation we did before!
if check.count(check[0]) == len(check) and check[0] != 0: print("Winner!")
game = [[1, 0, 1], [1, 2, 0], [1, 2, 0]] check = [] for row in game: print(row) check.append(row[0]) if check.count(check[0]) == len(check) and check[0] != 0: print("Winner!")
[1, 0, 1] [1, 2, 0] [1, 2, 0] Winner! >>>
So... of course we could get absurd and do:
for row in game: check.append(row[0]) if check.count(check[0]) == len(check) and check[0] != 0: print("Winner!") check = [] for row in game: check.append(row[1]) if check.count(check[0]) == len(check) and check[0] != 0: print("Winner!") check = [] for row in game: check.append(row[2]) if check.count(check[0]) == len(check) and check[0] != 0: print("Winner!")
But yeah, repetition and scalability! We can break this down further if we know the columns in advanced, right? Something like:
game = [[1, 0, 1], [0, 2, 1], [1, 2, 1]] columns = [0, 1, 2] for col in columns: check = [] for row in game: check.append(row[col]) if check.count(check[0]) == len(check) and check[0] != 0: print("Winner!")
Now we just cant make the game any larger, or we'd have to modify our columns variable. We can handle for this in a variety of ways. One option is to just get the size of the first row, which would be the number of columns, using the built-in function: len()
, which returns the length of a thing:
print(len(game[0]))
If you didn't know length existed, you could have 1: Googled it. 2: Created a counter and did +=1 for each item in the list to determine the length.
Now that we know the length, we need to check that many indecies. We could use a counter again and stop at the length, or...use another built-in function! range()
. While range() really is listed in the built-in functions, and we can treat it like one, it isn't actually a function. You can learn more about range() here.
Let's see an example of using range(3):
for i in range(3): print(i)
0 1 2 >>>
Perfect, that's all we need! So we can actually just do:
for col in range(len(game[0])): check = [] for row in game: check.append(row[col]) if check.count(check[0]) == len(check) and check[0] != 0: print("Winner!")
Winner! >>>
Great, done! Now our full winning check code is:
game = [[1, 2, 1], [0, 2, 2], [1, 2, 0]] def win(current_game): for row in game: print(row) if row.count(row[0]) == len(row) and row[0] != 0: print(f"Player {row[0]} is the winner horizontally!") for col in range(len(game[0])): check = [] for row in game: check.append(row[col]) if check.count(check[0]) == len(check) and check[0] != 0: print(f"Player {check[0]} is the winner vertically!") win(game)
Go ahead and check it now, with some horizontal and vertical winnings. Make sure it's all good. I have been known to make mistakes!
Alright, now we're ready to move on to the diagonals. Slightly more challenging, but we can figure it out!