generated from mwc/lab_tic_tac_toe
(Commit summary. Replace this with a one-line description of this commit.)
All of nim What I changed (Replace this with a description of what you changed in this commit. This should be 1-2 sentences.) Completed the nim game. Why I changed it (Describe why you made these changes. Were you working toward a goal? Did you reorganize your code? This should be 1-2 sentences.) Made additions to work towards completing the game. Tried my best to make the game as properly functional as I could. Estimate for remaining time to finish assignment: [0 minutes]
This commit is contained in:
parent
f5b1b03b9a
commit
9d0c6174ec
|
@ -32,3 +32,49 @@ class NimGameStub:
|
||||||
|
|
||||||
def get_objective(self, state):
|
def get_objective(self, state):
|
||||||
return max if state["first_player"] else min
|
return max if state["first_player"] else min
|
||||||
|
|
||||||
|
class NimGame:
|
||||||
|
"""This will hopefully work
|
||||||
|
"""
|
||||||
|
def get_initial_state(self):
|
||||||
|
return {
|
||||||
|
"board": [1, 3, 5, 7],
|
||||||
|
"first_player": True
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_next_state(self, state, action):
|
||||||
|
working=state["board"].copy()
|
||||||
|
working[action[0]-1]=working[action[0]-1]-action[1]
|
||||||
|
next_state = {
|
||||||
|
"board": working,
|
||||||
|
"first_player": not state["first_player"],
|
||||||
|
}
|
||||||
|
return next_state
|
||||||
|
|
||||||
|
def get_actions(self, state):
|
||||||
|
loo=[]
|
||||||
|
for row in range(4):
|
||||||
|
if state["board"][row]<3:
|
||||||
|
for i in range(state["board"][row]):
|
||||||
|
loo.append((row+1,i+1))
|
||||||
|
else:
|
||||||
|
for i in range(3):
|
||||||
|
loo.append((row+1,i+1))
|
||||||
|
return loo
|
||||||
|
def get_reward(self, state):
|
||||||
|
for i in state["board"]:
|
||||||
|
if i!=0:
|
||||||
|
return 0
|
||||||
|
if state["first_player"]:
|
||||||
|
return 1
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def is_over(self, state):
|
||||||
|
for i in state["board"]:
|
||||||
|
if i!=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_stub import NimGame
|
||||||
from strategy.lookahead_strategy import LookaheadStrategy
|
from strategy.lookahead_strategy import LookaheadStrategy
|
||||||
|
|
||||||
class HumanNimPlayer:
|
class HumanNimPlayer:
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.game = NimGameStub()
|
self.game = NimGame()
|
||||||
|
|
||||||
def choose_action(self, state):
|
def choose_action(self, state):
|
||||||
actions = self.game.get_actions(state)
|
actions = self.game.get_actions(state)
|
||||||
|
@ -26,7 +26,7 @@ class HumanNimPlayer:
|
||||||
class ComputerNimPlayer:
|
class ComputerNimPlayer:
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.strategy = LookaheadStrategy(NimGameStub(), max_depth=3, deterministic=False)
|
self.strategy = LookaheadStrategy(NimGame(), max_depth=6, deterministic=False)
|
||||||
|
|
||||||
def choose_action(self, state):
|
def choose_action(self, state):
|
||||||
action = self.strategy.choose_action(state)
|
action = self.strategy.choose_action(state)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
from nim.game_stub import NimGameStub
|
from nim.game_stub import NimGame
|
||||||
|
|
||||||
class NimView:
|
class NimView:
|
||||||
def __init__(self, player0, player1):
|
def __init__(self, player0, player1):
|
||||||
self.players = [player0, player1]
|
self.players = [player0, player1]
|
||||||
self.game = NimGameStub()
|
self.game = NimGame()
|
||||||
|
|
||||||
def greet(self):
|
def greet(self):
|
||||||
print(f"{self.players[0].name} and {self.players[1].name}, welcome to Nim.")
|
print(f"{self.players[0].name} and {self.players[1].name}, welcome to Nim.")
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
from nim.game_stub import NimGameStub
|
from nim.game_stub import NimGame
|
||||||
from nim.view import NimView
|
from nim.view import NimView
|
||||||
from nim.player import HumanNimPlayer, ComputerNimPlayer
|
from nim.player import HumanNimPlayer, ComputerNimPlayer
|
||||||
|
|
||||||
player0 = HumanNimPlayer(input("What's your name? "))
|
player0 = HumanNimPlayer(input("What's your name? "))
|
||||||
player1 = ComputerNimPlayer("Robot")
|
player1 = ComputerNimPlayer("Robot")
|
||||||
view = NimView(player0, player1)
|
view = NimView(player0, player1)
|
||||||
game = NimGameStub()
|
game = NimGame()
|
||||||
|
|
||||||
view.greet()
|
view.greet()
|
||||||
state = game.get_initial_state()
|
state = game.get_initial_state()
|
||||||
|
|
Loading…
Reference in New Issue