-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-3.js
245 lines (138 loc) · 7.18 KB
/
1-3.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
//////////////////////////////////////////// Car Constructor/////////////////////////////////
function Car () {
this.name,
this.position,
this.turn,
this.kicked = false;
};
//////////////////////////////////////////// Functions /////////////////////////////////
//making number of moves for each car
function moveNumber ()
{
return (Math.floor(Math.random() * (10 - 1 + 1) + 1)); //randomize formula between '1' and '10' for moving
}
//move cars
function moveCar (car, index, prevCarPosition)
{
if (map[car[index].position] === "* ") //if there is no car in destination, then move the car
{
map[prevCarPosition] = "* "; //putting '*' instead of previous 'car[index].position'
map[car[index].position] = car[index].name + " "; //put the car in its new position
}
else //if there is a car in destination
for (let j = 0; j < carNum; j++)
{
//if an accident happened
if (map[car[index].position] === (car[j].name + " ") || map[car[index].position] === ("#" + car[j].name + "# "))
{
map[prevCarPosition] = "* "; //putting '*' instead of previous 'car[index].position'
map[car[j].position] = "#" + car[index].name + "# ";
// '#' means that this car has kicked another car in this position
car[j].position = 0; //change kicked 'car' position to start
car[j].kicked = true; // shows that this 'car' has been kicked
break; //don't continue the loop for all cars
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////// primitive information ///////////////////////////
//getting the number of cars from the user
let carNum = +prompt("Enter the number of cars:");
let car = []; //container for cars' information
//make 'carNum' number of cars
for (let i = 0; i < carNum; i++)
car[i] = new Car();
//getting car names from the user
for (let i = 0; i < carNum; i++)
car[i].name = prompt(`Enter the Name of Car ${i + 1}`);
//making a map with 100 '*'s
let map = [];
for (let i = 0; i < 100; i++)
map[i] = '* '
////////////////////////////// Start Positioning ///////////////////////////////
let testRandom; //for testing unequl random-number cerated
let randomNum = []; //container of random-numbers created
let carPosition = []; //container for car positions
//initializing the first position of cars
for (let i = 0; randomNum.length < carNum; i++)
{
testRandom = Math.floor(Math.random() * ((carNum - 1) - 0 + 1) + 0) //randomize formula between '0' and 'last position'
if (randomNum.indexOf(testRandom) === -1) //if 'testRandom' does not exist in 'randomNum'
randomNum.push(testRandom);
}
//assigning random numbers to car posiotions for start
for (let i = 0; i < carNum; i++)
car[i].position = randomNum[i];
//show the start position of the cars
for (let i = 0; i < carNum; i++)
console.log(`Start Position of Car '${car[i].name}': ${car[i].position}`);
//positioning the start position of cars
for (let i = 0; i < carNum; i++)
map[car[i].position] = car[i].name + " ";
console.log(map.join(''));
///////////////////////////// giving a reandom 'turn' to each car ////////////////////////////////
randomNum = []; //clearing 'randomNum' Array for another use
//unequal reandom 'turn' maker
for (let i = 0; randomNum.length < carNum; i++)
{
testRandom = Math.floor(Math.random() * (carNum - 1 + 1) + 1) //randomize formula between 'first car' and 'last car'
if (randomNum.indexOf(testRandom) === -1) //if 'testRandom' does not exist in 'randomNum'
randomNum.push(testRandom);
}
//giving a turn to each car
for (let i = 0; i < carNum; i++)
car[i].turn = randomNum[i];
//show the turn of each car
for (let i = 0; i < carNum; i++)
console.log(`Turn of Car '${car[i].name}': ${car[i].turn}`);
////////////////////////////// moving ///////////////////////////////
let prevCarPosition; //a container for position of cars before moving
let ranking = []; //an Array for containing Winners
//move cars
while (ranking.length < carNum) //while there is a car that hasn't finished the game
{
for (let turn = 1; turn <= carNum; turn++) //move the cars in turn
{
for (let i = 0; i < carNum; i++)
{
if (car[i].turn === turn) //find the car which is its 'turn'
{
//if the 'car' has been kicked by another 'car', don't move in this round
if (car[i].kicked === true)
{
car[i].kicked = false; //in this round, don't move and just clear 'car.kicked' for next round
console.log("not Allowed to move in this Round.");
break; //don't continue the loop for all cars. because when the car with 'turn' needed is found,
//other cars don't need to be checked by that 'turn'
}
else
{
prevCarPosition = car[i].position; // 'prevCarPosition' = current 'car.position'
car[i].position += moveNumber(); //chage its position
//put '***' in front of 'car' name when a 'car' finishes the game
if (car[i].position > 99)
console.log(`'${car[i].name}' position: `, `${car[i].position}***`);
//print 'car' name and its 'position'
else
console.log(`'${car[i].name}' position: `, car[i].position);
//if the car passed the last home (if won the game)
if (car[i].position > 99)
{
ranking.push(car[i].name);
map[prevCarPosition] = "* "; //remove the car from the map
car[i].turn = 0; //*** remove the car from the loop ('turn's start from 1) ***
}
else
moveCar(car, i, prevCarPosition);
break; //don't continue the loop for all cars. because when the car with 'turn' needed is found,
//other cars don't need to be checked by that 'turn'
}
}
}
}
console.log(map.join(''));
}
////////////////////////////// Announcing the Ranking ///////////////////////////////
console.log(`Winner is: '${ranking[0]}'`);
for (let i = 1; i <ranking.length; i++)
console.log(`Rank ${i + 1} is for: '${ranking[i]}'`);