Compare commits

...

5 Commits

Author SHA1 Message Date
Rampoina 4919a9903a Fix -s and add --help and -h 2023-03-05 21:43:12 +01:00
Rampoina 3e620d5f0b Add some error handling 2023-03-05 21:15:08 +01:00
Rampoina 59e2c7718b Add cli options 2023-03-05 20:03:03 +01:00
Rampoina ed2534e6ef Add header to ansi.bqn 2023-03-05 19:12:43 +01:00
Rampoina 6c4d926d1e Add missing ansi codes 2023-03-05 19:11:54 +01:00
3 changed files with 56 additions and 15 deletions

View File

@ -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
rede"[31m"
cyane"[36m"
yellowe"[33m"
defaultBe"[0m"
cursor{
hidee"[?25l"
showe"[?25h"
origine"[H"
}
clear{
screene"[2J"
}
resete"[0m"
InitTerm{𝕤
•term.RawMode 1
•Out ansi.cursor.hideansi.clear.screenansi.cursor.origin
}
RestoreTerm{𝕤•Out ansi.resetansi.cursor.show}
}

40
arc.bqn
View File

@ -5,14 +5,29 @@
ansi•Import "ansi.bqn"
Game•Import "logic.bqn"
eansi.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
}
gGame{𝕊: •Out •CurrentError @ •Exit 1} opts.nopts.levelsopts.charsopts.fcharsansi.defaultB,ansi.yellow,ansi.cyan,ansi.red
ansi.InitTerm @
clear""
gGame "levels"" λ$⊕⭍#/\-|+<>^v"" λ$⊕⭍#/\-|+<>^v"ansi.defaultB,ansi.yellow,ansi.cyan,ansi.red
{𝕤 # Loop until the user wins
•Out cleare"[H" # Cursor to origin
•Out clearansi.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 @
cleare"[2J"
clearansi.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,10,¯10,01}(key"hjklaswd")@
{𝕤g.Undo @}(key='u')@
{𝕤•Out e"[?12l"e"[?25h"•Exit 0}(key='q')@
{𝕤cleare"[2J"g.Reset @}(key='r')@
{𝕤cleare"[2J"}(((1+>(1+101))¯1+g.moves)key='u')@
{𝕤clear""ansi.clear.screen˜((1+>(•Repr))¯1+g.moves)g.Undo @}(key='u')@
{𝕤ansi.RestoreTerm @•Exit 0}(key='q')@
{𝕤clearansi.clear.screeng.Reset @}(key='r')@
}•_While_{𝕤g.Over @}@
•Out "Well played, you win!"
•Out e"[?12l"e"[?25h"
ansi.RestoreTerm @

View File

@ -5,7 +5,8 @@
FindIdx,SplitOnEmpty•Import "utils.bqn"
Game{ # The Game function creates a game object
𝕊 levelPathdcharscharscolors: # from parameters:
𝕊 nlevelPathdcharscharscolors: # 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
# -------------------------------------------------------------------------------
moves00 # list of moves, each move is a direction, we start without moving
currentLevel0
currentLeveln-1
"Invalid number of fchars" ! 15=chars
"Invalid number of chars" ! 15=dchars
"The level file contains illegal characters" ! ´chars˜´•Flines levelPath
levelsAscii2Matrix¨>¨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{movesmoves<𝕩}
Undo{𝕊:moves(-1<)moves}
Draw{𝕊:•Out¨ (currentLevellevels) DrawLevel moves}