Testv3235

AuthorAlex
Submission date2018-12-28 18:37:10.188133
Rating5113
Matches played264
Win rate45.45

Use rpsrunner.py to play unranked matches on your computer.

Source code:

from random import sample


# Add rotation_strat

def argmax(list):
    max = -1
    index = -1
    for i , elem in enumerate(list):
        if(elem > max):
            max = elem
            index = i
    return index
    
def counter_move(move):
    return action[(action.index(move) + 1)% 3]


def opponent_choice(last_move):
    last_move = last_move[1]
    if(last_move in action):
        return counter_move(last_move)
    else:
        return random_choice(last_move)

def most_used_choice(last_move):
    last_move = last_move[0]
    if(last_move in action):
        used_count[action.index(last_move)] += 1
        return counter_move(action[argmax(used_count)])
    else:
        return random_choice(last_move)
    
def random_choice(last_move):
    return sample(action , 1)[0]

def last_choice(last_move):
    last_move = last_move[0]
    if(last_move in moves):
        if(moves.index(last_move) + 1 < len(moves)):
            match = [x for x in moves if x == last_move]
            return counter_move(sample(match , 1)[0])
    return random_choice(last_move)

def decide_winner(input_player , input_ai):
    if(input_player == input_ai):
        return (1 , 1)
    if(action[(action.index(input_player) + 1)% 3] == input_ai):
        return (0, 1)
    return (1,  0)

def get_optimal_move(current_move_player , last_move_player , last_move_ai):
    for i , func in enumerate(strats):
        strats_points[i] += decide_winner(current_move_player , func((last_move_player , last_move_ai,)))[1]
    return strats[argmax(strats_points)]((last_move_player , last_move_ai,))


if not input:
    moves = []
    action  = ["R" , "P" , "S"]
    used_count = [0, 0, 0]
    strats_points = []
    strats = []
    strats.append(last_choice)
    strats.append(random_choice)
    strats.append(most_used_choice)
    strats.append(opponent_choice)
    strats_points = [0] * len(strats)
    last_move_player = ""
    last_move_ai = ""
    current_move_player = ""
    current_move_ai = "R"
else:
    current_move_player = input
    current_move_ai =  get_optimal_move(current_move_player , last_move_player , last_move_ai)
    output = current_move_ai  
    last_move_player = current_move_player
    last_move_ai = current_move_ai
    moves.append(last_move_player)
output = current_move_ai