Make a digging maze with Ruby2D

IMAGE ALT TEXT HERE I tried to implement a "drilling maze" using Gem "Ruby2D". You can see the "road" gradually growing in real time. The above image is linked to a YouTube video.

code

dig_maze.rb


require "ruby2d"

L = 20    #The size of the maze
W = L * 2 + 3

Block_w = 10   #The size of one block
set width: W * Block_w, height: W * Block_w, fps_cap: 10

blocks = W.times.map {|y|
  W.times.map {|x|
    Square.new x: x * Block_w, y: y * Block_w,
               size: Block_w, color: "green"
  }
}

field = Array.new(W) {Array.new(W, 1)}
#Place a "sentinel"
field[0] = field[-1] = Array.new(W, -1)
(1..W - 2).each {|y| field[y][0] = field[y][-1] = -1}

field.define_singleton_method(:show) do
  each_index do |y|
    self[y].each_index do |x|
      self[y][x].zero? ? blocks[y][x].remove : blocks[y][x].add
    end
  end
end

start = [2, 2]
stack = [start]
show_stack = [start]

dig = ->(now) {
  movable = []
  [[1, 0], [0, -1], [-1, 0], [0, 1]].each do |dx, dy|
    x = now[0] + dx * 2
    y = now[1] + dy * 2
    movable << [x, y] if field[y][x] == 1
  end
  if movable.empty?
    return if stack.empty?
    jump = stack.delete_at(rand(stack.size))
    dig.(jump)
  else
    nxt = movable.sample
    show_stack << [(now[0] + nxt[0]) / 2, (now[1] + nxt[1]) / 2]
    show_stack << nxt
    stack << nxt
  end
}


update do
  now = show_stack.shift
  next unless now
  field[now[1]][now[0]] = 0
  field.show
  dig.(now) if show_stack.empty?
end

show

I will briefly explain the methods of Ruby2D.

--set Set the size of the window, etc. --Square.new Create an object that represents a square. Objects can be shown or hidden by adding .add`` .remove. --ʻUpdate The block is updated in a certain period of time (usually 60fps). Here, it is displayed at 10 fps. --show` Enter the main loop.

The digging itself is implemented by simple recursion. If the array variable field represents a maze and the value is 0, it means that there is a "way". There are "sentinels" around, so that there is no "road" outside.

Recommended Posts

Make a digging maze with Ruby2D
Make a slideshow tool with JavaFX
Make a garbage reminder with line-bot-sdk-java
Make a list map with LazyMap
Make a typing game with ruby
[Java] I tried to make a maze by the digging method ♪
Let's make a Christmas card with Processing!
Make a family todo list with Sinatra
Make a family todo list with Sinatra
Make a login function with Rails anyway
[docker] [nginx] Make a simple ALB with nginx
Let's make a search function with Rails (ransack)
Make System.out a Mock with Spock Test Framework
Run Scala with GraalVM & make it a native image
Let's make a LINE Bot with Ruby + Sinatra --Part 2
[Java basics] Let's make a triangle with a for statement
[Personal application work memo] Make a calendar with simple_calendar
Make a reflection utility ②
Make a reflection utility ③
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Make a reflection utility ①
[Beginner] Try to make a simple RPG game with Java ①
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Let's make a simple API with EC2 + RDS + Spring boot ①
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ⑤ ~ Template standardization ~
Microservices with DevOps Make Changes
Create a playground with Xcode 12
[Java] Make it a constant
[Rails] Make a breadcrumb trail
Make a rhombus using Java
A story stuck with NotSerializableException
I want to make a button with a line break with link_to [Note]
How to make a jar file with no dependencies in Maven
Let's make a book management web application with Spring Boot part3
Try to make a cross-platform application with JRuby (jar file generation)
Let's make a book management web application with Spring Boot part2
Make a C compiler to use with Rust x CLion with Docker
I tried to make a group function (bulletin board) with Rails