Giveaway 2

Authorflug
Submission date2019-06-03 20:23:21.139682
Rating1543
Matches played208
Win rate13.94

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

Source code:

#Giveaway version 1
#Trying to lose as many rounds as possible and get to the bottom of the leaderboard
#This is difficult already with some of the (inadvertantly?) really bad RPS programs out there
#But it could get much more difficult fast if any clever folks start 
#really trying to be bad

import random
#rand = random.SystemRandom()
#choice1 = random.choice(["1","2","3"])
output = ""

if input == "":
   saveinput = ""
   saveoutput = ""
   rounds=0
   won=0
   lost=0   
   draw=0
   hold=0
   
   
   currConstant = random.choice(["S","P","R"])
     
   strategynamesall = ["constantr","constantp","constants", "mirror", "repeat","random"]
   strategynamespick = ["constantr","constantp","constants", "mirror", "repeat"]
   choice1 = random.choice(strategynamespick)
   #print "STARTING: ", choice1, rounds
   winlist = []
   strategylist = []
   winsperstrategylist = dict.fromkeys(strategynamesall,0)
   roundsperstrategylist = dict.fromkeys(strategynamesall,0)
   lastChange=0  
else:
    rounds += 1;

#if (choice1 == ""):
#   choice1 = "constant"


roundsperstrategylist[choice1] += 1
   

   
if saveoutput==input:
   draw += 1
   winlist.append(0)
   winsperstrategylist[choice1] += 0
elif (input == "R" and saveoutput == "S") or (input == "S" and saveoutput == "P") or (input == "P" and saveoutput == "R"): 
   lost += 1
   winlist.append(-1)
   winsperstrategylist[choice1] += -1   
else:
    won += 1
    winlist.append(1)
    winsperstrategylist[choice1] += 1
    
# Change the constant answer periodically
if (rounds % 67 == 0):
   currConstant = random.choice(["S","P","R"])  

    
recents = 40
minChange = 40
decisionPoint = -5
recentTot = 0
origChoice = choice1

if (hold == 0 and rounds - lastChange > minChange):
   for x in range(recents):
       if (rounds-x>0):
          recentTot += winlist[rounds-x]
   #print "rdrlm: ", recentTot, decisionPoint, rounds, lastChange, minChange
   if (recentTot > decisionPoint):
      
      if (rounds < 205):
         choice2 = choice1
         x = 0
         tries = len(strategynamespick)*10.
         while choice1 == choice2 and x < tries:
               choice2 = random.choice(strategynamespick)
               if roundsperstrategylist[choice2]>5: choice2=choice1
               x+=1
         choice1=choice2
         #print "CHANGING RANDOMLY: ", choice1, rounds
      else:
           choice2 = random.choice(strategynamespick)
           for n in strategynamespick:
               if roundsperstrategylist[n] > 100 and (roundsperstrategylist[choice2] < 1 or winsperstrategylist[n]/roundsperstrategylist[n] < winsperstrategylist[choice2]/roundsperstrategylist[choice2]):
                   choice2 = n 
           choice1 = choice2    
           #print "CHANGING STRATEGICALLY: ", choice1, rounds
               
      lastChange=rounds            

#choice1 = "constant"    
   
##OR if we have a decent little loss buildup, we just try to hold it   
if (hold == 1 or won < (lost - 150) or (rounds > 500 and won < (lost - 40))):
   
   choice1 = "random"
   #if hold == 0:
      #print rounds, won, lost, draw 
      #print "HOLDING: ", choice1, rounds
   hold = 1
   
   
##getting too close for comfort, switch back to losing   
if (hold == 1 and rounds < 700 and won > (lost - 10)):
   choice2 = random.choice(strategynamespick)
   for n in strategynamespick:
       if roundsperstrategylist[n] > 100 and (roundsperstrategylist[choice2] < 1 or winsperstrategylist[n]/roundsperstrategylist[n] < winsperstrategylist[choice2]/roundsperstrategylist[choice2]):
           choice2 = n 
   choice1 = choice2    

   #if hold == 1: print "LEAVING HOLD STRATEGICALLY: ", choice1, rounds
   hold = 0      
   
   
   
   
           
    
#switch = random.choice([0,1,1,1,1,1,1,1])        

#print choice1
       
## Just keep outputting the same response each time
#  Most bots trying to win should beat this handily
#  But it would be easily defeated by another giveaway bot
if choice1 == "constantr":   
   output = "R"
   
elif choice1 == "constantp":   
   output = "P"
   
elif choice1 == "constants":   
   output = "S"

##Mirror the opponent's move back to them one move later 
# This seems to lose badly to any bot trying to win at all
#
elif choice1 == "mirror":
   if (input == ""):
      output = currConstant
   else:
      output = input 

## Repeat a simple pattern   
elif choice1 == "repeat":
   if (saveoutput == "R"):
      output = "P"
   elif (saveoutput == "P"):
      output = "S"
   elif (saveoutput == "S"):
      output = "R"
   else:
      output = random.choice(["R","P","S"])
      
   
## If we're losing badly enough we can switch to random mode and no one should
#  be able to recover and beat us   

elif choice1 == "random":
   output = random.choice(["R","P","S"])
   
else:
   #print "HELPME!"
   output = currConstant
      
   
#if choice1 == "5":
#   if (input == "" or switch == 1): output = random.choice(["R","R","R","R","P","S"])
#   else: output = input
#if choice1 == "6":
#   if (input == "" or switch == 1): output = random.choice(["R","P","P","P","P","S"])
#   else: output = input
#if choice1 == "7":
#   if (input == "" or switch == 1): output = random.choice(["R","P","S","S","S","S"])
#   else: output = input
#print "hwld:" , hold, won, lost, draw
#print "choice1: ", choice1
#print "currConstant: ", currConstant
#print "Output: ", output

strategylist.append(choice1)

#if (rounds % 999 == 0 and rounds > 1): 
   #print '[%s]' % ', '.join(map(str, strategylist))
   #print ("Giveaway strategies: ", strategylist) 
   #print winsperstrategylist 
   #print roundsperstrategylist   
   #print "END:", rounds, won, lost, draw
   #if (won>lost): print "WON\n"
   #else: print "LOST\n"

saveinput = input  
saveoutput = output