我最近一直致力于研究我的Ruby技能,并在迷宫世代中遇到了一个漂亮的时髦演示。
我想实现一些算法,然后为迷宫生成图像文件。
我对这项工作的第二部分非常不确定:“生成迷宫的形象”。我想要一个简单的宝石,让我将迷宫映射到图像。
也许有一段时间我也希望将整个内容作为Web上的Ruby on Rails应用程序。
如何把所有这些放在一起?
答案 0 :(得分:3)
使用RMagick很容易:
require 'rubygems'
require 'RMagick'
maze = <<-MAZE
##############
.............#
############.#
#............#
#.#.########.#
#.#..........#
#.############
MAZE
maze = maze.split("\n").map{|line| line.split('')}
square_size = 50
height = maze.size
width = maze.first.size
img_height = height * square_size
img_width = width * square_size
img = Magick::Image.new(img_width, img_height)
img_width.times do |col|
img_height.times do |row|
line_idx = (row/square_size).floor
char_idx = (col/square_size).floor
char = maze[line_idx][char_idx]
color = (char == "#" ? "rgb(0, 0, 0)" : "rgb(255, 255, 255)")
img.pixel_color(col, row, color)
end
end
img.write('maze.png')
答案 1 :(得分:3)
chunky_png gem绝对值得一试。
答案 2 :(得分:1)
时间继续前进。 Jamis Buck现在已经为程序员编写了一本名为“迷宫”的书。在务实的书架上。我认为这是你参考Ruby和Mazes的参考资料。