Get rid of the magic numbers
This commit is contained in:
parent
3eed7bf204
commit
9245c619a9
37
arc.bqn
37
arc.bqn
|
@ -7,10 +7,14 @@
|
|||
# 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 # player, box and mirrors
|
||||
opaque←1‿2‿3‿12‿13‿14‿15‿6 # non laser reflecting
|
||||
empties←0‿9‿10‿11 # floor and laser beams
|
||||
chars←" λ$⊕⭍#/\-|+<>^v" # legal characters
|
||||
⟨floor,player,box,machine,pmachine,wall,lmirror,rmirror,hbeam,vbeam,xbeam,llaser,rlaser,ulaser,dlaser⟩←↕≠chars
|
||||
beams←hbeam‿vbeam‿xbeam
|
||||
mirrors←lmirror‿rmirror
|
||||
lasers←llaser‿rlaser‿ulaser‿dlaser
|
||||
movables←player‿box∾mirrors # player, box and mirrors
|
||||
opaque←player‿box‿machine‿wall∾lasers # non laser reflecting
|
||||
empties←floor∾beams # floor and laser beams
|
||||
ansi←{
|
||||
e⇐@+27
|
||||
red⇐e∾"[31m"
|
||||
|
@ -19,8 +23,15 @@ ansi←{
|
|||
defaultB⇐e∾"[0m"
|
||||
}
|
||||
|
||||
colors←(≠chars)⥊<ansi.defaultB # start with all glyphs being the default color
|
||||
colors ansi.yellow˙⌾(pmachine⊸⊑)↩
|
||||
colors ansi.cyan¨⌾(lmirror‿rmirror⊸⊏)↩
|
||||
colors ansi.red¨⌾(hbeam‿vbeam‿xbeam⊸⊏)↩
|
||||
Colorize←{𝕩∾˜colors⊑˜⊑chars⊐𝕩} # 𝕩: character | Turn character into color+character
|
||||
|
||||
lTiles←{⊑𝕩∊movables ? 𝕩‿floor; ≍𝕩}¨↕≠chars # list of all the possible tiles as lists of numbers
|
||||
# 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⊐𝕩)⊑lTiles}¨(⊢↑˝·≍⟜¬2+≢)
|
||||
|
||||
# 𝕨 Tiles 𝕩 | 𝕩: object coordinate (3‿1) | 𝕨: direction vector (¯1‿0)
|
||||
# result: ⟨ ⟨ 3 1 ⟩ ⟨ 2 1 ⟩ ⟨ 1 1 ⟩ ⟩
|
||||
|
@ -40,26 +51,20 @@ Move←{a‿b:⟨1↓a,(⊑a)∾b⟩}⍟{∨´(⥊movables≍⌜empties)≡⌜<
|
|||
Push←Move⌾(2↑⊢)Move⌾(1↓⊢)
|
||||
|
||||
FindIdx←/○⥊⟜(↕≢)∘⍷
|
||||
|
||||
# 𝕨 Beam 𝕩 | 𝕨: level | 𝕩: position | replace positions with the laser beam
|
||||
Beam←{{"--++"⊏˜" -|+"⊐𝕩}⌾(𝕩⊸⊑)𝕨}
|
||||
|
||||
# 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
|
||||
⊑opaque∊˜⊑w⊑x?(machine=⊑w⊑x)◶⟨x,⟨pmachine⟩˙⌾(w⊸⊑)x⟩@; # laser hits a non-mirror
|
||||
⊑empties∊˜⊑w⊑x ? # Empty space
|
||||
⟨w+d,d⟩S{((×⊑d)⊑⟨9‿9‿11‿11,10‿11‿10‿11⟩)⊏˜0‿9‿10‿11⊐𝕩}⌾(w⊸⊑)x; # Draw laser and recurse
|
||||
#⟨w+d,d⟩S{((×⊑d)⊑"--++"‿"|+|+")⊏˜" -|+"⊐𝕩}⌾(w⊸⊑)x; # Draw laser and recurse
|
||||
⟨w+d,d⟩S{((×⊑d)⊑⟨hbeam‿hbeam‿xbeam‿xbeam,vbeam‿xbeam‿vbeam‿xbeam⟩)⊏˜floor‿hbeam‿vbeam‿xbeam⊐𝕩}⌾(w⊸⊑)x; # Draw laser and recurse
|
||||
# laser hits a mirror
|
||||
d←⌽d×-⊸¬7=⊑w⊑x # calculate the mirror bounce direction
|
||||
d←⌽d×-⊸¬lmirror=⊑w⊑x # calculate the mirror bounce direction
|
||||
⟨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
|
||||
Colorize←{𝕊'⭍':ansi.yellow∾𝕩;⊑𝕩∊"|-+"?ansi.red∾𝕩;⊑𝕩∊"\/"?ansi.cyan∾𝕩;ansi.defaultB∾𝕩}
|
||||
Shoot←{𝕩 {𝕨Bounce´⌽𝕩} ∾⟨<0‿¯1,<0‿1,<¯1‿0,<1‿0⟩(⊣⋈˜¨+)¨ FindIdx⟜(⊑¨𝕩)¨ lasers}
|
||||
Step←{Push⌾((𝕨 Tiles ⊑player FindIdx ⊑¨𝕩)⊸⊑)𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | Step the game
|
||||
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∾<𝕩}
|
||||
|
|
Loading…
Reference in New Issue