Refactor
This commit is contained in:
parent
8abdfce09b
commit
c9d521e0d2
26
arc.bqn
26
arc.bqn
|
@ -3,17 +3,9 @@
|
||||||
# SPDX-FileCopyrightText: 2023 Rampoina <rampoina@protonmail.com>
|
# SPDX-FileCopyrightText: 2023 Rampoina <rampoina@protonmail.com>
|
||||||
#
|
#
|
||||||
# Arc
|
# Arc
|
||||||
# The level is a 2d matrix of lists (tiles)
|
# The level is a 2d matrix of lists (tiles)
|
||||||
# Each list contains the objects of the game:
|
# Each list contains the objects of the game represented as ints
|
||||||
# 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 ⟩ #####"
|
|
||||||
# ┘ ┘
|
|
||||||
moves←⟨0‿0⟩ # list of moves, each move is a direction, we start without moving
|
moves←⟨0‿0⟩ # list of moves, each move is a direction, we start without moving
|
||||||
chars←" λ$⊕λ⭍#/\-|+<>^v" # legal characters
|
chars←" λ$⊕λ⭍#/\-|+<>^v" # legal characters
|
||||||
movables←1‿2‿7‿8‿12‿13‿14‿15 # player, box mirrors and lasers
|
movables←1‿2‿7‿8‿12‿13‿14‿15 # player, box mirrors and lasers
|
||||||
|
@ -27,7 +19,7 @@ ansi←{
|
||||||
defaultB⇐e∾"[0m"
|
defaultB⇐e∾"[0m"
|
||||||
}
|
}
|
||||||
|
|
||||||
SplitOnEmpty←{𝕩⊔˜(⊢-˜+`׬)0=≠¨𝕩}
|
# Turns the level from ascii strings into a 2d matrix of lists
|
||||||
Ascii2Matrix←{(⊑chars⊐𝕩)⊑⟨≍0,1‿0,2‿0,≍3,1‿3,2‿3,≍6,7‿0,8‿0,≍9,≍10,≍11,12‿0,13‿0,14‿0,15‿0⟩}¨(⊢↑˝·≍⟜¬2+≢)
|
Ascii2Matrix←{(⊑chars⊐𝕩)⊑⟨≍0,1‿0,2‿0,≍3,1‿3,2‿3,≍6,7‿0,8‿0,≍9,≍10,≍11,12‿0,13‿0,14‿0,15‿0⟩}¨(⊢↑˝·≍⟜¬2+≢)
|
||||||
|
|
||||||
# 𝕨 Tiles 𝕩 | 𝕩: object coordinate (3‿1) | 𝕨: direction vector (¯1‿0)
|
# 𝕨 Tiles 𝕩 | 𝕩: object coordinate (3‿1) | 𝕨: direction vector (¯1‿0)
|
||||||
|
@ -52,7 +44,8 @@ FindIdx←/○⥊⟜(↕≢)∘⍷
|
||||||
# 𝕨 Beam 𝕩 | 𝕨: level | 𝕩: position | replace positions with the laser beam
|
# 𝕨 Beam 𝕩 | 𝕨: level | 𝕩: position | replace positions with the laser beam
|
||||||
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←{(w‿d)S x:{
|
Bounce←{(w‿d)S x:{
|
||||||
⊑opaque∊˜⊑w⊑x?(3=⊑w⊑x)◶⟨x,⟨5⟩˙⌾(w⊸⊑)x⟩@; # laser hits a non-mirror
|
⊑opaque∊˜⊑w⊑x?(3=⊑w⊑x)◶⟨x,⟨5⟩˙⌾(w⊸⊑)x⟩@; # laser hits a non-mirror
|
||||||
⊑empties∊˜⊑w⊑x ? # Empty space
|
⊑empties∊˜⊑w⊑x ? # Empty space
|
||||||
|
@ -63,14 +56,17 @@ Bounce←{(w‿d)S x:{
|
||||||
⟨w+d,d⟩S x } # recurse to the next position
|
⟨w+d,d⟩S x } # recurse to the next position
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Shoot 𝕩 | 𝕩: map | calculates the bounces for each laser
|
||||||
Shoot←{𝕩 {𝕨Bounce´⌽𝕩} ∾⟨<0‿¯1,<0‿1,<¯1‿0,<1‿0⟩(⊣⋈˜¨+)¨ FindIdx⟜(⊑¨𝕩)¨ 12‿13‿14‿15}
|
Shoot←{𝕩 {𝕨Bounce´⌽𝕩} ∾⟨<0‿¯1,<0‿1,<¯1‿0,<1‿0⟩(⊣⋈˜¨+)¨ FindIdx⟜(⊑¨𝕩)¨ 12‿13‿14‿15}
|
||||||
Step←{Push⌾((𝕨 Tiles ⊑1 FindIdx ⊑¨𝕩)⊸⊑)𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | Step the game
|
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
|
Win←{¬∨´∨˝3=⊑¨Shoot 𝕩}# W 𝕩 | 𝕩: level | [W]in condition, no unpowered goals after shooting laser
|
||||||
Next←{moves↩moves∾<𝕩}
|
Next←{moves↩moves∾<𝕩}
|
||||||
Undo←{𝕊:moves↩(-1<≠)⊸↓moves}
|
Undo←{𝕊:moves↩(-1<≠)⊸↓moves}
|
||||||
|
|
||||||
# Main loop
|
# Main loop
|
||||||
|
SplitOnEmpty←{𝕩⊔˜(⊢-˜+`׬)0=≠¨𝕩}
|
||||||
levels←Ascii2Matrix¨>¨SplitOnEmpty•FLines "levels" # Load file containing levels
|
levels←Ascii2Matrix¨>¨SplitOnEmpty•FLines "levels" # Load file containing levels
|
||||||
currentLevel←0
|
currentLevel←0
|
||||||
e←ansi.e
|
e←ansi.e
|
||||||
|
@ -93,7 +89,7 @@ clear←""
|
||||||
{𝕤⋄clear↩e∾"[2J"}⍟(((1⊸+>○(⌊1+10⋆⁼1⌈⊢)⊢)¯1+≠moves)∧key='u')@
|
{𝕤⋄clear↩e∾"[2J"}⍟(((1⊸+>○(⌊1+10⋆⁼1⌈⊢)⊢)¯1+≠moves)∧key='u')@
|
||||||
•Out e∾"8" # Restore cursor position
|
•Out e∾"8" # Restore cursor position
|
||||||
|
|
||||||
{𝕤⋄•Out clear∾ansi.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 clear∾ansi.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 "Invalid key: (hjkl or wasd) to move, u to undo, r to reset level"}⍟(¬⊑key∊"wasdhjkluqr")@
|
||||||
•Out¨ (currentLevel⊑levels) Draw moves
|
•Out¨ (currentLevel⊑levels) Draw moves
|
||||||
•Out "Moves: "∾•Repr ¯1+≠moves
|
•Out "Moves: "∾•Repr ¯1+≠moves
|
||||||
|
|
Loading…
Reference in New Issue