Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- —-OOP example, rock paper scissor
- —-this can be made better by only selecting 2 different action so there will always be a winner and a loser,
- function rockPaperScissor(amountOfPlayers)
- local round=0
- local Players={}
- local function logic(Act1,Act2)
- local v
- if Act1=='Scissor'then
- if Act2=='Rock' then
- v=Act2
- else
- v=Act1
- end
- elseif Act1=='Rock'then
- if Act2=='Scissor'then
- v=Act1
- else
- v=Act2
- end
- else
- if Act2=='Scissor' then
- v=Act2
- else
- v=Act1
- end
- end
- return v
- end
- local function analyze()
- local haveScissor=false
- local haveRock=false
- local havePaper=false
- for i,v in pairs(Players) do
- if v=='Scissor'then
- haveScissor=true
- elseif v=='Rock' then
- haveRock=true
- elseif v=='Paper' then
- havePaper=true
- end
- end
- local count=0
- if havePaper then
- count=1
- end
- if haveScissor then
- count=count+1
- end
- if haveRock then
- count=count+1
- end
- if count==2 then
- local a=haveScissor and'Scissor' or havePaper and'Paper' or'Rock'
- local b=haveScissor and'Scissor' or havePaper and'Paper' or'Rock'
- local winner=logic(a,b)
- print('\n'..winner,' won')
- local String=''
- for i,v in pairs(Players) do
- if v==winner then
- String=String..Players[i-1]..', '
- end
- end
- String=String:sub(0,#String-2)
- print('Winners are ',String)
- return true
- else
- return false
- end
- end
- local function report()
- round=round+1
- print('\nRound: ',round,'\n')
- for i,v in pairs(Players)do
- if tonumber(v:sub(2,2)) then
- print(v..': ',Players[i+1])
- end
- end
- end
- for i=1,amountOfPlayers do
- local random=math.random(0,2)
- local move=random==0 and 'Rock' or random==1 and 'Paper' or 'Scissor'
- table.insert(Players,'P'..i)
- table.insert(Players,move)
- end
- report()
- if not analyze() then
- repeat
- local t={}
- for i=1,amountOfPlayers do
- local random=math.random(0,2)
- local move=random==0 and 'Rock' or random==1 and 'Paper' or 'Scissor'
- table.insert(t,'P'..i)
- table.insert(t,move)
- end
- Players=t
- t=nil
- report()
- until analyze()
- end
- end
- rockPaperScissor(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement