From 00507589b4984d50a34a383975fc6323db19bcfa Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 3 Jun 2020 15:20:12 -0700 Subject: [PATCH] Add message string to the return value of add_move Now, instead of just returning a Boolean indicating whether the move was legal, add_move returns an object with both the Boolean "legal" property as well as a string "message" indicating what made the move illegal. --- lmno.js | 8 ++++---- test | 4 ++-- tictactoe.js | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lmno.js b/lmno.js index 60c0f44..b6c5b78 100644 --- a/lmno.js +++ b/lmno.js @@ -302,13 +302,13 @@ for (let key in engines) { const game = request.game; const move = request.body.move; - const legal = game.add_move(move); + const result = game.add_move(move); - /* Inform this client whether the move was legal. */ - response.send(JSON.stringify(legal)); + /* Feed move response back to the client. */ + response.json(result); /* And only if legal, inform all clients. */ - if (! legal) + if (! result.legal) return; game.broadcast_move(move); diff --git a/test b/test index 0e42462..55a4c29 100755 --- a/test +++ b/test @@ -299,12 +299,12 @@ TEST_SUBSECTION "Tic Tac Toe /move" TEST "Move to the center square" result=$(tictactoe_move 4) -test "$result" = "true" +test "$result" = '{"legal":true}' TEST_END TEST "Move to center square again is now illegal" result=$(tictactoe_move 4) -test "$result" = "false" +test "$result" = '{"legal":false,"message":"Square is already occupied"}' TEST_END TEST_REPORT diff --git a/tictactoe.js b/tictactoe.js index d5a07b2..da55c9f 100644 --- a/tictactoe.js +++ b/tictactoe.js @@ -15,7 +15,7 @@ class TicTacToe extends Game { add_move(square) { /* Cannot move to an occupied square. */ if (this.state.board[square]) - return false; + return { legal: false, message: "Square is already occupied" }; this.state.board[square] = this.state.next_player; this.state.moves.push(square); @@ -25,7 +25,7 @@ class TicTacToe extends Game { else this.state.next_player = "X"; - return true; + return { legal: true }; } } -- 2.45.2