From 2d0edd179898f0b28ee73a57e60f3c3519d2cdcc Mon Sep 17 00:00:00 2001 From: kated Date: Mon, 1 Jun 2026 10:55:36 -0400 Subject: [PATCH] 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) --- .envrc | 1 + notes.md | 8 ++++++++ poetry.lock | 34 ++++++++++++++++++++++++++++++++++ ttt/game.py | 7 +++++++ 4 files changed, 50 insertions(+) create mode 100644 .envrc create mode 100644 poetry.lock diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4a96c22 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +source .venv/bin/activate \ No newline at end of file diff --git a/notes.md b/notes.md index 67cc9f6..9d406a0 100644 --- a/notes.md +++ b/notes.md @@ -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 diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 0000000..03bfb49 --- /dev/null +++ b/poetry.lock @@ -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" diff --git a/ttt/game.py b/ttt/game.py index 2f0e302..7fb565f 100644 --- a/ttt/game.py +++ b/ttt/game.py @@ -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