Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- honour2 = 1
- his1 = ""
- his2 = ""
- math.randomseed(os.time())
- function run(bot,bot2)
- while true do
- moves = {"A","P","B","J","W"}
- move1 = bot1(his1,his2,health1,health2,honour1,honour2)
- move2 = bot2(his2,his1,health2,health1,honour2,honour1)
- print("Bot1 "..move1..", Bot2 "..move2)
- his1 = his1..move1
- his2 = his2..move2
- if move1 == "B" then
- honour1 = honour1+1
- elseif move1=="P" then
- honour1 = honour1-1
- elseif move1=="A" then
- if move2~="P" then
- health2=health2-1
- end
- elseif move1=="J" then
- honour1 = honour1-1
- if move2=="A" then
- health2 = health2-2
- elseif move2=="P" then
- honour1=honour1-1
- end
- end
- if move2=="A" then
- if move1~="P" then
- health1=health1-1
- end
- elseif move2=="P" then
- honour2=honour2-1
- elseif move2=="B" then
- honour2=honour2+1
- elseif move2=="J" then
- honour2 = honour2-1
- if move1=="A" then
- health1 = health1-2
- elseif move1=="P" then
- honour2 = honour2-1
- end
- end
- end
- --Winning
- --Both people died, go based on honour
- if health1<0 and health2<0 then
- if honour2<honour1 then
- return "Bot1"
- elseif honour1<honour2 then
- return "Bot2"
- else
- return "Draw"
- end
- end
- if health1<0 and honour2>-1 then
- return "Bot2"
- elseif health2<0 and honour1>-1 then
- return "Bot1"
- elseif honour1<0 then
- return "Bot2"
- elseif honour2<0 then
- return "Bot1"
- end
- end
- end
- --Bot1, the thinker (Probably winner)
- function bot1(hist,ehist,health,ehealth,honour,ehonour)
- --Start looking for bow patterns to attack
- if not bowdis or #ehist < 5 then
- for i=1,#ehist do
- j = ehist:sub(i,i)
- if j=="B" then
- fb=i
- break
- end
- end
- if fb then
- for i=fb,#ehist do
- j = ehist:sub(i,i)
- if j=="B" and i-fb~=0 then
- --print("Bot1: Found potential pattern in bows, every "..i-fb.." moves. Trying to confirm.")
- if ehist:sub(i+i-fb,i+i-fb) == "B" then
- bowdis=i-fb
- --print("Pattern confirmed.")
- break
- end
- end
- end
- end
- end
- --End bow patterns, start def patterns.
- if not pardis or #ehist < 5 then
- for i=1,#ehist do
- j = ehist:sub(i,i)
- if j=="P" then
- fp=i
- break
- end
- end
- if fp then
- for i=fp,#ehist do
- j = ehist:sub(i,i)
- if j=="P" and i-fp~=0 then
- --print("Bot1: Found potential pattern in parry, every "..i-fp.." moves. Trying to confirm.")
- if ehist:sub(i+i-fp,i+i-fp) == "P" then
- pardis = i-fp
- --print("Pattern confirmed.")
- break
- end
- end
- end
- end
- end
- --Start attack prediction
- if not attdis or #ehist < 5 then
- for i=1,#ehist do
- j = ehist:sub(i,i)
- if j=="A" then
- fa=i
- break
- end
- end
- if fa then
- for i=fa,#ehist do
- j = ehist:sub(i,i)
- if j=="A" and i-fa~=0 then
- --print("Bot1: Found potential pattern in attacks, every "..i-fa.." moves. Trying to confirm.")
- if ehist:sub(i+i-fa,i+i-fa) == "A" then
- attdis=i-fa
- --print("Pattern confirmed.")
- break
- end
- end
- end
- end
- end
- --Try to predict bows if possible
- if bowdis then
- for i=#ehist, 1,-1 do
- j = string.sub(ehist,i,i)
- if j=="B" then
- if #ehist-(i-1)==bowdis then
- return "A"
- end
- end
- end
- end
- --Try to predict parrys if possible
- if pardis then
- for i=#ehist, 1,-1 do
- j = string.sub(ehist,i,i)
- if j=="P" then
- if #ehist-(i-1)==pardis then
- return "B"
- end
- end
- end
- end
- --Try to predict attacks if possible
- if attdis then
- for i=#ehist, 1,-1 do
- j = string.sub(ehist,i,i)
- if j=="A" then
- if #ehist-(i-1)==attdis then
- if honour == 0 then
- return "B"
- else
- if honour > 4 then
- return "J"
- else
- return "P"
- end
- end
- end
- end
- end
- --End of prediction algorithms, start of "No pattern, use logic"
- --Check if the bot isn't doing anything
- if ehist:sub(#ehist-2,#ehist) == "WWW" then
- return "A"
- end
- --Special case, 0 honour
- if honour==0 then
- return "B"
- end
- --Special case, they have 0 honour
- if ehonour==0 then
- return "A"
- end
- --Kind of prediction a again :P Takes most used move and defends against it.
- his={}
- for i=1, #ehist do
- j=ehist:sub(i,i)
- table.insert(his,j)
- end
- final = 0
- if #his > 0 then
- for k, v in pairs(his) do
- tempcount = 0
- for key, val in pairs(his) do
- if v == val then
- tempcount = tempcount+1
- end
- if tempcount > final then
- final=tempcount
- mostlet = val
- end
- end
- end
- if mostlet=="A" then
- if honour>4 then
- return "J"
- else
- return "P"
- end
- elseif mostlet=="P" then
- return "B"
- elseif mostlet=="B" then
- return "A"
- elseif mostlet == "W" then
- return "A"
- elseif mostlet == "J" then
- return "P"
- end
- end
- return moves[math.random(1,#moves-1)]
- end
- --Bot2, the attacker
- bot2num = 0
- function bot2(hist,ehist,health,ehealth,honour,ehonour)
- if bot2num==0 then
- bot2num=1
- return "A"
- else
- bot2num=0
- return "P"
- end
- end
- bot1count = 0
- bot2count = 0
- for i = 1, 500 do
- g=run(bot1,bot2)
- if g=="Bot1" then
- bot1count=bot1count+1
- elseif g=="Bot2" then
- bot2count=bot2count+1
- end
- end
- if bot1count > bot2count then
- print("Bot 1 wins with a score of "..bot1count.." to "..bot2count)
- elseif bot1count < bot2count then
- print("Bot 2 wins with a score of "..bot2count.." to "..bot1count)
- end
Advertisement
Add Comment
Please, Sign In to add comment