Last active
September 1, 2024 10:15
-
-
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/4e1cd7d5419fdcc4276ddbf1354a1f146f3c27d
This file contains hidden or 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
// 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.4.2" | |
//> using dep "org.creativescala::doodle:0.23.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