PakKhU 13

AuthorPrakhar
Submission date2019-05-05 16:25:38.808613
Rating5263
Matches played238
Win rate50.42

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

Source code:

import random

def back_move(move):
    if move == 1:
        return 3
    elif move == 2:
        return 1
    else:
        return 3


def front_move(move):
    if move == 1:
        return 2
    elif move == 2:
        return 3
    else:
        return 1


def pakkhu_think():
    n = random.randint(1, 3) - 1

    if chances == 0 or chances == 1 or chances == 2 or chances == 3:
        return random.randint(1, 3)
    else:
        best_move = seq_type_1()

        if best_move == 0:
            best_move = seq_type_2()

        if best_move == 0:
            if chances >= 6:
                best_move = seq_type_3()
            else:
                best_move = back_move(human_moves_arr[chances - 1])

        if best_move != 0:
            return best_move

        else:
            
            o = strat_1_func()
            p = strat_2_func()
            q = strat_3_func()
            
            if strat_1 == strat_2 and strat_2 == strat_3:
                if n == 0:
                    last_strat_used = 1
                    return o
                elif n == 1:
                    last_strat_used = 2
                    return p
                else:
                    last_strat_used = 3
                    return q

            elif strat_1 == strat_2 and strat_3 < strat_1:
                n = random.randint(0, 1)
                if n == 0:
                    last_strat_used = 1
                    return o
                else:
                    last_strat_used = 2
                    return p

            elif strat_1 == strat_3 and strat_2 < strat_1:
                n = random.randint(0, 1)
                if n == 0:
                    last_strat_used = 1
                    return o
                else:
                    last_strat_used = 3
                    return q

            elif strat_3 == strat_2 and strat_1 < strat_3:
                n = random.randint(0, 1)
                if n == 0:
                    last_strat_used = 3
                    return q
                else:
                    last_strat_used = 2
                    return p

            elif strat_1 > strat_2 and strat_1 > strat_3:
                last_strat_used = 1
                return o

            elif strat_2 > strat_3:
                last_strat_used = 2
                return p

            else:
                last_strat_used = 3
                return q  


def seq_type_1():
    a = human_moves_arr[chances - 1]
    b = human_moves_arr[chances - 2]
    c = human_moves_arr[chances - 3]
    d = human_moves_arr[chances - 4]

    if a == b and b == c and c == d:
        return front_move(a)
    else:
        return 0


def seq_type_2():
    a = human_moves_arr[chances - 1]
    b = human_moves_arr[chances - 2]
    c = human_moves_arr[chances - 3]
    d = human_moves_arr[chances - 4]

    if a != b and a == c and b == d:
        return front_move(b)
    else:
        return 0


def seq_type_3():
    a = human_moves_arr[chances - 1]
    b = human_moves_arr[chances - 2]
    c = human_moves_arr[chances - 3]
    d = human_moves_arr[chances - 4]
    e = human_moves_arr[chances - 5]
    f = human_moves_arr[chances - 6]

    if a != b and b != c and c != a and a == d and b == e and c == f:
        return front_move(c)
    else:
        return 0


def strat_1_func():

    if result_arr[chances - 1] == 0:
        best_move = find_pattern(draw_rep_arr, draw_repi, 0)
    elif result_arr[chances - 1] == -1:
        best_move = find_pattern(lose_rep_arr, lose_repi, -1)
    else:
        best_move = find_pattern(win_rep_arr, win_repi, 1)

    return best_move


def strat_3_func():
    return find_pattern(rep_arr, repi, 4)


def strat_2_func():
    d = human_moves_arr[chances - 1]
    a = 0
    b = 0
    c = 0
    for i in range(chances - 2, -1, -1):
        if (i + 1) <= chances - 1 and human_moves_arr[i] == d:
            if human_moves_arr[i + 1] == 1:
                a += 2*(round(i+3)/3)
            elif human_moves_arr[i + 1] == 2:
                b += i*i
            else:
                c += i*i

    if a >= b and a >= c:
        return 2
    elif b >= c:
        return 3
    else:
        return 1


