Advertisement
KingAesthetic

Example 3

May 12th, 2024
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.35 KB | Source Code | 0 0
  1. // function that simulates Enemy encounter
  2. const enemies = [
  3.     { name: 'Shrek', health: 30, attackPower: 8, loot: 'small gem' },
  4.     { name: 'Orc', health: 50, attackPower: 12, loot: 'iron sword' },
  5.     { name: 'Dragon', health: 100, attackPower: 20, loot: 'dragon scale' },
  6. ];
  7.  
  8. function encounterEnemy(playerHealth) {
  9.     const randomEnemy = enemies[Math.floor(Math.random() * enemies.length)];
  10.     console.log(`You Encountered a ${randomEnemy.name}!`);
  11.  
  12.     // player vs enemy simulation battle
  13.     while (playerHealth > 0 && randomEnemy.health > 0) {
  14.         // player attacks enemy
  15.         const playerDamage = Math.floor(Math.random() * 10) + 1;
  16.         randomEnemy.health -= playerDamage;
  17.  
  18.         // enemy attacks player
  19.         const enemyDamage = randomEnemy.attackPower;
  20.         playerHealth -= enemyDamage;
  21.     }
  22.  
  23.     if (playerHealth <= 0) {
  24.         console.log("You are DEAD");
  25.     } else {
  26.         console.log(`Victory! You defeated the ${randomEnemy.name}.`);
  27.         console.log(`Loot obtained: ${randomEnemy.loot}`);
  28.     }
  29. }
  30.  
  31. const playerMaxHealth = 100;
  32. encounterEnemy(playerMaxHealth);
  33.  
  34. // Puzzle solvING Function
  35. function solvePuzzle(puzzlePieces) {
  36.     return true;
  37. }
  38.  
  39. const puzzlePieces = ['red', 'blue', 'green', 'yellow'];
  40. const isPuzzleSolved = solvePuzzle(puzzlePieces);
  41. console.log(`Puzzle solved: ${isPuzzleSolved}`);
  42.  
  43. // function for tracking achievements:
  44. const playerAchievements = {
  45.     defeatedDragon: false,
  46.     reachedLevel20: false,
  47.     collected100Gems: false,
  48. };
  49.  
  50. // Function to unlock an achievement
  51. function unlockAchievement(achievementName) {
  52.     playerAchievements[achievementName] = true;
  53. }
  54. unlockAchievement("reachedLevel20");
  55. console.log(`Achievements unlocked: ${Object.keys(playerAchievements).join(', ')}`);
  56.  
  57. // function that simulates the rolling of a Dice:
  58. function rollDice(sides) {
  59.     return Math.floor(Math.random() * sides) + 1;
  60. }
  61.  
  62. const numSides = 6; // Standard six-sided die
  63. const rollResult = rollDice(numSides);
  64. console.log(`You rolled a ${rollResult}!`);
  65.  
  66. // function that simulates a Tic Tac Toe Game:
  67. const board = [
  68.     ['', '', ''],
  69.     ['', '', ''],
  70.     ['', '', ''],
  71. ];
  72.  
  73. let currentPlayer = 'X';
  74.  
  75. function printBoard() {
  76.     console.log('Current board:');
  77.     for (const row of board) {
  78.         console.log(row.join(' '));
  79.     }
  80. }
  81.  
  82. function checkWin() {
  83.     // Checking for a diagonal win
  84.     for (let i = 0; i < 3; i++) {
  85.         if (board[i][0] === currentPlayer && board[i][1] === currentPlayer && board[i][2] === currentPlayer) {
  86.             return true;
  87.         }
  88.         if (board[0][i] === currentPlayer && board[1][i] === currentPlayer && board[2][i] === currentPlayer) {
  89.             return true;
  90.         }
  91.     }
  92.     if (board[0][0] === currentPlayer && board[1][1] === currentPlayer && board[2][2] === currentPlayer) {
  93.         return true;
  94.     }
  95.     if (board[0][2] === currentPlayer && board[1][1] === currentPlayer && board[2][0] === currentPlayer) {
  96.         return true;
  97.     }
  98.     return false;
  99. }
  100.  
  101. function makeMove(row, col) {
  102.     if (board[row][col] === '') {
  103.         board[row][col] = currentPlayer;
  104.         return true;
  105.     }
  106.     return false;
  107. }
  108. printBoard();
  109. console.log(`Player ${currentPlayer}'s turn.`);
  110. makeMove(1, 1);
  111. printBoard();
  112. currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
  113. console.log(`Player ${currentPlayer}'s turn.`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement