Facebook
From yaro, 2 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 234
  1. class LostTreasureAdventure {
  2.     constructor() {
  3.         this.gridSize = 5;
  4.         this.heroPosition = { x: 2, y: 2 };
  5.         this.treasurePosition = this.getRandomPosition();
  6.         this.obstaclePosition = this.getRandomPosition();
  7.  
  8.         while (this.positionsAreSame(this.treasurePosition, this.heroPosition) ||
  9.                this.positionsAreSame(this.obstaclePosition, this.heroPosition) ||
  10.                this.positionsAreSame(this.treasurePosition, this.obstaclePosition)) {
  11.             this.treasurePosition = this.getRandomPosition();
  12.             this.obstaclePosition = this.getRandomPosition();
  13.         }
  14.     }
  15.  
  16.     positionsAreSame(pos1, pos2) {
  17.         return pos1.x === pos2.x && pos1.y === pos2.y;
  18.     }
  19.  
  20.     getRandomPosition() {
  21.         return { x: Math.floor(Math.random() * this.gridSize), y: Math.floor(Math.random() * this.gridSize) };
  22.     }
  23.  
  24.     moveHero(direction) {
  25.         switch (direction) {
  26.             case "north":
  27.                 this.heroPosition.y = Math.max(0, this.heroPosition.y - 1);
  28.                 break;
  29.             case "south":
  30.                 this.heroPosition.y = Math.min(this.gridSize - 1, this.heroPosition.y + 1);
  31.                 break;
  32.             case "east":
  33.                 this.heroPosition.x = Math.min(this.gridSize - 1, this.heroPosition.x + 1);
  34.                 break;
  35.             case "west":
  36.                 this.heroPosition.x = Math.max(0, this.heroPosition.x - 1);
  37.                 break;
  38.             default:
  39.                 alert("Ye have chosen a path unknown! Choose: north, south, east, or west.");
  40.                 return;
  41.         }
  42.  
  43.         if (this.positionsAreSame(this.heroPosition, this.treasurePosition)) {
  44.             alert("By the gods! You have found the hidden treasure at (" + this.heroPosition.x + "," + this.heroPosition.y + ")!");
  45.             if (confirm("Wilt thou embark on another quest?")) {
  46.                 this.resetAdventure();
  47.             }
  48.         } else if (this.positionsAreSame(this.heroPosition, this.obstaclePosition)) {
  49.             alert("Alas! Ye have encountered a perilous beast at (" + this.heroPosition.x + "," + this.heroPosition.y + ") and met thy doom. Valiant was thy effort.");
  50.             if (confirm("Dare ye tempt fate once more?")) {
  51.                 this.resetAdventure();
  52.             }
  53.         } else {
  54.             alert("Thou art now in the land of (" + this.heroPosition.x + "," + this.heroPosition.y + "). Press onward in your quest!");
  55.             this.promptMove();
  56.         }
  57.     }
  58.  
  59.     resetAdventure() {
  60.         this.heroPosition = { x: 2, y: 2 };
  61.         this.treasurePosition = this.getRandomPosition();
  62.         this.obstaclePosition = this.getRandomPosition();
  63.  
  64.         while (this.positionsAreSame(this.treasurePosition, this.heroPosition) ||
  65.                this.positionsAreSame(this.obstaclePosition, this.heroPosition) ||
  66.                this.positionsAreSame(this.treasurePosition, this.obstaclePosition)) {
  67.             this.treasurePosition = this.getRandomPosition();
  68.             this.obstaclePosition = this.getRandomPosition();
  69.         }
  70.  
  71.         this.promptMove();
  72.     }
  73.  
  74.     promptMove() {
  75.         const direction = prompt("Choose thy path, brave adventurer! (north, south, east, west)");
  76.         this.moveHero(direction);
  77.     }
  78.  
  79.     start() {
  80.         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!");
  81.         this.promptMove();
  82.     }
  83. }
  84.  
  85. const adventure = new LostTreasureAdventure();
  86. adventure.start();