This commit is contained in:
Rampoina 2023-03-04 19:33:22 +01:00
parent 8abdfce09b
commit c9d521e0d2
1 changed files with 11 additions and 15 deletions

26
arc.bqn
View File

@ -3,17 +3,9 @@
# SPDX-FileCopyrightText: 2023 Rampoina <rampoina@protonmail.com>
#
# Arc
# The level is a 2d matrix of lists (tiles)
# Each list contains the objects of the game:
# 0: floor, 1: player, 2: box, 3: goal, 4: wall
# 4: player on goal, 5: box on goal
#
# Example: ASCII:
# ┌─ ┌─
# ╵ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ╵"#####
# ⟨ 4 ⟩ ⟨ 1 0 ⟩ ⟨ 2 0 ⟩ ⟨ 0 ⟩ ⟨ 4 ⟩ #@*.#
# ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ #####"
# ┘ ┘
# The level is a 2d matrix of lists (tiles)
# Each list contains the objects of the game represented as ints
moves00 # list of moves, each move is a direction, we start without moving
chars" λ$⊕λ⭍#/\-|+<>^v" # legal characters
movables127812131415 # player, box mirrors and lasers
@ -27,7 +19,7 @@ ansi←{
defaultBe"[0m"
}
SplitOnEmpty{𝕩˜(-˜+`׬)0=¨𝕩}
# Turns the level from ascii strings into a 2d matrix of lists
Ascii2Matrix{(chars𝕩)0,10,20,3,13,23,6,70,80,9,10,11,120,130,140,150}¨(˝·¬2+)
# 𝕨 Tiles 𝕩 | 𝕩: object coordinate (3‿1) | 𝕨: direction vector (¯1‿0)
@ -52,7 +44,8 @@ FindIdx←/○⥊⟜(↕≢)∘⍷
# 𝕨 Beam 𝕩 | 𝕨: level | 𝕩: position | replace positions with the laser beam
Beam{{"--++"˜" -|+"𝕩}(𝕩)𝕨}
# Recursive Bounce
# w‿d Bounce x | x: map | w‿d: w: current position, d: direction of the laser
# Calculates the bounces of a laser beam recursively
Bounce{(wd)S x:{
opaque˜wx?(3=wx)x,5˙(w)x@; # laser hits a non-mirror
empties˜wx ? # Empty space
@ -63,14 +56,17 @@ Bounce←{(w‿d)S x:{
w+d,dS x } # recurse to the next position
}
# Shoot 𝕩 | 𝕩: map | calculates the bounces for each laser
Shoot{𝕩 {𝕨Bounce´𝕩} <0¯1,<01,<¯10,<10(˜¨+)¨ FindIdx(¨𝕩)¨ 12131415}
Step{Push((𝕨 Tiles 1 FindIdx ¨𝕩))𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | Step the game
Draw{´¨<˘{𝕊'⭍':ansi.yellow𝕩;𝕩"|-+"?ansi.red𝕩;𝕩"\/"?ansi.cyan𝕩;ansi.defaultB𝕩}¨chars˜+´¨Shoot 𝕨 Step´ 𝕩} # 𝕨 Draw 𝕩 | 𝕨: levels | 𝕩: moves | Draw the game in ASCII
Colorize{𝕊'⭍':ansi.yellow𝕩;𝕩"|-+"?ansi.red𝕩;𝕩"\/"?ansi.cyan𝕩;ansi.defaultB𝕩}
Draw{´¨<˘Colorize¨chars˜+´¨Shoot 𝕨 Step´ 𝕩} # 𝕨 Draw 𝕩 | 𝕨: levels | 𝕩: moves | Draw the game in ASCII
Win{¬´˝3=¨Shoot 𝕩}# W 𝕩 | 𝕩: level | [W]in condition, no unpowered goals after shooting laser
Next{movesmoves<𝕩}
Undo{𝕊:moves(-1<)moves}
# Main loop
SplitOnEmpty{𝕩˜(-˜+`׬)0=¨𝕩}
levelsAscii2Matrix¨>¨SplitOnEmpty•FLines "levels" # Load file containing levels
currentLevel0
eansi.e
@ -93,7 +89,7 @@ clear←""
{𝕤cleare"[2J"}(((1+>(1+101))¯1+moves)key='u')@
•Out e"8" # Restore cursor position
{𝕤•Out clearansi.yellow"⭍"ansi.defaultB" Power the machines (⊕) by moving the mirrors ("ansi.cyan"\/"ansi.defaultB") "(@+10)"Controls: (hjkl or wasd) to move, u to undo, r to reset level, q to quit"}(key"wasdhjkluqr")@
{𝕤•Out clearansi.yellow"⭍"ansi.defaultB" Power the machines (⊕) by moving the mirrors ("ansi.cyan"\/"ansi.defaultB")"(@+10)"Controls: (hjkl or wasd) to move, u to undo, r to reset level, q to quit"}(key"wasdhjkluqr")@
{𝕤•Out "Invalid key: (hjkl or wasd) to move, u to undo, r to reset level"}(¬key"wasdhjkluqr")@
•Out¨ (currentLevellevels) Draw moves
•Out "Moves: "•Repr ¯1+moves