From be5e5ee1dae13ac691050d4510cd2bb2daa2b5a8 Mon Sep 17 00:00:00 2001 From: Carter Supple Date: Thu, 7 Oct 2021 00:21:56 -0700 Subject: [PATCH 1/3] shop function --- assets/js/game.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/assets/js/game.js b/assets/js/game.js index f4d0259..7de15b3 100644 --- a/assets/js/game.js +++ b/assets/js/game.js @@ -64,6 +64,12 @@ var startGame = function (){ enemyHealth = 50; fight(pickedEnemyName); + if (playerHealth > 0 && i < enemyNames.length - 1) { + var storeConfirm = window.confirm("The fight is over, visit the store before the next round?"); + if (storeConfirm) { + shop(); + } + } } else { @@ -72,5 +78,61 @@ var startGame = function (){ } } +endGame(); +}; + +var endGame = function() { + if (playerHealth > 0) { + window.alert("Great job, you've survived the game! You now have a score of " + playerMoney + "."); + } + else { + window.alert("You've lost your robot in battle."); + } + +var playAgainConfirm = window.confirm("Would you like to play again?"); +if (playAgainConfirm) { + startGame(); +} else { + window.alert("Thank you for playing Robot Gladiators! Come back soon!"); +} }; -startGame(); \ No newline at end of file +var shop = function() { + var shopOptionPrompt = window.prompt( + 'Would you like to REFILL your health, UPGRADE your attack, or LEAVE the store? Please enter one "REFILL", "UPGRADE", or "LEAVE" to make a choice.' + ); + + switch (shopOptionPrompt) { + case 'REFILL': + case 'refill': + if (playerMoney >= 7) { + window.alert("Refilling player's health by 20 for 7 dollars."); + playerHealth = playerHealth + 20; + playerMoney = playerMoney - 7; + } + else { + window.alert("You don't have enough money!"); + } + break; + case 'UPGRADE': + case 'upgrade': + if (playerMoney >= 7) { + window.alert("Upgrading player's attack by 6 for 7 dollars."); + playerAttack = playerAttack + 6; + playerMoney = playerMoney - 7; + } + else { + window.alert("You don't have enough money!"); + } + break; + case 'LEAVE': + case 'leave': + window.alert('Leaving the store.'); + break; + default: + window.alert('You did not pick a valid option. Try again.'); + shop(); + break; + } +}; + startGame(); + From 9d37edccf03a6465c9a525f3f6c15055b07a0ddf Mon Sep 17 00:00:00 2001 From: Carter Supple Date: Thu, 7 Oct 2021 00:59:38 -0700 Subject: [PATCH 2/3] random health and attack values --- assets/js/game.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/assets/js/game.js b/assets/js/game.js index 7de15b3..fe8f4d7 100644 --- a/assets/js/game.js +++ b/assets/js/game.js @@ -11,21 +11,28 @@ console.log(enemyNames.length); console.log(enemyNames[0]); console.log(enemyNames[3]); - var fight = function(enemyName) { +var randomNumber = function(min, max) { + var value = Math.floor(Math.random() * (max - min + 1) + min); + + return value; +}; + +var fight = function(enemyName) { while (playerHealth > 0 && enemyHealth > 0) { var promptFight = window.prompt('Would you like to FIGHT or SKIP this battle? Enter "FIGHT" or "SKIP" to choose.'); if (promptFight === "skip" || promptFight === "SKIP") { var confirmSkip = window.confirm("Are you sure you'd like to quit?"); if (confirmSkip) { window.alert(playerName + ' has decided to skip this fight. Goodbye!'); - playerMoney = playerMoney - 10; + playerMoney = Math.max(0, playerMoney - 10); console.log("playerMoney", playerMoney) break; } } - enemyHealth = enemyHealth - playerAttack; - console.log( + var damage = randomNumber(playerAttack - 3, playerAttack); + enemyHealth = Math.max(0, enemyHealth - damage); + console.log( playerName + ' attacked ' + enemyName + '. ' + enemyName + ' now has ' + enemyHealth + ' health remaining.' ); @@ -37,7 +44,8 @@ console.log(enemyNames[3]); window.alert(enemyName + ' still has ' + enemyHealth + ' health left.'); } - playerHealth = playerHealth - enemyAttack; + var damage = randomNumber(enemyAttack - 3, enemyAttack); + playerHealth = Math.max(0, playerHealth - damage); console.log( enemyName + ' attacked ' + playerName + '. ' + playerName + ' now has ' + playerHealth + ' health remaining.' ); @@ -61,7 +69,7 @@ var startGame = function (){ if (playerHealth > 0) { window.alert('Welcome to Robot Gladiators! Round ' + (i + 1)); var pickedEnemyName = enemyNames[i]; - enemyHealth = 50; + enemyHealth = randomNumber(40, 60); fight(pickedEnemyName); if (playerHealth > 0 && i < enemyNames.length - 1) { @@ -78,9 +86,7 @@ var startGame = function (){ } } -endGame(); -}; - +} var endGame = function() { if (playerHealth > 0) { window.alert("Great job, you've survived the game! You now have a score of " + playerMoney + "."); @@ -135,4 +141,3 @@ var shop = function() { } }; startGame(); - From 5f1bc1378ce9de8c19fc0cec896de64b0fa05f33 Mon Sep 17 00:00:00 2001 From: Carter Supple Date: Thu, 7 Oct 2021 01:54:33 -0700 Subject: [PATCH 3/3] object optimization --- assets/js/game.js | 247 +++++++++++++++++++++++++++------------------- 1 file changed, 143 insertions(+), 104 deletions(-) diff --git a/assets/js/game.js b/assets/js/game.js index fe8f4d7..71462e8 100644 --- a/assets/js/game.js +++ b/assets/js/game.js @@ -1,107 +1,105 @@ -var playerName = window.prompt("What is your robot's name?"); -var playerHealth = 100; -var playerAttack = 10; -var playerMoney = 10; - -var enemyNames = ['Roborto', 'Amy Android', 'Robo Trumble']; -var enemyHealth = 50; -var enemyAttack = 12; -console.log(enemyNames); -console.log(enemyNames.length); -console.log(enemyNames[0]); -console.log(enemyNames[3]); - var randomNumber = function(min, max) { var value = Math.floor(Math.random() * (max - min + 1) + min); return value; }; -var fight = function(enemyName) { - while (playerHealth > 0 && enemyHealth > 0) { - var promptFight = window.prompt('Would you like to FIGHT or SKIP this battle? Enter "FIGHT" or "SKIP" to choose.'); - if (promptFight === "skip" || promptFight === "SKIP") { - var confirmSkip = window.confirm("Are you sure you'd like to quit?"); - if (confirmSkip) { - window.alert(playerName + ' has decided to skip this fight. Goodbye!'); - playerMoney = Math.max(0, playerMoney - 10); - console.log("playerMoney", playerMoney) - break; - } - } - - var damage = randomNumber(playerAttack - 3, playerAttack); - enemyHealth = Math.max(0, enemyHealth - damage); - console.log( - playerName + ' attacked ' + enemyName + '. ' + enemyName + ' now has ' + enemyHealth + ' health remaining.' - ); - - if (enemyHealth <= 0) { - window.alert(enemyName + ' has died!'); - playerMoney = playerMoney + 20; - break; - } else { - window.alert(enemyName + ' still has ' + enemyHealth + ' health left.'); - } - - var damage = randomNumber(enemyAttack - 3, enemyAttack); - playerHealth = Math.max(0, playerHealth - damage); - console.log( - enemyName + ' attacked ' + playerName + '. ' + playerName + ' now has ' + playerHealth + ' health remaining.' - ); - - if (playerHealth <= 0) { - window.alert(playerName + ' has died!'); +var fight = function(enemy) { + while (playerInfo.health > 0 && enemy.health > 0) { + var promptFight = window.prompt('Would you like to FIGHT or SKIP this battle? Enter "FIGHT" or "SKIP" to choose.'); + + if (promptFight === "skip" || promptFight === "SKIP") { + var confirmSkip = window.confirm("Are you sure you'd like to quit?"); + if (confirmSkip) { + window.alert(playerInfo.name + ' has decided to skip this fight. Goodbye!'); + playerInfo.money = Math.max(0, playerInfo.money - 10); + console.log("playerInfo.money", playerInfo.money) break; - } else { - window.alert(playerName + ' still has ' + playerHealth + ' health left.'); } } - }; - - - -var startGame = function (){ - playerHealth = 100; - playerAttack = 10; - playerMoney = 10; - for(var i = 0; i < enemyNames.length; i++) { - if (playerHealth > 0) { - window.alert('Welcome to Robot Gladiators! Round ' + (i + 1)); - var pickedEnemyName = enemyNames[i]; - enemyHealth = randomNumber(40, 60); - - fight(pickedEnemyName); - if (playerHealth > 0 && i < enemyNames.length - 1) { - var storeConfirm = window.confirm("The fight is over, visit the store before the next round?"); - if (storeConfirm) { - shop(); - } + + var damage = randomNumber(playerInfo.attack - 3, playerInfo.attack); + + enemy.health = Math.max(0, enemy.health - damage); + console.log( + playerInfo.name + ' attacked ' + enemy.name + '. ' + enemy.name + ' now has ' + enemy.health + ' health remaining.' + ); + + if (enemy.health <= 0) { + window.alert(enemy.name + ' has died!'); + + playerInfo.money = playerInfo.money + 20; + + break; + } else { + window.alert(enemy.name + ' still has ' + enemy.health + ' health left.'); + } + + var damage = randomNumber(enemy.attack - 3, enemy.attack); + + playerInfo.health = Math.max(0, playerInfo.health - damage); + + console.log( + enemy.name + ' attacked ' + playerInfo.name + '. ' + playerInfo.name + ' now has ' + playerInfo.health + ' health remaining.' + ); + + if (playerInfo.health <= 0) { + window.alert(playerInfo.name + ' has died!'); + break; + } else { + window.alert(playerInfo.name + ' still has ' + playerInfo.health + ' health left.'); } } - - else { - window.alert('You have lost your robot in battle! Game Over!'); - break; +}; + +var startGame = function() { + playerInfo.reset(); + + for (var i = 0; i < enemyInfo.length; i++) { + if (playerInfo.health > 0) { + window.alert('Welcome to Robot Gladiators! Round ' + (i + 1)); + var pickedEnemyObj = enemyInfo[i]; + + pickedEnemyObj.health = randomNumber(40, 60); + + fight(pickedEnemyObj); + + if (playerInfo.health > 0 && i < enemyInfo.length - 1) { + var storeConfirm = window.confirm("The fight is over, visit the store before the next round?"); + + if (storeConfirm) { + shop(); + } + } + } + else { + window.alert("You have lost your robot in battle! Game Over!"); + break; + } } -} -} + + endGame(); +}; + var endGame = function() { - if (playerHealth > 0) { - window.alert("Great job, you've survived the game! You now have a score of " + playerMoney + "."); - } - else { - window.alert("You've lost your robot in battle."); + window.alert("The game has now ended. Let's see how you did!"); + + if (playerInfo.health > 0) { + window.alert("Great job, you've survived the game! You now have a score of " + playerInfo.money + '.'); + } else { + window.alert("You've lost your robot in battle!"); } -var playAgainConfirm = window.confirm("Would you like to play again?"); -if (playAgainConfirm) { - startGame(); -} else { - window.alert("Thank you for playing Robot Gladiators! Come back soon!"); -} + var playAgainConfirm = window.confirm('Would you like to play again?'); + + if (playAgainConfirm) { + startGame(); + } else { + window.alert('Thank you for playing Robot Gladiators! Come back soon!'); + } }; + var shop = function() { var shopOptionPrompt = window.prompt( 'Would you like to REFILL your health, UPGRADE your attack, or LEAVE the store? Please enter one "REFILL", "UPGRADE", or "LEAVE" to make a choice.' @@ -110,34 +108,75 @@ var shop = function() { switch (shopOptionPrompt) { case 'REFILL': case 'refill': - if (playerMoney >= 7) { - window.alert("Refilling player's health by 20 for 7 dollars."); - playerHealth = playerHealth + 20; - playerMoney = playerMoney - 7; - } - else { - window.alert("You don't have enough money!"); - } + playerInfo.refillHealth(); break; case 'UPGRADE': case 'upgrade': - if (playerMoney >= 7) { - window.alert("Upgrading player's attack by 6 for 7 dollars."); - playerAttack = playerAttack + 6; - playerMoney = playerMoney - 7; - } - else { - window.alert("You don't have enough money!"); - } + playerInfo.upgradeAttack(); break; case 'LEAVE': case 'leave': window.alert('Leaving the store.'); + break; default: window.alert('You did not pick a valid option. Try again.'); + shop(); break; } }; - startGame(); + +var playerInfo = { + name: window.prompt("What is your robot's name?"), + health: 100, + attack: 10, + money: 10, + reset: function() { + this.health = 100; + this.money = 10; + this.attack = 10; + }, + refillHealth: function() { + if (this.money >= 7) { + window.alert("Refilling player's health by 20 for 7 dollars."); + this.health += 20; + this.money -= 7; + } + else { + window.alert("You don't have enough money!"); + } + }, + upgradeAttack: function() { + if (this.money >= 7) { + window.alert("Upgrading player's attack by 6 for 7 dollars."); + this.attack += 6; + this.money -= 7; + } + else { + window.alert("You don't have enough money!"); + } + } +}; + +var enemyInfo = [ + { + name: 'Roborto', + attack: randomNumber(10, 14) + }, + { + name: 'Amy Android', + attack: randomNumber(10, 14) + }, + { + name: 'Robo Trumble', + attack: randomNumber(10, 14) + } +]; + +console.log(enemyInfo); +console.log(enemyInfo[0]); +console.log(enemyInfo[0].name); +console.log(enemyInfo[0]['attack']); + +startGame();