lab_tic_tac_toe/notes.md

230 lines
6.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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.
Its the most obvious choice to get “3 Xs” in a row.
Second Game:
I would choose “X” in 3rd column, 2nd row.
Im 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 its “Xs” turn and if not its “0s” 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.
*****Getting errors entering code in python
I did all the preliminary with the poetry shell
from ttt.game import TTTGame "zsh: command not found: from"
Can't get beyond this. What am I doing incorrectly?
from ttt.game import TTTGame
from strategy.lookahead_strategy import LookaheadStrategy
game = TTTGame()
strategy = LookaheadStrategy(game, explain=True)
state = {"board": ['-','O','O','X','X','-','-','-','-'], "player_x": True} >>> strategy.get_current_and_future_reward(state, explain=True)
Nim Game
Wrote the NimGameRev class and replaced NimGameStub
The Nim Game was stuck with 1 line left and wouldn't end.
Honestly, I used AI to help me better understand the reasoning for the code.
I was stuck with def get_actions(self, state):
And used from AI
def get_actions(self, state):
actions = []
for row_index, lines_in_row in enumerate(state["board"]):
for num_lines_to_remove in range(1, lines_in_row + 1):
actions.append((row_index, num_lines_to_remove))
return actions
I have AI be more definitive because the use of 'i' and 'j' was too abstract.
The Nim Game worked with the Robot winning.
The Nim Game
What's your name? Kathryn
Kathryn and Robot, welcome to Nim.
|
| | |
| | | | |
| | | | | | |
0. Remove 1 from row 0.
1. Remove 1 from row 1.
2. Remove 2 from row 1.
3. Remove 3 from row 1.
4. Remove 1 from row 2.
5. Remove 2 from row 2.
6. Remove 3 from row 2.
7. Remove 4 from row 2.
8. Remove 5 from row 2.
9. Remove 1 from row 3.
10. Remove 2 from row 3.
11. Remove 3 from row 3.
12. Remove 4 from row 3.
13. Remove 5 from row 3.
14. Remove 6 from row 3.
15. Remove 7 from row 3.
> 1
|
| |
| | | | |
| | | | | | |
Robot removes 1 from row 3
|
| |
| | | | |
| | | | | |
0. Remove 1 from row 0.
1. Remove 1 from row 1.
2. Remove 2 from row 1.
3. Remove 1 from row 2.
4. Remove 2 from row 2.
5. Remove 3 from row 2.
6. Remove 4 from row 2.
7. Remove 5 from row 2.
8. Remove 1 from row 3.
9. Remove 2 from row 3.
10. Remove 3 from row 3.
11. Remove 4 from row 3.
12. Remove 5 from row 3.
13. Remove 6 from row 3.
> 2
|
| | | | |
| | | | | |
Robot removes 3 from row 2
|
| |
| | | | | |
0. Remove 1 from row 0.
1. Remove 1 from row 2.
2. Remove 2 from row 2.
3. Remove 1 from row 3.
4. Remove 2 from row 3.
5. Remove 3 from row 3.
6. Remove 4 from row 3.
7. Remove 5 from row 3.
8. Remove 6 from row 3.
> 8
|
| |
Robot removes 1 from row 2
|
|
0. Remove 1 from row 0.
1. Remove 1 from row 2.
> 1
|
Robot removes 1 from row 0
Congratulations, Robot!