Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active August 18, 2023 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/a520baaafaa6fe85e0f203ccc735035c to your computer and use it in GitHub Desktop.
Save dacr/a520baaafaa6fe85e0f203ccc735035c to your computer and use it in GitHub Desktop.
Drawing a labyrinth using doodle library. / published by https://github.com/dacr/code-examples-manager #32793934-a545-4d97-ad1a-93bb0d071799/261bf92721e0c2c863dd56400c6d82fc06a7eacb
// summary : Drawing a labyrinth using doodle library.
// keywords : scala, vector-graphics, doodle
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 32793934-a545-4d97-ad1a-93bb0d071799
// created-on : 2019-06-25T20:15:55Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "org.creativescala::doodle:0.20.0"
// ---------------------
import doodle.core.*
import doodle.syntax.*
import doodle.syntax.all.*
import doodle.image.*
import doodle.image.syntax.*
import doodle.image.syntax.core.*
import doodle.image.syntax.all.*
import doodle.java2d.*
import doodle.java2d.effect.*
import cats.effect.unsafe.implicits.global
// generated with https://www.dcode.fr/maze-generator
val labyrinth =
"""###############################
|#S# # # # # # #
|# # ### ##### # ### # ### ### #
|# # # # # # #
|# ### ### ### # # ### ### #####
|# # # # # # # # # #
|# ##### # # ### # # # ### ### #
|# # # # # # # # # #
|##### # ### # # ### ######### #
|# # # # # # # #
|# # # ### ####### # ### ### # #
|# # # # # # # #
|# # ### ####### ##### ####### #
|# # # # # #
|### ### # ##### ##### # ### ###
|# # # # # # # # # #
|# ### # ######### # ##### ### #
|# # # # # # # # # #
|# # # # ### ##### # ##### # # #
|# # # # # #E#
|###############################""".stripMargin.trim
val sz = 20
val wall = Image.rectangle(sz, sz).fillColor(Color.black)
val floor = Image.rectangle(sz, sz).fillColor(Color.beige).strokeColor(Color.beige.darkenBy(5.normalized))
val start = Image.circle(sz).fillColor(Color.green)
val goal = Image.triangle(sz, sz).fillColor(Color.red)
def charToImage(ch: Char) = ch match {
case '#' => wall
case ' ' => floor
case 'S' => start
case 'E' => goal
}
val img =
labyrinth
.split("\n")
.zipWithIndex
.map { case (row, rowIndex) =>
if (rowIndex % 2 == 0) row.map(charToImage).reduce((c1, c2) => c1.beside(c2))
else row.reverse.map(charToImage).reduce((c1, c2) => c2.beside(c1))
}
.reduce((r1, r2) => r2.below(r1))
img.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment