diff --git a/notes.md b/notes.md index 9c0b4a8..6d34917 100644 --- a/notes.md +++ b/notes.md @@ -72,6 +72,8 @@ def __init__(self, name): The init action sets up the player's ability to intera action = int(prompt("> ", type=choices, show_choices=False)) This uses "int" to assign integers to the spaces in the tic tac toe game, and shows the choices in the form of numerical spaces (choices) and X and Os (moves already played/invalid options). ## Checkpoint 2 Notes +A simple way to check and see if ther eis a winner is to list all of the options for a win, then let the program check threm. It would iterate through a list of combinations and check one by one. +So, I started by drawing the board with the 1-8 integers. Then listed the options based off the wins available. I started by organizing the, into the three ways to win, rows, columns, then diagonals to ensure that I didn't miss any. After this I continued with the video and saw that you had them same idea! That was really exciting to reaize, and reassuring that my thought process was making sense. I used the video to help write the remaining code for the combos and listing them, then iterating through them and checking for a winning move. ### TTT Strategy diff --git a/ttt/game.py b/ttt/game.py index 2f0e302..d6dfe09 100644 --- a/ttt/game.py +++ b/ttt/game.py @@ -58,4 +58,24 @@ class TTTGame: def check_winner(self, state, symbol): "Checks whether the player with `symbol` has won the game." + combo = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8], + [0, 3, 6], + [1, 4, 7], + [2, 5, 8], + [0, 4, 8], + [2, 4, 6], + ] + for combo in combos: + if self.check_winning_combo(state, symbol, combo): + return True return False + def check_winning_combo(self, state, symbol, combo): + "Checks to see if the X's and O'x are in a row, column, or diagonal- to create a winning three in a row." + + for index in combo: + if state["board"][index] != | symbol: + return False + return True