Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <label>
  <span>Target</span>
  <select id="target">
    <option value="0">Step 0</option>
    <option value="1">Step 1</option>
    <option value="2">Step 2</option>
    <option value="3">Step 3</option>
    <option value="4">Step 4</option>
    <option value="5">Step 5</option>
    <option value="6" selected>Step 6</option>
    <option value="7">Step 7</option>
  </select>
</label>
<label>
  <span>Repeats</span>
  <input id="repeat" type="number" value="1000"/>
</label>
<label>
  <button id="submit" type="button">Run Simulation</button>
</label>
<pre id="output"></pre>
              
            
!

CSS

              
                body {
  color:#333;
  background:#EEE;
  font-family:monospace;
}

label {
  display:inline-block;
  padding:10px;

  span {
    display:block;
    line-height:2em;
  }
  
  input, select {
    width:80px;
    height:1.5em;
    box-sizing:border-box;
    font-family: inherit;
  }
}

pre {
  padding:10px;
}
.success { color:#2A2; }
.failure { color:#C44; }
              
            
!

JS

              
                let targetNode = document.getElementById('target');
let repeatNode = document.getElementById('repeat');
let submitNode = document.getElementById('submit');
let outputNode = document.getElementById('output')
let extremeLabels = {
    SuccessMinMoney: 'Success using least money',
    SuccessMinStones: 'Success using least stones',
    SuccessMaxMoney: 'Success using most money',
    SuccessMaxStones: 'Success using most stones',
    FailureMinMoney: 'Failure using least money',
    FailureMinStones: 'Failure using least stones',
    FailureMaxMoney: 'Failure using most money',
    FailureMaxStones: 'Failure using most stones',
};

function UpgradeTo(target) {
  let money = 0;
  let stones = 0;
  let current = 0;
  let history = '0';
  while (current != target && current != 'X') {
    stones += 1;
    switch (current) {
      case 0:
        money += 30;
        current += 1;
        break;
      case 1:
      case 2:
        money += 30;
        current += (Math.random() < .5) ? 1 : -1;
        break;
      case 3:
      case 4:
        money += 50;
        current += (Math.random() < .45) ? 1 : -1;
        break;
      case 5:
        money += 50;
        current = (Math.random() < .45) ? current + 1 : 'X';
        break;
      case 6:
        money += 500;
        current = (Math.random() < .30) ? current + 1 : 'X';
        break;
    }
    history += current;
  }
  return {
    money: money,
    stones: stones,
    history: history,
    success: current == target,
  };
}

function RepeatUpgradeTo(target, repeat) {
  let money = 0;
  let stones = 0;
  let success = 0;
  let failure = 0;
  let history = [];
  let extremes = {
    SuccessMinMoney: {money:Infinity},
    SuccessMinStones: {stones:Infinity},
    SuccessMaxMoney: {money:0},
    SuccessMaxStones: {stones:0},
    FailureMinMoney: {money:Infinity},
    FailureMinStones: {stones:Infinity},
    FailureMaxMoney: {money:0},
    FailureMaxStones: {stones:0},
  }
  while (repeat--) {
    let infos = UpgradeTo(target);
    money += infos.money;
    stones += infos.stones;
    history.push(infos.history);
    infos.success ? success++ : failure++;
    if (infos.success) {
      if (infos.money < extremes.SuccessMinMoney.money) extremes.SuccessMinMoney = infos;
      if (infos.stones < extremes.SuccessMinStones.stones) extremes.SuccessMinStones = infos;
      if (infos.money > extremes.SuccessMaxMoney.money) extremes.SuccessMaxMoney = infos;
      if (infos.stones > extremes.SuccessMaxStones.stones) extremes.SuccessMaxStones = infos;
    } else {
      if (infos.money < extremes.FailureMinMoney.money) extremes.FailureMinMoney = infos;
      if (infos.stones < extremes.FailureMinStones.stones) extremes.FailureMinStones = infos;
      if (infos.money > extremes.FailureMaxMoney.money) extremes.FailureMaxMoney = infos;
      if (infos.stones > extremes.FailureMaxStones.stones) extremes.FailureMaxStones = infos;
    }
  }
  return {
    money: money,
    stones: stones,
    success: success,
    failure: failure,
    history: history,
    extremes: extremes,
  };
}

function Process() {
  let target = targetNode.value;
  let repeat = repeatNode.value;
  let results = RepeatUpgradeTo(target, repeat);
  let output = '';
  output += 'Running ' + repeat + ' attempts to reach step ' + target + '\n';
  output += 'Average success rate is ' + (100 * results.success / repeat).toFixed(1) + '%\n';
  output += '\n';
  output += 'Stones used :\n';
  output += '  * Per attempt : ' + Math.round(results.stones / repeat) + '\n';
  output += '  * Per success : ' + Math.round((results.stones / repeat) / (results.success / repeat)) + '\n';
  output += '\n';
  output += 'Gold spent :\n';
  output += '  * Per attempt : ' + Math.round(results.money / repeat) + 'k\n';
  output += '  * Per success : ' + Math.round((results.money / repeat) / (results.success / repeat)) + 'k\n';
  for(let i in results.extremes) {
    if (!extremeLabels[i]) continue;
    if (!results.extremes[i].history) continue;
    output += '\n';
    output += extremeLabels[i] + ' :\n';
    output += '  * Money : ' + results.extremes[i].money + 'k\n';
    output += '  * Stones : ' + results.extremes[i].stones + '\n';
    output += '  * Details : ' + results.extremes[i].history + '\n';
  }
  output += '\n';
  output += 'Attempt details :\n';
  for (let i = 0; i < results.history.length; i++) {
    let color = results.history[i].substr(-1) == 'X' ? 'failure' : 'success';
    output += '<span class="' + color + '">' + results.history[i] + '</span>\n';
  }
  outputNode.innerHTML = output;
}

submitNode.addEventListener('click', Process, false);

/*
store = {success:{}, failure:{}};
for(let i = 0; i < 100000; i++) {
	let result = UpgradeTo(6);
	let key = result.success ? 'success' : 'failure'
	let val = (store[key][result.stones] || 0) + 1;
	store[key][result.stones] = val;
}
*/
              
            
!
999px

Console