From 9d0c6174ec2cd5ad888ceb94c4a0fd1e28c8494d Mon Sep 17 00:00:00 2001 From: root Date: Thu, 22 Feb 2024 08:51:45 -0500 Subject: [PATCH] (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] --- nim/game_stub.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ nim/player.py | 6 +++--- nim/view.py | 4 ++-- play_nim.py | 4 ++-- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/nim/game_stub.py b/nim/game_stub.py index b8a7d36..5af5349 100644 --- a/nim/game_stub.py +++ b/nim/game_stub.py @@ -32,3 +32,49 @@ class NimGameStub: def get_objective(self, state): 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 + diff --git a/nim/player.py b/nim/player.py index 32fd9fc..62a3bcf 100644 --- a/nim/player.py +++ b/nim/player.py @@ -1,10 +1,10 @@ -from nim.game_stub import NimGameStub +from nim.game_stub 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=6, deterministic=False) def choose_action(self, state): action = self.strategy.choose_action(state) diff --git a/nim/view.py b/nim/view.py index 16e3fdf..7494921 100644 --- a/nim/view.py +++ b/nim/view.py @@ -1,9 +1,9 @@ -from nim.game_stub import NimGameStub +from nim.game_stub 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.") diff --git a/play_nim.py b/play_nim.py index 61f5cf2..563f2bc 100644 --- a/play_nim.py +++ b/play_nim.py @@ -1,11 +1,11 @@ -from nim.game_stub import NimGameStub +from nim.game_stub import NimGame from nim.view import NimView from nim.player import HumanNimPlayer, ComputerNimPlayer player0 = HumanNimPlayer(input("What's your name? ")) player1 = ComputerNimPlayer("Robot") view = NimView(player0, player1) -game = NimGameStub() +game = NimGame() view.greet() state = game.get_initial_state()