I added a new method that would check for winning

combinations in the tic tac toe game, then completed the existing
method for checking the winner.

I outlined my strategy a little in the notes section as well, but like a lof of my code,
I plan on paper. It remainded me of the same thing I tell my students when they are writing an essay.
Even if it seems obvious, you have to tell the reader (or in theis case the computer) what you
mean. Or it will fill in the gaps based on what you already said, and that is how things get
misconstrude. I started by writing the ways that the game can end.

1. The board can be full and there is no winner.
2. X can will by getting three in a row; row, column, and diagonal.
3. 0 can win by getting three in a row; row, column, and diagonal.

The I elimated the options that are alread accounted for. We have a solution for when the
board is completely full, so there needs to be options to end the game when a winner actually
wins.

So, I listed the ways that you can win, the integer combos that are options for a win. I listed them
in order by the direction of the win. Row then column, then diagonal. Finally, I listed the steps that
I would need to add this to the code.

1. I needed to adjust the def_winner method to return something other than just false.
2. To make it able to tell True from False, I needed to tell it what was true; so I also would
have to list the combos that would win.
3. I need a new method that will check the board against the index provided in the def_winner method.
This commit is contained in:
Rebecca Hankey 2024-11-23 14:09:33 -05:00
parent 77aab6c085
commit ec7b133190
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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