Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Random level generation (140 characters)
- # In a grid, start on the left edge and move up, down or right until you reach the right edge.
- 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
- # Output:
- irb(main):01:0> l(20,8).map{|r| puts r.map{|c| c || ' ' }.join(' ') }
- 1 1 1 1 1
- 1 1 1 1 1 1 1
- 1 1 1 1 1 1 1 1 1 1
- 1 1 1 1 1 1 1 1 1 1 1 1
- 0 1 1 1 1 1 1 1
- 1 1 2
- 1 1
- => [nil, nil, nil, nil, nil, nil, nil, nil]
Add Comment
Please, Sign In to add comment