Skip to content

Instantly share code, notes, and snippets.

@furai-no-ffff
Last active September 12, 2018 07:51
Show Gist options
  • Save furai-no-ffff/1737412 to your computer and use it in GitHub Desktop.
Save furai-no-ffff/1737412 to your computer and use it in GitHub Desktop.
格子マップでの店出現位置
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
@noromanba
Copy link

shop_in_gridmap.rb

Run

C:\>ver
Microsoft Windows XP [Version 5.1.2600]
C:\>ruby --version
ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mswin32]
C:\>shop_in_gridmap.rb
   0  738  296  303  755    0
   0  252    0    0  280    0
   0  415    0    0  453    0
   0 3456 2030  257  765    0
C:\>

Environment

Running on ActiveScriptRuby :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment