-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzombie.js
188 lines (171 loc) · 4.84 KB
/
zombie.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
//Chanege input Here,make sure the positions of zombies and creature is in range, or an exception with an error message will be throw
//The program cannot recognize commands other than "L", "R", "U" and "D",
const input = {"gridSize": 5,
"zombie": [
{
"x": 3,
"y": 1
},
{
"x": 1,
"y": 4
}
],
"creatures": [
{
"x": 0,
"y": 1
},
{
"x": 1,
"y": 2
},
{
"x": 1,
"y": 1
},
{
"x": 4,
"y": 4
},
{
"x": 3,
"y": 0
}
],
"commands": "RDLDD"
}
//variables
let zombieList;
let creatureList;
let commands;
let zombieNum = 0;
let mapSize;
const creatureClassList = [];
const zombieClassList = []
let zombieQueue = [];
/**
*
* @param {*} input
*/
function deconstructure(input) {
commands = input.commands;
zombieList = input.zombie;
creatureList = input.creatures;
mapSize = input.gridSize;
zombieList.forEach(zombie => {
let x = zombie.x;
let y = zombie.y;
//value check;
if (x < 0 || y < 0 || x >= mapSize || y >= mapSize || mapSize <= 0) {
console.log("Wrong position input, please check the value!")
}
let zombie_ = new Zombie(x, y, commands, `zombie${zombieNum}`);
zombieNum++;
zombieClassList.push(zombie_);
zombieQueue.push(zombie_);
});
creatureList.forEach(creature => {
let x = creature.x;
let y = creature.y;
let creature_ = new Creature(x, y);
creatureClassList.push(creature_);
});
console.log("zombie num: " ,zombieClassList.length)
console.log("creature num: ", creatureClassList.length)
}
//Creature class
class Creature {
constructor(x, y) {
this.x = x;
this.y = y;
this.isInfected = false;
}
infected() {
this.isInfected = true;
}
toString() {
return `position: (${this.x}, ${this.y})`;
}
}
//Zombie class
class Zombie extends Creature {
constructor(x, y, movement, name) {
super(x, y);
this.movement = movement;
this.name = name;
this.isInfected = true;
this.isMovementEnd = false;
this.commandTable = {
"L": [-1, 0],
"R": [1, 0],
"U": [0, 1],
"D": [0, -1]
}
}
//move one step
oneStep(move) {
let newPosition = [this.x + this.commandTable[move][0], this.y + this.commandTable[move][1]];
let newP = [];
newPosition.forEach(element => {
if (element === mapSize) {
element = 0;
} else if (element === -1) {
element = mapSize - 1
}
newP.push(element);
});
this.x = newP[0];
this.y = newP[1];
}
move() {
for (let i = 0; i < this.movement.length; i++) {
this.oneStep(this.movement[i]);
console.log(`${this.name} moved to (${this.x}, ${this.y})`);
//check inflect
for (let i = 0; i < creatureClassList.length; i++) {
if (this.x === creatureClassList[i].x &&
this.y === creatureClassList[i].y &&
creatureClassList[i].isInfected === false) {
creatureClassList[i].infected();
let newZombie = new Zombie(creatureClassList[i].x, creatureClassList[i].y, commands, `zombie${zombieNum}`)
zombieNum++;
zombieClassList.push(newZombie);
zombieQueue.push(newZombie);
console.log(`${this.name} infected a creature at (${this.x}, ${this.y})`);
}
}
}
this.isMovementEnd = true;
}
//print the final position
finalPosition() {
return `${this.name} is at ${this.toString()}`
}
}
//Main function
function run() {
deconstructure(input);
let count = 0;
while (zombieQueue.length > 0) {
console.log("--------------Round: " + count + "-----------------");
zombieQueue[0].move();
zombieQueue.shift();
count++;
}
console.log("---------------------------------------")
console.log("Alive creatures: ")
creatureClassList.forEach(element => {
if (element.isInfected === false) {
console.log("A creature is alive at " + element.toString())
}
});
console.log("Number of zombies: ", zombieClassList.length);
zombieClassList.forEach(element => {
console.log(element.finalPosition());
});
}
//程序使用说明书:
//1.打开terminal,调整目录到zombie
//2.安装node之后在命令行输入node zombie.js, 结果会自动显示在terminal上。
run()