demondownload

Ruby Golf

Oct 2nd, 2017 (edited)
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.03 KB | None | 0 0
  1. # ------------------------------------------------------------
  2. # D&D Ability score (46 characters, Ruby 2.4+)
  3. # Roll 4d6 and sum the three highest results
  4. def a;(1..4).map{rand 1..6}.sort[1..3].sum;end
  5.  
  6. # Get a set of six scores (63 characters)
  7. def s;(1..6).map{(1..4).map{rand 1..6}.sort[1..3].sum}.sort;end
  8.  
  9. # ------------------------------------------------------------
  10. # Random level generation (140 characters)
  11. # In a grid, start on the left edge and move up, down or right until you reach the right edge.
  12. 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
  13.  
  14. # Output:
  15. irb(main):01:0> l(20,8).map{|r| puts r.map{|c| c || ' ' }.join(' ') }
  16.                         1 1 1     1 1
  17.   1 1                   1   1   1 1 1
  18.   1 1             1 1 1 1   1 1 1   1
  19.   1 1 1 1 1 1 1 1 1           1 1   1
  20. 0 1     1 1   1 1                   1 1
  21.               1 1                     2
  22.               1 1
  23.  
  24. => [nil, nil, nil, nil, nil, nil, nil, nil]
Add Comment
Please, Sign In to add comment