Bloomberg Flower

This program has been disqualified.


AuthorNeutrinoPlus
Submission date2016-01-21 20:11:07.806656
Rating6579
Matches played232
Win rate68.1

Source code:

import random

if input == "":
	# first time, initialize variables
	wins = ['RS', 'SP', 'PR']
	loopArray = [3,4,5,6,7]
	backUpStrategyCount = 2
	isDebug = False
	isSummary = False

	_initialScore = 0 # -inf-inf : 0.1
	_scaleFactor = 1 # 1-10 : 0.1
	_decayFactor = 0.95 # 0.1-1 : 0.1
	_occuranceFactor = 0.5 # 0-1 : 0.1
	_confidenceFactor = 0 # -inf-inf : 0.1

	myPlay = ''
	hisPlay = ''
	myScore = ''
	predictorPlay = []
	predictorScore = []
	lastPredictedPlay = ''

	output = random.choice(['R','P','S'])
else:
	# record opponent play
	hisPlay += input
	# get my last play
	output = myPlay[-1]
	# determine scoring
	if (output+input) in wins:
		myScore += '2'
	elif (input+output) in wins:
		myScore += '0'
	else:
		myScore += '1'
	# update score with decay factor
	predictorScore[:] = [x*_decayFactor for x in predictorScore]
	# update predictors with past move
	for N in loopArray:
		# update score based on win / tie / lose
		if lastPredictedPlay in predictorPlay:
			i = predictorPlay.index(lastPredictedPlay)
			predictorScore[i] += _scaleFactor * (int(myScore[-1]) - 1)
		else:
			if (lastPredictedPlay != ''):
				predictorPlay.append(lastPredictedPlay)
				predictorScore.append(_initialScore + _scaleFactor * (int(myScore[-1]) - 1))
		# update predictors
		if len(hisPlay) >= N+1:
			nP = getLastN1Play(N, hisPlay)
			# if predictor already contains our past play, update predictor
			if nP in predictorPlay:
				i = predictorPlay.index(nP)
				predictorScore[i] += _occuranceFactor
			# if first time, initialize our past play into predictor
			else:
				predictorPlay.append(nP)
				predictorScore.append(_initialScore)
	# if less than 10 times played, return random
	if len(myPlay) <= 9:
		# return random
		output = random.choice(['R','P','S'])
	else:
		# generate our best estimated play
		tmpNPlay = []
		tmpNScore = []
		for N in loopArray:
			nP = getLastNPlay(N-1, hisPlay)
			tmpScore = []
			letterCase = ['R','P','S']
			for letter in letterCase:
				tmpP = nP
				tmpP += letter
				if tmpP in predictorPlay:
					i = predictorPlay.index(tmpP)
					tmpScore.append(predictorScore[i])
				else:
					tmpScore.append(-99999)
			maxScore = max(tmpScore)
			i = tmpScore.index(maxScore)
			tmpNPlay.append(nP + letterCase[i])
			tmpNScore.append(maxScore)
		if isDebug:
			print 'tmpNScore: ' + '-'.join('%0.5f'%num for num in tmpNScore)
			print 'tmpNPlay:' + '-'.join(tmpNPlay)
		finalMaxScore = max(tmpNScore)
		if isDebug:
			print 'finalMaxScore: ' + str('%0.5f'%finalMaxScore)
		if finalMaxScore >= _confidenceFactor:
			i = tmpNScore.index(finalMaxScore)
			lastPredictedPlay = tmpNPlay[i]
			output = beatIt(lastPredictedPlay[-1])
		else:
			output = random.choice(['R','P','S'])
			lastPredictedPlay = getLastNPlay(backUpStrategyCount,hisPlay) + output
		if isDebug:
			print 'predictor selected: ' + lastPredictedPlay
if isDebug:
	print '-------'
if isSummary:
	if isDebug:
		print 'hisPlay:' + hisPlay
		print 'myPlay: ' + myPlay
		print 'lastPredictedPlay: ' + lastPredictedPlay
	for i, v in enumerate(predictorPlay):
		print predictorPlay[i] + ': ' + '%0.2f' % predictorScore[i],
	#print 'predictorScore: ' + '-'.join('%0.2f'%num for num in predictorScore)
	#print 'predictorPlay: ' + '-'.join(predictorPlay)
	print
	print '**************'
# record our play
myPlay += output

def getLastN1Play(n, data):
	return data[len(data)-n-1:len(data)-1]

def getLastNPlay(n, data):
	return data[len(data)-n:]

def beatIt(play):
	if play == 'R':
		return 'P'
	elif play == 'P':
		return 'S'
	elif play == 'S':
		return 'R'
	else:
		return 'X'

def loseIt(play):
	if play == 'R':
		return 'S'
	elif play == 'P':
		return 'R'
	elif play == 'S':
		return 'P'
	else:
		return 'X'