Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.97 KB | None | 0 0
  1. --[[RPS bot testing program
  2.  
  3.     - Add your bot(s) inside of the bots function
  4.     - The main function is passed two tables, which contain
  5.       the history of moves made by the opponent
  6.       the history of moves made by the bot
  7.     - The main function returns another function
  8.       which returns its move as a number, 0 or 1 or 2
  9.       1 beats 0; 2 beats 1; 0 beats 2
  10.  
  11.     A list of bots, their win percentages and scores will be printed to the output.
  12.     Win percentages are calculated by wins/(wins+losses) and do not include draws.
  13.     Un-comment the "details" line for a breakdown of percentages between bots.
  14.    
  15.     Submitting bots to RPS botwar
  16.     https://www.roblox.com/games/437771957/RPS-Botwar
  17.     Submit the function to hastebin and copy the link ID into the submission form in the game.
  18.     The name of the bot is read from the name of the function.
  19. ]]
  20.  
  21. local bots=(function()local ENV=getfenv(setfenv(1,{}))
  22.  
  23.     function Rock() -- returns rock every round
  24.         return function()
  25.             return 0
  26.         end
  27.     end
  28.     function Random() -- returns a random move every round
  29.         return function()
  30.             return math.random(3)-1
  31.         end
  32.     end
  33.     function Copy(t) -- returns the opponent's previous move
  34.         return function()
  35.             return t[#t]or math.random(3)-1
  36.         end
  37.     end
  38.     function BeatLast(t) -- beats the opponent's previous move
  39.         return function()
  40.             return((t[#t]or math.random(3))+1)%3
  41.         end
  42.     end
  43.     function WinStay(t) -- returns the same move if it won last round, otherwise changes
  44.         local last=math.random(3)-1
  45.         return function()
  46.             if #t~=0 and last~=(t[#t]+1)%3 then
  47.                 last=(last+math.random(2))%3
  48.             end
  49.             return last
  50.         end
  51.     end
  52.     function History(t) -- plays against a random move the opponent has made in the past
  53.         return function()
  54.             return #t==0 and math.random(3)-1 or(t[math.random(#t)]+1)%3
  55.         end
  56.     end
  57.     function Count(t) -- plays against the opponent's most common move
  58.         local r,p,s=0,0,0
  59.         return function()
  60.             local m=t[#t]
  61.             if m==0 then r=r+1 elseif m==1 then p=p+1 elseif m==2 then s=s+1 end
  62.             if r>p and r>s then
  63.                 return 1
  64.             elseif p>s then
  65.                 return 2
  66.             else
  67.                 return 0
  68.             end
  69.         end
  70.     end
  71.     function Repetition(t,t2) -- predicts the opponent's next move from repeated strings of responses
  72.         return function()
  73.             local r,len=math.random(3)-1,0
  74.             for i=#t-1,1,-1 do
  75.                 for j=0,i do
  76.                     if t[i-j]==t[#t-j]and t2[i-j]==t2[#t2-j]then
  77.                         if j>=len then
  78.                             r,len=(t[i+1]+1)%3,j+1
  79.                         end
  80.                     else
  81.                         break
  82.                     end
  83.                 end
  84.             end
  85.             return r
  86.         end
  87.     end
  88.  
  89. return ENV end)()
  90.  
  91. local names={}
  92. for name,v in pairs(bots)do
  93.     if type(v)=='function'then
  94.         local bot=setfenv(v,{tostring=tostring,tonumber=tonumber,type=type,unpack=unpack,pcall=pcall,pairs=pairs,ipairs=ipairs,next=next,select=select,string=string,table=table,math=math})
  95.         names[#names+1]=name
  96.     end
  97. end
  98. local games,scores={},{}
  99. for i=1,#names-1 do
  100.     scores[i]={}
  101.     for j=i+1,#names do
  102.         local score={0,0,0}
  103.         scores[i][j]=score
  104.         local x,y=names[i],names[j]
  105.         local xmoves=setmetatable({},{__newindex=function()end})
  106.         local ymoves=setmetatable({},{__newindex=function()end})
  107.         local a,b=bots[x](ymoves,xmoves),bots[y](xmoves,ymoves)
  108.         local game=0
  109.         local function f()
  110.             local A,B=a(),b()
  111.             if A~=0 and A~=1 and A~=2 then error(x.." did not return 0 or 1 or 2")end
  112.             if B~=0 and B~=1 and B~=2 then error(y.." did not return 0 or 1 or 2")end
  113.             if A==(B+1)%3 then
  114.                 score[1]=score[1]+1
  115.             elseif A==B then
  116.                 score[2]=score[2]+1
  117.             else
  118.                 score[3]=score[3]+1
  119.             end
  120.             game=game+1
  121.             rawset(xmoves,game,A)
  122.             rawset(ymoves,game,B)
  123.         end
  124.         games[#games+1]=f
  125.     end
  126. end
  127.  
  128. for round=1,100 do
  129.     for _,f in pairs(games)do
  130.         f()
  131.     end
  132. end
  133.  
  134. local points={}
  135. for i=1,#names do points[i]=0 end
  136. for i=1,#names-1 do
  137.     for j=i+1,#names do
  138.         local s=scores[i][j]
  139.         local rate=s[1]/(s[1]+s[3])
  140.         if not(s[1]==0 and s[3]==0)then
  141.             points[i]=points[i]+rate
  142.             points[j]=points[j]+1-rate
  143.         end
  144.     end
  145. end
  146. local points2={}
  147. for i=1,#names do points2[i]=0 end
  148. for i=1,#names-1 do
  149.     for j=i+1,#names do
  150.         local s=scores[i][j]
  151.         local rate=s[1]/(s[1]+s[3])
  152.         if not(s[1]==0 and s[3]==0)then
  153.             points2[i]=points2[i]+rate*points[j]
  154.             points2[j]=points2[j]+(1-rate)*points[i]
  155.         end
  156.     end
  157. end
  158. local indices={}
  159. for i=1,#names do indices[i]=i end
  160. for i=1,#names-1 do
  161.     for j=i+1,#names do
  162.         local I,J=indices[i],indices[j]
  163.         if points2[J]>points2[I]then
  164.             indices[i],indices[j]=J,I
  165.         end
  166.     end
  167. end
  168. local namelen=1
  169. for i=1,#names do namelen=math.max(namelen,#names[i]+1)end
  170. local totalpoints=0
  171. for i=1,#points do totalpoints=totalpoints+points[i]end
  172. for i=1,#names do
  173.     i=indices[i]
  174.     print(string.format("%-"..namelen.."sWin:%6.02f%% Score:%6.02f",names[i],points[i]/(#names-1)*100,points2[i]*100/(totalpoints-points[i])))
  175.     for j=1,#names do
  176.         j=indices[j]
  177.         if j~=i then
  178.             local r=i<j and scores[i][j]or scores[j][i]
  179.             if i>j then r={r[3],r[2],r[1]}end
  180.             --details
  181.             --print(string.format(" - %-"..namelen.."s%.02f%%",names[j],(r[1]==0 and r[3]==0)and 0 or 100*r[1]/(r[1]+r[3])))
  182.         end
  183.     end
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement