Skip to content

Instantly share code, notes, and snippets.

@hjstn
Created October 26, 2014 09:33
Show Gist options
  • Save hjstn/22f2bf88ccfbb6b1dc0e to your computer and use it in GitHub Desktop.
Save hjstn/22f2bf88ccfbb6b1dc0e to your computer and use it in GitHub Desktop.
Thai Monopoly Calculator
/*jslint devel: true */
var mapping = ["GO", "CHUMSAENG", "COMMUNITY CHEST 1", "PAKNAMPHO", "INCOME TAX", "KHON KHAEN ST.", "BAN PHAI", "CHANCE 1", "THA PHRA", "SRI CHAN", "JAIL VISIT", "JANA", "ELECTRIC COMPANY", "PHET KIRI", "HAT YAI", "SURAT THAN ST.", "CHAIYA", "KO PHA NGAN", "KO SAMUI", "FREE PARKING", "KO LAN", "CHANCE 2", "JOM THIAN", "SRIRACHA", "CHIANG MAI ST.", "SAN KAMPAENG", "THA PHAE", "WATER WORKS", "CHANG KLAN", "JAIL GOTO", "RAWAI", "KA RON", "COMMUNITY CHEST 2", "PA TONG", "HUA LUMPHONG ST.", "CHANCE 3", "SUKHUMVIT", "VAT TAX", "SILOM"];
var size = mapping.length;
var passed = [];
var hasStarted = false;
function start(turns, step) {
"use strict";
hasStarted = true;
if (isNaN(turns)) {
console.error("Please enter a valid integer.");
return;
}
if (isNaN(step) || step < 0 || step > size) {
console.warn("Start position is invalid, defaulting to GO");
step = 0;
}
function toSort(a, b) {
return (b - a);
}
function dice() {
return Math.floor((Math.random() * 6) + 1);
}
for (var i = 0; i < turns; i++) {
var dicetotal = dice() + dice();
if ( (step + dicetotal) >= size) {
dicetotal = dicetotal - (size - step);
step = 0;
}
step = dicetotal + step;
if(isNaN(passed[step])) {
passed[step] = 0;
}
passed[step] = passed[step] + 1;
if(step == 29) {
step = 10;
}
}
passed.sort(toSort);
passed.forEach(function(value, index) {
console.log(mapping[index] + " - " + value);
});
}
function pass(location) {
if(!hasStarted) {
console.error("Please start a calculation with start(<turns>, [starting location]) first!");
return;
}
if(location < 0 || location > size) {
console.error("Invalid location!");
return;
}
console.log(mapping[location] + ": " + passed[location]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment