Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // function that simulates Enemy encounter
- const enemies = [
- { name: 'Shrek', health: 30, attackPower: 8, loot: 'small gem' },
- { name: 'Orc', health: 50, attackPower: 12, loot: 'iron sword' },
- { name: 'Dragon', health: 100, attackPower: 20, loot: 'dragon scale' },
- ];
- function encounterEnemy(playerHealth) {
- const randomEnemy = enemies[Math.floor(Math.random() * enemies.length)];
- console.log(`You Encountered a ${randomEnemy.name}!`);
- // player vs enemy simulation battle
- while (playerHealth > 0 && randomEnemy.health > 0) {
- // player attacks enemy
- const playerDamage = Math.floor(Math.random() * 10) + 1;
- randomEnemy.health -= playerDamage;
- // enemy attacks player
- const enemyDamage = randomEnemy.attackPower;
- playerHealth -= enemyDamage;
- }
- if (playerHealth <= 0) {
- console.log("You are DEAD");
- } else {
- console.log(`Victory! You defeated the ${randomEnemy.name}.`);
- console.log(`Loot obtained: ${randomEnemy.loot}`);
- }
- }
- const playerMaxHealth = 100;
- encounterEnemy(playerMaxHealth);
- // Puzzle solvING Function
- function solvePuzzle(puzzlePieces) {
- return true;
- }
- const puzzlePieces = ['red', 'blue', 'green', 'yellow'];
- const isPuzzleSolved = solvePuzzle(puzzlePieces);
- console.log(`Puzzle solved: ${isPuzzleSolved}`);
- // function for tracking achievements:
- const playerAchievements = {
- defeatedDragon: false,
- reachedLevel20: false,
- collected100Gems: false,
- };
- // Function to unlock an achievement
- function unlockAchievement(achievementName) {
- playerAchievements[achievementName] = true;
- }
- unlockAchievement("reachedLevel20");
- console.log(`Achievements unlocked: ${Object.keys(playerAchievements).join(', ')}`);
- // function that simulates the rolling of a Dice:
- function rollDice(sides) {
- return Math.floor(Math.random() * sides) + 1;
- }
- const numSides = 6; // Standard six-sided die
- const rollResult = rollDice(numSides);
- console.log(`You rolled a ${rollResult}!`);
- // function that simulates a Tic Tac Toe Game:
- const board = [
- ['', '', ''],
- ['', '', ''],
- ['', '', ''],
- ];
- let currentPlayer = 'X';
- function printBoard() {
- console.log('Current board:');
- for (const row of board) {
- console.log(row.join(' '));
- }
- }
- function checkWin() {
- // Checking for a diagonal win
- for (let i = 0; i < 3; i++) {
- if (board[i][0] === currentPlayer && board[i][1] === currentPlayer && board[i][2] === currentPlayer) {
- return true;
- }
- if (board[0][i] === currentPlayer && board[1][i] === currentPlayer && board[2][i] === currentPlayer) {
- return true;
- }
- }
- if (board[0][0] === currentPlayer && board[1][1] === currentPlayer && board[2][2] === currentPlayer) {
- return true;
- }
- if (board[0][2] === currentPlayer && board[1][1] === currentPlayer && board[2][0] === currentPlayer) {
- return true;
- }
- return false;
- }
- function makeMove(row, col) {
- if (board[row][col] === '') {
- board[row][col] = currentPlayer;
- return true;
- }
- return false;
- }
- printBoard();
- console.log(`Player ${currentPlayer}'s turn.`);
- makeMove(1, 1);
- printBoard();
- currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
- console.log(`Player ${currentPlayer}'s turn.`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement