Created
August 18, 2015 21:11
-
-
Save Johnicholas/dc5a32101752660d5ba8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a hub world corresponds to a partial order on levels | |
// | |
// an example play sequence is: | |
// 1. the player starts in the hub world, | |
// - which several open (or unlocked) doors and several closed (or locked) doors | |
// 2. the player chooses an open door, and plays through a level, and fails | |
// 3. the player is returned to the hub world | |
// 4. the player chooses the same open door, plays through the same level, and succeeds | |
// 5. the player is returned to the hub world, but there is a change: | |
// - the door they went through may be open or closed but it is definitely marked differently | |
// - one or more doors that were closed (locked) have become open | |
// | |
function PartialOrder(items) { | |
this.items = items; | |
this.done = {}; | |
} | |
PartialOrder.prototype.open = function PartialOrder_open(door) { | |
if (!this.items[door]) { | |
return false; | |
} | |
if (this.done[door]) { | |
return false; // return true here if you prefer doors to stay open for replaying | |
} | |
var prereqs = this.items[door].prereqs; | |
for (var i = 0; i < prereqs.length; i += 1) { | |
var prereq = prereqs[i]; | |
if (!this.done[prereq]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
// here's an example of using this class, in a node.js context | |
po = new PartialOrder({ | |
a: { prereqs: [] }, | |
b: { prereqs: [] }, | |
c: { prereqs: ['a'] }, | |
d: { prereqs: ['a', 'b'] }, | |
e: { prereqs: ['c'] }, | |
f: { prereqs: ['d'] }, | |
g: { prereqs: ['e', 'f'] }, | |
h: { prereqs: ['f'] } | |
}); | |
for (var count_done = 0; count_done < process.argv.length - 2; count_done += 1) { | |
console.log('Here are your options:') | |
for (var k in po.items) { | |
if (po.open(k)) { | |
console.log(k); | |
} | |
} | |
var it = process.argv[count_done + 2]; | |
console.log('btw, it is ' + it); | |
if (po.open(it)) { | |
po.done[it] = true; | |
} else { | |
console.log('ERR: ' + it + ' is closed!'); | |
} | |
} | |
console.log('all done!') | |
// an example of running this demo: | |
// | |
// $ js hub.js a b c d e f g h | |
// here are your options: | |
// a | |
// b | |
// btw, it is a | |
// here are your options: | |
// b | |
// c | |
// btw, it is b | |
// here are your options: | |
// c | |
// d | |
// btw, it is c | |
// here are your options: | |
// d | |
// e | |
// btw, it is d | |
// here are your options: | |
// e | |
// f | |
// btw, it is e | |
// here are your options: | |
// f | |
// btw, it is f | |
// here are your options: | |
// g | |
// h | |
// btw, it is g | |
// here are your options: | |
// h | |
// btw, it is h | |
// all done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment