forked from AlgorithmsMeetup/TowersOfHanoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.js
53 lines (45 loc) · 1.35 KB
/
display.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var config = require("./config");
var indent = config.columns <= 3 ? " " :
config.columns === 4 ? " " :
config.columns === 5 ? " " :
config.columns === 6 ? " " :
config.columns === 7 ? " " :
config.columns === 8 ? " " : "";
var displayElem = function(idx, tower) {
var elem = tower[idx];
return typeof elem === "undefined" ? " " : elem;
};
module.exports.init = function(towers) {
console.log("");
console.log(" =======================");
console.log(" The Towers of Hanoi ");
console.log(" =======================");
console.log("");
console.log("");
module.exports.towers(towers);
};
module.exports.towers = function(towers) {
for (var i = config.pieces; i--;) {
var str = indent + towers.map(displayElem.bind(null, i)).join(" ");
console.log(str);
}
var underline = indent;
for (i = 0; i < 4*config.columns - 3; i++) {
underline += "-";
}
console.log(underline + "\n");
};
module.exports.error = function(err) {
console.error(" ERROR:", err + ".");
console.log("\n\n");
process.exit(1);
};
module.exports.victory = function() {
console.log("\n");
console.log(" VICTORY");
console.log("\n\n");
process.exit(1);
};
if (process.argv[2] === "--norender") {
module.exports.towers = function() {};
}