Skip to content

Instantly share code, notes, and snippets.

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

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.

C:\>chcp 932
現在のコード ページ: 932

C:\>ver
Microsoft Windows XP [Version 5.1.2600]

C:\>ruby --version
ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mswin32]

C:\>uname -a
CYGWIN_NT-5.1 %COMPUTERNAME% 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

C:\>shop_uprise_simulator.rb | tee uprise.log
譬シ蟄・
   0  799  269  298  785    0
   0  288    0    0  321    0
   0  371    0    0  434    0
   0 3413 1995  286  741    0
no shop: 0

螟門捉:
   0    0    0    0    0    0
   0 1358    0    0 1798    0
   0 2334    0    0 3854    0
   0    0    0    0    0    0
no shop: 656
C:\>

Kanji charactor are garbled.

uprise.log

Created by Cygwin tee.

格子:
   0  799  269  298  785    0
   0  288    0    0  321    0
   0  371    0    0  434    0
   0 3413 1995  286  741    0
no shop: 0

外周:
   0    0    0    0    0    0
   0 1358    0    0 1798    0
   0 2334    0    0 3854    0
   0    0    0    0    0    0
no shop: 656

Created UTF-8 text file. decent answer.

Use UTF-8 Terminal

Script saved UTF-8.

C:\>chcp
現在のコード ページ: 932

C:\>chcp 65001
Active code page: 65001


C:\>shop_in_gridmap.rb
   0  776  253  284  756    0
   0  288    0    0  335    0
   0  361    0    0  444    0
   0 3390 2114  262  737    0

C:\>shop_uprise_simulator.rb
C:/shop_uprise_simulator.rb:193:in `write': Permission denied (Errno::EACCES)
        from C:/shop_uprise_simulator.rb:193:in `puts'
        from C:/shop_uprise_simulator.rb:193

C:\>

Oops...Clash! Access denied.

Safe Runnable pattern saved on Sift-JIS(cp932)

C:\>ruby shop_uprise_simulator_SJIS.rb
格子:
   0  785  269  279  768    0
   0  286    0    0  285    0
   0  404    0    0  437    0
   0 3470 2038  270  709    0
no shop: 0

外周:
   0    0    0    0    0    0
   0 1363    0    0 1831    0
   0 2309    0    0 3899    0
   0    0    0    0    0    0
no shop: 598

:EOF

Regards,
noromanba kss

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