I identified the classes needed to complete

the tasks in notes.md

The amount of code was difficult, but I watched the video beforehand and it really helped.
I found myself talking while the video was on and being able to identify the elements in real time
and before you mentioned them. My computational literacy in terms of actually being able to read
and decipher code is getting a lot better and these last two labs were a huge help in that process!

I want more of this! Given the desired objective and the code, with some errors, troubleshooting has been
an amazing learning experience. If I was teaching this as a class, it might be cool, at this point in the
curriculum to go back through the code that students had already written and do the same thing. Troubleshoot
something that we know already works and that we know the objective for.
This commit is contained in:
Rebecca Hankey 2024-11-23 13:34:44 -05:00
parent 383ce469a9
commit 77aab6c085
1 changed files with 57 additions and 0 deletions

View File

@ -6,13 +6,70 @@ 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