-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabrame13-RunDarwin.c++
527 lines (435 loc) · 9.42 KB
/
abrame13-RunDarwin.c++
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include <cassert> // assert
#include <cstdlib> // rand, srand
#include <iostream> // cout, endl
#include <stdexcept> // invalid_argument, out_of_range
#include <vector>
#include "darwin.h"
int main() {
// ----
// food
// ----
/*
0: left
1: go 0
*/
Species food('f');
food.addInstruction(I_LEFT);
food.addInstruction(I_GO, 0);
// ------
// hopper
// ------
/*
0: hop
1: go 0
*/
Species hopper('h');
hopper.addInstruction(I_HOP);
hopper.addInstruction(I_GO, 0);
// -----
// rover
// -----
/*
0: if_enemy 9
1: if_empty 7
2: if_random 5
3: left
4: go 0
5: right
6: go 0
7: hop
8: go 0
9: infect
10: go 0
*/
Species rover('r');
rover.addInstruction(I_ENEMY, 9);
rover.addInstruction(I_EMPTY, 7);
rover.addInstruction(I_RANDOM, 5);
rover.addInstruction(I_LEFT);
rover.addInstruction(I_GO, 0);
rover.addInstruction(I_RIGHT);
rover.addInstruction(I_GO, 0);
rover.addInstruction(I_HOP);
rover.addInstruction(I_GO, 0);
rover.addInstruction(I_INFECT);
rover.addInstruction(I_GO, 0);
// ----
// trap
// ----
/*
0: if_enemy 3
1: left
2: go 0
3: infect
4: go 0
*/
Species trap('t');
trap.addInstruction(I_ENEMY, 3);
trap.addInstruction(I_LEFT);
trap.addInstruction(I_GO, 0);
trap.addInstruction(I_INFECT);
trap.addInstruction(I_GO, 0);
// ----------
// darwin 8x8
// ----------
try {
cout << "*** Darwin 8x8 ***" << endl;
/*
8x8 Darwin
Food, facing east, at (0, 0)
Hopper, facing north, at (3, 3)
Hopper, facing east, at (3, 4)
Hopper, facing south, at (4, 4)
Hopper, facing west, at (4, 3)
Food, facing north, at (7, 7)
Simulate 5 moves.
Print every grid.
*/
Darwin game(8, 8);
game.addCreature(Creature(&food, D_EAST), 0, 0);
game.addCreature(Creature(&hopper, D_NORTH), 3, 3);
game.addCreature(Creature(&hopper, D_EAST), 3, 4);
game.addCreature(Creature(&hopper, D_SOUTH), 4, 4);
game.addCreature(Creature(&hopper, D_WEST), 4, 3);
game.addCreature(Creature(&food, D_NORTH), 7, 7);
int i;
game.print(cout);
for (i = 0; i < 5; i++) {
game.play();
game.print(cout);
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ----------
// darwin 7x9
// ----------
try {
cout << "*** Darwin 7x9 ***" << endl;
srand(0);
/*
7x9 Darwin
Trap, facing south, at (0, 0)
Hopper, facing east, at (3, 2)
Rover, facing north, at (5, 4)
Trap, facing west, at (6, 8)
Simulate 5 moves.
Print every grid.
*/
Darwin game(7, 9);
game.addCreature(Creature(&trap, D_SOUTH), 0, 0);
game.addCreature(Creature(&hopper, D_EAST), 3, 2);
game.addCreature(Creature(&rover, D_NORTH), 5, 4);
game.addCreature(Creature(&trap, D_WEST), 6, 8);
int i;
game.print(cout);
for (i = 0; i < 5; i++) {
game.play();
game.print(cout);
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ------------
// darwin 72x72
// without best
// ------------
try {
cout << "*** Darwin 72x72 without Best ***" << endl;
srand(0);
/*
Randomly place the following creatures facing randomly.
Call rand(), mod it with 5184 (72x72), and use that for the position
in a row-major order grid.
Call rand() again, mod it with 4 and use that for it's direction with
the ordering: west, north, east, south.
Do that for each kind of creature.
10 Food
10 Hopper
10 Rover
10 Trap
Simulate 1000 moves.
Print the first 10 grids (i.e. 0, 1, 2...9).
Print every 100th grid after that (i.e. 100, 200, 300...1000).
*/
Darwin game(72, 72);
int r;
int row, col;
int dir;
int i;
// place foods
for (i = 0; i < 10; i++) {
r = rand() % 5184;
row = r / 72;
col = r % 72;
dir = rand() % 4;
game.addCreature(Creature(&food, dir), row, col);
}
// place hoppers
for (i = 0; i < 10; i++) {
r = rand() % 5184;
row = r / 72;
col = r % 72;
dir = rand() % 4;
game.addCreature(Creature(&hopper, dir), row, col);
}
// place rovers
for (i = 0; i < 10; i++) {
r = rand() % 5184;
row = r / 72;
col = r % 72;
dir = rand() % 4;
game.addCreature(Creature(&rover, dir), row, col);
}
// place traps
for (i = 0; i < 10; i++) {
r = rand() % 5184;
row = r / 72;
col = r % 72;
dir = rand() % 4;
game.addCreature(Creature(&trap, dir), row, col);
}
// do the sim
game.print(cout);
for (i = 1; i <= 1000; i++){
game.play();
//print sometimes
if (i % 100 == 0 || i < 10)
game.print(cout);
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// my tests
// ------------
// darwin 100x100
// without best
// ------------
try {
cout << "*** Darwin 100x100 ***" << endl;
srand(0);
/*
Randomly place 20 of each creature
Simulate 100 moves.
Print every 10.
*/
Darwin game(100, 100);
int r;
int row, col;
int dir;
int i;
// place foods
for (i = 0; i < 20; i++) {
r = rand() % 10000;
row = r / 100;
col = r % 100;
dir = rand() % 4;
game.addCreature(Creature(&food, dir), row, col);
}
// place hoppers
for (i = 0; i < 20; i++) {
r = rand() % 10000;
row = r / 100;
col = r % 100;
dir = rand() % 4;
game.addCreature(Creature(&hopper, dir), row, col);
}
// place rovers
for (i = 0; i < 20; i++) {
r = rand() % 10000;
row = r / 100;
col = r % 100;
dir = rand() % 4;
game.addCreature(Creature(&rover, dir), row, col);
}
// place traps
for (i = 0; i < 20; i++) {
r = rand() % 10000;
row = r / 100;
col = r % 100;
dir = rand() % 4;
game.addCreature(Creature(&trap, dir), row, col);
}
// do the sim
game.print(cout);
for (i = 1; i <= 100; i++){
game.play();
//print sometimes
if (i % 10 == 0)
game.print(cout);
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ------------
// darwin 32x32
// ------------
try {
cout << "*** Darwin 32x32 ***" << endl;
srand(0);
/*
Randomly place 50 traps
then place a bunch of hoppers.
Simulate 100 moves.
Print every 10.
*/
Darwin game(32, 32);
int r;
int row, col;
int dir;
int i;
// place traps
for (i = 0; i < 50; i++) {
r = rand() % (32*32);
row = r / 32;
col = r % 32;
dir = rand() % 4;
game.addCreature(Creature(&trap, dir), row, col);
}
// place hoppers
for (i = 0; i < 50; i++) {
r = rand() % (32*32);
row = r / 100;
col = r % 100;
dir = rand() % 4;
game.addCreature(Creature(&hopper, dir), row, col);
}
// do the sim
game.print(cout);
for (i = 1; i <= 100; i++){
game.play();
//print sometimes
if (i % 10 == 0)
game.print(cout);
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ------------
// darwin 2x2
// ------------
try {
cout << "*** Darwin 2x2 ***" << endl;
srand(0);
/*
try to place 1000 creatures
there should only be 4 in the end
print the result
*/
Darwin game(2, 2);
int r;
int row, col;
int dir;
int i;
// place traps
for (i = 0; i < 1000; i++) {
r = rand() % 4;
row = r / 2;
col = r % 2;
dir = rand() % 4;
game.addCreature(Creature(&trap, dir), row, col);
}
// print the sim
game.print(cout);
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ------------
// darwin 1x50
// ------------
try {
cout << "*** Darwin 1x50 ***" << endl;
srand(0);
/*
place 5 rovers and 5 hoppers in a line
sim 20 times
print every 5
*/
Darwin game(1, 50);
int i;
// place rovers
for (i = 0; i < 5; i++) {
game.addCreature(Creature(&rover, D_EAST), 0, 10* i);
}
// place hoppers
for (i = 0; i < 5; i++) {
game.addCreature(Creature(&hopper, D_SOUTH), 0, 10* i + 5);
}
// do the sim
game.print(cout);
for (i = 1; i <= 20; i++) {
game.play();
if (i % 5 == 0) game.print(cout);
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ------------
// darwin 1x10
// ------------
try {
cout << "*** Darwin 1x10 ***" << endl;
srand(0);
/*
place a trap on the left and a line of hoppers to the right
sim 1 turn
everything should be a trap
*/
Darwin game(1, 10);
game.addCreature(Creature(&trap, D_EAST), 0, 0);
game.addCreature(Creature(&hopper, D_EAST), 0, 1);
game.addCreature(Creature(&hopper, D_EAST), 0, 2);
game.addCreature(Creature(&hopper, D_EAST), 0, 3);
game.addCreature(Creature(&hopper, D_EAST), 0, 4);
game.addCreature(Creature(&hopper, D_EAST), 0, 5);
game.addCreature(Creature(&hopper, D_EAST), 0, 6);
game.addCreature(Creature(&hopper, D_EAST), 0, 7);
game.addCreature(Creature(&hopper, D_EAST), 0, 8);
game.addCreature(Creature(&hopper, D_EAST), 0, 9);
// do the sim
game.print(cout);
game.play();
game.print(cout);
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ------------
// darwin 1x10
// ------------
try {
cout << "*** Darwin 1x10 ***" << endl;
srand(0);
/*
2 hoppers sit in the middle unable to move for all of time
print the intial and after 10th turn
*/
Darwin game(1, 10);
game.addCreature(Creature(&hopper, D_EAST), 0, 4);
game.addCreature(Creature(&hopper, D_WEST), 0, 5);
// do the sim
game.print(cout);
for (int i = 0; i < 10; i++) game.play();
game.print(cout);
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
return 0;
}