From adf951b53fe1f6e8039fa8ad933f704666273f57 Mon Sep 17 00:00:00 2001 From: Seoyeon Lee Date: Sun, 15 Dec 2024 16:51:49 -0500 Subject: [PATCH] I found a mistake in the check_winner() function. It was working only with three or less X or O signs. I have changed it to work for all combinations --- notes.md | 6 +++++- play_ttt.py | 2 +- ttt/game.py | 6 ++++-- ttt/player.py | 3 ++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/notes.md b/notes.md index edf3f94..972198a 100644 --- a/notes.md +++ b/notes.md @@ -36,9 +36,13 @@ and it's your turn, which action would you take? Why? ---+---+--- ---+---+--- ---+---+--- ---+---+--- | | | | O | | | | +6 for the first case becase I can win right away. +6 for the second case becase I need to stop the other player winning. +1 for the third case becase I have two possible winning senario with this choice. +5 for the fourth case becase the other player is forced to put 9, then I will the next turn. ### Initial game state You can get the inital game state using game.get_initial_state(). What is the current and future reward for this state? What does this mean? - +reward is zero, it means that no one can win unless someone make a mistake. diff --git a/play_ttt.py b/play_ttt.py index ef5530a..d1b6995 100644 --- a/play_ttt.py +++ b/play_ttt.py @@ -3,7 +3,7 @@ from ttt.view import TTTView from ttt.player import TTTHumanPlayer, TTTComputerPlayer player0 = TTTHumanPlayer("Player 1") -player1 = TTTHumanPlayer("Player 2") +player1 = TTTComputerPlayer("Player 2") game = TTTGame() view = TTTView(player0, player1) diff --git a/ttt/game.py b/ttt/game.py index 0d7149a..c1e08da 100644 --- a/ttt/game.py +++ b/ttt/game.py @@ -61,6 +61,8 @@ class TTTGame: winning_combinations=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]] board=state["board"] combination=[index for index in range(9) if board[index]==symbol] - if combination in winning_combinations: - return True + for combo in winning_combinations: + tot = sum([board[index] == symbol for index in combo]) + if tot == 3: + return True return False diff --git a/ttt/player.py b/ttt/player.py index bfbbe15..9939743 100644 --- a/ttt/player.py +++ b/ttt/player.py @@ -1,5 +1,6 @@ from click import Choice, prompt from strategy.random_strategy import RandomStrategy +from strategy.lookahead_strategy import LookaheadStrategy from ttt.game import TTTGame import random @@ -24,7 +25,7 @@ class TTTComputerPlayer: def __init__(self, name): "Sets up the player." self.name = name - self.strategy = RandomStrategy(TTTGame()) + self.strategy = LookaheadStrategy(TTTGame()) def choose_action(self, state): "Chooses a random move from the moves available."