Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/hard/castles/plain.md
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 26 additions & 0 deletions src/hard/castles/pseudo.md
Original file line number Diff line number Diff line change
@@ -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
74 changes: 74 additions & 0 deletions src/hard/castles/solution.js
Original file line number Diff line number Diff line change
@@ -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)