Node.js 讀取 Console 輸入與迴圈


因為在寫 Console 版的玩意.所以需要從 Console 取得用戶輸入.
這裡面可以用 readline 這個自帶的 modules.
不過由於需要多次跑迥圈.就出現了不停地嵌套.
簡單記錄一下主程式 (Loop method).

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var readline = require('readline');
var ChessBoard = require("./ChessBoard"),
Piece = require("./Piece"),
Position = require("./Position");

Array.prototype.containPoint = function(k) {
for(p in this) {
if(this[p].x == k.x && this[p].y == k.y) {
return true;
}
}
return false;
}

function ChineseChessBoard() {
this.playerCounter = 1,
this.playerColorInt = 0,
this.chessBoard = new ChessBoard(),

this.buf = readline.createInterface({
input: process.stdin,
output: process.stdout
});

this.loop();
}

ChineseChessBoard.prototype.loop = function() {
var self = this;
this.askHeader(function() {
self.askNormalMove(function() {
self.askTargetMove(function() {
self.movePiece(function() {
self.checkWinner(function() {
self.loop();
});
});
});
});
})
}

ChineseChessBoard.prototype.askHeader = function(next_step) {
if (this.playerCounter % 2 == 0) {
this.playerColorName = "BLACK";
this.playerColorInt = Piece.BLACK;
}else{
this.playerColorName = "RED";
this.playerColorInt = Piece.RED;
}

this.showMessage(this.chessBoard.toString());
this.showMessage("[" + this.playerColorName + "]\n");

next_step();
}

ChineseChessBoard.prototype.askNormalMove = function(next_step) {
var self = this;

this.buf.question("Please choose a chess to move (x y): ", function(answer) {
var tempPos = answer.split(" ");

if (tempPos[0] == "-1") {
process.exit(1);
}else if (tempPos.length != 2) {
self.showError("Please enter a X and Y");
self.askNormalMove(next_step);
}else if (/^[0-9]+$/.test(tempPos[0]) === false) {
self.showError("x is not a digi");
self.askNormalMove(next_step);
}else if (/^[0-9]+$/.test(tempPos[1]) === false) {
self.showError("y is not a digi");
self.askNormalMove(next_step);
}else{
self.selfX = tempPos[0];
self.selfY = tempPos[1];

if (self.chessBoard.getPieceAt(self.selfX, self.selfY) == null) {
self.showError("(" + self.selfX + "," + self.selfY + ") not a piece");
self.askNormalMove(next_step);
}else if (self.chessBoard.getPieceAt(self.selfX, self.selfY).getColor() != self.playerColorInt) {
self.showError("This is not your piece");
self.askNormalMove(next_step);
}else{
var name = self.chessBoard.getPieceAt(self.selfX, self.selfY).getName();

self.showMessage("\nYou have chosen a " + name + ", the " + name + " can move to\n");

self.availableMovement = self.chessBoard.getPieceAt(self.selfX, self.selfY).getAvailableMovement();

if (self.availableMovement.length <= 0) {
self.showError("Not position can move");
self.askNormalMove(next_step);
}else{
self.showMessage(self.availableMovement.join(" "));
next_step();
}
}
}
});
}

ChineseChessBoard.prototype.askTargetMove = function(next_step) {
var self = this;

this.buf.question("\nPlease enter the target position (x y): ", function(answer) {
var tempPos = answer.split(" ");

if (tempPos[0] == "-1") {
process.exit(1);
}else if (tempPos.length != 2) {
self.showError("Please enter a X and Y");
self.askTargetMove(next_step);
}else if (/^[0-9]+$/.test(tempPos[0]) === false) {
self.showError("x is not a digi");
self.askTargetMove(next_step);
}else if (/^[0-9]+$/.test(tempPos[1]) === false) {
self.showError("y is not a digi");
self.askTargetMove(next_step);
}else{
self.targetX = tempPos[0];
self.targetY = tempPos[1];

if (!self.availableMovement.containPoint(new Position(self.targetX, self.targetY))) {
self.showError("X and Y is can not go to (" + self.targetX + "," + self.targetY + ")");
self.askTargetMove(next_step);
}else{
next_step();
}
}
});
}

ChineseChessBoard.prototype.movePiece = function(next_step) {
this.piece = this.chessBoard.getPieceAt(this.targetX, this.targetY);
this.flag = false;
if (!this.chessBoard.isEmpty(this.targetX, this.targetY)) {
this.showMessage("*** a "+ (this.piece.getColor() == Piece.BLACK ? "BLACK" : "RED") + " "+ this.piece.getName() +" was captured ***\n\n");
this.piece.captured();
this.flag = true;
}

this.chessBoard.getPieceAt(this.selfX, this.selfY).move(this.targetX, this.targetY);

next_step();
}

ChineseChessBoard.prototype.checkWinner = function(next_step) {
if (this.flag && this.piece.getName() == "Marshal") {
this.showMessage(this.chessBoard+"\n\n");
this.showMessage("*** " + this.playerColorName + " WIN ***\n\n");
process.exit(1);
}

this.playerCounter++;

next_step();
}

ChineseChessBoard.prototype.showMessage = function(message) {
console.log(message);
}

ChineseChessBoard.prototype.showError = function(message) {
console.log("[Error]: " + message);
}

new ChineseChessBoard();