From 0c7db307d60a604387662404434d18bae1467d38 Mon Sep 17 00:00:00 2001 From: Iku-turso Date: Tue, 14 Mar 2023 12:43:59 +0200 Subject: [PATCH] Refactor for reduced nesting Co-authored-by: Janne Savolainen Signed-off-by: Iku-turso --- .../kata-for-gabriel/src/monster-beatdown.ts | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/packages/kata-for-gabriel/src/monster-beatdown.ts b/packages/kata-for-gabriel/src/monster-beatdown.ts index ebd81a273c..98619b81d7 100644 --- a/packages/kata-for-gabriel/src/monster-beatdown.ts +++ b/packages/kata-for-gabriel/src/monster-beatdown.ts @@ -11,54 +11,56 @@ const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies messageToPlayer(`You encounter a monster with ${monsterHitPoints} hit-points`); const playerWantsToAttack = await questionToPlayer("Attack the monster?"); - if (playerWantsToAttack) { - while (true) { - messageToPlayer("You attack the monster."); + if (!playerWantsToAttack) { + messageToPlayer( + "You chose not to attack the monster, and the monster eats you dead, in disappointment.", + ); - const dieResult = await castDie(); + messageToPlayer("You lose. Game over!"); - const playerLandsHitOnMonster = dieResult > 3; - if (playerLandsHitOnMonster) { - monsterHitPoints--; + return; + } - if (!monsterHitPoints) { - messageToPlayer( - "You successfully land a final hit on the monster, and it is now properly beat.", - ); + while (true) { + messageToPlayer("You attack the monster."); - messageToPlayer("You win. Congratulations!"); + const dieResult = await castDie(); - return; - } + const playerLandsHitOnMonster = dieResult > 3; + if (playerLandsHitOnMonster) { + monsterHitPoints--; + if (!monsterHitPoints) { messageToPlayer( - `You successfully land a hit on the monster, and it now only has ${monsterHitPoints} hit-points remaining.`, - ); - } else { - messageToPlayer( - `You fail to land a hit on the monster, and it still has ${monsterHitPoints} hit-points remaining.`, - ); - } - - const playerWantsToAttackAgain = await questionToPlayer("Do you want to attack again?"); - - if (!playerWantsToAttackAgain) { - messageToPlayer( - "You lose your nerve mid-beat-down, and try to run away. You get eaten by a sad, disappointed monster.", + "You successfully land a final hit on the monster, and it is now properly beat.", ); - messageToPlayer("You lose. Game over!"); + messageToPlayer("You win. Congratulations!"); return; } + + messageToPlayer( + `You successfully land a hit on the monster, and it now only has ${monsterHitPoints} hit-points remaining.`, + ); + } else { + messageToPlayer( + `You fail to land a hit on the monster, and it still has ${monsterHitPoints} hit-points remaining.`, + ); + } + + const playerWantsToAttackAgain = await questionToPlayer("Do you want to attack again?"); + + if (!playerWantsToAttackAgain) { + messageToPlayer( + "You lose your nerve mid-beat-down, and try to run away. You get eaten by a sad, disappointed monster.", + ); + + messageToPlayer("You lose. Game over!"); + + return; } } - - messageToPlayer( - "You chose not to attack the monster, and the monster eats you dead, in disappointment.", - ); - - messageToPlayer("You lose. Game over!"); }, });