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.
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);
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
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);
else
this.state.next_player = "X";
- return true;
+ return { legal: true };
}
}