-
-
Save metaxy/271969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtCore/QCoreApplication> | |
#include <QMap> | |
#include <QMapIterator> | |
#include <QDebug> | |
#define START_POP 70 | |
#define MW_ASYM 0.5 | |
#define MAR_YEAR 25 | |
#define CHILD_COUNT 4 | |
#define MAX_YEARS 130 | |
struct people | |
{ | |
int year; | |
bool male; | |
int childCount; | |
int id; | |
int marWith; | |
}; | |
int random(int start,int end) | |
{ | |
return qrand()*(end-start)+end; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QMap<int, struct people> peoples; | |
QCoreApplication a(argc, argv); | |
for(int i = 0; i < START_POP;i++) { | |
struct people p; | |
p.year = 0; | |
p.id = i; | |
p.childCount = 0; | |
if(random(0,1) == 1) | |
p.male = true; | |
else | |
p.male = false; | |
p.marWith = -1; | |
peoples[p.id] = p; | |
} | |
int lastID = START_POP; | |
for(int y=0; y < MAX_YEARS;y++){ | |
QMapIterator<int, struct people> i(peoples); | |
while (i.hasNext()) { | |
i.next(); | |
struct people p = i.value(); | |
p.year++; | |
if(p.year == MAR_YEAR) { | |
//qDebug() << "search for mar"; | |
QMapIterator<int, struct people> k(peoples); | |
while (k.hasNext()) { | |
k.next(); | |
struct people part = k.value(); | |
if(/*part.male != p.male &&*/ part.year >= MAR_YEAR && part.marWith == -1 ) { | |
p.marWith = part.id; | |
part.marWith = p.id; | |
peoples[part.id] = part; | |
//qDebug() << "mar 1 = " << p.id << " 2 = " << part.id; | |
} | |
} | |
} | |
if(random(0,2) >= 1 && p.marWith != -1 && p.childCount < CHILD_COUNT) { | |
//qDebug() << "make chi " << p.id; | |
p.childCount++; | |
peoples[p.marWith].childCount++; | |
struct people n; | |
n.year = 0; | |
n.id = lastID; | |
n.childCount = 0; | |
if(random(0,1) == 1) | |
n.male = true; | |
else | |
n.male = false; | |
n.marWith = -1; | |
peoples[lastID] = n; | |
lastID++; | |
} | |
if(p.childCount >= 4) | |
{ | |
qDebug() << "removed"; | |
peoples.remove(p.id); | |
} | |
peoples[p.id] = p; | |
} | |
qDebug() << "year = " << y; | |
} | |
qDebug() << "end pop.count = " << peoples.size(); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment