3.9 KiB
Tic Tac Toe notes
Kathryn Odell-Hamilton
Checkpoint 1 Notes
Which class is responsible for each of the following behaviors? For each, explain how the behavior is accomplished.
Checking to see whether the game is over
The Class is class TTTGame: "Models a tic-tac-toe game."
def is_over(self, state): "Checks whether the game is over."
Determining which actions are available at a particular state
The Class is class TTTGame: "Models a tic-tac-toe game."
def get_actions(self, state): "Returns a list of the indices of empty spaces" return [index for index in range(9) if state["board"][index] == '-']
Can it also be considered this in addition?
def get_next_state(self, state, action): """Given a state and an action, returns the resulting state. In the resulting state, the current player's symbol has been placed in an empty board space, and it is the opposite player's turn.""" new_board = state["board"].copy() new_board[action] = 'X' if state["player_x"] else 'O' return { "board": new_board, "player_x": not state["player_x"], }
Showing the board
The Class is class TTTView:
def get_initial_state(self): "Returns the game's initial state." "Represents an empty tic-tac-toe board where player X starts the game.” return { "board": ['-', '-', '-', '-', '-', '-', '-', '-', '-'], "player_x": True, }
Choosing which action to play on a turn
The Class is class TTTView:
def get_next_state(self, state, action): """Given a state and an action, returns the resulting state. In the resulting state, the current player's symbol has been placed in an empty board space, and it is the opposite player's turn. """ new_board = state["board"].copy() new_board[action] = 'X' if state["player_x"] else 'O' return { "board": new_board, "player_x": not state["player_x"], }
Checkpoint 2 Notes
TTT Strategy
For each of the following board states, if you are playing as X and it's your turn, which action would you take? Why?
| O | O | | O | X | X | O |
---+---+--- ---+---+--- ---+---+--- ---+---+---
X | X | | X | X | O | O | |
---+---+--- ---+---+--- ---+---+--- ---+---+---
| | | | O | | | |
First Game: I would choose “X” is middle row, last right column. It’s the most obvious choice to get “3 X’s” in a row.
Second Game: I would choose “X” in 3rd column, 2nd row. I’m going to block “O” from getting a vertical 3 in a row in 3rd column.
Third Game: I would choose “X” in 1st column, 1st row. “O” will lose the game, unable to block “X” from winning. Whether “O” chooses either 3rd column/1st row or 1st column/3rd row. “X” will choose the other empty spot to win.
Fourth Game: I would choose “X” in very middle of game board, 2nd column/2nd row. No matter how “0” tries to block 3 in a row, “X” will win. “X” will have the advantage of 3 in a row vertical, diagonal, or horizontal.
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?
Begins in file: play_tt.py “state = game.get_initial_state()”
This is returning the games initial state
Next in file: game.py. “def get_initial_state(self):”
Is returning the game's initial state with the Tic Tac Toe board ready for the 1st player to play the initial round. Within “next state” there is a Boolean of True to see if it’s “X’s” turn and if not it’s “0’s” turn to play. The future reward for the state is that the game proceeds and updates until the all spots have been used by both player with an outcome of a winner or not.