I'm learning plain JavaScript on the command line, using Node.js to execute my scripts.
I've been trying to rewrite a simple Ruby implementation of 'Towers of Hanoi'.
Here is some of my code so far:
function Tower() {
this.rods = [[8, 7, 6, 5, 4, 3, 2, 1], [], []];
this.gameOver = false;
}
Tower.prototype.showTowers = function () {
var i;
for (i = 0; i < 3; i++) {
console.log(i + 1 + ': ' + this.rods[i]);
}
};
Tower.prototype.moveRod = function (from, to) {
...
}
I am struggling with writing the play loop. I think the issue is that I'm not familiar with asynchronous functions. My play loop pseudocode is something like:
until game.win? {
game.show_towers
until valid_move?(move) {
move = request_move
}
make_move(move)
}
game.congrats
I've tried prompt and readline. I'm assuming neither are 'blocking' because in both cases my while loop infinitely cycles through user input requests repeatedly without stopping.
Any advice?
Thanks.