def run_match(self, blue, red): b = self.players[blue](self.size, HexBoard.BLUE, self.seed) r = self.players[red](self.size, HexBoard.RED, self.seed) if self.names[blue] is None: self.names[blue] = b.name() if self.names[red] is None: self.names[red] = r.name() board = HexBoard(self.size) blue_turn = True while not board.is_game_over(): if blue_turn: b.make_move(board) else: r.make_move(board) blue_turn = not blue_turn return not blue_turn, blue, red
class HexGame: def __init__(self, size, player1, player2): self.board = HexBoard(size) self.player1 = player1 self.player2 = player2 self.win = "" self.lose = "" def step(self, renders=('board', 'win')): if 'board' in renders: self.board.render(1000) if self.player1 and self.board.turn() == HexColour.RED: move = self.player1.get_move(self.board, HexColour.RED, renders) if self.player2 and self.board.turn() == HexColour.BLUE: move = self.player2.get_move(self.board, HexColour.BLUE, renders) self.board.do_move(move) def play(self, renders=('board', 'win')): while not self.board.is_game_over(): self.step(renders) if 'board' in renders: self.board.render(1000) if self.board.check_win(HexColour.RED): self.win = [self.player1, 1] self.lose = [self.player2, 2] if 'win' in renders: print('Red Wins!') return HexColour.RED if self.board.check_win(HexColour.BLUE): self.win = [self.player2, 2] self.lose = [self.player1, 1] if 'win' in renders: print('Blue Wins!') return HexColour.BLUE
opp_color = HexBoard.RED play_color = HexBoard.BLUE if args.r: opp_color = HexBoard.BLUE play_color = HexBoard.RED board = HexBoard(args.BOARD_SIZE) board.print() opponent_class = importlib.import_module("players." + args.PLAYER).export opponent = opponent_class(args.BOARD_SIZE, opp_color, args.SEED) print("Playing vs: " + opponent.name()) is_player_turn = not args.r while not board.is_game_over(): if is_player_turn: try: text_in = input("move?> ") x, y = text_in.split(',') x = int(x) y = ord(y) - ord('a') if x >= 0 and x < board.size and y >= 0 and y < board.size and board.is_empty( (y, x)): board.place((y, x)) is_player_turn = not is_player_turn else: print("invalid move") except ValueError: print("invalid move") else: