-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtowersOfHanoi.Js
78 lines (60 loc) · 1.76 KB
/
towersOfHanoi.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
77
78
(function (root){
var Hanoi = root.Hanoi = (root.Hanoi || {});
var readline = require('readline');
var reader = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// BOARD class
function Board() {
};
Board.prototype.create = function() {
this.towers = [[ 1, 2, 3, 4, 5], [], []];
console.log(this.towers);
};
Board.prototype.validMove = function(fromCol, toCol) {
console.log(this.towers);
return (this.towers[toCol] || this.towers[fromCol] ||
this.towers[fromCol][0] < this.towers[toCol][0]);
};
Board.prototype.movePiece = function(fromCol, toCol) {
if (this.validMove(fromCol, toCol)) {
var disk = this.towers[fromCol].shift();
console.log(toCol);
this.towers[toCol].unshift(disk);
console.log(this.towers);
};
};
// GAME class
var Game = Hanoi.Game = function() {
this.setupGame();
};
Game.prototype.setupGame = function() {
this.board = new Board();
this.board.create();
console.log("BOARD IN SETUP" + this.board)
}
Game.prototype.makeMove = function() {
var self = this;
reader.question("Enter move in integers (ex: from, to)", function(move){
var move = move.split(',')
var userFrom = parseInt(move[0]);
var userTo = parseInt(move[1]);
if (self.board.validMove(userFrom, userTo)){
self.board.movePiece(userFrom, userTo);
if (userTo !== 0 && self.board.towers[userTo].length === 5) {
// game over
console.log("you win!")
reader.close();
} else {
self.makeMove();
};
} else {
console.log("Invalid move, try again");
self.makeMove();
}
});
};
})(this);
var game = new this.Hanoi.Game();
game.makeMove();