Get rid of the magic numbers

This commit is contained in:
Rampoina 2023-03-05 03:05:16 +01:00
parent 3eed7bf204
commit 9245c619a9
1 changed files with 21 additions and 16 deletions

37
arc.bqn
View File

@ -7,10 +7,14 @@
# 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
movables1278 # player, box and mirrors
opaque123121314156 # non laser reflecting
empties091011 # floor and laser beams
chars" λ$⊕⭍#/\-|+<>^v" # legal characters
floor,player,box,machine,pmachine,wall,lmirror,rmirror,hbeam,vbeam,xbeam,llaser,rlaser,ulaser,dlaserchars
beamshbeamvbeamxbeam
mirrorslmirrorrmirror
lasersllaserrlaserulaserdlaser
movablesplayerboxmirrors # player, box and mirrors
opaqueplayerboxmachinewalllasers # non laser reflecting
emptiesfloorbeams # floor and laser beams
ansi{
e@+27
rede"[31m"
@ -19,8 +23,15 @@ ansi←{
defaultBe"[0m"
}
colors(chars)<ansi.defaultB # start with all glyphs being the default color
colors ansi.yellow˙(pmachine)
colors ansi.cyan¨(lmirrorrmirror)
colors ansi.red¨(hbeamvbeamxbeam)
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,10,20,3,13,23,6,70,80,9,10,11,120,130,140,150}¨(˝·¬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)≡⌜<
PushMove(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{(wd)S x:{
opaque˜wx?(3=wx)x,5˙(w)x@; # laser hits a non-mirror
opaque˜wx?(machine=wx)x,pmachine˙(w)x@; # laser hits a non-mirror
empties˜wx ? # Empty space
w+d,dS{((×d)991111,10111011)˜091011𝕩}(w)x; # Draw laser and recurse
#⟨w+d,d⟩S{((×⊑d)⊑"--++"‿"|+|+")⊏˜" -|+"⊐𝕩}⌾(w⊸⊑)x; # Draw laser and recurse
w+d,dS{((×d)hbeamhbeamxbeamxbeam,vbeamxbeamvbeamxbeam)˜floorhbeamvbeamxbeam𝕩}(w)x; # Draw laser and recurse
# laser hits a mirror
dd×-¬7=wx # calculate the mirror bounce direction
dd×-¬lmirror=wx # calculate the mirror bounce direction
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
Colorize{𝕊'⭍':ansi.yellow𝕩;𝕩"|-+"?ansi.red𝕩;𝕩"\/"?ansi.cyan𝕩;ansi.defaultB𝕩}
Shoot{𝕩 {𝕨Bounce´𝕩} <0¯1,<01,<¯10,<10(˜¨+)¨ 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{movesmoves<𝕩}