8.1 KiB
Tic Tac Toe notes
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 responsible for checking to see if the game is over is TTTGame, specifially the method, def is_over. However, def check_winner should be playing a part in this as well.
def is_over(self, state): "Checks whether the game is over." (This Doctring explains what the following method does in the game itself) return self.board_is_full(state) or self.check_winner(state, 'X') or self.check_winner(state, 'O')- The following steps prompt the program to run through the three senairos in which the game can end. Either the game ends when the board is full (eturn self.board_is_full(state)). Or the game ends when a winner gets three in a row. (self.check_winner(state, 'X') and or self.check_winner(state, 'O')). this will establish the winner as either X or O. The code checks the state of the board (the boxes that are full and with what symbol), then categorizes the end of the game into one of these three senarios.
def check_winner should also be playing a role in determining the end of the game because the is_over method relies on check_winner to check if there is a winner (meaning one of the symbols has three in a row). At this point, there is not enough information in this method and therefore the only way the game will register a winner is if the board is full and it will report a draw.
Determining which actions are available at a particular state
The class that is responsible for the actions that are availble in a state is TTTGame specifically the following methods: def get_initial_state, def get_next_state, and def get_actions.
In order the determine the actions of any particular state, we need to define what the initial state of the game is. That is done by using def get_initial_state "Returns the game's initial state." return { "board": ['-', '-', '-', '-', '-', '-', '-', '-', '-'], (This outlines the board as it is with the dashes representing empty spaces) "player_x": True, get_next_state allows for the new board (state) to be copied and relfect a player's action. def get_next_state(self, state, action): new_board = state["board"].copy() new_board[action] = 'X' (If the action belonged to player X, then complete the move for x) if state["player_x"] else 'O' return { (If the action was player o, then refelct player 0). "board": new_board, "player_x": not state["player_x"],
Next the class uses the method def get_actions. 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] == '-']- This sets up an index to iterate through that establishes the spaces on the board. There are 8 space in total, so the range is 9, meaning 1-8. If the state of the board has a number (space) equal, this it is categorized as a "-" in the rest of the code. This identifies empty spaces and assigns them a number to make the players choice easy. they simply choose a number (an available action) in that particular state, then the game will fill in that space and adjust the state accordingly to reflect the move.
Showing the board
TTTView shows the board. The methods are broken down into sections; the greetings, tha actions, the board itself, and the conclusion. At the top of this page is the dialogue of the game. This (as stated in the video) makes it easier to edit and amed if the need presentes itself. To show the board specifically, the method def print_board is used.
def print_board(self, state): "Prints the current board, showing indices of available spaces" (docstring) print(self.format_row(state, [0, 1, 2])) print(self.divider) print(self.format_row(state, [3, 4, 5])) print(self.divider) print(self.format_row(state, [6, 7, 8])) print commmands the board to physically print the following items. Then the board is broken into rows and each row is defined. The top rown of the tic tac toe game has three square spaces and they are labeled 0,1, and 2. Then on to the next row; 3,4,and 5, and so on.
Choosing which action to play on a turn
There are two classes that determine which actions to play on a turn. In order to determine which one to use, we need to decide if the player is a computer or a human. The two classes are; class TTTHumanPlayer: and class TTTComputerPlayer:.
At this point in the coade the game is being played using only human players, according to play_ttt. See below.
player0 = TTTHumanPlayer("Player 1")
player1 = TTTHumanPlayer("Player 2")
So I will explain how the behavior for the human choice in action.
def init(self, name): The init action sets up the player's ability to interact with the game, I think? To be honest, the init methods confuse me a little. I know they create a new istance of the object. I think this is because it makes the new object one you can manipulate in the new context without distrupting the original.
"Sets up the player."
self.name = name The player's self gets a name.
self.game = TTTGame() The game's self gets a name.
def choose_action(self, state):
"Chooses an action by prompting the player for a choice."
actions = self.game.get_actions(state) This determines the actions that are availabe for the player to choose. It reads the actions from the current state of the game then reports the following informaiton.
choices = Choice([str(action) for action in actions]) This establishes a string of options for the actions.
action = int(prompt("> ", type=choices, show_choices=False)) This uses "int" to assign integers to the spaces in the tic tac toe game, and shows the choices in the form of numerical spaces (choices) and X and Os (moves already played/invalid options).
Checkpoint 2 Notes
A simple way to check and see if ther eis a winner is to list all of the options for a win, then let the program check threm. It would iterate through a list of combinations and check one by one. So, I started by drawing the board with the 1-8 integers. Then listed the options based off the wins available. I started by organizing the, into the three ways to win, rows, columns, then diagonals to ensure that I didn't miss any. After this I continued with the video and saw that you had them same idea! That was really exciting to reaize, and reassuring that my thought process was making sense. I used the video to help write the remaining code for the combos and listing them, then iterating through them and checking for a winning move.
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?
State 1: Playing as X the player should go in space 5 winning the game. State 2: Playing as X the player should go in space 5 as well, this will block 0 from winning and open up options for winng by way of a row 3, 4, 5 win or allowing for an eventual draw, if the computer continues to make the best moves. State 3: X should play in soace 6. this allows for the possibility of a win through column 0, 3, 6 or for row 6, 7, 8. It would also block the possibility for 0 to win diagonally at any point in the future of the game. State 4: X should play in space 3. If they do not, then (if the computer is playing intuitively) it will go in space 3. If that happens then there is no way for X to win. So, going in spot 3 blocks that option and ensures that the game canc continue.
| O | O | | O | X | X | O |
---+---+--- ---+---+--- ---+---+--- ---+---+---
X | X | | X | X | O | O | |
---+---+--- ---+---+--- ---+---+--- ---+---+---
| | | | O | | | |
Initial game state
The inital game state is 0. At this point in the game, either X or 0 has an equal probability of winning.
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?