Advertisement
CollinJSimpson

Lua Shuffle List

Aug 11th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1. #!/usr/bin/lua
  2.  
  3. io.write('How many people? ')
  4. p = tonumber(io.read())
  5. io.write('How many teams? ')
  6. t = tonumber(io.read())
  7.  
  8. if t > p then
  9.  print('Error. You can\'t have more teams than players!')
  10.  os.exit(0)
  11. end
  12.  
  13. -- input
  14. names = {}
  15. for i=1,p do
  16.  io.write(i .. ' > ')
  17.  --table.insert(names, io.read())
  18.  table.insert(names, string.char(64 + i))
  19. end
  20.  
  21. -- shuffle
  22. for i=1,p do
  23.  a = math.random(1,p)
  24.  tmp = names[a]
  25.  names[a] = names[i]
  26.  names[i] = tmp
  27. end
  28.  
  29. -- output
  30. print('')
  31. nt = p / t -- base number of people per team
  32. rem = p % t -- number of extra people
  33. cpi = 1 -- current player index
  34. for ti=0,t-1 do
  35.  nt2 = nt
  36.  if ti < rem then nt2 = nt + 1 end
  37.  print('')
  38.  for pi=0,nt2-1 do
  39.   print(names[cpi] .. ' on team ' .. ti)
  40.   cpi = cpi + 1
  41.  end
  42. end
  43.  
  44. os.exit(0)
  45. for i=1,p do
  46.  print(names[i])
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement