diff options
author | dozens | 2024-06-23 15:41:31 -0600 |
---|---|---|
committer | dozens | 2024-06-23 15:41:31 -0600 |
commit | 9bf31e86cef62bed76a35353e791768960b14d70 (patch) | |
tree | ef04493ee8d754fde3d4fc18bb6d704924227bae /main.fnl | |
parent | c7b2c982004e350f5e3032321baadfc9021b6bad (diff) |
Move error handling to front-end
game.validate-move will now return an error code is the move is invalid. default error messages are found in lib/constants.fnl. error handling is now handled on the front end.
Diffstat (limited to 'main.fnl')
-rw-r--r-- | main.fnl | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/main.fnl b/main.fnl index c205454..8144b11 100644 --- a/main.fnl +++ b/main.fnl @@ -34,17 +34,23 @@ ;; game loop (while (not (= game.stage const.stages.complete)) (with-board game.moves) - ;; validation loop (var is-valid false) (var move "") + ;; validation loop (while (not is-valid) (set move (get-move)) - (set is-valid (game.validate-move move)) - (if (not is-valid) - (print "Try again.") - (do - (print (string.format "Turn %d: You chose %s" game.turns move)) - (game:update move))))) + (case (pcall game.validate-move move) + (false msg) + (do + (let [(i j) (string.find msg ": ") + key (string.sub msg (+ 1 j))] + (print (. const.errors key))) + (print "Try again.")) + ok + (do + (set is-valid true) + (print (string.format "Turn %d: You chose %s" game.turns move)) + (game:update move))))) ;; game is complete (print "Congratulations!") (print (string.format "Player %d is the winner!" game.player))) |