-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
104 lines (67 loc) · 2.28 KB
/
main.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
#include "Win.h"
#include "DisplayFunctions.h"
#include <iostream>
#include <iterator>
#include <list>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int total_iterations_to_simulate=20;
const int starting_population_size = 50;
list<win*> create_initial_population()
{
list<win*> wins;
//create a list of wins of size "starting_population_size"
for (int i = 0; i < starting_population_size; i++)
wins.push_back(new win());
//display starting population
cout<<"Starting population of wins is: "<<endl;
display_wins(wins);
return wins;
}
//attempt to fight or mate wins in the list with one another
list<win*> interact_with_each_other(list<win*> wins){
for (list<win *>::iterator list_position = wins.begin(); next(list_position) != wins.end(); list_position++)
{
win* current_win = (*list_position);
win* next_win = (*next(list_position));
if(current_win->alive())
{
current_win->grow_older();
if (current_win->potential_mate(*next_win))
{
win* new_child = new win( mate(*current_win, *next_win) );
wins.push_back(new_child);
}
else
fight(*current_win, *next_win);
}
}
return wins;
}
//predicate as function to be used in the "std::list<>.remove_if" call
bool is_dead(win* w1)
{
return !w1->alive();
}
int main(){
//seed a new time for random for all rand() calls that wins use
srand(time(0));
list<win *> wins = create_initial_population();
for (int iterations = 0; iterations < total_iterations_to_simulate && wins.size() > 1; iterations++)
{
display_iteration_info(iterations, wins.size());
//mate or fight the wins in the current iteration
wins = interact_with_each_other(wins);
//remove the dead and display
wins.remove_if(is_dead);
display_wins(wins);
}
cout<<"There is/are "<<wins.size()<<" survivor(s)."<<endl;
cout<<"The survivors are: "<<endl;
display_wins(wins);
display_stats_labels();
display_config_warning();
return 0;
}