From 70c8ff13087ad131adff0391fd5e7f80a0773947 Mon Sep 17 00:00:00 2001 From: Cory Date: Mon, 8 Apr 2024 10:12:37 -0400 Subject: [PATCH] (Completed Nim as part of the tic tac toe lab.) What I changed (I made a copy of game_stub.py called game.py and corrected the methods so that the game would work.) Why I changed it (To complete the lab.) Estimate for remaining time to finish assignment: [Done] --- .DS_Store | Bin 0 -> 6148 bytes nim/game.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ nim/player.py | 10 +++++----- nim/view.py | 4 ++-- play_nim.py | 4 ++-- 5 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 .DS_Store create mode 100644 nim/game.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..cc6fb7fdff47e5c58245c20815d495842326c1c0 GIT binary patch literal 6148 zcmeHKO^?$s5FIy#O{qXVfV3B+NL;JZ{Q!Zugzj?SN)Q|Xm86N5uxVT+Db=c~lr#Jo zuKW@BFPz|w?Ugp!g(Iqvry4(h%=6e!a$FOUm@MNxq5%=PaK`Qp6jvCJvtP1~>0zGW zIpi$LWP}z^XU!^L71(wK`0P5AQ%dvaS>yaZ#mMNFGPMa46|KsJIpyD?MV6M;c>F_j zobI*jJMNC#ci#mco 0: # if a row is not empty + for j in range(1,4): # check if you can remove 1, 2, or 3 ticks in the row + if j <= new_board[i]: # if so + actions.append((i,j)) # include that as an option + return actions + + def get_reward(self, state): + ''' Reports who wins. ''' + if state["first_player"]: + return 1 # If it is the player's turn and there is nothing on the board, then it was the robot who crossed off the last line. + return 0 # Otherwise, the first player made the last move and lost. + + def is_over(self, state): + ''' Reports true if there are no more lines to cross. ''' + if all(ticks == 0 for ticks in state["board"]): + return True + return False + + def get_objective(self, state): + ''' Reports the desired obejctive to help choose the move that + yields the optimal reward under the lookahead_strategy for + computer players. ''' + return max if state["first_player"] else min diff --git a/nim/player.py b/nim/player.py index 32fd9fc..9060167 100644 --- a/nim/player.py +++ b/nim/player.py @@ -1,16 +1,16 @@ -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) for i, action in enumerate(actions): row, lines_to_remove = action - print(f"{i}. Remove {lines_to_remove} from row {row}.") + print(f"{i}. Remove {lines_to_remove} from row {row+1}.") choice = self.get_int(len(actions)) return actions[choice] @@ -26,10 +26,10 @@ 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=5, 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}") + print(f"{self.name} removes {lines_to_remove} from row {row+1}") return action diff --git a/nim/view.py b/nim/view.py index 16e3fdf..8bd3d88 100644 --- a/nim/view.py +++ b/nim/view.py @@ -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.") diff --git a/play_nim.py b/play_nim.py index 61f5cf2..293b4ef 100644 --- a/play_nim.py +++ b/play_nim.py @@ -1,11 +1,11 @@ -from nim.game_stub import NimGameStub +from nim.game 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()