Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created March 24, 2016 22:44
Show Gist options
  • Select an option

  • Save mindplace/f34f5a424df3b3a68d90 to your computer and use it in GitHub Desktop.

Select an option

Save mindplace/f34f5a424df3b3a68d90 to your computer and use it in GitHub Desktop.
assets = {
0: ["kitchen gardens", "courtyards", "stables", "libraries", "great halls", "armories"],
1: ["depressed ghosts", "sarcastic cats", "opinionated teacups"],
2: ["knights with great cooking skills", "sword-wielding ladies", "highly educated serfs"],
3: ["golden chalices of drinking", "fluffy blankets of sleeping", "shiny rubies of coding"]
};
pests = {
0: ["dragons", "gorgons", "flesh-eating newts"],
1: ["children", "fleas", "overly adorable mice"],
2: ["sucker-upers", "fawning ministers", "shrill-voiced minstrels"],
3: ["brasseries of Great-Aunt Gwenyvere", "violent library books", "two haunted squeaky suits of armor"]
};
results = {
true: 0,
false: 0
};
inventory = {good: [], lessGood: []};
function introduction() {
console.log("\nWelcome to your new castle!!" +
"\n\nThis castle is quite wonderful with many awesome features and lots of potential." +
"\nBut unfortunately neighboring castles are suffering from various infestations " +
"\nof terrifying terrors like dragons, fawning ministers, and vicious library books! Oh no!" +
"\n\nOver the next 5 days, let's see if you survive life in your new castle." +
"\nEvery day, one new thing will be added to your castle." +
"\nIf it's a good thing, you'll have to try to keep it by creating a happy home for it." +
"\nBut if it's a bad thing, you'll have to try and get rid of it!" +
"\n\nIf at the end of the 5 days you have kept more good additions than bad ones, you'll win! " +
"\nIf not, you'll get rudely booted out of your castle, never to return!!" +
"\n\nYour castle is being built...\n\n");
}
function generateRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateMove() {
var boolean;
var addition;
var goodOrBad = generateRandomNumber(0, 10);
var location = generateRandomNumber(0, 3);
if (goodOrBad % 2 === 0) {
boolean = true;
if (!(assets[location.toString()])) {
location += 1;
}
for (var set in assets) {
if (set === location.toString()) {
var x = assets[set].length;
var subset = generateRandomNumber(0, (x - 1));
addition = assets[set].splice(subset, 1);
}
}
} else {
boolean = false;
if (!(pests[location.toString()])) {
location += 1;
}
for (var item in pests) {
if (item === location.toString()) {
var y = pests[item].length;
var subitem = generateRandomNumber(0, (y - 1));
addition = pests[item].splice(subitem, 1);
}
}
}
return [addition, boolean];
}
function generateDay(array, day) {
console.log("\nDay " + day + "...");
if (array[1]) {
console.log("You've got " + array[0] + "!!! How great!");
console.log("Let's see if the " + array[0] + " like living in your castle...");
var stayOrLeave = ((generateRandomNumber(1, 10)) % 2 === 0);
pause(3000);
if (stayOrLeave) {
console.log("Looks like the " + array[0] + " love it here! They're staying!!");
results.true += 1;
inventory.good.push(array[0][0]);
} else {
console.log("Oh no, the " + array[0] + " don't like it in this castle!" +
" They're packing up and leaving!!");
}
} else {
console.log("Oh no!! You've got " + array[0] + "!!!");
console.log("You're fighting them off--who's winning??");
var stayOrLeave = ((generateRandomNumber(1, 10)) % 2 === 0);
pause(3000);
if (stayOrLeave) {
console.log("Oh no, looks like the " + array[0] + " dug in! They're staying!!");
results.false += 1;
inventory.lessGood.push(array[0][0]);
} else {
console.log("Thank goodness, the " + array[0] + " don't like it in this castle!" +
" They're packing up and leaving!!");
}
}
}
function checkResults() {
if (results[true] >= results[false]) {
console.log("\nYesss!! You've beaten the odds and now the castle is yours!");
console.log("Live happily in your castle with these lovely and not-so-lovely denizens:");
} else {
console.log("\nOh no! You've been overrun by pests! Leave now and never come back!");
console.log("Your castle will have to figure out a way to live in peace with its denizens:");
}
console.log(inventory);
console.log("\nThanks for playing the Castle Game :)");
}
function pause(millis) {
var date = new Date();
var currentDate = null;
do { currentDate = new Date(); }
while((currentDate - date) < millis);
}
function runGame() {
introduction();
pause(13000);
for (var i=1; i < 6; i++) {
var array = generateMove(assets, pests);
pause(5000);
generateDay(array, i);
}
pause(1000);
checkResults();
}
runGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment