Exemplo n.º 1
0
def react(data):
  name, length, frequency = data.split(",")
  length = min(int(length)/10.0, 2)
  print('%s: %s,%s' % (name, length, frequency))
  base.play(length, int(frequency))
Exemplo n.º 2
0
                N[who == curwho][curlen] += 1
                self._add_runs(N, who, row[1:], row[0], 1)


if __name__ == "__main__":
    import sys
    n = int(sys.argv[1])
    f = None
    if len(sys.argv) > 2:
        fn = sys.argv[2]
        f = open(fn, "a")

    for i in xrange(n):
        agents = [base.HeuristicAgent("X", TTTHeuristic()), RandomAgent("O")]
        #agents = [base.MinMaxSearchAgent("X", "O", TTTHeuristic(), 2), RandomAgent("O")]
        #agents = [base.MinMaxSearchAgent("X", "O", TTTHeuristic(), 2), base.HeuristicAgent("O", TTTHeuristic())]
        final_board, moves, winner = base.play(agents, TTTBoard.empty())
        print moves
        for board in board_iterator(moves, TTTBoard.empty()):
            print board
            print ""
        pgn = parse.write_game("TicTacToe", agents, moves, winner)
        print pgn
        if f is not None:
            f.write(pgn)

    if f is not None:
        f.close()


Exemplo n.º 3
0
def main(args):
    mode = args[1]
    if mode == "play":
        board = ChessBoard.empty()
        agents = [ChessPlayerAgent(color) for color in ChessConstants.COLORS]
        final_board, moves, winner = base.play(agents, board)
    elif mode == "random":
        f = None
        if len(args) > 2:
            fn = args[2]
            f = open(fn, "a")
        board = ChessBoard.empty()
        #agents = [ChessRandomAgent(color) for color in ChessConstants.COLORS]
        #agents = [base.HeuristicAgent(ChessConstants.WHITE, ChessHeuristic()), ChessRandomAgent(ChessConstants.BLACK)]
        #agents = [ChessMinMaxSearchAgent(ChessConstants.WHITE, ChessConstants.BLACK, heuristic=ChessHeuristic(), max_depth=2), ChessRandomAgent(ChessConstants.BLACK)]
        #agents = [ChessMinMaxSearchAgent(ChessConstants.WHITE, ChessConstants.BLACK, heuristic=ChessHeuristic(), max_depth=2),ChessMinMaxSearchAgent(ChessConstants.BLACK, ChessConstants.WHITE, heuristic=ChessHeuristic(), max_depth=2)]
        #agents = [ChessMinMaxAlphaBetaAgent(ChessConstants.WHITE, ChessConstants.BLACK, heuristic=ChessHeuristic(), max_depth=2),ChessRandomAgent(ChessConstants.BLACK)]
        agents = [
            ChessMinMaxSearchAgent(ChessConstants.WHITE,
                                   ChessConstants.BLACK,
                                   heuristic=ChessHeuristic(),
                                   max_depth=2),
            ChessMinMaxSearchAgent(ChessConstants.BLACK,
                                   ChessConstants.WHITE,
                                   heuristic=AnotherChessHeuristic(),
                                   max_depth=2)
        ]

        final_board, moves, winner = base.play(agents, board)
        pgn = parse.write_game("Chess", agents, moves, winner)
        print ""
        print pgn
        if f is not None:
            f.write(pgn)
            f.close()
    elif mode == "manyrandom":
        while True:
            try:
                main(["", "random"])
            except IndexError:
                continue
    elif mode == "read":
        filename = args[2]
        with open(filename, 'r') as f:
            chars = f.read()
        game_chars = parse.split_games(chars)
        for chars in game_chars:
            print "NEW GAME"
            tokens = parse.tokenize(chars)
            game_parser = parse.PGNGameParser(tokens, ChessPGNMoveParser)
            game = game_parser.game
            board = ChessBoard.empty()
            print game
            print board
            turn = 0
            for move_parser in game.move_parsers:
                print move_parser
                it = move_parser.parse_moves(board.grid)
                first = True
                while True:
                    print ""
                    try:
                        if first:
                            move = it.next()
                            first = False
                        else:
                            move = it.send(board.grid)
                        who = ChessConstants.COLORS[turn]
                        print who + "'s turn"
                        #print move
                        trans_moves = board.translate_move(who, move)
                        print trans_moves
                        assert len(trans_moves) > 0
                        for move in trans_moves:
                            board = board.result(who, move)
                            print board
                            #r = raw_input("ok?")
                        turn = (turn + 1) % 2
                    except StopIteration:
                        break
