コード例 #1
0
ファイル: tester.py プロジェクト: d1str0/HungerGames
def run_tests(filename):
    players = []
    f = open(filename,'r')
    for script_name in f:
        try:
            user_module = importlib.import_module(script_name[:-4])
            if hasattr(user_module, 'Player'):
                try:
                    user_module = user_module.Player()
                except:
                    print("\nPlayer did not instantiate, make sure it is a class. "
                          "Proceeding assuming non OO code.\n")
        except:
            print ("\nCould not import %s\n" % script_name)
            raise
        players.append(user_module)
    test_player_functions(players)

    game = GameEngine(players)
    game.start()
コード例 #2
0
ファイル: AI_Master_Test.py プロジェクト: d1str0/HungerGames
            user_module = importlib.import_module(script_name[:-4])
            if hasattr(user_module, 'Player'):
                try:
                    user_module = user_module.Player()
                except:
                    print("\nPlayer did not instantiate, make sure it is a class. "
                          "Proceeding assuming non OO code.\n")
        except:
            print ("\nCould not import %s\n" % script_name)
            raise
        players.append(user_module)

    winners = []

    for i in range(0, 10000):
      game = GameEngine(players)
      game.start()
      winners.append(game.living)

    asdf = {}

    for i in winners:
      x = i[0]
      if x.module.name in asdf.keys():
        asdf[x.module.name] += 1
      else:
        asdf[x.module.name] = 1

    for i in asdf.keys():
      print i + ": " + str(asdf[i])
コード例 #3
0
    if args.playai:
        print(
            "Play \"Five or More\" game by AI. Please close game in terminal after closing window (i.e, Press Ctrl+C)."
        )

        from ai import AI
        from game import GameEngine

        ai = AI(state_shape=__default_state_shape__, verbose=verbose)
        if verbose:
            print("loading latest model: [{0}] ...".format(__filename__),
                  end="")
        ai.load_nnet(__filename__)
        if verbose:
            print("load OK!")

        gameengine = GameEngine(state_shape=__default_state_shape__,
                                ai=ai,
                                verbose=verbose)
        gameengine.start_ai()

    if args.play:
        print(
            "Play \"Five or More\" game. Please close game in terminal after closing window (i.e, Press Ctrl+C)."
        )
        from game import GameEngine

        gameengine = GameEngine(state_shape=__default_state_shape__,
                                verbose=verbose)
        gameengine.start()
コード例 #4
0
ファイル: app.py プロジェクト: aleksanderhan/phase-chess
class Main(QWidget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.game = GameEngine()
        self.board_view = BoardView(self.game.get_svg_board(), parent=self)
        self.game.set_board_view(self.board_view)
        self.game.start()
        self.command = QLineEdit()
        layout = QGridLayout(self)
        layout.addWidget(self.board_view)
        layout.addWidget(self.command)

        # Signaling
        self.command.returnPressed.connect(self.execute_command)

        self.commands = dict(
            # Exit application.
            qq=self.quit,
            # Save current board state to svg.
            sb=self.save_board,
            # Halt auto play.
            q=self.game.halt,
            # Toggle edit mode.
            ee=self.game.toggle_edit_mode,
            # Check result.
            cc=(lambda: logger.info(self.game.board.result())),
            # Print legal moves.
            lm=(lambda: logger.info(', '.join(
                [str(move) for move in self.game.board.legal_moves]))),
            # Print available commands.
            hh=(lambda: logger.info('Commands: ' + ', '.join(
                list(self.commands.keys()) + list(self.game.commands.keys())))
                ))

    def execute_command(self):
        cmd, *args = self.command.text().split(' ')
        self.command.clear()

        try:
            self.commands[cmd](*args)
        except KeyError:
            if self.game.auto_play:
                logger.info('Auto-play in progress. Send q to halt.')
            else:
                self.execute_game_command(cmd)
        except Exception as e:
            logger.exception(e)
            logger.error('cmd: {cmd}, args: {args}')

    def execute_game_command(self, cmd):
        self.game.execute(cmd)

    def closeEvent(self, event):
        super().closeEvent(event)
        self.game.stop()

    def quit(self):
        self.game.stop()
        sys.exit(0)

    def save_board(self, filename='board.svg'):
        filename = next(uniquename(filename))
        with open(filename, 'w') as file:
            file.write(str(self.game.get_svg_board()))
        logger.info('Board written to: ' + filename)