Advertisement
xth

I used a goto

xth
May 27th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1.  
  2. function Run()
  3.     GameObjects = {}
  4.     CIRCLE = 1
  5.     RECTANGLE = 2
  6.     io.stdout:setvbuf"no"
  7.     local ObjectsThatDidntMakeIt,
  8.           OriginalLengthOfPotentialObjects,
  9.           FinalLengthOfPotentialObjectsIgnoringNils,
  10.           TotalFailedAssigns;
  11.     local t = os.clock()
  12.     math.randomseed(os.time())
  13.     do
  14.         local rand = math.random
  15.         local UX, UY;
  16.         local i = 1;
  17.         local potential_objects = {}
  18.         for x=256,1,-1 do
  19.             for y=256,1,-1 do
  20.                 potential_objects[i] = {x, y, CIRCLE, 10}
  21.                 -- x, y, shape, radius
  22.                 i = i + 1;
  23.             end
  24.         end
  25.         local len = #potential_objects
  26.         local finallen = len
  27.         local failedAssignments = 0
  28.         for i = 1, 50000 do
  29.             ::redo::
  30.             local ind = rand(len)
  31.             if not potential_objects[ind] then
  32.                 failedAssignments = failedAssignments + 1
  33.                 goto redo
  34.             end
  35.             GameObjects[i] = potential_objects[ind]
  36.             potential_objects[ind] = nil;
  37.             finallen = finallen - 1
  38.         end
  39.         ObjectsThatDidntMakeIt = potential_objects
  40.         OriginalLengthOfPotentialObjects = len
  41.         FinalLengthOfPotentialObjectsIgnoringNils = finallen
  42.         TotalFailedAssigns = failedAssignments
  43.     end
  44.  
  45.  
  46.     print( "Creating " ..
  47.         #GameObjects ..
  48.         " GameObjects. Time Elapsed: " ..
  49.         (os.clock() - t) ..
  50.         " seconds")
  51.     print("Potential Objects started: ".. OriginalLengthOfPotentialObjects)
  52.     print( "Potential Objects remaining: " .. FinalLengthOfPotentialObjectsIgnoringNils)
  53.     print ("Total Failed Assignments from Potential Objects to Game Objects: ".. TotalFailedAssigns)
  54.  
  55. end
  56.  
  57. function love.keypressed(key)
  58.     if key=="r" then
  59.         Run()
  60.     end
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement