0

Again, I am making a game but yet I ran into another problem. I have a set of actions a player can do, until they run out of energy. The problem I have is this: When they choose an action, it prompts for another action. Yet, when they type anything into the prompt, it closes itself. I know one solution: Copy and paste the code and basically make the code 6x larger by repeating code. But I wanna know if it's possible to: How can I make it so I can rollback a case in a switch-statement to repeat itself, but keep the current stats? Here's my JavaScript (currently not finished)

var merchants = [{
  name : 'Weapons Merchant',
  items : [
    {type: "weapon", cost: 250, name: "Claymore"},
    {type: "weapon", cost: 75,  name: "Dagger"},
    {type: "weapon", cost: 350, name: "Magic Staff"},
    {type: "weapon", cost: 150, name: "Sword"},
    {type: "weapon", cost: 125, name: "Bow"},
    {type: "weapon", cost: 125, name: "Crossbow"},
    {type: "weapon", cost: 5,   name: "Arrow"},
    {type: "weapon", cost: 15,  name: "Bolt"}
  ]
}, {
  name : 'Armor Merchant',
  items : [
    {type: "clothing", slot: "head",     name: "Helmet"},
    {type: "clothing", slot: "head",     name: "Hood"},
    {type: "clothing", slot: "chest",    name: "Chestplate"},
    {type: "clothing", slot: "chest",    name: "Tunic"},
    {type: "clothing", slot: "chest",    name: "Robe"},
    {type: "clothing", slot: "leggings", name: "Legplates"},
    {type: "clothing", slot: "leggings", name: "Leggings"},
    {type: "clothing", slot: "leggings", name: "Undergarments"},
    {type: "clothing", slot: "feet",     name: "Boots"},
    {type: "clothing", slot: "feet",     name: "Armored Boots"}
  ]
},  {
  name: 'Material Merchant',
  items:  [
    {type: "material", name: "Leather", cost: 25},
    {type: "material", name: "Iron", cost: 50},
    {type: "material", name: "Steel", cost: 75},
    {type: "material", name: "Mythril", cost: 100},
    {type: "material", name: "Dragonbone", cost: 200}
  ]
}];

function findMerchant(merchants, name) {
  return find(merchants, function(merchant) {
    return merchant.name === name;
  });
}

function findItem(merchant, type, name) {
  return find(merchant.items, function(item) {
    return item.type === type && item.name === name;
  });
}

function findWithParams(arr, parameters) {
  return find(arr, function(item) {
    for (var parameter in parameters) {
      if (item[parameter] !== parameters[parameter]) {
        return false;
      }
      return true;
    }        
  });
}

function find(arr, predicateFn) {
  var found = null;
  arr.forEach(function(item, index, items) {
    if (predicateFn.apply(undefined, arguments)) {
      found = item;
      return true;
    }
    return false;
  });
  return found;
}

function fight()  {
  var armorMerchant = findMerchant(merchants, 'Armor Merchant');
  var weaponMerchant = findMerchant(merchants, 'Weapons Merchant');
  var materialMerchant = findMerchant(merchants, 'Material Merchant');
  var energy = 100;
  var day = 3;
  var money = 1000;
  var armor = ['Mythril Chestplate', 'Mythril Legplates', 'Leather Hood'];
  var weapon = [];
  var intro = prompt("You are a hero who saved your town from a dragon attack years ago. You had fun murdering that dragon, but sadly no dragon has attacked since. Just when all hope is lo, you hear the sirens ring through the city. You know what that means. Do you PREPARE, or IGNORE THE SIRENS?").toUpperCase();
  switch(intro)  {
    case 'PREPARE':
      if(intro === "PREPARE") {
        var decision1 = prompt("You decided to " + intro + ". You need to choose what you will do. Keep in mind, the more activities you do, the less energy you have! You only have 3 days to prepare! What do you do? Do you SEARCH ARMOR STAND, SEARCH WEAPON STAND, GO TO MERCHANT, FIGHT DRAGON, TRAIN, or SLEEP?").toUpperCase();
        if(decision1 === "SEARCH ARMOR STAND" && energy >= 5)  {
          energy -= 5;
          prompt("You check your armor rack, but happen to stub your toe on the way. You now have " + energy + " energy. You manage to check your armor rack, and you currently have: " + armor + " on your rack. What do you do now? SEARCH ARMOR STAND, SEARCH WEAPON STAND, GO TO MERCHANT, FIGHT DRAGON, TRAIN, or SLEEP?")
        }
        else  {
          alert("You don't have enough Energy!");
        }
        if(decision1 === "SEARCH WEAPON STAND" && energy >= 10)  {
          energy -= 10;
          prompt("You go downstairs to search your weapon stand. Your weapon stand includes: " + weapon + ". On your way back up, you cut your finger on the broken piece of glass. You now have " + energy + " energy. What do you do now? SEARCH ARMOR STAND, SEARCH WEAPON STAND, GO TO MERCHANT, FIGHT DRAGON, TRAIN, or SLEEP?")
        }
      }
  }
}
@import url(http://fonts.googleapis.com/css?family=Permanent+Marker);

html, body  {
  background: #000;
  margin: 0;
  padding: 0;
}
#wrap  {
  width: 760px;
  margin-left: auto;
  margin-right: auto;
}
.container  {
  position: relative;
  top: 50px;
  margin-left: auto;
  margin-right: auto;
  width: 570px;
  height: 350px;
  border: 6px ridge orange;
  padding: 0;
}
.container img  {
  position: absolute;
  bottom: 0px;
  width: 570px;
  height: 350px;
  z-index: -1;
}
p.intro  {
  color: black;
  text-shadow:
    -1px -1px 0 #FFF,
    1px -1px 0 #FFF,
    -1px 1px 0 #FFF,
    1px 1px 0 #FFF;  
}
h2.header  {
    text-shadow:
    -1px -1px 0 #FFA500,
    1px -1px 0 #FFA500,
    -1px 1px 0 #FFA500,
    1px 1px 0 #FFA500;  
}
.box  {
  float: left;
  min-width: 567px;
  min-height: 350px;
}
.box h2  {
  font-family: 'Permanent Marker', cursive;
  font-size: 200%;
  text-align: center;
}
.box p  {
  font-family: 'Permanent Marker', arial;
  text-align: center;
}
.box a  {
  position: absolute;
  left: 165px;
  display: inline-block;
  border: 3px groove #000;
  border-radius: 5px;
  background: red;
  margin-left: auto;
  margin-right: auto;
  width: 225px;
  height: 75px;
  font-family: 'Permanent Marker', cursive;
  color: #FFA500;
  text-shadow:
    -1px -1px 0 #000,
    1px -1px 0 #000,
    -1px 1px 0 #000,
    1px 1px 0 #000;  
  text-align: center;
}
.battles img  {
}
<html>
  <body>
    <div id="wrap">
      <div class="box">
        <div class="container">
          <h2 class="header">Dragon Slayer - REBORN!</h2>
          <p class="intro">You are a hero who saved your town from a dragon attack years ago. You had fun murdering that dragon, but sadly no dragon has attacked since. Just when all hope is lost, you hear the sirens ring through the city. You know what that means.</p>
          <a href="javascript:fight()"><br>BEGIN!</a>
          <img class="scenario"  src="http://www.thegaminghideout.com/school/stage1.png">
          <div class="battles">
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

THINGS TO NOTE

If possible, when the case rolls back to the previous prompt/step, it should keep the current energy/day variable, too. If possible, can it just go back to the previous prompt? If not possible, how can I shorten the code as much as possible to make it meet my requirement?

The Gaming Hideout
  • 574
  • 2
  • 10
  • 26
  • You really don't want to use a switch statement. I don't have time at work to write up a full answer, but generating some type of state engine, where each of the nodes in your adventure has links to the next nodes in the chain would give you more freedom. If you still don't have an answer before evening (US) I'll try and write up an example. – Steve Mitcham Feb 09 '15 at 16:31
  • Everything in programming can be done better than same code pasted *n* times over. If you catch yourself doing this, you have to take a step back and re-think what you're doing, because it's wrong. I don't have the time to fully study and understand your code, but one thing that comes to mind find a way to save current state (i.e. object storing energy, money and whatnot) and use a [stack](http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29) (fifo array). Luckily JS can work as a stack, using `.push` and `.pop` functions. [See this answer.](http://stackoverflow.com/a/1590262/1276128) – Marko Gresak Feb 09 '15 at 16:34

0 Answers0