-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hanoi.js
76 lines (63 loc) · 1.9 KB
/
Hanoi.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var Terminal = new ActiveXObject('Terminal.Controller');
Terminal.SetPrinter(function (sControlSequence) {
WScript.StdOut.Write(sControlSequence);
});
Terminal.HideCursor();
Terminal.SetTextColor('White', 'Black');
Terminal.ClearScreen();
var PrevState = [[1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]];
PlayHanoi(4, 0, 2, DeepCopy(PrevState));
function DeepCopy(State) {
var Copy = [];
for (var i = 0; i < 3; i++) {
Copy.push(State[i].slice());
}
return Copy;
}
function DrawTower(a, b, c) {
var TowerTemplate = [
[' ©¦ ', ' ©à '],
[' ©¦ ', ' ©¤©à©¤ '],
[' ©¦ ', ' ©¤©¤©à©¤©¤ '],
[' ©¦ ', ' ©¤©¤©¤©à©¤©¤©¤ '],
['¨\¨T¨T¨T¨T¨k¨T¨T¨T¨T¨_', '¨\¨T¨T¨T¨T¨k¨T¨T¨T¨T¨_']
];
var Spaces = ' ';
var Tower = [];
for (var i = 1; i <= 4; i++) {
Tower.push(
TowerTemplate[i - 1][a[i - 1]] + Spaces +
TowerTemplate[i - 1][b[i - 1]] + Spaces +
TowerTemplate[i - 1][c[i - 1]]);
}
// Append the base
Tower.push(
TowerTemplate[4][0] + Spaces +
TowerTemplate[4][0] + Spaces +
TowerTemplate[4][0]);
return Tower;
}
function PlayHanoi(N, Src, Dst, State) {
var Aux = 3 - Src - Dst;
if (N == 1) {
State[Dst][N - 1] = 1;
State[Src][N - 1] = 0;
DrawState(N, Src, Dst, State);
return;
}
PlayHanoi(N - 1, Src, Aux, State);
State[Dst][N - 1] = 1;
State[Src][N - 1] = 0;
DrawState(N, Src, Dst, State);
PlayHanoi(N - 1, Aux, Dst, State);
}
function DrawState(N, Src, Dst, State) {
Terminal.MoveCursorTo(0, 0);
WScript.Echo('Move disk ' + N + ' from ' + (Src + 1) + ' to ' + (Dst + 1) + '.' + '\n');
WScript.Echo(DrawTower(State[0], State[1], State[2]).join('\n') + '\n');
WScript.Echo('Previous state:' + '\n');
WScript.Echo(DrawTower(PrevState[0], PrevState[1], PrevState[2]).join('\n') + '\n');
WScript.Sleep(1000);
// Save the current state
PrevState = DeepCopy(State);
}