PakKhU 4

AuthorPrakhar
Submission date2019-05-03 04:44:45.199255
Rating4977
Matches played241
Win rate47.3

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

Source code:

import random

last_strat_used = 0
chances = 0
strat_1 = 0
strat_2 = 0
strat_3 = 0
human_moves_arr = []
result_arr = []
rep_arr = []
repi = 0
draw_rep_arr = []
win_rep_arr = []
lose_rep_arr = []
draw_repi = 0
win_repi = 0
lose_repi = 0

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
    
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    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:
            if strat_1 == strat_2 and strat_2 == strat_3:
                if n == 0:
                    last_strat_used = 1
                    return strat_1_func()
                elif n == 1:
                    last_strat_used = 2
                    return strat_2_func()
                else:
                    last_strat_used = 3
                    return strat_3_func()

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

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

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

            elif strat_1 > strat_2 and strat_1 > strat_3:
                last_strat_used = 1
                return strat_1_func()

            elif strat_2 > strat_3:
                last_strat_used = 2
                return strat_2_func()

            else:
                last_strat_used = 3
                return strat_3_func()
    chances+=1     


def seq_type_1():
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    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():
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    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():
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    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():
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    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():
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    return find_pattern(rep_arr, repi, 4)


def strat_2_func():
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    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 += 2*(round(i+3)/3)
            else:
                c += 2*(round(i+3)/3)

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


def find_pattern(arr, n, q):
    global last_strat_used
    global strat_1
    global strat_2
    global strat_3
    global human_moves_arr
    global result_arr
    global rep_arr
    global repi
    global draw_rep_arr
    global win_rep_arr
    global lose_rep_arr
    global draw_repi
    global win_repi
    global lose_repi
    global chances

    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 += 2*(round(i+3)/3)
                elif i + 3 <= n and arr[i + 3] == 0:
                    d += 2*(round(i+3)/3)
            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])

for i in range(1, 501):
    rep_arr.append(int(0))
    draw_rep_arr.append(int(0))
    win_rep_arr.append(int(0))
    lose_rep_arr.append(int(0))
    human_moves_arr.append(int(0))
    result_arr.append(int(0))

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
else:

	if input == "R":

		human_moves_arr[chances-1] = 1

		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 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 game_move==1:
			result_arr[chances-1] = 1
		elif game_move==2:
			result_arr[chances-1] = -1
		else:
			result_arr[chances-1] = 0

	if result_arr[chances-1] == 1:
             if last_strat_used == 1:
                 strat_1 += 2
             elif last_strat_used == 2:
                 strat_2 += 2
             elif last_strat_used == 3:
                 strat_3 += 2
        elif result_arr[chances-1] == -1:
             if last_strat_used == 1:
                 strat_1 -= 3
             elif last_strat_used == 2:
                 strat_2 -= 3
             elif last_strat_used == 3:
                 strat_3 -= 3
        
        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"