diff --git a/packages/kata-for-gabriel/src/handle-landed-hit-on-monster-for.ts b/packages/kata-for-gabriel/src/handle-landed-hit-on-monster-for.ts new file mode 100644 index 0000000000..d0d244c15c --- /dev/null +++ b/packages/kata-for-gabriel/src/handle-landed-hit-on-monster-for.ts @@ -0,0 +1,33 @@ +import type { Dependencies } from "./monster-beatdown"; + +export type Monster = { + hitPoints: number; +}; + +export const handleLandedHitOnMonsterFor = + ({ + monster, + messageToPlayer, + }: { + monster: Monster; + messageToPlayer: Dependencies["messageToPlayer"]; + }) => + () => { + monster.hitPoints--; + + if (monster.hitPoints) { + messageToPlayer( + `You successfully land a hit on the monster, and it now only has ${monster.hitPoints} hit-points remaining.`, + ); + + return { monsterIsDead: false }; + } + + messageToPlayer( + "You successfully land a final hit on the monster, and it is now properly beat.", + ); + + messageToPlayer("You win. Congratulations!"); + + return { monsterIsDead: true }; + }; diff --git a/packages/kata-for-gabriel/src/monster-beatdown.ts b/packages/kata-for-gabriel/src/monster-beatdown.ts index 98619b81d7..f7d525e015 100644 --- a/packages/kata-for-gabriel/src/monster-beatdown.ts +++ b/packages/kata-for-gabriel/src/monster-beatdown.ts @@ -1,67 +1,63 @@ +import { handleLandedHitOnMonsterFor, Monster } from "./handle-landed-hit-on-monster-for"; + export type Dependencies = { messageToPlayer: (message: string) => void; questionToPlayer: (message: string) => Promise; castDie: () => Promise; }; -const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies) => ({ - start: async () => { - let monsterHitPoints = 3; +const createGame = ({ messageToPlayer, questionToPlayer, castDie }: Dependencies) => { + const monster: Monster = { hitPoints: 3 }; - messageToPlayer(`You encounter a monster with ${monsterHitPoints} hit-points`); + const handleLandedHitOnMonster = handleLandedHitOnMonsterFor({ messageToPlayer, monster }); - const playerWantsToAttack = await questionToPlayer("Attack the monster?"); - if (!playerWantsToAttack) { - messageToPlayer( - "You chose not to attack the monster, and the monster eats you dead, in disappointment.", - ); - - messageToPlayer("You lose. Game over!"); - - return; - } - - while (true) { - messageToPlayer("You attack the monster."); - - const dieResult = await castDie(); - - const playerLandsHitOnMonster = dieResult > 3; - if (playerLandsHitOnMonster) { - monsterHitPoints--; - - if (!monsterHitPoints) { - messageToPlayer( - "You successfully land a final hit on the monster, and it is now properly beat.", - ); - - messageToPlayer("You win. Congratulations!"); - - return; - } + return { + start: async () => { + messageToPlayer(`You encounter a monster with ${monster.hitPoints} hit-points`); + const playerWantsToAttack = await questionToPlayer("Attack the monster?"); + if (!playerWantsToAttack) { 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 chose not to attack the monster, and the monster eats you dead, in disappointment.", ); messageToPlayer("You lose. Game over!"); return; } - } - }, -}); + + while (true) { + messageToPlayer("You attack the monster."); + + const dieResult = await castDie(); + + const playerLandsHitOnMonster = dieResult > 3; + if (playerLandsHitOnMonster) { + const { monsterIsDead } = handleLandedHitOnMonster(); + if (monsterIsDead) { + return; + } + } else { + messageToPlayer( + `You fail to land a hit on the monster, and it still has ${monster.hitPoints} 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; + } + } + }, + }; +}; export { createGame }; +