Last active
September 12, 2018 07:52
-
-
Save furai-no-ffff/1768143 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
require 'enumerator' | |
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 # up | |
idx - 6 | |
when 2 # right | |
idx + 1 | |
when 4 # down | |
idx + 6 | |
when 8 # left | |
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_grid | |
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 make_passages_circum | |
array = [ | |
6, 10, 10, 10, 10, 12, | |
5, 0, 0, 0, 0, 5, | |
5, 0, 0, 0, 0, 5, | |
3, 10, 10, 10, 10, 9 | |
] | |
make_branch(array, 0x22, 1, 8) | |
make_branch(array, 0x23, 1, 8) | |
make_branch(array, 0x24, 1, 2) | |
make_branch(array, 0x25, 1 ,2) | |
make_branch(array, 0x32, 4, 8) | |
make_branch(array, 0x33, 4, 8) | |
make_branch(array, 0x34, 4, 2) | |
make_branch(array, 0x35, 4, 2) | |
make_branch(array, 0x23, 4, 2) | |
make_branch(array, 0x24, 4, 8) | |
make_branch(array, 0x33, 1, 2) | |
make_branch(array, 0x34, 1, 8) | |
array | |
end | |
def search_room(rooms, v) | |
num = nil | |
rooms.each_with_index{| n,i | | |
if n == v then | |
num = i | |
break | |
end | |
} | |
num | |
end | |
def count_entrance(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) | |
next unless pos | |
c = count_entrance(passages[pos]) | |
if c == 1 then | |
return pos | |
end | |
} | |
nil | |
end | |
def idx2coord(idx) | |
y,x = idx.divmod(6) | |
[x+1, y+1] | |
end | |
def main_common(&block) | |
shops = Array.new(24, 0) | |
no_shop = 0 | |
10000.times{ | |
rooms, passages = block.call | |
idx = search_shop(rooms, passages) | |
if idx then | |
shops[idx] += 1 | |
else | |
no_shop += 1 | |
end | |
} | |
shops.each_slice(6){| line | | |
puts line.map{| v | | |
v.to_s.rjust(4) | |
}.join(' ') | |
} | |
puts "no shop: #{no_shop}" | |
end | |
def main_grid | |
main_common{ | |
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_grid | |
[rooms, passages] | |
} | |
end | |
def main_circum | |
main_common{ | |
rooms = prune_room(shuffle_room([ | |
nil, nil, nil, nil, nil, nil, | |
nil, 0, 1, 2, 3, nil, | |
nil, 4, 5, 6, 7, nil, | |
nil, nil, nil, nil, nil, nil] | |
)) | |
passages = make_passages_circum | |
[rooms, passages] | |
} | |
end | |
puts "格子:" | |
main_grid | |
puts | |
puts "外周:" | |
main_circum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Action Report
About charactor encoding.
Named
shop_uprise_simulator.rb
UTF-8 Problems on Windows + ActiveScriptRuby
This ruby code must be save to Sift-JIS encode.
Default Shift_JIS Terminal
Script saved UTF-8.
Kanji charactor are garbled.
uprise.log
Created by Cygwin tee.
Created UTF-8 text file. decent answer.
Use UTF-8 Terminal
Script saved UTF-8.
Oops...Clash! Access denied.
Safe Runnable pattern saved on Sift-JIS(cp932)
:EOF
Regards,
noromanba