Advertisement
Weeeeb

why oop is a waste of time

Aug 25th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. -OOP example, rock paper scissor
  2. -this can be made better by only selecting 2 different action so there will always be a winner and a loser,
  3. function rockPaperScissor(amountOfPlayers)
  4.     local round=0
  5.     local Players={}
  6.     local function logic(Act1,Act2)
  7.         local v
  8.         if Act1=='Scissor'then
  9.             if Act2=='Rock' then
  10.                 v=Act2
  11.             else
  12.                 v=Act1
  13.             end
  14.         elseif Act1=='Rock'then
  15.             if Act2=='Scissor'then
  16.                 v=Act1
  17.             else
  18.                 v=Act2
  19.             end
  20.         else
  21.             if Act2=='Scissor' then
  22.                 v=Act2
  23.             else
  24.                 v=Act1
  25.             end
  26.         end
  27.         return v
  28.     end
  29.     local function analyze()
  30.         local haveScissor=false
  31.         local haveRock=false
  32.         local havePaper=false
  33.         for i,v in pairs(Players) do
  34.             if v=='Scissor'then
  35.                 haveScissor=true
  36.             elseif v=='Rock' then
  37.                 haveRock=true
  38.             elseif v=='Paper' then
  39.                 havePaper=true
  40.             end
  41.         end
  42.         local count=0
  43.         if havePaper then
  44.             count=1
  45.         end
  46.         if haveScissor then
  47.             count=count+1
  48.         end
  49.         if haveRock then
  50.             count=count+1
  51.         end
  52.         if count==2 then
  53.             local a=haveScissor and'Scissor' or havePaper and'Paper' or'Rock'
  54.             local b=haveScissor and'Scissor' or havePaper and'Paper' or'Rock'
  55.             local winner=logic(a,b)
  56.             print('\n'..winner,' won')
  57.             local String=''
  58.             for i,v in pairs(Players) do
  59.                 if v==winner then
  60.                    String=String..Players[i-1]..', '
  61.                 end
  62.             end
  63.             String=String:sub(0,#String-2)
  64.             print('Winners are ',String)
  65.             return true
  66.         else
  67.             return false
  68.         end
  69.     end
  70.     local function report()
  71.         round=round+1
  72.         print('\nRound: ',round,'\n')
  73.         for i,v in pairs(Players)do
  74.             if tonumber(v:sub(2,2)) then
  75.                 print(v..': ',Players[i+1])
  76.             end
  77.         end
  78.     end
  79.     for i=1,amountOfPlayers do
  80.         local random=math.random(0,2)
  81.         local move=random==0 and 'Rock' or random==1 and 'Paper' or 'Scissor'
  82.         table.insert(Players,'P'..i)
  83.         table.insert(Players,move)
  84.     end
  85.     report()
  86.     if not analyze() then
  87.         repeat
  88.             local t={}
  89.             for i=1,amountOfPlayers do
  90.                 local random=math.random(0,2)
  91.                 local move=random==0 and 'Rock' or random==1 and 'Paper' or 'Scissor'
  92.                 table.insert(t,'P'..i)
  93.                 table.insert(t,move)
  94.             end
  95.             Players=t
  96.             t=nil
  97.             report()
  98.         until analyze()
  99.     end
  100. end
  101. rockPaperScissor(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement