Guest User

Source code

a guest
Jun 27th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. honour2 = 1
  2. his1 = ""
  3. his2 = ""
  4. math.randomseed(os.time())
  5. function run(bot,bot2)
  6. while true do
  7.     moves = {"A","P","B","J","W"}
  8.     move1 = bot1(his1,his2,health1,health2,honour1,honour2)
  9.     move2 = bot2(his2,his1,health2,health1,honour2,honour1)
  10.     print("Bot1 "..move1..", Bot2 "..move2)
  11.     his1 = his1..move1
  12.     his2 = his2..move2
  13.     if move1 == "B" then
  14.      honour1 = honour1+1
  15.     elseif move1=="P" then
  16.         honour1 = honour1-1
  17.     elseif move1=="A" then
  18.         if move2~="P" then
  19.             health2=health2-1
  20.         end
  21.     elseif move1=="J" then
  22.         honour1 = honour1-1
  23.         if move2=="A" then
  24.             health2 = health2-2
  25.         elseif move2=="P" then
  26.             honour1=honour1-1
  27.         end
  28.     end
  29.     if move2=="A" then
  30.         if move1~="P" then
  31.             health1=health1-1
  32.         end
  33.     elseif move2=="P" then
  34.         honour2=honour2-1
  35.     elseif move2=="B" then
  36.         honour2=honour2+1
  37.     elseif move2=="J" then
  38.         honour2 = honour2-1
  39.         if move1=="A" then
  40.             health1 = health1-2
  41.         elseif move1=="P" then
  42.             honour2 = honour2-1
  43.         end
  44.     end
  45.     end
  46.     --Winning
  47.     --Both people died, go based on honour
  48.     if health1<0 and health2<0 then
  49.         if honour2<honour1 then
  50.             return "Bot1"
  51.         elseif honour1<honour2 then
  52.             return "Bot2"
  53.         else
  54.             return "Draw"
  55.         end
  56.     end
  57.     if health1<0 and honour2>-1 then
  58.         return "Bot2"
  59.     elseif health2<0 and honour1>-1 then
  60.         return "Bot1"
  61.     elseif honour1<0 then
  62.         return "Bot2"
  63.     elseif honour2<0 then
  64.         return "Bot1"
  65.     end
  66. end
  67. end
  68.  
  69. --Bot1, the thinker (Probably winner)
  70. function bot1(hist,ehist,health,ehealth,honour,ehonour)
  71.  --Start looking for bow patterns to attack
  72.  if not bowdis or #ehist < 5 then
  73.  for i=1,#ehist do
  74.    j = ehist:sub(i,i)
  75.    if j=="B" then
  76.     fb=i
  77.     break
  78.    end
  79.   end
  80.   if fb then
  81.   for i=fb,#ehist do
  82.     j = ehist:sub(i,i)
  83.     if j=="B" and i-fb~=0 then
  84.         --print("Bot1: Found potential pattern in bows, every "..i-fb.." moves. Trying to confirm.")
  85.         if ehist:sub(i+i-fb,i+i-fb) == "B" then
  86.             bowdis=i-fb
  87.             --print("Pattern confirmed.")
  88.             break
  89.         end
  90.     end
  91.   end
  92. end
  93. end
  94. --End bow patterns, start def patterns.
  95. if not pardis or #ehist < 5 then
  96. for i=1,#ehist do
  97.    j = ehist:sub(i,i)
  98.    if j=="P" then
  99.     fp=i
  100.     break
  101.    end
  102.   end
  103.   if fp then
  104.   for i=fp,#ehist do
  105.     j = ehist:sub(i,i)
  106.     if j=="P" and i-fp~=0 then
  107.         --print("Bot1: Found potential pattern in parry, every "..i-fp.." moves. Trying to confirm.")
  108.         if ehist:sub(i+i-fp,i+i-fp) == "P" then
  109.             pardis = i-fp
  110.             --print("Pattern confirmed.")
  111.             break
  112.         end
  113.     end
  114.   end
  115. end
  116. end
  117. --Start attack prediction
  118.  if not attdis or #ehist < 5 then
  119.  for i=1,#ehist do
  120.    j = ehist:sub(i,i)
  121.    if j=="A" then
  122.     fa=i
  123.     break
  124.    end
  125.   end
  126.   if fa then
  127.   for i=fa,#ehist do
  128.     j = ehist:sub(i,i)
  129.     if j=="A" and i-fa~=0 then
  130.         --print("Bot1: Found potential pattern in attacks, every "..i-fa.." moves. Trying to confirm.")
  131.         if ehist:sub(i+i-fa,i+i-fa) == "A" then
  132.             attdis=i-fa
  133.             --print("Pattern confirmed.")
  134.             break
  135.         end
  136.     end
  137.   end
  138. end
  139. end
  140. --Try to predict bows if possible
  141. if bowdis then
  142.     for i=#ehist, 1,-1 do
  143.         j = string.sub(ehist,i,i)
  144.         if j=="B" then
  145.             if #ehist-(i-1)==bowdis then
  146.                 return "A"
  147.             end
  148.         end
  149.     end
  150. end
  151. --Try to predict parrys if possible
  152. if pardis then
  153.     for i=#ehist, 1,-1 do
  154.         j = string.sub(ehist,i,i)
  155.         if j=="P" then
  156.             if #ehist-(i-1)==pardis then
  157.                 return "B"
  158.             end
  159.         end
  160.     end
  161. end
  162. --Try to predict attacks if possible
  163. if attdis then
  164.     for i=#ehist, 1,-1 do
  165.         j = string.sub(ehist,i,i)
  166.         if j=="A" then
  167.             if #ehist-(i-1)==attdis then
  168.                 if honour == 0 then
  169.                     return "B"
  170.                 else
  171.                     if honour > 4 then
  172.                         return "J"
  173.                     else
  174.                         return "P"
  175.                 end
  176.             end
  177.         end
  178.     end
  179. end
  180. --End of prediction algorithms, start of "No pattern, use logic"
  181. --Check if the bot isn't doing anything
  182. if ehist:sub(#ehist-2,#ehist) == "WWW" then
  183.  return "A"
  184. end
  185. --Special case, 0 honour
  186. if honour==0 then
  187.  return "B"
  188. end
  189. --Special case, they have 0 honour
  190. if ehonour==0 then
  191.     return "A"
  192. end
  193. --Kind of prediction a again :P Takes most used move and defends against it.
  194. his={}
  195. for i=1, #ehist do
  196.  j=ehist:sub(i,i)
  197.  table.insert(his,j)
  198. end
  199. final = 0
  200. if #his > 0 then
  201.  for k, v in pairs(his) do
  202.    tempcount = 0
  203.    for key, val in pairs(his) do
  204.     if v == val then
  205.         tempcount = tempcount+1
  206.     end
  207.      if tempcount > final then
  208.         final=tempcount
  209.         mostlet = val
  210.      end
  211.    end
  212.  end
  213.  if mostlet=="A" then
  214.     if honour>4 then
  215.         return "J"
  216.     else
  217.         return "P"
  218.     end
  219.  elseif mostlet=="P" then
  220.     return "B"
  221.  elseif mostlet=="B" then
  222.     return "A"
  223.  elseif mostlet == "W" then
  224.     return "A"
  225.  elseif mostlet == "J" then
  226.     return "P"
  227.  end
  228. end
  229. return moves[math.random(1,#moves-1)]
  230. end
  231.  
  232. --Bot2, the attacker
  233. bot2num = 0
  234. function bot2(hist,ehist,health,ehealth,honour,ehonour)
  235.     if bot2num==0 then
  236.         bot2num=1
  237.         return "A"
  238.     else
  239.         bot2num=0
  240.         return "P"
  241.     end
  242. end
  243. bot1count = 0
  244. bot2count = 0
  245. for i = 1, 500 do
  246.     g=run(bot1,bot2)
  247.     if g=="Bot1" then
  248.         bot1count=bot1count+1
  249.     elseif g=="Bot2" then
  250.         bot2count=bot2count+1
  251.     end
  252. end
  253.  
  254. if bot1count > bot2count then
  255.     print("Bot 1 wins with a score of "..bot1count.." to "..bot2count)
  256. elseif bot1count < bot2count then
  257.     print("Bot 2 wins with a score of "..bot2count.." to "..bot1count)
  258. end
Advertisement
Add Comment
Please, Sign In to add comment