Compare commits
5 Commits
7432282107
...
4919a9903a
Author | SHA1 | Date |
---|---|---|
Rampoina | 4919a9903a | |
Rampoina | 3e620d5f0b | |
Rampoina | 59e2c7718b | |
Rampoina | ed2534e6ef | |
Rampoina | 6c4d926d1e |
20
ansi.bqn
20
ansi.bqn
|
@ -1,7 +1,27 @@
|
|||
#!/usr/bin/env BQN
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# SPDX-FileCopyrightText: 2023 Rampoina <rampoina@protonmail.com>
|
||||
|
||||
ansi⇐{
|
||||
e⇐@+27
|
||||
red⇐e∾"[31m"
|
||||
cyan⇐e∾"[36m"
|
||||
yellow⇐e∾"[33m"
|
||||
defaultB⇐e∾"[0m"
|
||||
cursor⇐{
|
||||
hide⇐e∾"[?25l"
|
||||
show⇐e∾"[?25h"
|
||||
origin⇐e∾"[H"
|
||||
}
|
||||
clear⇐{
|
||||
screen⇐e∾"[2J"
|
||||
}
|
||||
reset⇐e∾"[0m"
|
||||
|
||||
InitTerm⇐{𝕤
|
||||
•term.RawMode 1
|
||||
•Out ansi.cursor.hide∾ansi.clear.screen∾ansi.cursor.origin
|
||||
}
|
||||
|
||||
RestoreTerm⇐{𝕤⋄•Out ansi.reset∾ansi.cursor.show}
|
||||
}
|
||||
|
|
40
arc.bqn
40
arc.bqn
|
@ -5,14 +5,29 @@
|
|||
⟨ansi⟩←•Import "ansi.bqn"
|
||||
⟨Game⟩←•Import "logic.bqn"
|
||||
|
||||
e←ansi.e
|
||||
•term.RawMode 1 # set terminal to raw mode
|
||||
•Out e∾"[?25l"∾e∾"[2J"∾e∾"[H" # Cursor to origin, hide it and clear screen
|
||||
opts←{
|
||||
a←•args
|
||||
{𝕤
|
||||
•Out "Usage: "∾•name∾" [OPTION]...
|
||||
--help: Display this help message
|
||||
--levels,-l FILE: load levels from FILE
|
||||
--chars STRING: Characters to display for each game object, needs to have length 15
|
||||
--fchars STRING: Characters to represent each game object in the levels file, needs to have length 15"
|
||||
•Exit 0
|
||||
}⍟((⊑"-h"<⊸⍷a)∨⊑"--help"<⊸⍷a) @
|
||||
|
||||
chars⇐{0=≠𝕩?" λ$⊕⭍#/\-|+<>^v";⊑𝕩}(»(<"--chars")⊸⍷)⊸/a
|
||||
fchars⇐{0=≠𝕩?" λ$⊕⭍#/\-|+<>^v";⊑𝕩}(»(<"--fchars")⊸⍷)⊸/a
|
||||
l←{0=≠𝕩?"levels";⊑𝕩}(»(<"--levels")⊸⍷)⊸/a
|
||||
levels⇐{0=≠𝕩?l;⊑𝕩}(»(<"-l")⊸⍷)⊸/a
|
||||
n⇐{0=≠𝕩?1;1⌈⊑𝕩}•BQN¨⎊{𝕊: •Out "Invalid starting level number" ⋄ •Exit 1}(»(<"-s")⊸⍷)⊸/a
|
||||
}
|
||||
|
||||
g←Game⎊{𝕊: •Out •CurrentError @ ⋄ •Exit 1} opts.n‿opts.levels‿opts.chars‿opts.fchars‿⟨ansi.defaultB,ansi.yellow,ansi.cyan,ansi.red⟩
|
||||
ansi.InitTerm @
|
||||
clear←""
|
||||
|
||||
g←Game "levels"‿" λ$⊕⭍#/\-|+<>^v"‿" λ$⊕⭍#/\-|+<>^v"‿⟨ansi.defaultB,ansi.yellow,ansi.cyan,ansi.red⟩
|
||||
{𝕤 # Loop until the user wins
|
||||
•Out clear∾e∾"[H" # Cursor to origin
|
||||
•Out clear∾ansi.cursor.origin
|
||||
clear↩""
|
||||
•Out "Level: "∾•Repr 1+g.currentLevel
|
||||
•Out ansi.yellow∾"⭍"∾ansi.defaultB∾" Power the machines (⊕) by moving the mirrors ("∾ansi.cyan∾"\/"∾ansi.defaultB∾") "
|
||||
|
@ -22,16 +37,15 @@ g←Game "levels"‿" λ$⊕⭍#/\-|+<>^v"‿" λ$⊕⭍#/\-|+<>^v"‿⟨ansi.de
|
|||
g.WinLevel @ ?
|
||||
•Out "Good job!, press any key to continue to the next level"
|
||||
key←•term.CharB @
|
||||
clear↩e∾"[2J"
|
||||
clear↩ansi.clear.screen
|
||||
g.NextLevel @
|
||||
{𝕤⋄•Out e∾"[?12l"∾e∾"[?25h"⋄•Exit 0}⍟(key='q')@
|
||||
{𝕤⋄ansi.RestoreTerm @⋄•Exit 0}⍟(key='q')@
|
||||
;
|
||||
key←•term.CharB @
|
||||
{𝕤⋄g.Next ⊑("hjklaswd"=key)/∾˜⟨0‿¯1,1‿0,¯1‿0,0‿1⟩}⍟(⊑key∊"hjklaswd")@
|
||||
{𝕤⋄g.Undo @}⍟(key='u')@
|
||||
{𝕤⋄•Out e∾"[?12l"∾e∾"[?25h"⋄•Exit 0}⍟(key='q')@
|
||||
{𝕤⋄clear↩e∾"[2J"⋄g.Reset @}⍟(key='r')@
|
||||
{𝕤⋄clear↩e∾"[2J"}⍟(((1⊸+>○(⌊1+10⋆⁼1⌈⊢)⊢)¯1+≠g.moves)∧key='u')@
|
||||
{𝕤⋄clear↩""‿ansi.clear.screen⊑˜((1⊸+>○(≠•Repr)⊢)¯1+≠g.moves)⋄g.Undo @}⍟(key='u')@
|
||||
{𝕤⋄ansi.RestoreTerm @⋄•Exit 0}⍟(key='q')@
|
||||
{𝕤⋄clear↩ansi.clear.screen⋄g.Reset @}⍟(key='r')@
|
||||
}•_While_{𝕤⋄g.Over @}@
|
||||
•Out "Well played, you win!"
|
||||
•Out e∾"[?12l"∾e∾"[?25h"
|
||||
ansi.RestoreTerm @
|
||||
|
|
11
logic.bqn
11
logic.bqn
|
@ -5,7 +5,8 @@
|
|||
⟨FindIdx,SplitOnEmpty⟩←•Import "utils.bqn"
|
||||
|
||||
Game⇐{ # The Game function creates a game object
|
||||
𝕊 levelPath‿dchars‿chars‿colors: # from parameters:
|
||||
𝕊 n‿levelPath‿dchars‿chars‿colors: # from parameters:
|
||||
# n: starting level
|
||||
# levelPath: the path of the file containing the levels
|
||||
# dchars: the characters to use for drawing
|
||||
# chars: the characters that are used in the level representation
|
||||
|
@ -125,8 +126,14 @@ Game⇐{ # The Game function creates a game object
|
|||
# -------------------------------------------------------------------------------
|
||||
|
||||
moves⇐⟨0‿0⟩ # list of moves, each move is a direction, we start without moving
|
||||
currentLevel⇐0
|
||||
currentLevel⇐n-1
|
||||
"Invalid number of fchars" ! 15=≠chars
|
||||
"Invalid number of chars" ! 15=≠dchars
|
||||
"The level file contains illegal characters" ! ∧´chars∊˜∾´•Flines levelPath
|
||||
levels←Ascii2Matrix¨>¨SplitOnEmpty•FLines levelPath # Load file containing levels
|
||||
"Some levels don't contain any player" ! ¬∨´0=≠¨{player FindIdx ⊑¨𝕩}¨levels
|
||||
"Some levels don't contain any machine" ! ¬∨´0=≠¨{machine FindIdx ⊑¨𝕩}¨levels
|
||||
"The starting level is higher than the number of levels" ! currentLevel<≠levels
|
||||
Next⇐{moves↩moves∾<𝕩}
|
||||
Undo⇐{𝕊:moves↩(-1<≠)⊸↓moves}
|
||||
Draw⇐{𝕊:•Out¨ (currentLevel⊑levels) DrawLevel moves}
|
||||
|
|
Loading…
Reference in New Issue