Add some error handling
This commit is contained in:
parent
59e2c7718b
commit
3e620d5f0b
7
arc.bqn
7
arc.bqn
|
@ -7,15 +7,16 @@
|
||||||
|
|
||||||
opts←{
|
opts←{
|
||||||
a←•args
|
a←•args
|
||||||
•Show a
|
|
||||||
chars ⇐{0=≠𝕩?" λ$⊕⭍#/\-|+<>^v";⊑𝕩}(»(<"--chars")⊸⍷)⊸/a
|
chars ⇐{0=≠𝕩?" λ$⊕⭍#/\-|+<>^v";⊑𝕩}(»(<"--chars")⊸⍷)⊸/a
|
||||||
fchars⇐{0=≠𝕩?" λ$⊕⭍#/\-|+<>^v";⊑𝕩}(»(<"--fchars")⊸⍷)⊸/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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g←Game⎊{𝕊: •Out •CurrentError @ ⋄ •Exit 1} opts.n‿opts.levels‿opts.chars‿opts.fchars‿⟨ansi.defaultB,ansi.yellow,ansi.cyan,ansi.red⟩
|
||||||
ansi.InitTerm @
|
ansi.InitTerm @
|
||||||
clear←""
|
clear←""
|
||||||
g←Game opts.levels‿opts.chars‿opts.fchars‿⟨ansi.defaultB,ansi.yellow,ansi.cyan,ansi.red⟩
|
|
||||||
{𝕤 # Loop until the user wins
|
{𝕤 # Loop until the user wins
|
||||||
•Out clear∾ansi.cursor.origin
|
•Out clear∾ansi.cursor.origin
|
||||||
clear↩""
|
clear↩""
|
||||||
|
|
11
logic.bqn
11
logic.bqn
|
@ -5,7 +5,8 @@
|
||||||
⟨FindIdx,SplitOnEmpty⟩←•Import "utils.bqn"
|
⟨FindIdx,SplitOnEmpty⟩←•Import "utils.bqn"
|
||||||
|
|
||||||
Game⇐{ # The Game function creates a game object
|
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
|
# levelPath: the path of the file containing the levels
|
||||||
# dchars: the characters to use for drawing
|
# dchars: the characters to use for drawing
|
||||||
# chars: the characters that are used in the level representation
|
# 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
|
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
|
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∾<𝕩}
|
Next⇐{moves↩moves∾<𝕩}
|
||||||
Undo⇐{𝕊:moves↩(-1<≠)⊸↓moves}
|
Undo⇐{𝕊:moves↩(-1<≠)⊸↓moves}
|
||||||
Draw⇐{𝕊:•Out¨ (currentLevel⊑levels) DrawLevel moves}
|
Draw⇐{𝕊:•Out¨ (currentLevel⊑levels) DrawLevel moves}
|
||||||
|
|
Loading…
Reference in New Issue