matching

AuthorNitin Bandaru
Submission date2015-12-07 08:15:24.876986
Rating5001
Matches played459
Win rate50.54

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

Source code:

import random

rps = ['R', 'P', 'S']
tempHistoryLength = 10


if not input:
	fullHistory = ""
	tempHistory = ""
	counterMove = {'R':'P', 'P':'S', 'S':'R'}
	output = random.choice(rps)
else:
	if output:
		gameRound = input+output
		fullHistory += gameRound
		if len(tempHistory) == tempHistoryLength:
			tempHistory = tempHistory[2:] + gameRound
		else:
			tempHistory += gameRound
	lastFoundIndex = fullHistory.rfind(tempHistory, 0, len(fullHistory))
	if lastFoundIndex == -1:
		output = random.choice(rps)
	else:
		index = lastFoundIndex + len(tempHistory)
		if len(fullHistory) == index:
			output = random.choice(rps)
		else:
			predictedInput = fullHistory[index]
			output = counterMove[predictedInput]