generated from mwc/lab_tic_tac_toe
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
This commit is contained in:
parent
740872f7b6
commit
adf951b53f
6
notes.md
6
notes.md
|
@ -36,9 +36,13 @@ and it's your turn, which action would you take? Why?
|
||||||
---+---+--- ---+---+--- ---+---+--- ---+---+---
|
---+---+--- ---+---+--- ---+---+--- ---+---+---
|
||||||
| | | | O | | | |
|
| | | | 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
|
### Initial game state
|
||||||
|
|
||||||
You can get the inital game state using game.get_initial_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?
|
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.
|
||||||
|
|
|
@ -3,7 +3,7 @@ from ttt.view import TTTView
|
||||||
from ttt.player import TTTHumanPlayer, TTTComputerPlayer
|
from ttt.player import TTTHumanPlayer, TTTComputerPlayer
|
||||||
|
|
||||||
player0 = TTTHumanPlayer("Player 1")
|
player0 = TTTHumanPlayer("Player 1")
|
||||||
player1 = TTTHumanPlayer("Player 2")
|
player1 = TTTComputerPlayer("Player 2")
|
||||||
game = TTTGame()
|
game = TTTGame()
|
||||||
view = TTTView(player0, player1)
|
view = TTTView(player0, player1)
|
||||||
|
|
||||||
|
|
|
@ -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]]
|
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"]
|
board=state["board"]
|
||||||
combination=[index for index in range(9) if board[index]==symbol]
|
combination=[index for index in range(9) if board[index]==symbol]
|
||||||
if combination in winning_combinations:
|
for combo in winning_combinations:
|
||||||
return True
|
tot = sum([board[index] == symbol for index in combo])
|
||||||
|
if tot == 3:
|
||||||
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from click import Choice, prompt
|
from click import Choice, prompt
|
||||||
from strategy.random_strategy import RandomStrategy
|
from strategy.random_strategy import RandomStrategy
|
||||||
|
from strategy.lookahead_strategy import LookaheadStrategy
|
||||||
from ttt.game import TTTGame
|
from ttt.game import TTTGame
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -24,7 +25,7 @@ class TTTComputerPlayer:
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
"Sets up the player."
|
"Sets up the player."
|
||||||
self.name = name
|
self.name = name
|
||||||
self.strategy = RandomStrategy(TTTGame())
|
self.strategy = LookaheadStrategy(TTTGame())
|
||||||
|
|
||||||
def choose_action(self, state):
|
def choose_action(self, state):
|
||||||
"Chooses a random move from the moves available."
|
"Chooses a random move from the moves available."
|
||||||
|
|
Loading…
Reference in New Issue