From 9bf31e86cef62bed76a35353e791768960b14d70 Mon Sep 17 00:00:00 2001 From: dozens Date: Sun, 23 Jun 2024 15:41:31 -0600 Subject: 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. --- main.fnl | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'main.fnl') 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))) -- cgit 1.4.1-2-gfad0