Example #1
0
def run_sim(player_programs):

    game_type = re.search('([a-z]*)', player_programs[0]).group(0)  # ugh

    n = 10
    player1 = create_sandbox(SCRIPT_PATH, player_programs[0])
    player2 = create_sandbox(SCRIPT_PATH, player_programs[1])
    cycleit = itertools.cycle([player1, player2])
    engine = Engine(game_type)  # rps

    # stats_obj = get_player_stats(player1)
    for player in [next(cycleit) for i in range(n)]:
        world_state = engine.getState()
        ret = player.take_turn(world_state)
        print ret
        engine.update(ret)
    player1.update_stats()
    player2.update_stats()
Example #2
0
def run_sim(player_programs, cb = lambda x: x):
    print "playing", player_programs[0], "against", player_programs[1]
    
    game_type = player_programs[0].split('-')[0]

    player1 = create_sandbox(SCRIPT_PATH, player_programs[0])
    player2 = create_sandbox(SCRIPT_PATH, player_programs[1])
    #creates player object for each challenger
    players = [player1, player2]
    cycleit = itertools.cycle([0, 1])
    #cycle object to iterate between p1 and p2

    engine = Engine(game_type)
    
    #game engine object with game type entered
    player_1_history = []
    player_2_history = []

    # stats_obj = get_player_stats(player1)
    rps_mapping = {'r': "Rock", 'p': 'Paper', 's': "Scissors"}
    
    rps_counter = 0
    rps_max = 10

    print '---------'

    # for player_id in [next(cycleit) for i in range()]:
    while True:
        player_id = next(cycleit)
        print '(', player_id, ')'
        current_player = players[player_id]

        world_state = engine.getState()

        valid_moves = engine.getValidMoves()
        
        try:
            if rps_counter >= rps_max:
                break
            if game_type == 'rps' and rps_counter < rps_max:
                rps_counter = rps_counter + 1

                ret = current_player.take_turn({"valid_moves": valid_moves, "player": 1, "history": player_1_history})
                print type(ret)
                print "RET TAG", ret['tag']
                engine.makeMove({'contents': [], 'tag': rps_mapping[ret['tag']]})
                status = engine.getStatus()

                cb("HELLO WORLDDDDDD")
                print "SVG:", engine.renderState()

                if status['tag'] == 'PlayerWin' and status['contents'] == 1:
                    print player_id, "player wins"
                    players[1].win()
                    players[0].lose()
                    engine.reset()
                    continue
                
                if status['tag'] == 'PlayerWin' and status['contents'] == 0:
                    print player_id, "player loses"
                    players[0].win()
                    players[1].lose()
                    engine.reset()
                    continue

                if status['tag'] == 'Drawn':
                    print player_id, "player drawn"
                    players[0].tie()
                    players[1].tie()
                    engine.reset()
                    continue
        except:
            print sys.exc_info()
            break
        if game_type == 'tron':
            state = engine.getState()
            
            # p1_pos = state[0][0] (x, y)
            # p1_dir = state[0][1]['tag']
            # p2_pos_dir = state[1] (x, y, )
            # grid = state[2][1]
            # bounds = state[2][0]
            # coords = grid[i][j][0]
            # maybe wall_owner = grid[i][j][1] 


            cb(engine.renderState())
            p1_pos = state[0][0]
            p1_dir = state[0][1]['tag']

            p2_pos = state[1][0]
            p2_dir = state[1][1]['tag']

            curr_pos = state[player_id][0]
            curr_dir = state[player_id][1]['tag'] # 'North', 'South', 'East', 'West'

            grid = state[2][1]
            bounds = state[2][0]
            # coords = grid[i][j][0]

            dir_py_to_hask = {'r':'East','l':'West','d':'South','u':'North'}
            dir_hask_to_py = {'East':'r','West':'l','South':'d','North':'u'} #todo dict comp (faster to not use new syntax now)
            moves_legend_py_to_hask = {'l':'TurnCW', 'r':'TurnCCW', 's':'KeepGoing'}

            # simlu updates
            moves = []
            for i in [0,1]:
                current_player = players[i]

                ret = current_player.take_turn({
                    "valid_moves": ['l','r','s'], 
                    "player": i, 
                    "history": player_1_history,
                    "board": {tuple(k):v for (k, v) in grid},
                    "bounds": bounds,
                    "current_direction": dir_hask_to_py[curr_dir]
                    })

                print ret
                moves.append({'contents':[], 'tag':moves_legend_py_to_hask[ret]})

            move = [move for move in moves]
            print json.dumps(move)
            engine.makeMove(move)

            status = engine.getStatus()
            print status

            if status['tag'] == 'PlayerWin' and status['contents'] == 1:
                print player_id, "player wins"
                players[1].win()
                players[0].lose()
                engine.reset()
                break
            
            if status['tag'] == 'PlayerWin' and status['contents'] == 0:
                print player_id, "player loses"
                players[0].win()
                players[1].lose()
                engine.reset()
                break

            if status['tag'] == 'Drawn':
                print player_id, "player drawn"
                players[0].tie()
                players[1].tie()
                engine.reset()
                break
