-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathczhang-RunDarwin.c++
693 lines (583 loc) · 15.4 KB
/
czhang-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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// -----------------------------
// projects/darwin/RunDarwin.c++
// Copyright (C) 2013
// Glenn P. Downing
// -----------------------------
/*
To run the program:
% g++ -pedantic -std=c++0x -Wall Darwin.c++ RunDarwin.c++ -o RunDarwin
% valgrind RunDarwin > RunDarwin.out
To configure Doxygen:
doxygen -g
That creates the file Doxyfile.
Make the following edits:
EXTRACT_ALL = YES
EXTRACT_PRIVATE = YES
EXTRACT_STATIC = YES
GENERATE_LATEX = NO
To document the program:
doxygen Doxyfile
*/
// --------
// includes
// --------
#include <cassert> // assert
#include <cstdlib> // rand, srand
#include <iostream> // cout, endl
#include <stdexcept> // invalid_argument, out_of_range
#include "Darwin.h"
// ----
// main
// ----
int main () {
using namespace std;
// ----
// food
// ----
/*
0: left
1: go 0
*/
Species food_species("f");
food_species.add_instruction(Instruction::LEFT);
food_species.add_instruction(Instruction::GO, 0);
Creature food(food_species);
// ------
// hopper
// ------
/*
0: hop
1: go 0
*/
Species hopper_species("h");
hopper_species.add_instruction(Instruction::HOP);
hopper_species.add_instruction(Instruction::GO, 0);
Creature hopper(hopper_species);
// -----
// 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_species("r");
rover_species.add_instruction(Instruction::IF_ENEMY, 9);
rover_species.add_instruction(Instruction::IF_EMPTY, 7);
rover_species.add_instruction(Instruction::IF_RANDOM, 5);
rover_species.add_instruction(Instruction::LEFT);
rover_species.add_instruction(Instruction::GO, 0);
rover_species.add_instruction(Instruction::RIGHT);
rover_species.add_instruction(Instruction::GO, 0);
rover_species.add_instruction(Instruction::HOP);
rover_species.add_instruction(Instruction::GO, 0);
rover_species.add_instruction(Instruction::INFECT);
rover_species.add_instruction(Instruction::GO, 0);
Creature rover(rover_species);
// ----
// trap
// ----
/*
0: if_enemy 3
1: left
2: go 0
3: infect
4: go 0
*/
Species trap_species("t");
trap_species.add_instruction(Instruction::IF_ENEMY, 3);
trap_species.add_instruction(Instruction::LEFT);
trap_species.add_instruction(Instruction::GO, 0);
trap_species.add_instruction(Instruction::INFECT);
trap_species.add_instruction(Instruction::GO, 0);
Creature trap(trap_species);
// ----
// best
// ----
/*
0: if_wall 3
1: if_empty 7
2: if_enemy 5
3: left
4: go 0
5: infect
6: go 3
7: hop
8: go 2
*/
Species best_species("b");
best_species.add_instruction(Instruction::IF_WALL, 3);
best_species.add_instruction(Instruction::IF_EMPTY, 7);
best_species.add_instruction(Instruction::IF_ENEMY, 5);
best_species.add_instruction(Instruction::LEFT);
best_species.add_instruction(Instruction::GO, 0);
best_species.add_instruction(Instruction::INFECT);
best_species.add_instruction(Instruction::GO, 3);
best_species.add_instruction(Instruction::HOP);
best_species.add_instruction(Instruction::GO, 2);
Creature best(best_species);
// ----------
// darwin 8x8
// ----------
try {
cout << "*** Darwin 8x8 ***" << endl;
// 8x8 Darwin
Darwin darwin_1(8, 8);
// Food, facing east, at (0, 0)
darwin_1.add_creature(food, 0, 0, Direction::EAST);
// Hopper, facing north, at (3, 3)
darwin_1.add_creature(hopper, 3, 3, Direction::NORTH);
// Hopper, facing east, at (3, 4)
darwin_1.add_creature(hopper, 3, 4, Direction::EAST);
// Hopper, facing south, at (4, 4)
darwin_1.add_creature(hopper, 4, 4, Direction::SOUTH);
// Hopper, facing west, at (4, 3)
darwin_1.add_creature(hopper, 4, 3, Direction::WEST);
// Food, facing north, at (7, 7)
darwin_1.add_creature(food, 7, 7, Direction::NORTH);
// Simulate 5 moves.
// Print every grid.
darwin_1.print_out();
for (int i = 0; i < 5; ++i)
{
darwin_1.step();
darwin_1.print_out();
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ----------
// darwin 7x9
// ----------
try {
cout << "*** Darwin 7x9 ***" << endl;
srand(0);
// 7x9 Darwin
Darwin darwin_2(7, 9);
// Trap, facing south, at (0, 0)
darwin_2.add_creature(trap, 0, 0, Direction::SOUTH);
// Hopper, facing east, at (3, 2)
darwin_2.add_creature(hopper, 3, 2, Direction::EAST);
// Rover, facing north, at (5, 4)
darwin_2.add_creature(rover, 5, 4, Direction::NORTH);
// Trap, facing west, at (6, 8)
darwin_2.add_creature(trap, 6, 8, Direction::WEST);
// Simulate 5 moves.
// Print every grid.
darwin_2.print_out();
for (int i = 0; i < 5; ++i)
{
darwin_2.step();
darwin_2.print_out();
}
}
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);
Darwin darwin_3(72, 72);
// 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
for (int i = 0; i < 4; ++i)
{
Creature c = food;
switch(i)
{
case 0:
c = food;
break;
case 1:
c = hopper;
break;
case 2:
c = rover;
break;
case 3:
c = trap;
break;
}
for (int j = 0; j < 10; ++j)
{
int full_pos = rand() % 5184;
int x = full_pos % 72;
int y = full_pos / 72;
int full_dir = rand() % 4;
Direction direction;
switch(full_dir)
{
case 0:
direction = Direction::WEST;
break;
case 1:
direction = Direction::NORTH;
break;
case 2:
direction = Direction::EAST;
break;
case 3:
direction = Direction::SOUTH;
break;
}
darwin_3.add_creature(c, x, y, direction);
}
}
// 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_3.print_out();
for (;darwin_3.get_turn_number() < 1000;)
{
darwin_3.step();
if (darwin_3.get_turn_number() < 10 || !(darwin_3.get_turn_number() % 100))
{
darwin_3.print_out();
}
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
// ------------
// darwin 72x72
// with best
// ------------
try {
cout << "*** Darwin 72x72 with Best ***" << endl;
srand(0);
Darwin darwin_4(72, 72);
/*
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: 0:west, 1:north, 2:east, 3:south.
Do that for each kind of creature.
10 Food
10 Hopper
10 Rover
10 Trap
10 Best
Simulate 1000 moves.
Best MUST outnumber ALL other species for the bonus pts.
Print the first 10 grids (i.e. 0, 1, 2...9).
Print every 100th grid after that (i.e. 100, 200, 300...1000).
*/
for (int i = 0; i < 5; ++i)
{
Creature c = food;
switch(i)
{
case 0:
c = food;
break;
case 1:
c = hopper;
break;
case 2:
c = rover;
break;
case 3:
c = trap;
break;
case 4:
c = best;
break;
}
for (int j = 0; j < 10; ++j)
{
int full_pos = rand() % 5184;
int x = full_pos % 72;
int y = full_pos / 72;
int full_dir = rand() % 4;
Direction direction;
switch(full_dir)
{
case 0:
direction = Direction::WEST;
break;
case 1:
direction = Direction::NORTH;
break;
case 2:
direction = Direction::EAST;
break;
case 3:
direction = Direction::SOUTH;
break;
}
darwin_4.add_creature(c, x, y, direction);
}
}
// 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_4.print_out();
for (;darwin_4.get_turn_number() < 1000;)
{
darwin_4.step();
if (darwin_4.get_turn_number() < 10 || !(((int)darwin_4.get_turn_number()) % 100))
{
darwin_4.print_out();
}
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
try {
cout << "*** Darwin Random with Best ***" << endl;
srand(0);
// Create a random size grid where height < 150 and width < 150
// Fill with a random number of each creature (include best), of at most 50 of that creature
// Place at random location
// Run 1000x, printing at same places as before
int height = rand() % 150;
int width = rand() % 150;
Darwin darwin_4(height, width);
for (int i = 0; i < 5; ++i)
{
Creature c = food;
switch(i)
{
case 0:
c = food;
break;
case 1:
c = hopper;
break;
case 2:
c = rover;
break;
case 3:
c = trap;
break;
case 4:
c = best;
break;
}
int num_c = rand() % 50;
for (int j = 0; j < num_c; ++j)
{
int full_pos = rand() % (height * width);
int x = full_pos % width;
int y = full_pos / width;
int full_dir = rand() % 4;
Direction direction;
switch(full_dir)
{
case 0:
direction = Direction::WEST;
break;
case 1:
direction = Direction::NORTH;
break;
case 2:
direction = Direction::EAST;
break;
case 3:
direction = Direction::SOUTH;
break;
}
darwin_4.add_creature(c, y, x, direction);
}
}
// 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_4.print_out();
for (;darwin_4.get_turn_number() < 1000;)
{
darwin_4.step();
if (darwin_4.get_turn_number() < 10 || !(((int)darwin_4.get_turn_number()) % 100))
{
darwin_4.print_out();
}
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);
}
try {
cout << "*** Darwin Random without Best ***" << endl;
srand(0);
// Create a random size grid where height < 150 and width < 150
// Fill with a random number of each creature (excluding best), of at most 50 of that creature
// Place at random location
// Run 1000x, printing at same places as before
int height = rand() % 150;
int width = rand() % 150;
Darwin darwin_4(height, width);
for (int i = 0; i < 4; ++i)
{
Creature c = food;
switch(i)
{
case 0:
c = food;
break;
case 1:
c = hopper;
break;
case 2:
c = rover;
break;
case 3:
c = trap;
break;
}
int num_c = rand() % 50;
for (int j = 0; j < num_c; ++j)
{
int full_pos = rand() % (height * width);
int x = full_pos % width;
int y = full_pos / width;
int full_dir = rand() % 4;
Direction direction;
switch(full_dir)
{
case 0:
direction = Direction::WEST;
break;
case 1:
direction = Direction::NORTH;
break;
case 2:
direction = Direction::EAST;
break;
case 3:
direction = Direction::SOUTH;
break;
}
darwin_4.add_creature(c, y, x, direction);
}
}
// 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_4.print_out();
for (;darwin_4.get_turn_number() < 1000;)
{
darwin_4.step();
if (darwin_4.get_turn_number() < 10 || !(((int)darwin_4.get_turn_number()) % 100))
{
darwin_4.print_out();
}
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);
}
try {
cout << "*** BChean's 2x2 (#1) ***" << endl;
Darwin darwin(2, 2);
darwin.add_creature(rover, 0, 0, Direction::EAST);
darwin.add_creature(rover, 1, 1, Direction::SOUTH);
darwin.add_creature(trap, 0, 1, Direction::EAST);
darwin.add_creature(trap, 1, 0, Direction::WEST);
darwin.print_out();
for (int i = 0; i < 4; ++i)
{
darwin.step();
darwin.print_out();
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
try {
cout << "*** BChean's 4x12 (#5) ***" << endl;
Darwin darwin(12, 4);
darwin.add_creature(food, 4, 0, Direction::SOUTH);
darwin.add_creature(food, 5, 0, Direction::SOUTH);
darwin.add_creature(food, 6, 0, Direction::SOUTH);
darwin.add_creature(food, 7, 0, Direction::SOUTH);
darwin.add_creature(rover, 0, 0, Direction::EAST);
darwin.add_creature(rover, 0, 1, Direction::EAST);
darwin.add_creature(rover, 11, 0, Direction::WEST);
darwin.add_creature(rover, 11, 1, Direction::WEST);
darwin.add_creature(trap, 4, 2, Direction::NORTH);
darwin.add_creature(trap, 5, 2, Direction::NORTH);
darwin.add_creature(trap, 6, 2, Direction::NORTH);
darwin.add_creature(trap, 7, 2, Direction::NORTH);
darwin.add_creature(trap, 4, 3, Direction::NORTH);
darwin.add_creature(trap, 5, 3, Direction::NORTH);
darwin.add_creature(trap, 6, 3, Direction::NORTH);
darwin.add_creature(trap, 7, 3, Direction::NORTH);
darwin.print_out();
for (int i = 0; i < 28; ++i)
{
darwin.step();
darwin.print_out();
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
try {
cout << "*** NLaz's 5x5 ***" << endl;
Darwin darwin(5, 5);
darwin.add_creature(trap, 4, 0, Direction::WEST);
darwin.add_creature(hopper, 4, 1, Direction::NORTH);
darwin.add_creature(hopper, 4, 2, Direction::EAST);
darwin.add_creature(hopper, 4, 3, Direction::SOUTH);
darwin.add_creature(food, 4, 4, Direction::NORTH);
for (int i = 0; i < 5; ++i)
{
darwin.step();
darwin.print_out();
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
try {
cout << "*** Josh2Xg 5x5 ***" << endl;
Darwin darwin(5, 5);
darwin.add_creature(trap, 0, 0, Direction::SOUTH);
darwin.add_creature(hopper, 0, 1, Direction::EAST);
darwin.add_creature(rover, 1, 0, Direction::NORTH);
darwin.add_creature(trap, 1, 1, Direction::WEST);
for (int i = 0; i < 5; ++i)
{
darwin.step();
darwin.print_out();
}
}
catch (const invalid_argument&) {
assert(false);}
catch (const out_of_range&) {
assert(false);}
return 0;
}