From 2f710463f4ede962cb1e1dbc3b534d529e1e7c87 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 19 Feb 2022 21:53:38 -0500 Subject: [PATCH] [+] A2 P2.3 Create WinningPlayer --- assignments/A2/a2_part2.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/assignments/A2/a2_part2.py b/assignments/A2/a2_part2.py index 19945a5..c8bc6f0 100644 --- a/assignments/A2/a2_part2.py +++ b/assignments/A2/a2_part2.py @@ -154,6 +154,27 @@ class GreedyTreePlayer(a2_minichess.Player): return move +class WinningPlayer(a2_minichess.Player): + """A Minichess player that computes tree before every move, computationally heavier but wins + almost every time + """ + + def make_move(self, game: a2_minichess.MinichessGame, previous_move: Optional[str]) -> str: + """Make a move given the current game. Since this is only a demo, it only supports white + player. + + previous_move is the black player's most recent move, or None if no moves have been made. + + Preconditions: + - There is at least one valid move for the given game + """ + if not previous_move: + previous_move = '*' + + tree = generate_complete_game_tree(previous_move, game, 3) + return max(tree.get_subtrees(), key=lambda x: x.white_win_probability).move + + def part2_runner(d: int, n: int, white_greedy: bool) -> None: """Create a complete game tree with the given depth, and run n games where one player is a GreedyTreePlayer and the other is a RandomPlayer.