Example #3
0
def run_sim(player_programs, cb=lambda x: x):
    print "playing", player_programs[0], "against", player_programs[1]

    game_type = player_programs[0].split('-')[0]

    player1 = create_sandbox(SCRIPT_PATH, player_programs[0])
    player2 = create_sandbox(SCRIPT_PATH, player_programs[1])
    #creates player object for each challenger
    players = [player1, player2]
    cycleit = itertools.cycle([0, 1])
    #cycle object to iterate between p1 and p2

    engine = Engine(game_type)

    #game engine object with game type entered
    player_1_history = []
    player_2_history = []

    # stats_obj = get_player_stats(player1)
    rps_mapping = {'r': "Rock", 'p': 'Paper', 's': "Scissors"}

    rps_counter = 0
    rps_max = 10

    print '---------'

    # for player_id in [next(cycleit) for i in range()]:
    while True:
        player_id = next(cycleit)
        print '(', player_id, ')'
        current_player = players[player_id]

        world_state = engine.getState()

        valid_moves = engine.getValidMoves()

        try:
            if rps_counter >= rps_max:
                break
            if game_type == 'rps' and rps_counter < rps_max:
                rps_counter = rps_counter + 1

                ret = current_player.take_turn({
                    "valid_moves": valid_moves,
                    "player": 1,
                    "history": player_1_history
                })
                print type(ret)
                print "RET TAG", ret['tag']
                engine.makeMove({
                    'contents': [],
                    'tag': rps_mapping[ret['tag']]
                })
                status = engine.getStatus()

                cb("HELLO WORLDDDDDD")
                print "SVG:", engine.renderState()

                if status['tag'] == 'PlayerWin' and status['contents'] == 1:
                    print player_id, "player wins"
                    players[1].win()
                    players[0].lose()
                    engine.reset()
                    continue

                if status['tag'] == 'PlayerWin' and status['contents'] == 0:
                    print player_id, "player loses"
                    players[0].win()
                    players[1].lose()
                    engine.reset()
                    continue

                if status['tag'] == 'Drawn':
                    print player_id, "player drawn"
                    players[0].tie()
                    players[1].tie()
                    engine.reset()
                    continue
        except:
            print sys.exc_info()
            break
        if game_type == 'tron':
            state = engine.getState()

            # p1_pos = state[0][0] (x, y)
            # p1_dir = state[0][1]['tag']
            # p2_pos_dir = state[1] (x, y, )
            # grid = state[2][1]
            # bounds = state[2][0]
            # coords = grid[i][j][0]
            # maybe wall_owner = grid[i][j][1]

            cb(engine.renderState())
            p1_pos = state[0][0]
            p1_dir = state[0][1]['tag']

            p2_pos = state[1][0]
            p2_dir = state[1][1]['tag']

            curr_pos = state[player_id][0]
            curr_dir = state[player_id][1][
                'tag']  # 'North', 'South', 'East', 'West'

            grid = state[2][1]
            bounds = state[2][0]
            # coords = grid[i][j][0]

            dir_py_to_hask = {
                'r': 'East',
                'l': 'West',
                'd': 'South',
                'u': 'North'
            }
            dir_hask_to_py = {
                'East': 'r',
                'West': 'l',
                'South': 'd',
                'North': 'u'
            }  #todo dict comp (faster to not use new syntax now)
            moves_legend_py_to_hask = {
                'l': 'TurnCW',
                'r': 'TurnCCW',
                's': 'KeepGoing'
            }

            # simlu updates
            moves = []
            for i in [0, 1]:
                current_player = players[i]

                ret = current_player.take_turn({
                    "valid_moves": ['l', 'r', 's'],
                    "player":
                    i,
                    "history":
                    player_1_history,
                    "board": {tuple(k): v
                              for (k, v) in grid},
                    "bounds":
                    bounds,
                    "current_direction":
                    dir_hask_to_py[curr_dir]
                })

                print ret
                moves.append({
                    'contents': [],
                    'tag': moves_legend_py_to_hask[ret]
                })

            move = [move for move in moves]
            print json.dumps(move)
            engine.makeMove(move)

            status = engine.getStatus()
            print status

            if status['tag'] == 'PlayerWin' and status['contents'] == 1:
                print player_id, "player wins"
                players[1].win()
                players[0].lose()
                engine.reset()
                break

            if status['tag'] == 'PlayerWin' and status['contents'] == 0:
                print player_id, "player loses"
                players[0].win()
                players[1].lose()
                engine.reset()
                break

            if status['tag'] == 'Drawn':
                print player_id, "player drawn"
                players[0].tie()
                players[1].tie()
                engine.reset()
                break