Add restart key and improve comments
This commit is contained in:
parent
93b60a4e47
commit
3298ebccc4
|
@ -8,6 +8,7 @@ Vim style movement keys
|
|||
- k: up
|
||||
- l: right
|
||||
- u: undo
|
||||
- r: restart
|
||||
|
||||
## Running
|
||||
|
||||
|
|
22
soko.bqn
22
soko.bqn
|
@ -17,10 +17,10 @@
|
|||
moves←⟨0‿0⟩ # list of moves, each move is a direction, we start without moving
|
||||
chars←" @$.+*#" # legal characters
|
||||
|
||||
|
||||
SplitOnEmpty←{𝕩⊔˜(⊢-˜+`׬)0=≠¨𝕩}
|
||||
Ascii2Matrix←{(⊑chars⊐𝕩)⊑⟨≍0,1‿0,2‿0,≍3,1‿3,2‿3,≍6⟩}¨(⊢↑˝·≍⟜¬2+≢)
|
||||
|
||||
|
||||
# 𝕨 Tiles 𝕩 | 𝕩: object coordinate (3‿1) | 𝕨: direction vector (¯1‿0)
|
||||
# result: ⟨ ⟨ 3 1 ⟩ ⟨ 2 1 ⟩ ⟨ 1 1 ⟩ ⟩
|
||||
# returns 3 tiles in the specified direction from the
|
||||
|
@ -40,34 +40,34 @@ Move←{a‿b:⟨1↓a,(⊑a)∾b⟩}⍟{∨´(⥊1‿2≍⌜0‿3)≡⌜<⊑¨
|
|||
# and afterwards try to move the first one (player) if possible
|
||||
Push←Move⌾(2↑⊢)Move⌾(1↓⊢)
|
||||
|
||||
S←{Push⌾((𝕨 Tiles Player 𝕩 )⊸⊑)𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | step the game
|
||||
Draw←{chars⊏˜+´¨𝕨 S´ ⌽𝕩} # Draw 𝕩 | 𝕩: moves | Draw the game in ASCII
|
||||
S←{Push⌾((𝕨 Tiles Player 𝕩 )⊸⊑)𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | Step the game
|
||||
Draw←{chars⊏˜+´¨𝕨 S´ ⌽𝕩} # 𝕨 Draw 𝕩 | 𝕨: levels | 𝕩: moves | Draw the game in ASCII
|
||||
W←{(2≡¨⊑¨𝕩) =○(+´⥊) (<2‿3)≡¨𝕩} # W 𝕩 | 𝕩: level | [W]in condition
|
||||
N←{moves↩moves∾<𝕩}
|
||||
Undo←{𝕊:moves↩(-1<≠)⊸↓moves}
|
||||
While ← {𝕨{𝕊∘𝔾⍟𝔽𝕩}𝕩@}´
|
||||
|
||||
# Main loop
|
||||
# Load file containing levels
|
||||
levels←Ascii2Matrix¨>¨SplitOnEmpty•FLines "levels"
|
||||
levels←Ascii2Matrix¨>¨SplitOnEmpty•FLines "levels" # Load file containing levels
|
||||
currentLevel←0
|
||||
•term.RawMode 1 # set terminal to raw mode
|
||||
•Out "[?25l[2J[H" # Cursor to origin, hide it and clear screen
|
||||
clear←""
|
||||
While {𝕤⋄currentLevel<≠levels}‿{𝕤 # Loop until the user wins
|
||||
•Out "[H"
|
||||
•Out "[H" # Cursor to origin
|
||||
•Out "Level: "∾•Repr 1+currentLevel
|
||||
•Out "7"
|
||||
•Out "Press key"
|
||||
•Out "7" # Save cursor position
|
||||
•Out "Controls: (hjkl) to move, u to undo, r to reset level"
|
||||
•Out˘ (currentLevel⊑levels) Draw moves
|
||||
key←•term.CharB @
|
||||
{𝕤⋄N ⊑("hjkl"=key)/⟨0‿¯1,1‿0,¯1‿0,0‿1⟩}⍟(⊑key∊"hjkl")@
|
||||
{𝕤⋄Undo @}⍟(key='u')@
|
||||
{𝕤⋄•Out "[?12l[?25h"⋄•Exit 0}⍟(key='q')@
|
||||
{𝕤⋄moves↩⟨0‿0⟩}⍟(key='r')@
|
||||
{𝕤⋄clear↩"[2J"}⍟(((1⊸+>○(⌊1+10⋆⁼1⌈⊢)⊢)¯1+≠moves)∧key='u')@
|
||||
•Out "8"
|
||||
{𝕤⋄•Out clear∾"Press key"}⍟(⊑key∊"hjklu")@
|
||||
{𝕤⋄•Out "Wrong key"}⍟(¬⊑key∊"hjkluq")@
|
||||
•Out "8" # Restore cursor position
|
||||
{𝕤⋄•Out clear∾"Controls: (hjkl) to move, u to undo, r to reset level"}⍟(⊑key∊"hjkluqr")@
|
||||
{𝕤⋄•Out "Invalid key: (hjkl) to move, u to undo, r to reset level"}⍟(¬⊑key∊"hjkluqr")@
|
||||
•Out˘ (currentLevel⊑levels) Draw moves
|
||||
•Out "Moves: "∾•Repr ¯1+≠moves
|
||||
{𝕤⋄•Out "[H"⋄currentLevel↩currentLevel+1⋄moves↩⟨0‿0⟩}⍟(W (currentLevel⊑levels) S´⌽moves)@
|
||||
|
|
Loading…
Reference in New Issue