Last active
September 12, 2018 07:51
-
-
Save furai-no-ffff/1737412 to your computer and use it in GitHub Desktop.
格子マップでの店出現位置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shuffle_room(array) | |
(array.length-1).downto(0){| i | | |
x = rand(i+1) | |
next unless array[i] | |
next unless array[x] | |
array[i],array[x] = array[x],array[i] | |
} | |
array | |
end | |
def prune_room(array) | |
array.map{| v | | |
if not v or v >= 10 then | |
nil | |
else | |
v | |
end | |
} | |
end | |
def move(idx,dir) | |
case dir | |
when 1 | |
idx - 6 | |
when 2 | |
idx + 1 | |
when 4 | |
idx + 6 | |
when 8 | |
idx - 1 | |
end | |
end | |
def rev(dir) | |
case dir | |
when 1 | |
4 | |
when 2 | |
8 | |
when 4 | |
1 | |
when 8 | |
2 | |
end | |
end | |
def make_branch(array, pos, dir1, dir2) | |
x = (pos & 0xF) - 1 | |
y = (pos >> 4) - 1 | |
idx = y * 6 + x | |
if rand < 0.5 then | |
array[idx] |= dir1 | |
array[move(idx,dir1)] |= rev(dir1) | |
else | |
array[idx] |= dir2 | |
array[move(idx,dir2)] |= rev(dir2) | |
end | |
end | |
def make_passages | |
array = [ | |
0, 0, 4, 4, 0, 0, | |
0, 2, 15, 15, 8, 0, | |
0, 2, 15, 15, 8, 0, | |
0, 0, 1, 1, 0, 0 | |
] | |
make_branch(array, 0x12, 4, 2) | |
make_branch(array, 0x15, 4, 8) | |
make_branch(array, 0x42, 1, 2) | |
make_branch(array, 0x45, 1, 8) | |
make_branch(array, 0x13, 2, 2) if rand < 0.25 | |
make_branch(array, 0x22, 4, 4) if rand < 0.25 | |
make_branch(array, 0x25, 4, 4) if rand < 0.25 | |
make_branch(array, 0x43, 2, 2) if rand < 0.25 | |
array | |
end | |
def search_room(rooms, v) | |
num = nil | |
rooms.each_with_index{| n,i | | |
if n == v then | |
num = i | |
end | |
} | |
num | |
end | |
def count_gates(n) | |
count = 0 | |
[1, 2, 4, 8].each{| v | | |
if n & v != 0 then | |
count += 1 | |
end | |
} | |
count | |
end | |
def search_shop(rooms, passages) | |
9.downto(0){| i | | |
pos = search_room(rooms, i) | |
if count_gates(passages[pos]) == 1 then | |
return pos | |
end | |
} | |
end | |
def idx2coord(idx) | |
y,x = idx.divmod(6) | |
[x+1, y+1] | |
end | |
require 'enumerator' | |
shops = Array.new(24, 0) | |
10000.times{ | |
rooms = prune_room(shuffle_room([ | |
nil, 0, 1, 2, 3, nil, | |
nil, 4, nil, nil, 5, nil, | |
nil, 6, nil, nil, 7, nil, | |
nil, 8, 9, 10,11, nil] | |
)) | |
passages = make_passages | |
shops[search_shop(rooms, passages)] += 1 | |
} | |
shops.each_slice(6){| line | | |
puts line.map{| v | | |
v.to_s.rjust(4) | |
}.join(' ') | |
} | |
__END__ | |
=> | |
0 773 290 270 724 0 | |
0 287 0 0 295 0 | |
0 445 0 0 432 0 | |
0 3482 2028 257 717 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shop_in_gridmap.rb
Run
Environment
Running on ActiveScriptRuby :-)