generated from mwc/lab_tic_tac_toe
Kathryn Odell-Hamilton
4.6.24
Corrected bugs with Nim game.
Had issues trying to update TTTGame:
To find the value of the first state, enter the
Python shell (python) and run the following:
-This was the typed and error message
from ttt.game import TTTGame
zsh: command not found: from
How do I accomplish the last task with TTTGame?
Thank you in advance.
This commit is contained in:
BIN
nim/.DS_Store
vendored
Normal file
BIN
nim/.DS_Store
vendored
Normal file
Binary file not shown.
51
nim/game_rev.py
Normal file
51
nim/game_rev.py
Normal file
@@ -0,0 +1,51 @@
|
||||
class NimGameRev:
|
||||
"""This is the NimGameRev. game_stub.py was used as the beginning
|
||||
shell code in reference to correct methods, inputs, and outputs.
|
||||
I entered the code for the game to be functional.
|
||||
"""
|
||||
def get_initial_state(self):
|
||||
return {
|
||||
"board": [1, 3, 5, 7],
|
||||
"first_player": True
|
||||
}
|
||||
|
||||
def get_next_state(self, state, action):
|
||||
next_state = {
|
||||
"board": state["board"].copy(),
|
||||
"first_player": not state["first_player"],
|
||||
}
|
||||
row,num_lines=action
|
||||
next_state["board"][row]-=num_lines
|
||||
return next_state
|
||||
|
||||
"""def get_actions(self, state):
|
||||
"initalize with actions"
|
||||
actions = [
|
||||
(0, 0),
|
||||
(1, 0), (1, 1),
|
||||
(2, 0), (2, 1), (2, 2),
|
||||
(3, 0), (3, 1), (3, 2), (3, 3),
|
||||
]
|
||||
return actions"""
|
||||
|
||||
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
|
||||
|
||||
def get_reward(self, state):
|
||||
if self.is_over(state):
|
||||
return -1 if state["first_player"] else 1
|
||||
return 0
|
||||
|
||||
def is_over(self, state):
|
||||
"""Determines if the game is over.
|
||||
Checks a condition, whether all rows are empty.
|
||||
If all rows are empty game is over.
|
||||
"""
|
||||
return all(lines == 0 for lines in state["board"])
|
||||
|
||||
def get_objective(self, state):
|
||||
return max if state["first_player"] else min
|
||||
@@ -1,10 +1,10 @@
|
||||
from nim.game_stub import NimGameStub
|
||||
from nim.game import NimGame
|
||||
from strategy.lookahead_strategy import LookaheadStrategy
|
||||
|
||||
class HumanNimPlayer:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.game = NimGameStub()
|
||||
self.game = NimGame()
|
||||
|
||||
def choose_action(self, state):
|
||||
actions = self.game.get_actions(state)
|
||||
@@ -26,7 +26,7 @@ class HumanNimPlayer:
|
||||
class ComputerNimPlayer:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.strategy = LookaheadStrategy(NimGameStub(), max_depth=3, deterministic=False)
|
||||
self.strategy = LookaheadStrategy(NimGame(), max_depth=3, deterministic=False)
|
||||
|
||||
def choose_action(self, state):
|
||||
action = self.strategy.choose_action(state)
|
||||
|
||||
35
nim/player_rev.py
Normal file
35
nim/player_rev.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from nim.game_rev import NimGameRev
|
||||
from strategy.lookahead_strategy import LookaheadStrategy
|
||||
|
||||
class HumanNimPlayer:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.game = NimGameRev()
|
||||
|
||||
def choose_action(self, state):
|
||||
actions = self.game.get_actions(state)
|
||||
for i, action in enumerate(actions):
|
||||
row, lines_to_remove = action
|
||||
print(f"{i}. Remove {lines_to_remove} from row {row}.")
|
||||
choice = self.get_int(len(actions))
|
||||
return actions[choice]
|
||||
|
||||
def get_int(self, maximum):
|
||||
while True:
|
||||
response = input("> ")
|
||||
if response.isdigit():
|
||||
value = int(response)
|
||||
if value < maximum:
|
||||
return value
|
||||
print("Invalid input.")
|
||||
|
||||
class ComputerNimPlayer:
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.strategy = LookaheadStrategy(NimGameRev(), max_depth=3, deterministic=False)
|
||||
|
||||
def choose_action(self, state):
|
||||
action = self.strategy.choose_action(state)
|
||||
row, lines_to_remove = action
|
||||
print(f"{self.name} removes {lines_to_remove} from row {row}")
|
||||
return action
|
||||
32
nim/view_rev.py
Normal file
32
nim/view_rev.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from nim.game_rev import NimGameRev
|
||||
|
||||
class NimViewRev:
|
||||
def __init__(self, player0, player1):
|
||||
self.players = [player0, player1]
|
||||
self.game = NimGameRev()
|
||||
|
||||
def greet(self):
|
||||
print(f"{self.players[0].name} and {self.players[1].name}, welcome to Nim.")
|
||||
|
||||
def show_board(self, state):
|
||||
for lines_in_row in state["board"]:
|
||||
print("| " * lines_in_row)
|
||||
|
||||
def get_action(self, state):
|
||||
self.show_board(state)
|
||||
player = self.get_current_player(state)
|
||||
return player.choose_action(state)
|
||||
|
||||
def get_current_player(self, state):
|
||||
if state["first_player"]:
|
||||
return self.players[0]
|
||||
else:
|
||||
return self.players[1]
|
||||
|
||||
def conclude(self, state):
|
||||
self.show_board(state)
|
||||
if self.game.get_reward(state) > 0:
|
||||
winner = self.players[0]
|
||||
else:
|
||||
winner = self.players[1]
|
||||
print(f"Congratulations, {winner.name}!")
|
||||
Reference in New Issue
Block a user