diff --git a/src/hard/castles/plain.md b/src/hard/castles/plain.md index 73370f1..c24ad4a 100644 --- a/src/hard/castles/plain.md +++ b/src/hard/castles/plain.md @@ -1 +1,9 @@ # Plain English Solution + +First, I will make a function to create a group of castles and rooms with random values for their tresures and random chance of success when attacking the creature. + +Then I will create the main function which will be the actual game, where I will loop through the different rooms in the different castles. Since there is no penalty for bluffing the creature, I will always try to bluff it first, and if that doesn't succeed, I will try attacking it. If the attack fails, a life is taken away, so after every attack we will have to check if the lives have reached 0, and if so, we break the loop. + +I will need two separate functionalities for bluffing and attacking, which will probably be extracted in separate functions. + +I will also need some way of keeping track of the remaining lives of the player, as well as maybe the treasure they collected so far. diff --git a/src/hard/castles/pseudo.md b/src/hard/castles/pseudo.md index 520841f..9292af2 100644 --- a/src/hard/castles/pseudo.md +++ b/src/hard/castles/pseudo.md @@ -1 +1,27 @@ # Pseudo Code Solution + function createCastle { + const castles [] + for every iteration [0-5) + create a new castle + for every iteration [0-7) + create newRoom = {randomTreasureAmount, randomCreatureChanceOfSuccess} + + return castles + } + + playerLives =9; + playerTreasureAmount = 0; + + function playGame(castles) { + for every iteration [0-5) // castles + for every iteration [0-7) // rooms + bluffCreature() ? playerTreasureAmount += room.treasureAmount : attackCreature() + if attackCreature is false -> lives-- + if lives ==0 -> break + else + playerTreasureAmount += room.treasureAmount + return playerTreasureAmount + } + + function bluff -> return Math.random < 0.3 + function attack -> return Math.random < room.creatureChanceOfSuccess \ No newline at end of file diff --git a/src/hard/castles/solution.js b/src/hard/castles/solution.js index e69de29..6d40c79 100644 --- a/src/hard/castles/solution.js +++ b/src/hard/castles/solution.js @@ -0,0 +1,74 @@ +function createCastles() { + const maxCastles = 5 + const maxRooms = 7 + + const castles = [] + + for (let i = 0; i <=maxCastles; i++) { + const castle = [] + castles.push(castle) + for (let j = 0; j <= maxRooms; j++) { + const room = { + treasure: Math.floor(Math.random() * 100) + 1, // generate a value between 1 and 100 + creature: Math.random() // generate a number between 0 and 1 + } + castles[i].push(room) + } + } + return castles +} + + +function bluffCreature() { + const successChance = 0.3 + const randomValue = Math.random() + return successChance >= randomValue +} + +function attackCreature(room) { + const successChance = room.creature + const randomValue = Math.random() + return successChance >= randomValue +} + +function calculateTotalTreasure(castles) { + let totalValue = 0 + castles.forEach(castle => { + castle.forEach(room => { + totalValue += room.treasure + }) + }); + return totalValue +} + +function playGame(player,castles) { + const totalTreasuresValue = calculateTotalTreasure(castles) + for (let index = 0; index < castles.length; index++) { + const castle = castles[index]; + for (let j = 0; j < castle.length; j++) { + const room = castle[j]; + if (bluffCreature()) { + player.treasureCollected+= room.treasure + } else { + if (attackCreature(room)) { + player.treasureCollected+= room.treasure + } else { + player.lives-- + } + } + if (player.lives === 0) { + break + } + } + } + if (player.treasureCollected === totalTreasuresValue) { + console.log(`Congratulations! The player collected all the available treasure!\nTreasure collected: ${totalTreasuresValue}`) + } else { + console.log(`Game over.\nThe player collected ${player.treasureCollected} treasures.\nTreasures remaining: ${totalTreasuresValue-player.treasureCollected}`) + } +} + +const player = {lives: 9, treasureCollected: 0} +const castles = createCastles() + +playGame(player,castles) \ No newline at end of file