From c9d521e0d27a61b29e86754bcc423a387a5e3e05 Mon Sep 17 00:00:00 2001 From: Rampoina Date: Sat, 4 Mar 2023 19:33:22 +0100 Subject: [PATCH] Refactor --- arc.bqn | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/arc.bqn b/arc.bqn index 02cb871..1ac48a1 100755 --- a/arc.bqn +++ b/arc.bqn @@ -3,17 +3,9 @@ # SPDX-FileCopyrightText: 2023 Rampoina # # 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 + moves←⟨0‿0⟩ # list of moves, each move is a direction, we start without moving chars←" λ$⊕λ⭍#/\-|+<>^v" # legal characters movables←1‿2‿7‿8‿12‿13‿14‿15 # player, box mirrors and lasers @@ -27,7 +19,7 @@ ansi←{ 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+≢) # 𝕨 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←{(w‿d)S x:{ ⊑opaque∊˜⊑w⊑x?(3=⊑w⊑x)◶⟨x,⟨5⟩˙⌾(w⊸⊑)x⟩@; # laser hits a non-mirror ⊑empties∊˜⊑w⊑x ? # Empty space @@ -63,14 +56,17 @@ Bounce←{(w‿d)S x:{ ⟨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} 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←{moves↩moves∾<𝕩} Undo←{𝕊:moves↩(-1<≠)⊸↓moves} # Main loop +SplitOnEmpty←{𝕩⊔˜(⊢-˜+`׬)0=≠¨𝕩} levels←Ascii2Matrix¨>¨SplitOnEmpty•FLines "levels" # Load file containing levels currentLevel←0 e←ansi.e @@ -93,7 +89,7 @@ clear←"" {𝕤⋄clear↩e∾"[2J"}⍟(((1⊸+>○(⌊1+10⋆⁼1⌈⊢)⊢)¯1+≠moves)∧key='u')@ •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¨ (currentLevel⊑levels) Draw moves •Out "Moves: "∾•Repr ¯1+≠moves