def __init__(self, master): super(GameUi, self).__init__(master) self.width = 1920 self.height = 1080 self.items = { Items.BOARD_WIDTH: self.width * 0.8, Items.BOARD_HEIGHT: self.height * 0.8, Items.CONTROL_HEIGHT: self.height * 0.8, Items.CHECKER_DIAMETER: self.height * 0.8 / 6, Items.CHECKER_BUFFER: None } self.board_canvas = tk.Canvas(self, bg='#966F33', width=self.items[Items.BOARD_WIDTH], height=self.items[Items.BOARD_HEIGHT]) self.board_canvas.grid(row=0, column=0, sticky="n", rowspan=2) self.board: GameBoard = GameBoard(self.board_canvas, items=self.items) self.items[Items.BOARD] = self.board self.control_panel_frame = tk.Frame( self, bg="red", width=Setting.CONTROL_PANEL_WIDTH.value, height=self.items[Items.BOARD_HEIGHT] / 2) self.control_panel_frame.grid_propagate(False) self.control_panel_frame.grid(row=0, column=1, sticky="n") self.control_panel = ControlPanel(self.control_panel_frame, self.items) self.offBoard_canvas = tk.Canvas( self, bg="blue", width=Setting.CONTROL_PANEL_WIDTH.value, height=self.items[Items.BOARD_HEIGHT] / 2) # self.offBoard_canvas.grid(row=1, column=1, sticky="n") # self.offBoard = OffBoard(self.offBoard_canvas, self.items) # self.items[Items.OFF_BOARD] = self.offBoard self.pack() self.setup_game() self.board_canvas.focus_set() self.player_1 = HumanPlayer(EngineChecker.BLACK, self.board, self.items) self.player_2 = AiPlayer(EngineChecker.WHITE) self.game = Game(self.player_1, self.player_2, False) self.game.run() ###### TEST ###### self.toggle = False self.checker = Checker(self.board.canvas, "red") self.board_canvas.bind('<Up>', lambda _: self.test_add_checker()) self.board_canvas.bind('<Down>', lambda _: self.test_remove_checker())
class Simulation: def __init__(self): self.player1 = RandomPlayer(Checker.WHITE) self.player2 = RandomPlayer(Checker.BLACK) self.game = Game(self.player1, self.player2, create_protocol=False) def run(self): self.game.run() def runSimulation(self, game_proto: Protocol, log=False): status = [] if game_proto.whowonNumber() == 1: winner = self.player1.color else: winner = self.player2.color for i, turn in enumerate(game_proto.game_proto): if i % 2 == 0: player = self.player1 next_player = self.player2 else: player = self.player2 next_player = self.player2 for move in turn.moves: if i % 2 == 0: if log: print("Player:" + str(player.color) + " Dice:" + str(turn.die) + " Src: " + str(move.src) + " Trg:" + str(move.trg)) self.game.board.move(player.color, int(move.src), int(move.trg)) else: if log: print("Player:" + str(player.color) + " Dice:" + str(turn.die) + " Src: " + str((25 - int(move.src)) % 25) + " Trg:" + str((25 - int(move.trg)) % 25)) self.game.board.move(player.color, (25 - int(move.src)) % 25, (25 - int(move.trg)) % 25) status.append((self.game.board.get_view(), deepcopy(next_player.color), deepcopy(winner))) if log: print(self.game.board) return status
def test_black_move_out(self): rand_black = RandomPlayer(Checker.BLACK) human_white = RandomPlayer(Checker.WHITE) game = Game(player_1=human_white, player_2=rand_black, create_protocol=False) game.board.board = game.board.clear_board() game.current_player = rand_black game.current_dice = Die(4, 6) game.board.place_at(19, Checker.BLACK, 1) game.board.place_at(22, Checker.BLACK, 1) game.board.place_at(18, Checker.WHITE, 4) game.run() moves = generate_moves_serial(self.black, Die(4, 6), game.board.get_view(True)) print(moves)
def play_against_ai(): player1 = AiPlayer(Checker.WHITE) player2 = RandomPlayer(Checker.BLACK) game = Game(player_1=player1, player_2=player2, create_protocol=True) game.run()
def setUp(self) -> None: self.game = Game(self.black, self.white)
def __init__(self): self.player1 = RandomPlayer(Checker.WHITE) self.player2 = RandomPlayer(Checker.BLACK) self.game = Game(self.player1, self.player2, create_protocol=False)
def load_latest_game_state() -> GameStateLog: path = os.path.abspath("log/board_state.log") with open(path, "r") as f: last_line = f.readlines()[-1] obj = jsonpickle.decode(last_line) return obj class DebugPlayer(Player): def __init__(self, color: Checker, move): super().__init__(color) self.move = move def calculate_moves(self, dices: Die, board) -> [(int, int)]: return self.move if __name__ == "__main__": game_state = load_latest_game_state() game_state player2_color = Checker.WHITE if game_state.player.color == Checker.BLACK else Checker.BLACK debug_player: DebugPlayer = DebugPlayer(game_state.player.color, game_state.moves) player2: HumanPlayer = HumanPlayer(player2_color) game: Game = Game(debug_player, player2) game.current_player = debug_player game.board.board = game_state.board game.current_dice = game_state.die game.play()
def setUp(self) -> None: self.game = Game(self.black, self.white, create_protocol=False)