Testv5235

AuthorAlex
Submission date2018-12-29 08:25:41.750773
Rating4644
Matches played253
Win rate43.08

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 (0 , 0)
    if(action[(action.index(input_player) + 1)% 3] == input_ai):
        return (-1, 1)
    return (1,  -1)

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_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