From 83a763fb21908c5aadf89442c34b83596e550d5c Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Tue, 21 Oct 2025 13:49:23 +0200 Subject: [PATCH] ass3: init bucket_game --- assignment3/bucket_game.py | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 assignment3/bucket_game.py diff --git a/assignment3/bucket_game.py b/assignment3/bucket_game.py new file mode 100644 index 0000000..ea431ac --- /dev/null +++ b/assignment3/bucket_game.py @@ -0,0 +1,44 @@ +State = tuple[int, list[str | int]] # Tuple of player (whose turn it is), +# and the buckets (as str) +# or the number in a bucket +Action = str | int # Bucket choice (as str) or choice of number + + +class Game: + def initial_state(self) -> State: + return 0, ["A", "B", "C"] + + def to_move(self, state: State) -> int: + player, _ = state + return player + + def actions(self, state: State) -> list[Action]: + _, actions = state + return actions + + def result(self, state: State, action: Action) -> State: + if action == "A": + return (self.to_move(state) + 1) % 2, [-50, 50] + elif action == "B": + return (self.to_move(state) + 1) % 2, [3, 1] + elif action == "C": + return (self.to_move(state) + 1) % 2, [-5, 15] + assert type(action) is int + return (self.to_move(state) + 1) % 2, [action] + + def is_terminal(self, state: State) -> bool: + _, actions = state + return len(actions) == 1 + + def utility(self, state: State, player: int) -> float: + assert self.is_terminal(state) + _, actions = state + assert type(actions[0]) is int + return actions[0] if player == self.to_move(state) else -actions[0] + + def print(self, state): + print(f"The state is {state} and ", end="") + if self.is_terminal(state): + print(f"P1's utility is {self.utility(state, 0)}") + else: + print(f"it is P{self.to_move(state)+1}'s turn")