[Question]
  [
Many people consider RPS to be a game of chance. If both players play unpredictably, the best strategy is to play randomly. However, let's introduce a bit of predictability to it. 
Each bot will have a chance to tell the other bot what it's going to play simultaneously. Then there is a pause in which each bot will know what the other player announced. If it plays that weapon it announced it will score one point in addition to its points for a win loss or draw.
A win is worth two points, a draw, one point, and a loss 0 points.
```
     Honest Bot       Dishonest
Win     3                  2
Draw    2                  1
Loss    1                  0
```
It is in your best interest to be honest (but also to make sure your opponent does not believe you).
The matches will be played in a round robin format, and the objective will be to maximise your own total score across the matches you play. 
## I/O Format:
* Your bot will be a Python 2.7 function that takes 4 arguments, and must have a unique name (which will be used to represent your submission).
* The first two arguments will always be, in order: the opponent's past moves, followed by your past moves. These will be a list in order from first to most recent round, with each index containing a list with the move the opponent claimed they would make, followed by the move they actually made.
* The next two arguments will allow your bot to determine if this is an "honest" round or a "real" round. If it is an "honest" round, they will both be None. If it is a "real" round, they will be, in order, the move your opponent declared they would make, followed by the move you declared you would make.
* All arguments or portions of arguments that represent moves will use "R", "P", and "S" to represent rock, paper, and scissors, respectively.
* Your function should return either an "R" for rock, a "P" for paper, or an "S" for scissors. Bots that have the ability to return other values will be disqualified.
* Each bot will be run against every other bot 200 times, and itself 100 times. The goal is to be the bot with the most points at the end of the competition.
* In regards to discussion in the comments, submissions may not read from or write to any file, or in any way sabotage or read opponent's code.
## Examples:
These are four example bots I put together quickly. They will be joining the competition as additional bots. If you lose to the last one, you have some work to do. 
```
def honestpaper(I,dont,care,about_these):
    return "P"
def honestrock(I,dont,care,about_these):
    return "R"
def honestscissors(I,dont,care,about_these):
    return "S"
import random
def randombot(I,dont,care,about_these):
    return random.choice(["R","P","S"])
```
## Controller:
And here's the controller I'll be using. New submissions will be imported at the beginning and added to the bot\_map dictionary. 
```
from honestrock import honestrock
from honestpaper import honestpaper
from honestscissors import honestscissors
from randombot import randombot
bot_map = {
  0:honestrock, 1:honestpaper, 2:honestscissors, 3:randombot
}
player_num=len(bot_map)
def real(history1,history2,number,honest1,honest2):
    return bot_map[number](history1,history2,honest1,honest2)
def honest(history1,history2,number):
    return bot_map[number](history1,history2,None,None)
def play_match(num1,num2):
    history1=[]
    history2=[]
    score1=0
    score2=0
    for x in range(250):
        h1=honest(history2,history1,num1)
        h2=honest(history1,history2,num2)
        r1=real(history2,history1,num1,h2,h1)
        r2=real(history1,history2,num2,h1,h2)
        if h1==r1: score1+=1
        if h2==r2: score2+=1
        if r1==r2: score1+=1; score2+=1
        elif r1=="R":
            if r2=="P": score2+=2
            else: score1+=2
        elif r1=="P":
            if r2=="S": score2+=2
            else: score1+=2
        else:
            if r2=="R": score2+=2
            else: score1+=2
        history1.append([h1,r1])
        history2.append([h2,r2])
    return score1,score2
scores = []
for x in range(player_num):
    scores.append(0)
for _ in range(100):
    for x in range(player_num):
        for y in range(player_num):
            scorex,scorey=play_match(x,y)
            scores[x]+=scorex
            scores[y]+=scorey
for score in scores:
    print score
```
## Final Scores:
```
csbot                    3430397
thompson                 3410414
rlbot                    3340373
have_we_been_here_before 3270133
mason                    3227817
deepthought              3019363
adaptive_bot             2957506
THEbot                   2810535
dontlietome              2752984
irememberhowyoulie       2683508
learningbot4             2678388
betrayal                 2635901
averager                 2593368
honestrandom             2580764
twothirds                2568620
mirrorbot                2539016
tit4tat                  2537981
honestscissors           2486401
trusting_bot             2466662
rotate_scissors          2456069
rotate_paper             2455038
rotate_rock              2454999
honestpaper              2412600
honestrock               2361196
rockBot                  2283604
trustingRandom           2266456
user5957401bot           2250887
randombot                2065943
Dx                       1622238
liarliar                 1532558
everybodylies            1452785
```
  
[Answer]
## Mason
Tries to pick up information about other bots such as how honest they are and how they're effected by my first move. I then try and find other obvious bots that follow a pattern and exploit those into giving me more points. Finally, the Mason has a secret weapon: knowledge of a secret society where both bots participating mutually go for a full draw, gaining 500 points each. Unfortunately the secret is rather... Well secret and changes every time the Mason does.
```
def mason(op_hist, my_hist, op_move, my_move):
    win_map = {"R": "P", "P": "S", "S": "R"}
    lose_map = {"R": "S", "P": "R", "S": "P"}
    if not len(op_hist):
        return "S"
    if op_hist[0] == ['S', 'S']:
        code = "S" + "".join("RPS"[ord(i) % 3] if isinstance(i, str) else "RPS"[i % 3] for i in __import__("sys")._getframe().f_code.co_code)[1::2]
        honest, guess = zip(*op_hist)
        if honest == guess == tuple(code[:len(op_hist)]):
            return code[len(op_hist)]
    op_honesty = sum(len(set(round))-1 for round in op_hist) / float(len(op_hist))
    if not my_move:
        moves = "".join(i[1] for i in op_hist)
        # Identify rotators
        if "PSRPSR" in moves:
            return moves[-2]
        # Identify consecutive moves
        if "RRRRR" in moves[:-10] or "SSSSS" in moves[:-10] or "PPPPP" in moves[:-10]:
            return win_map[moves[-1]]
        # Try just what wins against whatever they choose most
        return win_map[max("RPS", key=moves.count)]
    op_beats_my_honest = sum(win_map[me[0]] == op[1] for op, me in zip(op_hist, my_hist)) / float(len(op_hist))
    op_draws_my_honest = sum(me[0] == op[1] for op, me in zip(op_hist, my_hist)) / float(len(op_hist))
    op_loses_my_honest = sum(lose_map[me[0]] == op[1] for op, me in zip(op_hist, my_hist)) / float(len(op_hist))
    if op_honesty <= 0.4:
        return win_map[op_move]
    max_prob = max((op_loses_my_honest, op_draws_my_honest, op_beats_my_honest))
    if max_prob >= 0.6:
        if op_beats_my_honest == max_prob:
            return lose_map[my_move]
        if op_draws_my_honest == max_prob:
            return win_map[my_move]
        if op_loses_my_honest == max_prob:
            return my_move
        assert False
    return my_move
```
[Answer]
# Rlbot: reinforcement learning
Uses a reinforcement learning approach, tackling this game in a way similar to the n-armed bandit problem. It does so in two ways: tries to learn which declaration is better against each opponent and sticks to that one (useful against constant bots), and tries to learn the outcome of various moves in previous similar situations (similar in regard to the relative plays, e.g. rock vs paper is similar to a previous paper vs scissor). The initial assumptions are optimistic, so this player will assume that being honest will give it 3 points and lying will give 2, and will therefore always be honest until proven otherwise.
*Update:* The first tournament results highlighted a problem with this bot, which was its inability to detect patterns in its opponents' declarations (which made it play suboptimally against rotators). I've then added a pattern-matching component to the code for the honest rounds, which uses a regex to look for the longest suffix in the history of opponent declarations that is present somewhere previously in that history, and what move was played after that. We assume that the opponent will play the same move again, and use reinforcement learning as before to decide what the best answer to that should be.
```
import re
def rlbot(hismoves,mymoves,hismove,mymove):
 def score(d,m1,m2):
  s=0
  if m1==m2:
   s=1
  elif (m1+m2) in "RPSR":
   s=2
  return s+(d==m2)
 alpha=0.2
 if mymove:
  history=[([d1,m1],[d2,m2]) for ((d1,m1),(d2,m2)) in zip(hismoves,mymoves) if score(None,hismove,mymove)==score(None,d1,d2)]
  bestscore=-1
  bestmove=""
  for move in "RPS":
   ev=2+(move==mymove)
   for ((d1,m1),(d2,m2)) in history:
    if score(None,move,mymove)==score(None,m2,d2):
     ev=(1-alpha)*ev+alpha*score(d2,m1,m2)
   if ev>bestscore:
    bestscore=ev
    bestmove=move
  return bestmove
 else:
  if len(hismoves)==0:
   return "R"
  bestscore=-1
  bestmove=""
  hisdeclarations="".join(d for [d,m] in hismoves)
  predicted_move=re.search(r'(.*)\n.*\1(.)',hisdeclarations+'\n'+hisdeclarations).group(2)
  history=[([d1,m1],[d2,m2]) for ((d1,m1),(d2,m2)) in zip(hismoves,mymoves) if d1==predicted_move]
  for move in "RPS":
   ev=3
   for (his,my) in history:
    (d1,m1)=his
    (d2,m2)=my
    if d2==move:
     ev=(1-alpha)*ev+alpha*score(d2,m1,m2)
   if ev>bestscore:
    bestscore=ev
    bestmove=move
  return bestmove
```
[Try it online!](https://tio.run/##vVZLT9wwEL7vr7BWqnBYE2GjXqjcey8VgmOIopAYknYTR3bYsv3zdMZ2Xlu2rKqqe0hie75vnp7Zbt9XuhWvpXok8KFs3@WdMvQLK3XbsyI3iuUP@rnP@kpZFV2vCPyM6p9NS9Y369VqQhpdfD8ReLsA2qK2Vht7IvgOwHXTadMTk7elbhyV/3zQ/WksXjwuKl0XiiZgEQN3GJCnkbfN6D7vVea8qjgjlWCkthkLRiNd4KKIxmgwNC5KtqoFREQ@kKt0weVj@x5Z4GEYpj@QjVF7jy9QOQuXfK9DGJUn3mL4qto2eqcsa/b@HTbCGqlR2BbaKFqyhrNGuNhaeQnP@pE0XMpGuHBbyeGltrBLG74BSVJj/m/ubtdBQKzGpNgNLREKGSD5tqtyeRnDMVI63YgAa3pt9jKhSQmqecqSUoAJaUQetSGUut2IUbcbOX0/6@43tyKk9U58hZgdeinl7AwoSxGloP3BlSscyAseligu12tYoX5cDS56D9VOig11UjKQ4/ZRY4ODvlqXNh41sBFooIegQsovXPyic7XbuK/zkC8RErby5Gr3eXTJwycP1W7ccNbjY8rVsA2pUlvrwMDnqitEGky8dJyzW/9OBAFaqmKbm7yvdWthM/6m65aWLlwJVFsaQuQ1AKQzqqyLXpWZozEqtio3RUXNGY3Po/s2Pr/nNI7O2AH55uy@PdscbEbxk9HPHXUB@qelVsKlWNqa/qlkrsYaATYg@r00gnYJm2HtrIAaGyqnFFIO1@a/18Vrt833ylgiSTLNBzYbMuyg/bOxhbNZ72Xz3skOeh90RK8ma58bicUXtA49XOVbGqLGWfgQzAsFW3h4i@WA8DJvgA9R80l2VNeJ3O4y4yOwomDW5D2UMzjIGTwGMwewTNL5WgxrlyvuWnJYiLDAqnrBcoJ4PykqPl4OnQNpuFy6IthoJpoQTZJCHnXa2TlKGi7niThgZDC7qhmvEfJI2pAVRAGAAyL8oFjBZmn4dXB540bOdAiXwIhwKPBwfmr47BShn2aCg5wbXyi5DkOLzBmAHwbrpEAsJFxvHOnFG5w3Rzjv/oLTqre5bk/mmtIbwh/nXafakiYQeMPT6FBATAKCGREEhoHueJlXDXfV1G1P1rbSP@r26Rqmujsh8P@sNu5zvcLq7LA6w00Ghzysi7OszRuVZSvP@@J593J2S9xfGNZFA2gh@PoL "Python 2 – Try It Online")
[Answer]
I've never really used python much so I'm sure I've made a mistake somewhere.
```
import random
def learningbot3(opponentlist,a,opponent,me):
 #tell the other bot a random thing
 if opponent==None:
  return random.choice(["R","P","S"])
 #check whether the other bot has mostly told the truth in the last 10 rounds
 truth=0
 for game in opponentlist[-10:]:
  truth-=1
  if game[0]==game[1]:
   truth+=2
 #assume the other bot will tell the truth
 if truth>=3:
  if me==opponent:
    return me
  elif opponent=="R":
   return "P"
  elif opponent=="P":
   return "S"
  elif opponent=="S":
   return "R"
 #assume the other bot is lying
 elif truth<=-3:
  return random.choice([me,opponent])
  #return opponent
 #pick whatever we said we would
 else:
  return me
```
It should check the last 10 rounds to see how often the opponent lied, then chose a different response depending on that.
[Answer]
Here is my adaptive bot. It analyses the last 2 moves of the opponent to determine if it is an honest bot or not and plays accordingly:
Edit 1: If the other bot is a constant bot (ie. always plays the same weapon) this bot crushes it by playing the winning weapon and being honest at the same time.
Edit 2: Improved constant bot detector to work with rotator bots too.
```
import random
def adaptive_bot(other_past, my_past, other_next, my_next):
    winners = {"R": "P", "P": "S", "S": "R"}
    if my_next is None:
        return winners[other_past[-6:][0][1]] if other_past else random.choice(list(winners.keys()))
    else:
        is_other_honest = all([other_claim == other_move for other_claim, other_move in other_past[-2:]])
        return winners[other_next] if is_other_honest else my_next
```
[Answer]
# csbot
```
def csbot(ophist,myhist,opdecl,mydecl):
  import random
  RPS = "RPS"
  def value(opd,myd,opmove,mymove):
    if opmove==mymove:
      val = 9
    elif opmove+mymove in RPS+RPS:
      val = 20
    else:
      val = -2
    return val+10*(myd==mymove)-(opd==opmove)
  def best(od,md):
    l = float(len(ophist))
    weights = dict([ (m, random.random()/8) for m in RPS ])
    for n in range(len(ophist)):
      if ophist[n][0]==od and myhist[n][0]==md:
        weights[ophist[n][1]] += 1+4*((n+1)/l)**2
    sw = sum([ weights[m] for m in RPS ])
    bestexpect = 0
    for m in RPS:
      expect = sum([ weights[om]/sw*value(od,md,om,m) for om in RPS ])
      if expect > bestexpect:
        bestexpect = expect
        bestmove = m
    return bestmove, bestexpect
  honest = all ([ decl==mv for decl, mv in ophist ])
  if honest:
    if mydecl<>None:
      return mydecl
    expnxt = set();
    for i in range(len(ophist)-1):
      if ophist[i][0]==ophist[-1][0]:
        expnxt.add(ophist[i+1][0])
    if len(expnxt)==1:
      return RPS[ (RPS.index(expnxt.pop())+1) % 3 ]
  if mydecl==None:
    l = float(len(ophist))
    weights = dict([ (m, random.random()) for m in RPS ])
    for n in range(len(ophist)):
      weights[ophist[n][0]] += 1+((n+1)/l)**2
    sw = sum([ weights[m] for m in RPS ])
    bestexpect = 0
    worstexpect = 99
    for m in RPS:
      expect = sum([ best(od,m)[1]/sw*weights[od] for od in RPS ])
      if expect > bestexpect:
        bestexpect = expect
        bestmove = m
      if expect < worstexpect:
        worstexpect = expect
    if bestexpect-worstexpect < 3:
      bestmove = random.choice(RPS)
    return bestmove
  return best(opdecl,mydecl)[0]
```
Be honest as long as the other player is, and detect simple deterministic bots. Play the move that maximizes the expected value, where we mostly go for our points, but also like to not give points to the other player. But own points are better by a factor of ten, hence the unusual numbers in the `value` function. Opponent moves are expected according to how often we have seen them before in this situation (declared moves), but recently seen moves are weighted more than moves seen earlier. For random initial moves (situations never seen before) and some extra fuzzyness, the weights include small extra random numbers.
**Update:** Use expected results also in the honest round. To be able to do this, normalize and take the additional point the opponent may get for honesty into account - it couldn't influence our decission in the real round but is needed now. I considered doing this from the beginning, but wrongfully thought it wouldn't be worthwhile. I saw that it would be possible to give `trusting_bot` less points (but that bot isn't a strong opponent anyway), but missed that extra points could be gained from `rockbot` by good play in the honest round even though its play in this round is random.
[Answer]
# Betrayal
```
def betrayal(yours, mine, you ,me):
    import random
    if you is None:
        pick = random.choice(['R','P','S'])
    else:
        you = you[0]
        me = me[0]
        if len(yours) < 50: #Build myself a reputation of honesty
            pick = me
        else:
            if len(yours) >= 50 and len(yours) < 100:
                honesty = sum([1 if y[0]==y[1] else 0 for y in yours])/float(len(yours))
                if honesty <= 0.5: #If dishonest try to outwit
                    pick = 'S' if me=='R' else 'R' if me == 'P' else 'P'
                else: #Else just plain cheat
                    pick = 'P' if you=='R' else 'R' if you=='S' else 'S'
            elif len(yours) >= 100: #When dishonest moves outweight honest moves, change tactics...
                honesty = sum([1 if y[0]==y[1] else 0 for y in yours[50:]])/float(len(yours[50:]))
                if honesty <= 0.5: #... and just play according to most likely pick
                    what_did_you_do = [k[1] for k in yours if k[1]!=k[0]]
                    index = [i for i,k in enumerate(yours) if k[1]!=k[0]]
                    what_i_said_i_ll_do = [k[0] for i,k in enumerate(mine) if i in index]
                    matches = zip(what_i_said_i_ll_do, what_did_you_do)
                    what_you_might_answer = [k[1] for k in matches if k[0]==me]
                    table = [len([k for k in what_you_might_answer if k=='R']),len([k for k in what_you_might_answer if k=='P']),len([k for k in what_you_might_answer if k=='S'])]
                    maybe_your_pick = ['R','P','S'][table.index(max(table))]
                    pick = 'P' if maybe_your_pick=='R' else 'R' if maybe_your_pick=='S' else 'S'
                else:
                    pick = 'P' if you=='R' else 'R' if you=='S' else 'S'
    return pick
```
The idea is that for the first 50 moves I play honestly, and then once I have lured the opponent into thinking i am honest, play dishonestly, trying to play what would counter what the opponent will play (based on whether he was honest or dishonest in the past). When I reach the point at which i played as often honestly than dishonestly I change tactics and pick the most likely move of the opponent based on previous known configurations.
[Answer]
```
import random
def honestrandom(a, b, c, move):
    if move == None:
        return random.choice(["R","P","S"])
    return move
```
[Answer]
# Bot Name: I Remember How You Lie
```
import random
#Bot Name: I Remember How You Lie
def irememberhowyoulie(opponentlist, mylist, opponentmove, mymove):
    random.seed()
    
    wintable = {
                "R": {"R": 1, "P": 0, "S": 2},
                "P": {"R": 2, "P": 1, "S": 0},
                "S": {"R": 0, "P": 2, "S": 1}
               }
    
    winprob = {
               "R": {"R": 0.0, "P": 0.0, "S": 0.0},
               "P": {"R": 0.0, "P": 0.0, "S": 0.0},
               "S": {"R": 0.0, "P": 0.0, "S": 0.0}
              }
    
    totalprob = {"R": 0, "P": 0, "S": 0}
    
    # Calculate the probability that the opponent will lie base on the probability that it lied in the last 15 ~ 25 rounds
    # And calculate the probability that what the bot will show next
    picklength = min(random.randint(15, 25), len(opponentlist))
    lying, tempsum = 0, 0.0
    pickedup = {"R": 0, "P": 0, "S": 0}
    if picklength == 0:
        lying = 0.5
    else:
        for eachround in opponentlist[-picklength:]:
            pickedup[eachround[1]] += 1
            if eachround[0] != eachround[1]:
                lying += 1
        lying = lying * 1.0 / picklength
    for s in pickedup:
        pickedup[s] = 1.0 / (1 + pickedup[s])
        tempsum += pickedup[s]
        
    #Honest Round
    if opponentmove is None and mymove is None:
        a = random.random() * tempsum
        if a < pickedup["R"]:
            return "R"
        elif a < pickedup["R"] + pickedup["P"]:
            return "P"
        else:
            return "S"
    
    #Real Round
    else:                
        for me in winprob:
            ishonest = 0
            if me == mymove:
                ishonest = 1
            for op in winprob[me]:
                if op == opponentmove:
                    winprob[me][op] = (wintable[me][op] + ishonest) * (1 - lying)
                else:
                    winprob[me][op] = (wintable[me][op] + ishonest) * lying * pickedup[op] / (tempsum - pickedup[opponentmove])
                totalprob[me] += winprob[me][op]
        
        optimalmove, optimalvalue = "R", -9999999.0
        for me in totalprob:
            if totalprob[me] > optimalvalue:
                optimalmove, optimalvalue = me, totalprob[me]
        return optimalmove
```
Tested for several 100-round runs, and turned out that the winner scores about 220 in average. Rather honest I think;)
First time for me to participate in KOTH challenges, so I think there is still room for improvement
[Answer]
## Tit for Tat
The classic Axelrodian contestant: hopeful, yet petty; simple, yet robust. This isn't Prisoner's Dilemma and I made no attempt to predict the opponent's move, so I highly doubt it'll be truly competitive. But "cooperating" still produces the most overall points for contestants, so I think it'll at least do middlingly.
```
import random
def tit4tat(opphist, myhist, oppfut, myfut):
    if (not myfut): return random.choice(['R','P','S'])
    if (not opphist) or opphist[-1][0]==opphist[-1][1]: return myfut
    return random.choice(['R','P','S'])
```
[Answer]
## Two Thirds
Uses the strategy Peter Taylor mentioned in the Sandbox and in [this comment](https://codegolf.stackexchange.com/questions/145175/honest-rock-paper-scissors/145208#comment355477_145175).
It uses the [Nash equilibrium](https://en.wikipedia.org/wiki/Nash_equilibrium).
```
import random
def two_thirds(h_opp, h_me, opp, me):
    def result(opp, me):
        if opp==me: return 0
        if opp=="R" and me=="S" or opp=="S" and me=="P" or opp=="P" and me=="R": return -1
        return 1
    moves = {"R", "P", "S"}
    honest = (opp == None)
    if honest:
        return random.choice(list(moves))
    else:
        res = result(opp, me)
        if res==-1:
            counter = list(moves - {opp, me})[0]
            return random.choice([me,counter,counter])
        if res==1:
            return random.choice([me,me,opp])
        return me
```
[Answer]
# DeepThought
```
def check_not_loose_bot(opHist, myHist):
    not_loose_points = 0
    for i in range(0, len(opHist)):
        if opHist[i][1] == opHist[i][0] or myHist[i][0] == win_map[opHist[i][0]] and opHist[i][1] == win_map[myHist[i][0]]:
            not_loose_points += 1
    not_loose_percent = float(not_loose_points) / len(opHist)
    if not_loose_percent > 0.9:
    #    print("is not willing to loose")
        return True
    return False
def check_trick_bot(opHist, myHist):
    trick_points = 0
    for i in range(0, len(opHist)):
        if opHist[i][1] == win_map[myHist[i][0]]:
            trick_points += 1
    trick_percent = float(trick_points) / len(opHist)
    if trick_percent > 0.9:
  #      print("is tricking me")
        return True
    return False
def check_honest_bot(opHist):
  #  print("check honest")
    honest_points = 0
    for i in range(0, len(opHist)):
        if opHist[i][0] == opHist[i][1] :
            honest_points += 1
    honest_percent = float(honest_points) / len(opHist)
    if honest_percent > 0.9:
    #    print("is honest")
        return True
    return False
def check_self_match(opHist, myHist):
    for i in range(0, len(myHist)):
        if opHist[i][0] != myHist[i][0]:
            # im not playing against myself, because the other one was claiming a different value than i did
#            print("differ: "+str(opHist)+", "+str(myHist))
            return False
        if opHist[i][1] != opHist[i][0]:
#            print("lie")
            # im not playing against myself, because the other bot wasn't honest (and i'm always honest as long as i think i play against myself)
            return False
    return True
def check_equal(move1, move2, fullCheck): # WARNING: FOR COMPABILITY THIS IS RETURNING NEQ INSTEAD OF EQ
    if fullCheck:
        return move1 != move2
    else:
        return move1[0] != move2[0] #only check claims
def is_pattern(opHist, pattern_start, prob_pattern_start, pattern_length, full_check):
    for i in range(0, pattern_length-1):
        if check_equal(opHist[pattern_start + i] , opHist[prob_pattern_start + i], full_check):
            return False
    return True
win_map = {"R": "P", "P": "S", "S": "R"}
def deterministic_best_guess(opHist, full_check = True):
    size = 0
    random_result = random.choice(["R", "P", "S"])
    for pattern_length in range(2, (len(opHist)+1)/2): #a pattern has to occur at least twice
        for pattern_start in range(0, len(opHist) - 2 * pattern_length):
            if not is_pattern(opHist, pattern_start, len(opHist) - pattern_length + 1, pattern_length, full_check):
                 continue
            is_repeated = False
            is_fooled = False
            for repeated_pattern_start in range(pattern_start + pattern_length, len(opHist) - pattern_length):
                if not is_pattern(opHist, pattern_start, repeated_pattern_start, pattern_length, full_check):
                     continue
                is_repeated = True
                if check_equal(opHist[pattern_start + pattern_length - 1], opHist[repeated_pattern_start + pattern_length - 1], full_check):
                    is_fooled = True
                    break
    #            print("pattern found: " + str(opHist[pattern_start : pattern_start + pattern_length]) +" at "+str(pattern_start)+" and "+str(repeated_pattern_start))
   #             print("check: "+str(opHist))
            if is_fooled or not is_repeated:
                break
            #we have found a deterministic best guess
  #          print("most likely next step: "+ str(opHist[pattern_start + pattern_length - 1]))
            if full_check:
                return win_map[opHist[pattern_start + pattern_length - 1][1]], True
            return win_map[opHist[pattern_start + pattern_length - 1][0]], True # if we don't have a full check, the pattern only applies to claims. So pretend to win against the claimed result.
            
    #fallback
 #   print("fallback")
    return random_result, False
def DeepThought(opHist, myHist, opMove, myMove):
    if opMove == None:
    #claiming phase
        if len(myHist) == 0:
        #seed random to be able to be deterministic when chosing randomly
            #The seed is secret (kind of)
            random.seed(133427)
        else:
            #seed random according to my previous claims
            seed = 133427
            for i in range(0, len(myHist)):
                if myHist[i][0] == "R":
                    seed = seed*3+1
                elif myHist[i][0] == "S":
                    seed = seed*7+1
                elif myHist[i][0] == "P":
                    seed = seed*11+1
                while seed%2 == 0:
                    seed /= 2
            random.seed(seed)
        if check_self_match(opHist, myHist):
            #claim a random value, will happen in the first round or in a self-match
            result = random.choice(["R", "P", "S"])
            return result
      #  print("differ detected")
        if check_trick_bot(opHist, myHist) and len(myHist) > 10:
            # i play against a trick bot. I can reduce its points by trieing to guess its claim, and force him to lie
            result, sure = deterministic_best_guess(opHist, False)
        else:
            result, sure = deterministic_best_guess(opHist)
        random.seed(0)
        return result
    if check_self_match(opHist, myHist):
        #i play against myself, i can only hope for a honest draw, so do that
        return myMove
#    print("no self-math")
    #dbg needs a valid seed, so provide it
    random.seed(133427)
    result, sure = deterministic_best_guess(opHist)
    if sure:
        #i'm sure i play against a deterministic bot. I'll be honestly winning. YEY.
        return myMove
    if check_honest_bot(opHist) and len(opHist) > 10:
        #i play against an honest bot. I'll accept a draw, but i will not accept a loss
        if win_map[myMove] == opMove:
            return win_map[opMove]
        return myMove
    if check_trick_bot(opHist, myHist) and len(opHist) > 10:
        #i play against a tricking bot. He'll make me either loose honestly (1 Pnt) or i have to be dishonest (2 Pnt). So let's lie.
        return win_map[win_map[myMove]]
    if check_not_loose_bot(opHist, myHist) and len(opHist) > 10:
        #i play against a bot thats not willing to loose. If it looks like i won, i can loose honestly (1Pnt, 2Pnt for him),
        #or i have to be dishonest (2 Pnt, 0 Pnt for him). So let's lie in that case.
        #If it looks like its a draw, i'll be honest (conservative way), and get my 2 : 2 Pnt.
        #If it lokks like i'll loose, I'll not accept it. I'll lie to win for 2 : 1 Pnt.
        if myMove == opMove:
            return myMove
        if myMove == win_map[opMove]:
            # He'll lie. So lie together and keep smiling.
            return opMove
        # I'll loose. NO!!!! Not gonna happen
        return win_map[opMove]
    return myMove
```
Just a few notes on it:
* DeepThought likes to think. A lot. I'm sorry about it, but i don't really know how to fix it. I blame Python.
* DeepThought tries to be honest. Beeing honest does provide you one aditional point, which is the same as the expected value for normale RPS
* But: DeepThought gets in averange even more than 2 Points per Game. He uses some detection to find some common behaviours (like tricking, beeing honest, etc.), and adapts according to that.
* DeepThought is purely deterministic, so it'll draw against itself, because it'll always do the same decition on both ends.
* To be sure not to lie against itself, it has a special detection, like some other bots here, too. This is a very aggressive one, even assuming to be true after one round (and in the first round, too). Basically, as long as the opponent moves are exactly mine, i'll assume its a mirror match.
* The interesting part (and the part that has dozens of false-positives) is the check for a deterministic bot, that is only dependend on its own prevoius result. That check search for any pattern of size n, that is repeated twice, and that could be describing the last n-1 moves, predicting the opponent's claim and move in advance. This part is taking some time, sadly.
I'm new to both, koth and Python, so tell me if I messed anything up in this bot. I don't think it can beat reinforced learning (because I guess it'll learn my moves too quickly), but let's give it a try.
I like this challenge, and if I find some time, I'd like to add an organic computing approch (althogh there might be too less pressure on the higher dimensions). Is it allowed to add multiple suggestions? Or is it forbidden to prevent boosing your main bot by inserting some that only aim for loosing to your primary?
EDIT: Fixed code-typo that caracterized me as non-native english speaker
[Answer]
```
import random
def user5957401bot(a,b,c,d):
    if d == None: 
       return random.choice(["R","P","S"])
    else:
       return random.choice(["R","P","S",d])
```
[Answer]
# have\_we\_been\_here\_before
Simply asks "have we been here before", and chooses the move which would have given the best average outcome in any such previous games.
**Edit: Honesty Club.** I've added a small block of code because another bot (mason) has done extremely well by forming a secret club with itself. Notice however, that playing honestly against honest opponents has on average exactly the same payoff when playing against oneself, and perhaps there are broader mutual benefits to be had too?
**Edit2:** At the time of writing the two bots ahead of me both exploit rotators, so I'm going to add another block of code in order to jump on that bandwagon too. I guess my code must seem quite old school - sticking to familiar constructs found in any programming language because I really don't know Python.
```
import random
def have_we_been_here_before(opponentList, myList, opponent, me):
    def win(x):
        if x=="R": return "P"
        elif x=="P": return "S"
        elif x=="S": return "R"
    def calc_score(r1, r2):
        if r1==r2: return 1
        elif r1==win(r2): return 2
        else: return 0
    def have_we(opponentList, myList, opponent, me, me2):
        score, count = 0, 0
        for n in range(len(opponentList)):
            if (opponent == opponentList[n][0] and me == myList[n][0]):
                score += calc_score(me2, opponentList[n][1])
                count += 1
        if count == 0: return 0
        else: return float(score) / float(count)
    if opponent == None:
        # exploit rotators
        if len(opponentList) >= 3:
            rotator = True
            for n in range(3, len(opponentList)):
                if opponentList[n][1] != opponentList[n % 3][1]:
                    rotator = False
                    break
            if rotator: return win(opponentList[len(opponentList) % 3][1])
        if len(opponentList) == 0:
            return random.choice(["R", "P", "S"])
        else:
            # crude attempt to exploit the house bots
            prev = random.choice(opponentList)[1]
            return win(prev)
    # Play honestly if opponent has played honestly so far
    honest = True
    for oppMove in opponentList:
        if (oppMove[0] != oppMove[1]):
            honest = False
            break
    if honest: return me
    # Done playing honestly
    # Have we been here before?
    rock = have_we(opponentList, myList, opponent, me, "R")
    paper = have_we(opponentList, myList, opponent, me, "P")
    sissors = have_we(opponentList, myList, opponent, me, "S")
    if rock > paper and rock > sissors: return "R"
    elif paper > rock and paper > sissors: return "P"
    elif sissors > paper and sissors > rock: return "S"
    else: return win(opponent)
```
[Answer]
## THEbot: THE Honest Exploiter
```
import random 
def thebot(ho,hm,om,mm):
    hands = {"R": "P", "P": "S", "S": "R"}
    if om == None:
        if (len(set([i[0] for i in ho])) < 3) and (len(ho) > 2):
            return hands[random.choice(list(set([i[0] for i in ho])))]
        else:
            return random.choice(["R","P","S"])
    else:
        if sum(1 for i in ho if i[0]==i[1]) > (len(ho)/3):
            if om == mm:
                return om
            else:
                return hands[om]
        else:
            return mm
```
[Answer]
**Thompson**
```
import math
import random
moves = list(range(3))
names = "RPS"
from_name = dict(zip(names, moves))
to_name = dict(zip(moves, names))
#Payoff matrices given each relationship between honest moves.
A = [
    [[2, 1, 3], [2, 1, 0], [0, 2, 1]],
    [[1, 3, 2], [1, 0, 2], [2, 1, 0]],
    [[3, 2, 1], [0, 2, 1], [1, 0, 2]]
]
#Add a 1.2% penalty for the opponent's score (idea shamelessly stolen from csbot).
for d_h in range(3):
    for i in range(3):
        for j in range(3):
            A[d_h][i][j] -= 0.012 * A[[0, 2, 1][d_h]][j][i]
third = 1. / 3
two_thirds = 2 * third
nash_prior = [
    [[1, 0, 0], [two_thirds, 0, third], [third, 0, two_thirds]], 
    [[third, 0, two_thirds], [1, 0, 0], [two_thirds, 0, third]], 
    [[two_thirds, 0, third], [third, 0, two_thirds], [1, 0, 0]]
]
def mult_m_v(M, v):
    w = [0 for _ in v]
    for i, M_i in enumerate(M):
        for M_ij, v_j in zip(M_i, v):
            w[i] += M_ij * v_j
    return w
def mean_belief(counts):
    c = 1. / sum(counts)
    return [n * c for n in counts]
def sample_belief(counts):
    return mean_belief([random.gammavariate(n, 1) for n in counts])
def thompson(h_opp, h_me, opp, me):
    #Prior rationality of opponent.
    a = 0.95
    #Confidence in priors.
    n0_h = 0.5
    n0_m = 0.5
    def v(x):
        return [x for _ in range(3)]
    h_p = [v(n0_h * third) for _ in range(3)]
    m_p0 = [v(None) for _ in range(3)]
    m_p1 = [v(None) for _ in range(3)]
    #Expected prior is a mixture between nash equilibrium and uniform distribution.
    for h_i in range(3):
        for h_j in range(3):
            m_p0[h_i][h_j] = [n0_m * (a * nash + (1 - a) * third) for nash in nash_prior[h_i][h_j]] 
    for d_j_prev in range(3):
        for d_ij in range(3):
            m_p1[d_j_prev][d_ij] = list(m_p0[0][d_ij])
    #Track whether it's better to model the real moves based on the exact honest moves or
    #just the relationship between honest moves together with the opponent's defection strategy in the previous round.
    log_mp0 = 0
    log_mp1 = 0
    #Identify myself and always cooperate.
    is_me = True
    for (t, ((h_i, m_i), (h_j, m_j))) in enumerate(zip(h_me, h_opp)):
        h_i, m_i, h_j, m_j = from_name[h_i], from_name[m_i], from_name[h_j], from_name[m_j]
        d_j = (m_j - h_j) % 3
        d_ij = (h_j - h_i) % 3
        if t:
            h_j_prev = from_name[h_opp[t - 1][0]]
            m_j_prev = from_name[h_opp[t - 1][1]]
            h_p[h_j_prev][h_j] += 1
            d_j_prev = (m_j_prev - h_j_prev) % 3
            log_mp0 += math.log(m_p0[h_i][h_j][d_j] / sum(m_p0[h_i][h_j]))
            log_mp1 += math.log(m_p1[d_j_prev][d_ij][d_j] / sum(m_p1[d_j_prev][d_ij]))
            m_p1[d_j_prev][d_ij][d_j] += 1
        m_p0[h_i][h_j][d_j] += 1
        if is_me and ((h_i != h_j) or (h_j != m_j)):
            is_me = False
    if is_me:
        random.seed(len(h_me) + 1337)
        me_next = random.randrange(3)
    log_ps = [log_mp0, log_mp1]
    log_p_max = max(log_ps)
    ps = [math.exp(log_p - log_p_max) for log_p in log_ps]
    p0 = ps[0] / sum(ps)
    #We have to blend between the predictions of our 2 models for the real rounds.  
    def sample_expectation(h_i, h_j, d_j_prev=None):
        d_ij = (h_j - h_i) % 3
        if d_j_prev is None or random.random() < p0:
            p = m_p0[h_i][h_j]
        else:
            p = m_p1[d_j_prev][d_ij]
        return mult_m_v(A[d_ij], sample_belief(p))
    def take_expectation(h_i, h_j, d_j_prev=None):
        d_ij = (h_j - h_i) % 3
        e0 = mult_m_v(A[d_ij], mean_belief(m_p0[h_i][h_j]))
        if d_j_prev is None:
            return e0
        e1 = mult_m_v(A[d_ij], mean_belief(m_p1[d_j_prev][d_ij]))
        return [p0 * e0i + (1 - p0) * e1i for e0i, e1i in zip(e0, e1)]
    #We use thompson sampling, selecting the optimal deterministic strategy
    #with respect to a random opponent sampled from the posterior.
    #Actually, we use optimistic thompson sampling which clips samples to have >= than the mean expected value.
    if opp == None:
        #For the honest round we perform a lookahead to the real round to choose our strategy.
        if h_opp:
            if is_me:
                return to_name[me_next]
            h_j_prev = from_name[h_opp[-1][0]]
            m_j_prev = from_name[h_opp[-1][1]]
            d_j_prev = (m_j_prev - h_j_prev) % 3
            h_p_s = sample_belief(h_p[h_j_prev])
            h_p_u = mean_belief(h_p[h_j_prev])
            s_i = [0] * 3
            s_i_u = [0] * 3
            for h_i in range(3):
                for h_j in range(3):
                    s_i[h_i] += h_p_s[h_j] * max(sample_expectation(h_i, h_j, d_j_prev))
                    s_i_u[h_i] += h_p_u[h_j] * max(take_expectation(h_i, h_j, d_j_prev))
                s_i[h_i] = max(s_i[h_i], s_i_u[h_i])
            return to_name[s_i.index(max(s_i))]
        else:
            return to_name[me_next]
    else:
        if h_opp:
            if is_me:
                return me
            h_j_prev = from_name[h_opp[-1][0]]
            m_j_prev = from_name[h_opp[-1][1]]
            d_j_prev = (m_j_prev - h_j_prev) % 3
        else:
            if opp == me:
                return me
            d_j_prev = None
        h_i, h_j = from_name[me], from_name[opp]
        s_i = [max(s0, s1) for s0, s1 in zip(sample_expectation(h_i, h_j, d_j_prev), take_expectation(h_i, h_j, d_j_prev))]
        return to_name[(h_i + s_i.index(max(s_i))) % 3]
```
[Answer]
# mirrorbot
```
import random
def mirrorbot(op_hist, my_hist, op_move, my_move):
    if my_move == None :
        return random.choice(["R","P","S"])
    else :
        for x in range(len(op_hist)):
            if ((op_hist[len(op_hist) -x-1][0] == my_move) and (my_hist[len(op_hist) -x-1][0] == op_move)):
                return op_hist[len(op_hist) -x-1][1]
        return my_move
```
I will try a simple bot that redo the last play of its opponent in these conditions
[Answer]
```
def rotate_rock(h1, h2, is_, honest):
 return ("R", "P", "S")[len(h1) % 3]
```
```
def rotate_paper(h1, h2, is_, honest):
 return ("P", "S", "R")[len(h1) % 3]
```
```
def rotate_scissors(h1, h2, is_, honest):
 return ("S", "R", "P")[len(h1) % 3]
```
The idea here is to maximize the score while playing self while still being randomly competitive during other stages against other bad bots.
[Answer]
## Dx
I only wrote this bot so i can have an smiley in my bot name xD.
```
def Dx(ophist, myhist, opmove, mymove):
    from random import choice
    import math
    def honest(hist):
        return [int(x[0]==x[1]) for x in hist]
    def avg(arr):
        if len(arr)==0:
            return 0
        return sum(arr)/float(len(arr))
    def clamp(i, lo, hi):
        return min(hi, max(lo, i))
    def deltas(arr):
        return [a-b for a,b in zip(arr[1:],arr[:-1])]
    def delta_based_prediction(arr,l):
        deltarr = []
        i=0
        while len(arr)<0:
            deltarr[i]=avg(arr[-l:])
            i+=1
            arr = deltas(arr)
        return sum(deltarr)
    next_honesty = delta_based_prediction(honest(ophist),int(math.sqrt(len(ophist))))
    if abs(next_honesty-0.5)<0.1 or not opmove:
        return choice(['R','P','S'])
    next_honesty=int(clamp(round(next_honesty),0,1))
    winner = {'S': 'R', 'R': 'P', 'P': 'S'}
    if next_honesty > 0:
        return winner[opmove]
    return choice([opmove, winner[winner[opmove]]])
```
[Answer]
## Everybody Lies
```
import random
def everybodylies (opphist, myhist, oppmove, mymove):
    if mymove == None:
        return random.choice(["R","P","S"])
    elif mymove == "R": return "S"
    elif mymove == "P": return "R"
    elif mymove == "S": return "P"
```
It lies about its move ("I'll play Scissors!"), and assumes the opponent was also lying and that they'll try to beat what I said my move would be ("hmm, Rock beats Scissors so I'm playing that"), but I actually play the move that beats that move ("Paper! Surprise!").
[Answer]
# Trusting Bot
```
def trusting_bot(h_opp, h_me, opp, me):
    if opp=="S":
        return "R"
    elif opp=="R":
        return "P"
    else:
        return "S"
```
Always claims to throw scissors, but will do whatever beats what the opponent said. Will reliably draw with itself.
[Answer]
# Bot name: Liar Liar
Can't stop lying. 
```
import random
def liarliar (herHistory, myHistory, herMove, myMove):
    options = ["R", "P", "S"]
    if myMove == None:
        return random.choice(options)
    else:
        options.remove(myMove);
        return random.choice(options)
```
[Answer]
**RockBot**
Assumes the opponent will be honest and tries to beat them, but refuses to play rock.
```
import random
def rockBot(oppHist,myHist,oppMove,myMove):
    if oppMove == None:
        return random.choice(["R","P","S"])
    else:
        if(oppMove == "R"):
            return "P"
        elif(oppMove == "P"):
            return "S"
        elif(myMove != "R"):
            return myMove
        else:
            return random.choice(["P","S"])
```
[Answer]
# Bot name: dontlietome
Determines if the opponent is lying or not depending on how many times the opponent lied in the last 10 rounds. Selects move depending on if the opponent is lying or not. If the opponent is determined to be lying, then plays what the hint was.
```
import random
def dontlietome(opp_moves, my_moves, opp_hint, my_hint):
    def is_trustworthy(moves, length):
        length = max(-length, -len(moves))
        history = [1 if move[0] == move[1] else 0 for move in moves[length:]]
        prob_honest = float(sum(history))/float(len(history))
        choice = random.uniform(0., 1.)
        if choice <= prob_honest:
            return True
        else:
            return False
    moves = ["R", "P", "S"]
    lose_against_map = {"S":"R", "R":"P", "P":"S"}
    length = 10
    if opp_hint == None:
        # Honest round
        return random.choice(moves)
    else:
        # Real round
        if len(opp_moves) < length:
            return my_hint
        if is_trustworthy(opp_moves, length):
            return lose_against_map[opp_hint]
        else:
            return my_hint
```
[Answer]
```
import random
def trustingRandom(a,b,c,d):
  move = random.choice(["R","P","S"])
  if c == "R":
    move = "P"
  elif c == "P":
    move = "S"
  elif c == "S":
    move = "R"
  return move
```
[Answer]
# Averager
```
def averager(op, mp, od, md):
  import random
  if od == md == None:
    if op == mp == []:
      return random.choice('RPS')
    else:
      opa = [i[1] for i in op]
      copa = [opa.count(i) for i in 'RPS']
      copam = [i for i, j in zip('RPS', copa) if j == max(copa)]
      opd = [i[0] for i in op]
      copd = [opd.count(i) for i in 'RPS']
      copm = [i for i, j in zip('RPS', copd) if j == max(copd) and i in copam]
      return random.choice(copam if copm == [] else copm)
  else:
    if op == mp == []:
      return md
    else:
      hop = sum([1. if i[0] == i[1] else 0. for i in op]) / len(op)
      hmp = sum([1. if i[0] == i[1] else 0. for i in mp]) / len(mp)
      return 'PSR'['RPS'.index(od)] if hmp >= 0.75 and hop >= 0.50 else md
```
[Answer]
Just a little better than my previous entry...
```
def learningbot4(yourlist,mylist,you,me):
  CHECK={"R":{"R":0,"P":1,"S":-1},"P":{"R":-1,"P":0,"S":1},"S":{"R":1,"P":-1,"S":0}}
  results={None:{"R":0,"P":0,"S":0},"R":{"R":0,"P":0,"S":0},"P":{"R":0,"P":0,"S":0},"S":{"R":0,"P":0,"S":0}}
  for i in range(len(yourlist)):
    res=CHECK[yourlist[i][1]][mylist[i][1]]
    if mylist[i][0]==mylist[i][1]: res+=0.5
    results[yourlist[i][0]][mylist[i][1]]+=res
    results[None][mylist[i][0]]+=res
  return max(results[you],key=results[you].get)
```
[Answer]
## csbot on steroids
I think the suggestion that @user1502040 makes in the comments should be followed. Otherwise this bot would have an advantage that I would consider unfair. I submit it so that the difference that it makes can be assessed. With the suggested random seeding the steroids would be neutralized and the bot would be equivalent to `csbot`, so then only one should participate in the contest.
```
from random import seed
from csbot import csbot
def csbot_on_steroids(ophist,myhist,opdecl,mydecl):
  seed()
  m = csbot(ophist,myhist,opdecl,mydecl)
  seed(0)
  return m
```
]