demondownload

Level

Jun 25th, 2019 (edited)
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.65 KB | None | 0 0
  1. # Random level generation (140 characters)
  2. # In a grid, start on the left edge and move up, down or right until you reach the right edge.
  3. def l w,h;g=(1..h).map{[]};y=rand h;x=g[y][0]=0;(g[y][x]||=1;n=y+rand(-1..1);n>=0&&n<h&&g[n][x].nil?? y=n : x+=1)while x<w;g[y][x-1]=2;g;end
  4.  
  5. # Output:
  6. irb(main):01:0> l(20,8).map{|r| puts r.map{|c| c || ' ' }.join(' ') }
  7.                         1 1 1     1 1
  8.   1 1                   1   1   1 1 1
  9.   1 1             1 1 1 1   1 1 1   1
  10.   1 1 1 1 1 1 1 1 1           1 1   1
  11. 0 1     1 1   1 1                   1 1
  12.               1 1                     2
  13.               1 1
  14.  
  15. => [nil, nil, nil, nil, nil, nil, nil, nil]
Add Comment
Please, Sign In to add comment