-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
318 lines (308 loc) · 9.35 KB
/
server.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const readline = require('readline');
var io = require('socket.io')();
io.on('connection', (socket)=>{
console.log("A user has been connected.");
io.emit("message", "Connected");
socket.on('start', (data)=>{
io.emit("message", "Start Cleaning");
console.log(`Start cleaning at ${data.time}`);
Robot.getInstance().run();
})
});
io.listen(8080);
console.log('listening on 8080');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
/* const MAPSTRING =
"##########\n"+
"# #\n"+
"#### #\n"+
"# #\n"+
"#### #\n"+
"# #\n"+
"##########\n"; */
const MAPSTRING =
"##############\n"+
"# ## # #\n"+
"# # # # ## # #\n"+
"# # # #\n"+
"# # ### ## # #\n"+
"# # #\n"+
"# # ### ## # #\n"+
"# # # #\n"+
"# # ## ## # #\n"+
"# #\n"+
"##############\n";
class Robot {
constructor(v, h){
this.v = v;
this.h = h;
console.log("The robot is established at ( vertical: " + v + ", horizental: " +h +")");
this.clean();
this.route = new Array();
}
static initInstance(v, h){
if(!this.instance){
this.instance = new Robot(v, h);
}
return this.instance;
}
static getInstance(){
return this.instance;
}
getPosition(){
return [this.v, this.h];
}
clean(){
Map.getInstance().mapCharset[this.v][this.h] = '-';
}
showPosition(){
Map.getInstance().mapCharset[this.v][this.h] = '*';
}
moveRight(){
const max = Map.getInstance().mapCharset[this.v].length-1;
if(this.h === max){ // Robot is at the very right point
return false; // Fail to move right
}
if( Map.getInstance().mapCharset[this.v][this.h+1] === '#'){
// Wall to robot's right
return false; // Fail to move right
}
this.h += 1;
this.clean();
return true;
}
moveLeft(){
const min = 0
if(this.h === min){ // Robot is at the very left point
return false; // Fail to move left
}
if( Map.getInstance().mapCharset[this.v][this.h-1] === '#'){
// Wall to robot's left
return false; // Fail to move left
}
this.h -= 1;
this.clean();
return true;
}
moveUp(){
const min = 0
if(this.v === min){ // Robot is at the very top point
return false; // Fail to move up
}
if( Map.getInstance().mapCharset[this.v-1][this.h] === '#'){
// Wall to robot's top
return false; // Fail to move up
}
this.v -= 1;
this.clean();
return true;
}
moveDown(){
const max = Map.getInstance().mapCharset.length-1;
if(this.v === max){ // Robot is at the very bottom point
return false; // Fail to move down
}
if( Map.getInstance().mapCharset[this.v+1][this.h] === '#'){
// Wall to robot's bottom
return false; // Fail to move down
}
this.v += 1;
this.clean();
return true;
}
isTopWallorEdge(){
var result = false;
// if edge
if(this.v-1 === 0) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v-1][this.h] === '#') return true;
return result;
}
isBottomWallorEdge(){
var result = false;
// if edge
if(this.v+1 === Map.getInstance().mapCharset.length) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v+1][this.h] === '#') return true;
return result;
}
isLeftWallorEdge(){
var result = false;
// if edge
if(this.h-1 === 0) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v][this.h-1] === '#') return true;
return result;
}
isRightWallorEdge(){
var result = false;
// if edge
if(this.h+1 === Map.getInstance().mapCharset[this.v].length) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v][this.h+1] === '#') return true;
return result;
}
//if it's edge, return true(clean)
isTopClean(){
var result = false;
// if edge
if(this.v-1 === 0) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v-1][this.h] === '-') return true;
return result;
}
isBottomClean(){
var result = false;
// if edge
if(this.v+1 === Map.getInstance().mapCharset.length) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v+1][this.h] === '-') return true;
return result;
}
isLeftClean(){
var result = false;
// if edge
if(this.h-1 === 0) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v][this.h-1] === '-') return true;
return result;
}
isRightClean(){
var result = false;
// if edge
if(this.h+1 === Map.getInstance().mapCharset[this.v].length) return true;
// if is wall
if(Map.getInstance().mapCharset[this.v][this.h+1] === '-') return true;
return result;
}
run(){
let startTime = Date.now();
var interval = setInterval(()=>{
//6. if everywhere is cleaned, end
//1. detect top, move if not clean and movable
//2. detect right, move if not clean and movable
//3. detect bottom move if not clean and movable
//4. detect left move if not clean and movable
//5. backward
if(Map.getInstance().getNumberOfClean() === Map.getInstance().totalSpace){
this.clean();
clearInterval(interval);
let endTime = Date.now();
let timeDuration = endTime - startTime;
console.log("Cleaning task is done.");
console.log(Map.getInstance().mapCharset);
io.emit('finish', {
timeDuration: timeDuration*0.001,
msg: "Task complete."
})
return;
}
if(!this.isTopWallorEdge() && !this.isTopClean()){
this.move(1); this.route.push(-1); return;
}
if(!this.isRightWallorEdge() && !this.isRightClean()){
this.move(2); this.route.push(-2); return;
}
if(!this.isBottomWallorEdge() && !this.isBottomClean()){
this.move(-1); this.route.push(1); return;
}
if(!this.isLeftWallorEdge() && !this.isLeftClean()){
this.move(-2); this.route.push(2); return;
}
// backward
this.move(this.route.pop());
//console.log('move');
},200);
}
move(direction){
console.log('moving...' + direction);
// 1: top
// 2: right
// -1 down
// -2 left
switch(direction){
case 1:
this.moveUp();break;
case 2:
this.moveRight();break;
case -1:
this.moveDown();break;
case -2:
this.moveLeft();break;
default: break;
}
//console.log(Map.getInstance().mapCharset);
console.log("Progress: " + Map.getInstance().getNumberOfClean() + "/" + Map.getInstance().totalSpace);
io.emit('data refresh', {
charMap: Map.getInstance().mapCharset,
position: Robot.getInstance().getPosition(),
totalSpace: Map.getInstance().totalSpace,
cleanSpace: Map.getInstance().getNumberOfClean()
})
}
}
class Map {
// Singleton
constructor(mapstring){
var charArray = mapstring.split('');
var buffer = new Array();
var spaceCount = 0;
this.mapCharset = new Array();
charArray.forEach(element => {
if(element !== '\n'){
if(element === ' ')
spaceCount++;
buffer.push(element);
}
else {
this.mapCharset.push(buffer);
buffer = [];
}
});
this.totalSpace = spaceCount;
}
static initInstance(mapstring) {
if(!this.instance) {
this.instance = new Map(mapstring);
}
return this.instance;
}
static getInstance() {
return this.instance;
}
getNumberOfClean(){
var count = 0;
for(i=0; i<this.mapCharset.length; i++){
this.mapCharset[i].forEach(element => {
if(element === '-'){
count++;
}
});
}
return count;
}
getElement(v, h){
return this.mapCharset[v][h];
}
cleanElement(v, h) {
this.mapCharset[v][h] = '-';
}
}
console.log(MAPSTRING);
// Initialize a map
Map.initInstance(MAPSTRING);
// Initialize a robot in a resonable radom postion;
var i = 0, j = 0;
while ( Map.getInstance().getElement(i, j) !== ' ' ){
i = Math.floor(Math.random() * (Map.getInstance().mapCharset.length - 0) );
j = Math.floor(Math.random() * (Map.getInstance().mapCharset[i].length - 0) );
}
Robot.initInstance(i, j);
/*
rl.question('Press any button to start cleaning.', ()=>{
Robot.getInstance().run();
}) */
/* Robot.getInstance().run(); */