Exemplo n.º 4
0
def main(args):
    mode = args[1]
    if mode == "play":
        board = ChessBoard.empty()
        agents = [ChessPlayerAgent(color) for color in ChessConstants.COLORS]
        final_board, moves, winner = base.play(agents, board)
    elif mode == "random":
        f = None
        if len(args) > 2:
            fn = args[2]
            f = open(fn, "a")
        board = ChessBoard.empty()
        #agents = [ChessRandomAgent(color) for color in ChessConstants.COLORS]
        #agents = [base.HeuristicAgent(ChessConstants.WHITE, ChessHeuristic()), ChessRandomAgent(ChessConstants.BLACK)]
        #agents = [ChessMinMaxSearchAgent(ChessConstants.WHITE, ChessConstants.BLACK, heuristic=ChessHeuristic(), max_depth=2), ChessRandomAgent(ChessConstants.BLACK)]
        #agents = [ChessMinMaxSearchAgent(ChessConstants.WHITE, ChessConstants.BLACK, heuristic=ChessHeuristic(), max_depth=2),ChessMinMaxSearchAgent(ChessConstants.BLACK, ChessConstants.WHITE, heuristic=ChessHeuristic(), max_depth=2)]
        #agents = [ChessMinMaxAlphaBetaAgent(ChessConstants.WHITE, ChessConstants.BLACK, heuristic=ChessHeuristic(), max_depth=2),ChessRandomAgent(ChessConstants.BLACK)]
        agents = [ChessMinMaxSearchAgent(ChessConstants.WHITE, ChessConstants.BLACK, heuristic=ChessHeuristic(), max_depth=2),
                  ChessMinMaxSearchAgent(ChessConstants.BLACK, ChessConstants.WHITE, heuristic=AnotherChessHeuristic(), max_depth=2)]

        final_board, moves, winner = base.play(agents, board)
        pgn = parse.write_game("Chess", agents, moves, winner)
        print ""
        print pgn
        if f is not None:
            f.write(pgn)
            f.close()
    elif mode == "manyrandom":
        while True:
            try:
                main(["", "random"])
            except IndexError:
                continue
    elif mode == "read":
        filename = args[2]
        with open(filename, 'r') as f:
            chars = f.read()
        game_chars = parse.split_games(chars)
        for chars in game_chars:
            print "NEW GAME"
            tokens = parse.tokenize(chars)
            game_parser = parse.PGNGameParser(tokens, ChessPGNMoveParser)
            game = game_parser.game
            board = ChessBoard.empty()
            print game
            print board
            turn = 0
            for move_parser in game.move_parsers:
                print move_parser
                it = move_parser.parse_moves(board.grid)
                first = True
                while True:
                    print ""
                    try:
                        if first:
                            move = it.next()
                            first = False
                        else:
                            move = it.send(board.grid)
                        who = ChessConstants.COLORS[turn]
                        print who+"'s turn"
                        #print move
                        trans_moves = board.translate_move(who, move)
                        print trans_moves
                        assert len(trans_moves) > 0
                        for move in trans_moves:
                            board = board.result(who, move)
                            print board
                            #r = raw_input("ok?")
                        turn = (turn + 1) % 2
                    except StopIteration:
                        break
Exemplo n.º 5
0
                self._add_runs(N, who, row[1:], curwho, curlen + 1)
            else:
                N[who == curwho][curlen] += 1
                self._add_runs(N, who, row[1:], row[0], 1)


if __name__ == "__main__":
    import sys
    n = int(sys.argv[1])
    f = None
    if len(sys.argv) > 2:
        fn = sys.argv[2]
        f = open(fn, "a")

    for i in xrange(n):
        agents = [base.HeuristicAgent("X", TTTHeuristic()), RandomAgent("O")]
        #agents = [base.MinMaxSearchAgent("X", "O", TTTHeuristic(), 2), RandomAgent("O")]
        #agents = [base.MinMaxSearchAgent("X", "O", TTTHeuristic(), 2), base.HeuristicAgent("O", TTTHeuristic())]
        final_board, moves, winner = base.play(agents, TTTBoard.empty())
        print moves
        for board in board_iterator(moves, TTTBoard.empty()):
            print board
            print ""
        pgn = parse.write_game("TicTacToe", agents, moves, winner)
        print pgn
        if f is not None:
            f.write(pgn)

    if f is not None:
        f.close()