Giveaway 4

Authorflug
Submission date2019-06-04 00:16:13.250417
Rating1323
Matches played234
Win rate13.68

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

Source code:

#Giveaway version 4
#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

#BADCHOICES algorithm is based on "Really losing program" by Gandaro
#Submission date	2011-06-29 19:55:29.758877

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

BADCHOICES = {
    "R": "S",
    "P": "R",
    "S": "P"
}

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

   badcount = {}
   badcount['R'] = badcount['P'] = badcount['S'] = 0

else:
    rounds += 1;
    badcount[input] += 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 = 35
minChange = 35
decisionPoint = -1
recentTot = 0
origChoice = choice1
initialRounds = (minChange+1) * len(strategynamespick)
#if (debug == 1): print initialRounds

if (hold == 0 and rounds - lastChange > minChange):
   for x in range(recents):
       if (rounds-x>0):
          recentTot += winlist[rounds-x]
   #if (debug == 1): print "rdrlm: ", recentTot, decisionPoint, rounds, lastChange, minChange
   if (recentTot > decisionPoint):
      
      if (strategiestried < len(strategynamespick) - 1 ):
         #print "strategiestried: ", strategiestried, len(strategynamespick)
         if (strategynamespick.index(choice1) + 1 < len(strategynamespick)):
            choice1 = strategynamespick[strategynamespick.index(choice1) + 1]         
         strategiestried += 1
         #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
         if (debug == 1): print "SELECTING NEXT TRIAL STRATEGY: ", choice1, rounds
      else:
           choice2 = random.choice(strategynamespick)
           for n in strategynamespick:
               if (roundsperstrategylist[n] >= minChange) and (roundsperstrategylist[choice2] < 1 or float(winsperstrategylist[n])/float(roundsperstrategylist[n]) <= float(winsperstrategylist[choice2])/float(roundsperstrategylist[choice2])):
                  if (debug == 1): print n, choice2, ": ", roundsperstrategylist[n], roundsperstrategylist[choice2], float(winsperstrategylist[n])/float(roundsperstrategylist[n]),  float(winsperstrategylist[choice2])/float(roundsperstrategylist[choice2])
                  choice2 = n 
           
           
           choice1 = choice2    
           if (debug == 1): print "CHANGING STRATEGICALLY: ", choice1, rounds
           if (debug == 1): print winsperstrategylist 
           if (debug == 1): print roundsperstrategylist   

               
      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))):
if (hold == 1 or won < (lost - 25) or (rounds > 500 and won < (lost - 15))):   
   choice1 = "random"
   if hold == 0:
      if (debug == 1): print rounds, won, lost, draw 
      if (debug == 1): print "HOLDING: ", choice1, rounds
   hold = 1
   
   
##getting too close for comfort, switch back to losing   
if (hold == 1 and won > (lost - 5)):
           choice2 = random.choice(strategynamespick)
           for n in strategynamespick:
           
               nsuccess = 0
               if ( roundsperstrategylist[n] != 0):
                  nsuccess = float(winsperstrategylist[n])/float(roundsperstrategylist[n])
                  
               csuccess = 0
               if roundsperstrategylist[choice2] != 0:
                  float(winsperstrategylist[choice2])/float(roundsperstrategylist[choice2])   
                  
               if (roundsperstrategylist[n] >= minChange) and (roundsperstrategylist[choice2] < 1 or nsuccess <= csuccess):
                  if (debug == 1): print n, choice2, ": ", roundsperstrategylist[n], roundsperstrategylist[choice2], nsuccess, csuccess
                  choice2 = n 
           
           
           choice1 = choice2    
           if (debug == 1): print "LEAVING HOLD STRATEGICALLY: ", choice1, rounds
           if (debug == 1): print winsperstrategylist 
           if (debug == 1): print roundsperstrategylist   
           hold = 0      
   
   
   
   
           
    
#switch = random.choice([0,1,1,1,1,1,1,1])        

#if (debug == 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":  
   if random.randint(0,6)==1: 
      output = "R"
   else: output = random.choice(["R","P","S"])
   
elif choice1 == "constantp":
   if random.randint(0,6)==1:   
      output = "P"
   else: output = random.choice(["R","P","S"])
   
elif choice1 == "constants":   
   if random.randint(0,6)==1:
      output = "S"
   else: output = random.choice(["R","P","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"])
      
elif choice1 == "badchoices":
     for name, num in badcount.iteritems():
         if num == max(badcount.itervalues()):
            bad_mostly_used = name
            break

     output = BADCHOICES[bad_mostly_used]
      
      
   
## 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
#if (debug == 1): print "hwld:" , hold, won, lost, draw
#if (debug == 1): print "choice1: ", choice1
#if (debug == 1): print "currConstant: ", currConstant
#if (debug == 1): print "Output: ", output

strategylist.append(choice1)

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

saveinput = input  
saveoutput = output