Add some error handling

This commit is contained in:
Rampoina 2023-03-05 21:15:08 +01:00
parent 59e2c7718b
commit 3e620d5f0b
2 changed files with 13 additions and 5 deletions

View File

@ -7,15 +7,16 @@
opts{
a•args
•Show a
chars {0=𝕩?" λ$⊕⭍#/\-|+<>^v";𝕩}(»(<"--chars"))/a
fchars{0=𝕩?" λ$⊕⭍#/\-|+<>^v";𝕩}(»(<"--fchars"))/a
levels{0=𝕩?"levels";𝕩}(»(<"--levels"))/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 opts.levelsopts.charsopts.fcharsansi.defaultB,ansi.yellow,ansi.cyan,ansi.red
{𝕤 # Loop until the user wins
•Out clearansi.cursor.origin
clear""

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}