forked from Rampoina/Sokobqn
Add support for multiple levels
This commit is contained in:
parent
c6f3c31871
commit
93b60a4e47
|
@ -0,0 +1,15 @@
|
||||||
|
#######
|
||||||
|
#.@ # #
|
||||||
|
#$* $ #
|
||||||
|
# $ #
|
||||||
|
# .. #
|
||||||
|
# * #
|
||||||
|
#######
|
||||||
|
|
||||||
|
#######
|
||||||
|
#.@ # #
|
||||||
|
#$* $ #
|
||||||
|
# $ #
|
||||||
|
# .. #
|
||||||
|
# * #
|
||||||
|
#######
|
59
soko.bqn
59
soko.bqn
|
@ -14,64 +14,63 @@
|
||||||
# ⟨ 4 ⟩ ⟨ 1 0 ⟩ ⟨ 2 0 ⟩ ⟨ 0 ⟩ ⟨ 4 ⟩ #@*.#
|
# ⟨ 4 ⟩ ⟨ 1 0 ⟩ ⟨ 2 0 ⟩ ⟨ 0 ⟩ ⟨ 4 ⟩ #@*.#
|
||||||
# ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ #####"
|
# ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ ⟨ 4 ⟩ #####"
|
||||||
# ┘ ┘
|
# ┘ ┘
|
||||||
moves←⟨0‿0⟩
|
moves←⟨0‿0⟩ # list of moves, each move is a direction, we start without moving
|
||||||
chars←" @$.+*#"
|
chars←" @$.+*#" # legal characters
|
||||||
level ← >⟨
|
|
||||||
"#######"
|
|
||||||
"#.@ # #"
|
|
||||||
"#$* $ #"
|
|
||||||
"# $ #"
|
|
||||||
"# .. #"
|
|
||||||
"# * #"
|
|
||||||
"#######"
|
|
||||||
⟩
|
|
||||||
|
|
||||||
ll←{(⊑chars⊐𝕩)⊑⟨≍0,1‿0,2‿0,≍3,1‿3,2‿3,≍6⟩}¨(⊢↑˝·≍⟜¬2+≢)level
|
SplitOnEmpty←{𝕩⊔˜(⊢-˜+`׬)0=≠¨𝕩}
|
||||||
#ll←≍¨(⊢↑˝·≍⟜¬2+≢)chars⊐level # ascii to numbers, 0pad and listify
|
Ascii2Matrix←{(⊑chars⊐𝕩)⊑⟨≍0,1‿0,2‿0,≍3,1‿3,2‿3,≍6⟩}¨(⊢↑˝·≍⟜¬2+≢)
|
||||||
#_change←{ll↩𝔽¨⌾((ll∊≍¨𝕩)/○⥊⊢)ll} # Add 0s to the boxes and player: ⟨2⟩ -> ⟨2,0⟩
|
|
||||||
#∾⟜0 _change 1‿2 ⋄ (-≍○⊑⊢)⟜3 _change 4‿5 # change 5('*') to ⟨2,3⟩, 5('+') to ⟨1,3⟩
|
|
||||||
#Rle←(∾´∾¨)∘((+⟜(×⟜10)´ -⟜'0'∘⌽)⊸⥊⟜<¨)˜´(≠⊸⥊⟜1‿0)⊸⊔∘(1⊸↓)∘(((+`»⊸≠) '0'⊸< ∧ '9'⊸>)⊸⊔)`)))
|
|
||||||
|
|
||||||
# 𝕨 C 𝕩 | 𝕩: object coordinate (3‿1) | 𝕨: direction vector (¯1‿0)
|
|
||||||
|
# 𝕨 Tiles 𝕩 | 𝕩: object coordinate (3‿1) | 𝕨: direction vector (¯1‿0)
|
||||||
# result: ⟨ ⟨ 3 1 ⟩ ⟨ 2 1 ⟩ ⟨ 1 1 ⟩ ⟩
|
# result: ⟨ ⟨ 3 1 ⟩ ⟨ 2 1 ⟩ ⟨ 1 1 ⟩ ⟩
|
||||||
# returns 3 tiles in the specified direction from the
|
# returns 3 tiles in the specified direction from the
|
||||||
Tiles←{⟨𝕩,𝕩+𝕨,𝕩+2×𝕨⟩} # given object (including itself)
|
Tiles←{⟨𝕩,𝕩+𝕨,𝕩+2×𝕨⟩} # given object (including itself)
|
||||||
|
|
||||||
Player←{⊑/○⥊⟜(↕≢)1⍷⊑¨𝕩} # P 𝕩 | 𝕩:level | returns the coordinate of the [P]layer
|
Player←{⊑/○⥊⟜(↕≢)1⍷⊑¨𝕩} # Player 𝕩 | 𝕩:level | returns the coordinate of the [P]layer
|
||||||
|
|
||||||
# M 𝕩 | 𝕩: ⟨⟨1,0⟩,⟨0⟩⟩ (2 tiles) | result: ⟨⟨0⟩,⟨1,0⟩⟩
|
# Move 𝕩 | 𝕩: ⟨⟨1,0⟩,⟨0⟩⟩ (2 tiles) | result: ⟨⟨0⟩,⟨1,0⟩⟩
|
||||||
# [M]ove the first object in the first tile to the second tile.
|
# Move the first object in the first tile to the second tile.
|
||||||
# Only move Player/Box -> Floor/Goal
|
# Only move Player/Box -> Floor/Goal
|
||||||
# the second tile can't be a box because we moved it previously
|
# the second tile can't be a box because we moved it previously
|
||||||
# if it is it means that the box was unmovable (next to a wall) so we do nothing
|
# if it is it means that the box was unmovable (next to a wall) so we do nothing
|
||||||
Move←{a‿b:⟨1↓a,(⊑a)∾b⟩}⍟{∨´(⥊1‿2≍⌜0‿3)≡⌜<⊑¨𝕩}
|
Move←{a‿b:⟨1↓a,(⊑a)∾b⟩}⍟{∨´(⥊1‿2≍⌜0‿3)≡⌜<⊑¨𝕩}
|
||||||
|
|
||||||
# P 𝕩 | 𝕩: ⟨⟨1,0⟩,⟨2,0⟩,⟨0,0)⟩ (3 tiles) | result: ⟨⟨0⟩,⟨1,0⟩,⟨2,0)⟩
|
# Push 𝕩 | 𝕩: ⟨⟨1,0⟩,⟨2,0⟩,⟨0,0)⟩ (3 tiles) | result: ⟨⟨0⟩,⟨1,0⟩,⟨2,0)⟩
|
||||||
# Given 3 tiles try to [P]ush the second tile (possible box)
|
# Given 3 tiles try to [P]ush the second tile (possible box)
|
||||||
# and afterwards try to move the first one (player) if possible
|
# and afterwards try to move the first one (player) if possible
|
||||||
Push←Move⌾(2↑⊢)Move⌾(1↓⊢)
|
Push←Move⌾(2↑⊢)Move⌾(1↓⊢)
|
||||||
|
|
||||||
S←{Push⌾((𝕨 Tiles Player 𝕩 )⊸⊑)𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | step the game
|
S←{Push⌾((𝕨 Tiles Player 𝕩 )⊸⊑)𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | step the game
|
||||||
Draw←{chars⊏˜+´¨ll S´ ⌽𝕩} # Draw 𝕩 | 𝕩: level | Draw the game in ASCII
|
Draw←{chars⊏˜+´¨𝕨 S´ ⌽𝕩} # Draw 𝕩 | 𝕩: moves | Draw the game in ASCII
|
||||||
W←{(2≡¨⊑¨𝕩) =○(+´⥊) (<2‿3)≡¨𝕩} # W 𝕩 | 𝕩: level | [W]in condition
|
W←{(2≡¨⊑¨𝕩) =○(+´⥊) (<2‿3)≡¨𝕩} # W 𝕩 | 𝕩: level | [W]in condition
|
||||||
N←{moves↩moves∾<𝕩}
|
N←{moves↩moves∾<𝕩}
|
||||||
Undo←{𝕊:moves↩(-1<≠)⊸↓moves}
|
Undo←{𝕊:moves↩(-1<≠)⊸↓moves}
|
||||||
While ← {𝕨{𝕊∘𝔾⍟𝔽𝕩}𝕩@}´
|
While ← {𝕨{𝕊∘𝔾⍟𝔽𝕩}𝕩@}´
|
||||||
|
|
||||||
•term.RawMode 1
|
# Main loop
|
||||||
•Out "[?25l[2J[H"
|
# Load file containing levels
|
||||||
•Out "Press key" ⋄ •Out˘ Draw moves
|
levels←Ascii2Matrix¨>¨SplitOnEmpty•FLines "levels"
|
||||||
|
currentLevel←0
|
||||||
|
•term.RawMode 1 # set terminal to raw mode
|
||||||
|
•Out "[?25l[2J[H" # Cursor to origin, hide it and clear screen
|
||||||
clear←""
|
clear←""
|
||||||
While {𝕤⋄¬W ll S´⌽moves}‿{𝕤
|
While {𝕤⋄currentLevel<≠levels}‿{𝕤 # Loop until the user wins
|
||||||
•Out "[H"
|
•Out "[H"
|
||||||
|
•Out "Level: "∾•Repr 1+currentLevel
|
||||||
|
•Out "7"
|
||||||
|
•Out "Press key"
|
||||||
|
•Out˘ (currentLevel⊑levels) Draw moves
|
||||||
key←•term.CharB @
|
key←•term.CharB @
|
||||||
{𝕤⋄N ⊑("hjkl"=key)/⟨0‿¯1,1‿0,¯1‿0,0‿1⟩}⍟(⊑key∊"hjkl")@
|
{𝕤⋄N ⊑("hjkl"=key)/⟨0‿¯1,1‿0,¯1‿0,0‿1⟩}⍟(⊑key∊"hjkl")@
|
||||||
{𝕤⋄Undo @}⍟(key='u')@
|
{𝕤⋄Undo @}⍟(key='u')@
|
||||||
|
{𝕤⋄•Out "[?12l[?25h"⋄•Exit 0}⍟(key='q')@
|
||||||
{𝕤⋄clear↩"[2J"}⍟(((1⊸+>○(⌊1+10⋆⁼1⌈⊢)⊢)¯1+≠moves)∧key='u')@
|
{𝕤⋄clear↩"[2J"}⍟(((1⊸+>○(⌊1+10⋆⁼1⌈⊢)⊢)¯1+≠moves)∧key='u')@
|
||||||
|
•Out "8"
|
||||||
{𝕤⋄•Out clear∾"Press key"}⍟(⊑key∊"hjklu")@
|
{𝕤⋄•Out clear∾"Press key"}⍟(⊑key∊"hjklu")@
|
||||||
{𝕤⋄•Out "Wrong key"}⍟(¬⊑key∊"hjklu")@
|
{𝕤⋄•Out "Wrong key"}⍟(¬⊑key∊"hjkluq")@
|
||||||
•Out˘ Draw moves
|
•Out˘ (currentLevel⊑levels) Draw moves
|
||||||
•Out "Moves: "∾{'0'+⌽10|⌊𝕩÷10⋆↕(⌊1+10⋆⁼1⌈⊢)𝕩}¯1+≠moves
|
•Out "Moves: "∾•Repr ¯1+≠moves
|
||||||
|
{𝕤⋄•Out "[H"⋄currentLevel↩currentLevel+1⋄moves↩⟨0‿0⟩}⍟(W (currentLevel⊑levels) S´⌽moves)@
|
||||||
}
|
}
|
||||||
•Out "Well played!"
|
•Out "Well played, you win!"
|
||||||
•Out "[?12l[?25h"
|
•Out "[?12l[?25h"
|
||||||
|
|
Loading…
Reference in New Issue