wrote the function check_winner in game.py so that the game can check whether player X or player O scoring three-in-a-row in any possible arrangement is true (which would lead to that player winning)

This commit is contained in:
kated
2026-06-01 10:55:36 -04:00
parent 57e83744c8
commit 2d0edd1798
4 changed files with 50 additions and 0 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
source .venv/bin/activate

View File

@@ -7,12 +7,20 @@ For each, explain how the behavior is accomplished.
### Checking to see whether the game is over
TTTGame has a method called is_over which checks if either the board is full, player x won, or player o won is true.
### Determining which actions are available at a particular state
TTTGame has a method called get_actions which returns a list of numbers of the empty spaces that a player could choose in their next turn
### Showing the board
TTTView has a method print_board which prints the board with its currently available spaces
### Choosing which action to play on a turn
TTTView has a method get_action which shows the board and asks the current player for which number box they want to choose (in which that player must type in the number of their choice)
## Checkpoint 2 Notes

34
poetry.lock generated Normal file
View File

@@ -0,0 +1,34 @@
# This file is automatically @generated by Poetry 2.3.1 and should not be changed by hand.
[[package]]
name = "click"
version = "8.3.1"
description = "Composable command line interface toolkit"
optional = false
python-versions = ">=3.10"
groups = ["main"]
files = [
{file = "click-8.3.1-py3-none-any.whl", hash = "sha256:981153a64e25f12d547d3426c367a4857371575ee7ad18df2a6183ab0545b2a6"},
{file = "click-8.3.1.tar.gz", hash = "sha256:12ff4785d337a1bb490bb7e9c2b1ee5da3112e94a8622f26a6c77f5d2fc6842a"},
]
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "colorama"
version = "0.4.6"
description = "Cross-platform colored terminal text."
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
groups = ["main"]
markers = "platform_system == \"Windows\""
files = [
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
]
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<4.0"
content-hash = "0272075b8c7e01c3558d126d3efff1c07b71bcde638baf2353e2f48fa2bf5db5"

View File

@@ -58,4 +58,11 @@ class TTTGame:
def check_winner(self, state, symbol):
"Checks whether the player with `symbol` has won the game."
winning_combos = [(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 winning_combos:
#The syntax for refering to an element in the boards state is state["board"][index#] - in our case, our index# is the first item in the combo we are looking at for this iteration.
if state["board"][combo[0]] == symbol:
#check if the elements in the position on the board for each position in this combination are all equal to eachother
if state["board"][combo[0]] == state["board"][combo[1]] and state["board"][combo[0]] == state["board"][combo[2]]:
return True
return False