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)
class GameUi(tk.Frame): 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()) #### END TEST #### def setup_game(self): self.board_canvas.bind('<space>', lambda _: self.start_game()) def test_add_checker(self): logging.debug(str(self.checker)) self.board.cones[2].add_checker(self.checker) def test_remove_checker(self): self.board.cones[2].remove_checker(self.checker) def start_game(self): print("start") self.offBoard.add_checker(Checker(self.offBoard_canvas, "red"))
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()