def find_pattern(arr, n, q):
    c = 0
    d = 0
    if n >= 2:
        a = arr[n - 1]
        b = arr[n - 2]
        i = n - 3
        while (i >= 0):
            if arr[i] == b and arr[i + 1] == a:
                if i + 3 <= n and arr[i + 3] == 1:
                    c += i*i
                elif i + 3 <= n and arr[i + 3] == 0:
                    d += i*i
            i -= 1

        if d >= c:
            return front_move(human_moves_arr[chances - 1])
        else:
            return back_move(human_moves_arr[chances - 1])

    else:
        if arr[n - 1] == 1:
            return front_move(human_moves_arr[chances - 1])

        else:
            return back_move(human_moves_arr[chances - 1])
        
        
if input == "":
    last_strat_used = 0
    chances = 0
    strat_1 = 0
    strat_2 = 0
    strat_3 = 0
    repi = 0
    draw_repi = 0
    win_repi = 0
    lose_repi = 0
    o = 0
    p = 0
    q = 0
    human_moves_arr = [0]*1001
    result_arr = [0]*1001
    rep_arr = [0]*1001
    draw_rep_arr = [0]*1001
    win_rep_arr = [0]*1001
    lose_rep_arr = [0]*1001
    
else:
    chances+=1
    if input == "R":
        human_moves_arr[chances-1] = 1
        if o==2:
            strat_1+=2
        elif o==3:
            strat_1-=3
        if p==2:
            strat_2+=2
        elif p==3:
            strat_2-=3
        if q==2:
            strat_3+=2
        elif q==3:
            strat_3-=3    
            

        if game_move==1:
            result_arr[chances-1] = 0
        elif game_move==2:
            result_arr[chances-1] = 1
        else:
            result_arr[chances-1] = -1

    elif input == "P":  
        human_moves_arr[chances-1] = 2
        if o==3:
            strat_1+=2
        elif o==1:
            strat_1-=3
        if p==3:
            strat_2+=2
        elif p==1:
            strat_2-=3
        if q==3:
            strat_3+=2
        elif q==1:
            strat_3-=3

        if game_move==1:
            result_arr[chances-1] = -1
        elif game_move==2:
            result_arr[chances-1] = 0
        else:
            result_arr[chances-1] = 1

    elif input == "S": 
        human_moves_arr[chances-1] = 3
        if o==1:
            strat_1+=2
        elif o==2:
            strat_1-=3
        if p==1:
            strat_2+=2
        elif p==2:
            strat_2-=3
        if q==1:
            strat_3+=2
        elif q==2:
            strat_3-=3

        if game_move==1:
            result_arr[chances-1] = 1
        elif game_move==2:
            result_arr[chances-1] = -1
        else:
            result_arr[chances-1] = 0

    if chances >= 2:
        if result_arr[chances - 1] == 0:
            if human_moves_arr[chances - 2] == human_moves_arr[chances - 1]:
                draw_rep_arr[draw_repi] = 1
            else:
                draw_rep_arr[draw_repi] = 0
            draw_repi += 1
        elif result_arr[chances - 1] == -1:
            if human_moves_arr[chances - 2] == human_moves_arr[chances - 1]:
                lose_rep_arr[lose_repi] = 1
            else:
                lose_rep_arr[lose_repi] = 0
            lose_repi += 1
        else:
            if human_moves_arr[chances - 2] == human_moves_arr[chances - 1]:
                win_rep_arr[win_repi] = 1
            else:
                win_rep_arr[win_repi] = 0
            win_repi += 1

        if human_moves_arr[chances - 1] == human_moves_arr[chances - 2]:
            rep_arr[repi] = 1
        else:
            rep_arr[repi] = 0

        repi += 1           
    
game_move = pakkhu_think()

if game_move==1:
    output = "R"
elif game_move==2:
    output = "P"
else:
    output = "S"