Sokobqn/soko.bqn

78 lines
3.4 KiB
BQN
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env BQN
# SPDX-License-Identifier: AGPL-3.0-or-later
# SPDX-FileCopyrightText: 2022 Rampoina <rampoina@protonmail.com>
#
# Sokobqn
# 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 ⟩ #####"
# ┘ ┘
moves00
chars" @$.+*#"
level >
"#######"
"#.@ # #"
"#$* $ #"
"# $ #"
"# .. #"
"# * #"
"#######"
ll{(chars𝕩)0,10,20,3,13,23,6}¨(˝·¬2+)level
#ll←≍¨(⊢↑˝·≍⟜¬2+≢)chars⊐level # ascii to numbers, 0pad and listify
#_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)
# result: ⟨ ⟨ 3 1 ⟩ ⟨ 2 1 ⟩ ⟨ 1 1 ⟩ ⟩
# returns 3 tiles in the specified direction from the
Tiles{𝕩,𝕩+𝕨,𝕩+2×𝕨} # given object (including itself)
Player{/()1¨𝕩} # P 𝕩 | 𝕩:level | returns the coordinate of the [P]layer
# M 𝕩 | 𝕩: ⟨⟨1,0⟩,⟨0⟩⟩ (2 tiles) | result: ⟨⟨0⟩,⟨1,0⟩⟩
# [M]ove the first object in the first tile to the second tile.
# Only move Player/Box -> Floor/Goal
# 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
Move{ab:1a,(a)b}{´(1203)<¨𝕩}
# P 𝕩 | 𝕩: ⟨⟨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)
# and afterwards try to move the first one (player) if possible
PushMove(2)Move(1)
S{Push((𝕨 Tiles Player 𝕩 ))𝕩} # 𝕨 S 𝕩 | 𝕨: direction | 𝕩:level | step the game
Draw{chars˜+´¨ll S´ 𝕩} # Draw 𝕩 | 𝕩: level | Draw the game in ASCII
W{(2¨¨𝕩) =(+´) (<23)¨𝕩} # W 𝕩 | 𝕩: level | [W]in condition
N{movesmoves<𝕩}
Undo{𝕊:moves(-1<)moves}
While {𝕨{𝕊𝔾𝔽𝕩}𝕩@}´
•term.RawMode 1
•Out "[?25l"
•Out "Press key" •Out˘ Draw moves
clear""
While {𝕤¬W ll S´moves}{𝕤
•Out ""
key•term.CharB @
{𝕤N ("hjkl"=key)/0¯1,10,¯10,01}(key"hjkl")@
{𝕤Undo @}(key='u')@
{𝕤clear""}(((1+>(1+101))¯1+moves)key='u')@
{𝕤•Out clear"Press key"}(key"hjklu")@
{𝕤•Out "Wrong key"}(¬key"hjklu")@
•Out˘ Draw moves
•Out "Moves: "{'0'+10|𝕩÷10(1+101)𝕩}¯1+moves
}
•Out "Well played!"
•Out "[?12l[?25h"