class LostTreasureAdventure { constructor() { this.gridSize = 5; this.heroPosition = { x: 2, y: 2 }; this.treasurePosition = this.getRandomPosition(); this.obstaclePosition = this.getRandomPosition(); while (this.positionsAreSame(this.treasurePosition, this.heroPosition) || this.positionsAreSame(this.obstaclePosition, this.heroPosition) || this.positionsAreSame(this.treasurePosition, this.obstaclePosition)) { this.treasurePosition = this.getRandomPosition(); this.obstaclePosition = this.getRandomPosition(); } } positionsAreSame(pos1, pos2) { return pos1.x === pos2.x && pos1.y === pos2.y; } getRandomPosition() { return { x: Math.floor(Math.random() * this.gridSize), y: Math.floor(Math.random() * this.gridSize) }; } moveHero(direction) { switch (direction) { case "north": this.heroPosition.y = Math.max(0, this.heroPosition.y - 1); break; case "south": this.heroPosition.y = Math.min(this.gridSize - 1, this.heroPosition.y + 1); break; case "east": this.heroPosition.x = Math.min(this.gridSize - 1, this.heroPosition.x + 1); break; case "west": this.heroPosition.x = Math.max(0, this.heroPosition.x - 1); break; default: alert("Ye have chosen a path unknown! Choose: north, south, east, or west."); return; } if (this.positionsAreSame(this.heroPosition, this.treasurePosition)) { alert("By the gods! You have found the hidden treasure at (" + this.heroPosition.x + "," + this.heroPosition.y + ")!"); if (confirm("Wilt thou embark on another quest?")) { this.resetAdventure(); } } else if (this.positionsAreSame(this.heroPosition, this.obstaclePosition)) { alert("Alas! Ye have encountered a perilous beast at (" + this.heroPosition.x + "," + this.heroPosition.y + ") and met thy doom. Valiant was thy effort."); if (confirm("Dare ye tempt fate once more?")) { this.resetAdventure(); } } else { alert("Thou art now in the land of (" + this.heroPosition.x + "," + this.heroPosition.y + "). Press onward in your quest!"); this.promptMove(); } } resetAdventure() { this.heroPosition = { x: 2, y: 2 }; this.treasurePosition = this.getRandomPosition(); this.obstaclePosition = this.getRandomPosition(); while (this.positionsAreSame(this.treasurePosition, this.heroPosition) || this.positionsAreSame(this.obstaclePosition, this.heroPosition) || this.positionsAreSame(this.treasurePosition, this.obstaclePosition)) { this.treasurePosition = this.getRandomPosition(); this.obstaclePosition = this.getRandomPosition(); } this.promptMove(); } promptMove() { const direction = prompt("Choose thy path, brave adventurer! (north, south, east, west)"); this.moveHero(direction); } start() { alert("Hearken! Beware the great obstacle, for many have lost their souls in this pursuit of hidden treasure. Embark on this quest to find the treasure hidden deep within the realm!"); this.promptMove(); } } const adventure = new LostTreasureAdventure(); adventure.start();