generated from mwc/lab_tic_tac_toe
Submission for tic tac toe lab.
What I changed I completed checkpoints 1-3. I would have submitted all three separately, but a bug prohibited my initial submissions. I also started working on the nim section of the lab. Why I changed it I made these changes in order to make a functions tic tac toe and nim game. Estimate for remaining time to finish assignment: 15 minutes
This commit is contained in:
50
nim/game.py
Normal file
50
nim/game.py
Normal file
@@ -0,0 +1,50 @@
|
||||
class NimGame:
|
||||
|
||||
def get_initial_state(self):
|
||||
return {
|
||||
"board": [1, 3, 5, 7],
|
||||
"first_player": True
|
||||
}
|
||||
|
||||
def get_next_state(self, state, action):
|
||||
board = state["board"].copy()
|
||||
row = action[0]
|
||||
number_to_remove = action[1]
|
||||
board[row] = board[row] - number_to_remove
|
||||
next_state = {
|
||||
"board": board,
|
||||
"first_player": not state["first_player"],
|
||||
}
|
||||
return next_state
|
||||
|
||||
def get_actions(self, state):
|
||||
options = []
|
||||
for num in range(4):
|
||||
row = state["board"][num]
|
||||
if row >=3:
|
||||
options.append((num, 1))
|
||||
options.append((num, 2))
|
||||
options.append((num, 3))
|
||||
elif row == 2:
|
||||
options.append((num, 1))
|
||||
options.append((num, 2))
|
||||
elif row == 1:
|
||||
options.append((num, 1))
|
||||
return options
|
||||
|
||||
def get_reward(self, state):
|
||||
if state["first_player"]:
|
||||
return 1
|
||||
elif not state["first_player"]:
|
||||
return -1
|
||||
else:
|
||||
return 0
|
||||
|
||||
def is_over(self, state):
|
||||
for row in state["board"]:
|
||||
if row != 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from nim.game_stub import NimGameStub
|
||||
from nim.game import NimGame
|
||||
|
||||
class NimView:
|
||||
def __init__(self, player0, player1):
|
||||
self.players = [player0, player1]
|
||||
self.game = NimGameStub()
|
||||
self.game = NimGame()
|
||||
|
||||
def greet(self):
|
||||
print(f"{self.players[0].name} and {self.players[1].name}, welcome to Nim.")
|
||||
|
||||
Reference in New Issue
Block a user