-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnt.cpp
executable file
·640 lines (579 loc) · 20.1 KB
/
Ant.cpp
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
#include <iostream>
#include <fstream>
#include <random>
#include <algorithm>
#include <map>
#include <boost/process.hpp>
#include <stdio.h>
#include <unistd.h>
#include <cstdlib>
#include <signal.h>
#include "cxxopts.hpp"
#define _clear() printf("\033[H\033[J")
#define _disable_cursor() printf("\033[?25l")
#define _enable_cursor() printf("\e[?25h")
#define _gotoXY(x,y) printf("\033[%d;%dH", (y), (x))
#define _set_XY(x,y,s) printf("\033[%d;%dH%s", (y), (x), (s))
#define INIT_X 2
#define INIT_Y 3
#define EMPTY_SYMBOL " "
#define BORDER_HORIZONTAL_SYMBOL "═"
#define BORDER_VERTICAL_SYMBOL "║"
#define BORDER_CORNER_LU_SYMBOL "╔"
#define BORDER_CORNER_RU_SYMBOL "╗"
#define BORDER_CORNER_LD_SYMBOL "╚"
#define BORDER_CORNER_RD_SYMBOL "╝"
#define UNIVERSAL_SYMBOL "氣"
#define ANT_SYMBOL "O"
#define DOODLEBUG_SYMBOL "X"
#define PLANT_SYMBOL "▓"
#define SPECIES_NUM 3
#define ANT_REPRODUCE_CYCLE 7
//#define DOODLEBUG_REPRODUCE_CYCLE 8
#define DOODLEBUG_REPRODUCE_CYCLE 212
#define PLANT_REPRODUCE_CYCLE 28
//#define DOODLEBUG_HP 3
#define DOODLEBUG_HP 210
#define ANT_HP 7
#define PLANT_HP 2
class Space;
using namespace std;
class Info {
public:
Info() {
boost::process::system("rm info.txt");
out.open("info.txt", ios::app);
species[ANT_SYMBOL] = 0;
species[DOODLEBUG_SYMBOL] = 0;
species[PLANT_SYMBOL] = 0;
//boost::process::spawn("python3 info.py");
}
~Info() {
out.close();
}
void count_num_show() {
boost::process::spawn("python3 info.py");
}
void show() {
cycle++;
out << cycle << " "<< species[ANT_SYMBOL] << " " << species[DOODLEBUG_SYMBOL]
<<" "<< species[PLANT_SYMBOL] << endl;
}
void dec(string shape) {
if (species.find(shape) != species.end())
species[shape]--;
}
void add(string shape) {
if (species.find(shape) != species.end())
species[shape]++;
}
private:
ofstream out;
map<string, int> species;
int cycle = 0;
};
Info info;
int get_random_num(int a, int b) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(a,b); // distribution in range [a, b]
return dist(rng);
}
enum Action {
SAME=0, RIGHT=1, LEFT, UP, DOWN
};
class Matter {
public:
Matter(int x, int y, string shape) :
pos_X(x), pos_Y(y), shape(shape) {}
Matter(int x, int y, string shape, bool inorganic) :
pos_X(x), pos_Y(y), shape(shape), inorganic(inorganic) {}
Matter(string shape) : shape(shape) {}
Matter(string shape, bool inorganic) : shape(shape), inorganic(inorganic) {}
Matter() = default;
~Matter() = default;
int get_posX() {return pos_X;}
int get_posY() {return pos_Y;}
string get_shape() {return shape;}
bool isInorganic() {return inorganic;}
virtual bool isActived() {return moved;}
virtual int actived() {moved = true; return -1;}
virtual void reset_actived() {moved = false;}
void set_posX(int x) {pos_X = x;}
void set_posY(int y) {pos_Y = y;}
void set_shape(string s) {shape = s;}
void move_done() {moved = true;}
virtual bool canEat(string eat) {return false;}
virtual bool eat(Matter **matter) {return false;}
virtual int move(Space *matter) {return -1;}
virtual Matter* childbirth(int x, int y) {return nullptr;}
virtual void reproduce(Space *space) {}
virtual void rebirth(Space *space, string wantGo) {}
virtual int get_newPos(Space *space, int oldPos, string wantGo) {return -1;}
virtual int go_where(Space *space, int oldPos) {return -1;}
virtual bool strvation() {return false;}
virtual void refillHP() {return;}
virtual void dec_HP() {return;}
virtual int get_HP() {return 0;}
private:
string shape;
int pos_X;
int pos_Y;
bool inorganic = true;
protected:
bool moved = false;
};
/*
class Empty : public Matter {
public:
using Matter::Matter;
};*/
class Border : public Matter {
public:
using Matter::Matter;
};
class Space {
public:
Space(int width, int height) : width(width+2), height(height+2) {
int total = this->width * this->height;
cout << "cocotion test total =" << total << endl;
matter = new Matter*[total];
// ╔════X═════╗
// Y ║
// ╚══════════╝
for(int i = 0; i < this->height; i++) {
for(int j = 0; j < this->width; j++) {
if(i == 0 || i == this->height-1) {
if(j > 0 && j < this->width-1) {
matter[i*(this->width)+j] = new Border(j, i, BORDER_HORIZONTAL_SYMBOL);
} else {
if(i == 0 && j == 0) {
matter[i*(this->width)+j] = new Border(j, i, BORDER_CORNER_LU_SYMBOL);
} else if (i == 0 && j == this->width-1) {
matter[i*(this->width)+j] = new Border(j, i, BORDER_CORNER_RU_SYMBOL);
} else if (i == this->height-1 && j == 0) {
matter[i*(this->width)+j] = new Border(j, i, BORDER_CORNER_LD_SYMBOL);
} else if (i == this->height-1 && j == this->width-1) {
matter[i*(this->width)+j] = new Border(j, i, BORDER_CORNER_RD_SYMBOL);
}
}
} else {
//EMPTY or VER
if((j == 0 || j == this->width-1) && (i > 0 && i < this->height-1 )) {
matter[i*(this->width)+j] = new Border(j, i, BORDER_VERTICAL_SYMBOL);
} else {
matter[i*(this->width)+j] = new Border(j, i, EMPTY_SYMBOL);
}
}
}
}
}
~Space() {
int total = width * height;
for(int i = 0; i < total; i++) {
delete matter[i];
}
delete [] matter;
_enable_cursor();
}
void add_species_pos(string shape, int pos) {
species_pos[shape].push_back(pos);
}
void reset_species_pos(string shape) {
species_pos[shape].clear();
}
void initMap() {
_clear();
_disable_cursor();
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
display(*matter[i*width+j]);
}
}
//_gotoXY(width+3, height+4);
fflush(stdout);
}
int updatePos(Matter *matter, int action) {
int X = matter->get_posX();
int Y = matter->get_posY();
switch(action) {
case Action::RIGHT:
++X;
break;
case Action::LEFT:
--X;
break;
case Action::UP:
++Y;
break;
default:
--Y;
}
return Y*width+X;
}
bool try_jump(int src, int des) {
if(des < 0) return false;
matter[src]->dec_HP();
bool des_isInorganic = matter[des]->isInorganic();
if (matter[src]->eat(&matter[des])) {
//if(!des_isInorganic) {
// matter[des]->refillHP();
//}
int y = src/width;
int x = src%width;
matter[src] = new Border(x, y, EMPTY_SYMBOL);
display(*matter[src]);
display(*matter[des]);
return true;
}
return false;
}
void try_reproduce(int pos) {
matter[pos]->reproduce(this);
}
inline void run(int i, string shape) {
if((matter[i]->get_shape() == shape) && !(matter[i]->isActived())) {
// Doodlebug will try to prey. If fail to prey, HP--;
int newPos = matter[i]->go_where(this, i);
int update;
// try_jump: deal how to eat
if(try_jump(i, newPos)) {
update = newPos;
} else {
update = i;
}
int born_cycle = matter[update]->actived();
// Born first
if(born_cycle == 0) {
try_reproduce(update);
}
// Die after born
if(matter[update]->strvation()) {
info.dec(matter[update]->get_shape());
int x = matter[update]->get_posX();
int y = matter[update]->get_posY();
delete matter[update];
matter[update] = new Border(x, y, EMPTY_SYMBOL);
display(*matter[update]);
}
} else if ((runOrder[0] == shape) && !(matter[i]->isInorganic()) && !(matter[i]->isActived())) {
add_species_pos(matter[i]->get_shape(), i);
}
}
void organism_move() {
int num = width*height;
for(int order = 0; order < SPECIES_NUM; order++) {
string shape = runOrder[order];
if(order == 0) {
for(int i = 0; i < num; i++) { //if not ig
run(i,shape);
}
} else {
for(int i : species_pos[shape]) {
run(i,shape);
}
}
}
for(int i = 0; i < num; i++) { //if not ig
if(!(matter[i]->isInorganic()) && matter[i]->isActived()) {
matter[i]->reset_actived();
}
}
for(int order = 1; order < SPECIES_NUM; order++) {
string shape = runOrder[order];
species_pos[shape].clear();
}
fflush(stdout);
}
void run() {
initMap();
for(int i = 0; i < 1000000; i++) {
//fgetc(stdin);
organism_move();
info.show();
//sleep(1);
}
}
void display(Matter &matter) {
_set_XY(matter.get_posX()+INIT_X, matter.get_posY()+INIT_Y, matter.get_shape().c_str());
}
//default is random
void gen_matter(Matter *matter, int number) {
//ofstream out("info.txt");
for(int i = 0; i < number; i++) {
//if eat sucessfully return true
int pos;
do {
pos = get_random_num(0, width*height-1);
} while(this->matter[pos]->get_shape() != EMPTY_SYMBOL);
//TODO: update x, y by function
int x = this->matter[pos]->get_posX();
int y = this->matter[pos]->get_posY();
delete this->matter[pos];
this->matter[pos] = matter->childbirth(x,y);
//out << this->matter[pos]->get_shape() << "X: "<< this->matter[pos]->get_posX() << "Y: "<< this->matter[pos]->get_posY() << endl;
//out << this->matter[pos]->isInorganic();
}
//out.close();
}
Matter **get_matter() {return matter;}
int get_width() {return width;}
private:
int width;
int height;
Matter **matter;
string runOrder[SPECIES_NUM] = {
//DOODLEBUG first
DOODLEBUG_SYMBOL,
ANT_SYMBOL,
PLANT_SYMBOL
};
map<string, vector<int>> species_pos;
};
class Organism : public Matter {
public:
Organism(int x, int y, string shape) : Matter(x, y, shape, false) {
Predation.push_back(EMPTY_SYMBOL);
}
Organism(string shape) : Matter(shape, false) {
Predation.push_back(EMPTY_SYMBOL);
}
bool canEat(string eat) {
return (find(Predation.begin(), Predation.end(), eat) != Predation.end());
}
bool eat(Matter **matter) {
//if this can eat
if(canEat((*matter)->get_shape())) {
if(!((*matter)->isInorganic())) {
this->refillHP();
(*matter)->dec_HP();
if((*matter)->get_HP() != 0) {
return false;
}
}
info.dec((*matter)->get_shape());
this->set_posX((*matter)->get_posX());
this->set_posY((*matter)->get_posY());
delete *matter;
*matter = this;
return true;
} else {
//TODO: if equal, depend on somewhat..
return false;
}
}
int get_newPos(Space *space, int oldPos, string wantGo) {
if(wantGo == UNIVERSAL_SYMBOL) {
return move(space);
}
int det[4] = {-1}; //U, D, L, R
int newPos;
do {
newPos = move(space);
int dpos = oldPos - newPos;
if(det[0]+det[1]+det[2]+det[3] == 0) {
return -1;
//break;
} else {
if(dpos == 1) det[0] = 0;
else if(dpos == -1) det[1] = 0;
else if(dpos > 1) det[2] = 0;
else det[3] = 0;
}
} while ((space->get_matter())[newPos]->get_shape() != wantGo);
return newPos;
}
void rebirth(Space *space, string wantGo) {
int width = space->get_width();
int x = this->get_posX();
int y = this->get_posY();
int oldPos = y*width+x;
int newPos = get_newPos(space, oldPos, wantGo);
if(newPos < 0) return;
//newPos is ok, ready to born
//TODO: take get_matter by temp pointer
x = (space->get_matter())[newPos]->get_posX();
y = (space->get_matter())[newPos]->get_posY();
delete (space->get_matter())[newPos]; //delete EMPTY_SYMBOL
(space->get_matter())[newPos] = childbirth(x, y);
(space->get_matter())[newPos]->move_done();
space->display(*((space->get_matter())[newPos]));
}
void reproduce(Space *space) {
rebirth(space, EMPTY_SYMBOL);
}
int actived() {
moved = true;
born_cycle = (born_cycle+1)%reproduce_cycle;
return born_cycle;
}
int move(Space *space) {
int action = get_random_num(Action::RIGHT, Action::DOWN);
return space->updatePos(this, action);
}
protected:
int reproduce_cycle;
vector<string> Predation;
int born_cycle = 0;
//int HP = DOODLEBUG_HP;
};
class Ant : public Organism {
public:
using Organism::Organism;
Ant() :
Organism(ANT_SYMBOL) {
Predation.push_back(PLANT_SYMBOL);
reproduce_cycle = ANT_REPRODUCE_CYCLE;
}
Ant(int x, int y) :
Organism(x, y, ANT_SYMBOL) {
Predation.push_back(PLANT_SYMBOL);
reproduce_cycle = ANT_REPRODUCE_CYCLE;
}
Matter* childbirth(int x, int y) {
info.add(ANT_SYMBOL);
return new Ant(x,y);
}
int go_where(Space *space, int oldPos) {
return get_newPos(space, oldPos, UNIVERSAL_SYMBOL);
}
void refillHP() {
if(HP < ANT_HP) {
HP+=2;
}
}
void dec_HP() {HP--;}
void inc_HP() {HP++;}
int get_HP() {return HP;}
bool strvation() {
return !HP;
}
private:
int HP = ANT_HP;
};
class Doodlebug : public Organism {
public:
using Organism::Organism;
Doodlebug() :
Organism(DOODLEBUG_SYMBOL) {
Predation.push_back(ANT_SYMBOL);
reproduce_cycle = DOODLEBUG_REPRODUCE_CYCLE;
}
Doodlebug(int x, int y) :
Organism(x, y, DOODLEBUG_SYMBOL) {
Predation.push_back(ANT_SYMBOL);
reproduce_cycle = DOODLEBUG_REPRODUCE_CYCLE;
}
Matter* childbirth(int x, int y) {
info.add(DOODLEBUG_SYMBOL);
return new Doodlebug(x,y);
}
int go_where(Space *space, int oldPos) {
int pos = get_newPos(space, oldPos, ANT_SYMBOL);
if(pos == -1) {
//fail to eat
// --HP;
return get_newPos(space, oldPos, UNIVERSAL_SYMBOL);
} else {
//refillHP();
return pos;
}
}
void dec_HP() {HP--;}
void inc_HP() {HP++;}
int get_HP() {return HP;}
void refillHP() {HP = DOODLEBUG_HP;}
bool strvation() {
return !HP;
}
private:
int HP = DOODLEBUG_HP;
};
class Plant : public Organism {
public:
using Organism::Organism;
Plant() :
Organism(PLANT_SYMBOL) {
//Predation.push_back(ANT_SYMBOL);
reproduce_cycle = PLANT_REPRODUCE_CYCLE;
}
Plant(int x, int y) :
Organism(x, y, PLANT_SYMBOL) {
//Predation.push_back(ANT_SYMBOL);
reproduce_cycle = PLANT_REPRODUCE_CYCLE;
}
Matter* childbirth(int x, int y) {
info.add(PLANT_SYMBOL);
return new Plant(x,y);
}
int go_where(Space *space, int oldPos) {
/* int pos = get_newPos(space, oldPos, PLANT_SYMBOL);
if(pos == -1) {
//fail to eat
--HP;
return get_newPos(space, oldPos, UNIVERSAL_SYMBOL);
} else {
refillHP();
return pos;
}*/
//--HP;
refillHP();
return -1;
}
void refillHP() {
//if(HP < PLANT_HP) {
// ++HP;
//}
}
//bool strvation() {
// return !HP;
//}
void dec_HP() {HP--;}
void inc_HP() {HP++;}
int get_HP() {return HP;}
private:
int HP = PLANT_HP;
};
void signal_callback_handler(int signum) {
_enable_cursor();
// Terminate program
exit(signum);
}
int main(int argc, char** argv)
{
// int xlimit = 20;
// int ylimit = 20;
// int doodlebug_num = 100;
// int ant_num = 120
signal(SIGINT, signal_callback_handler);
cxxopts::Options options("./exe", "opeions");
options.add_options()
("x,xlimit", "width size of the world", cxxopts::value<int>()->default_value("20"))
("y,ylimit", "height size of the world", cxxopts::value<int>()->default_value("20"))
("d,doodlebug_num", "number of doodlebugs", cxxopts::value<int>()->default_value("100"))
("a,ant_num", "number of ants", cxxopts::value<int>()->default_value("120"))
("p,plant_num", "number of plants", cxxopts::value<int>()->default_value("100"))
("h,help", "Print usage")
;
auto result = options.parse(argc, argv);
if (result.count("help")){
std::cout << options.help() << std::endl;
exit(0);
}
int xlimit = result["xlimit"].as<int>();
int ylimit = result["ylimit"].as<int>();
int doodlebug_num = result["doodlebug_num"].as<int>();
int ant_num = result["ant_num"].as<int>();
int plant_num = result["plant_num"].as<int>();
Space space(xlimit, ylimit); //ok
Doodlebug doodlebug;
Ant ant;
Plant plant;
space.gen_matter(&doodlebug, doodlebug_num); //ok
space.gen_matter(&ant, ant_num); //ok
space.gen_matter(&plant, plant_num); //ok
info.count_num_show();
space.run();
cout << "delete all" << endl;
return EXIT_SUCCESS;
}