I'm new to c++ and qt, but I want to learn. The essence of the problem: the application hangs when you click on the button, although before some edits worked adequately (rolling back edits did not help). As far as I understand it is because of the while loop, but nowhere can I find an answer on how to fix it.
The program itself, what I need to implement, is to write the interaction of predators and herbivores, for a start on a primitive level.
So, please help me with the problem of hanging the application.
Creating classes
class Grass
{...};
class Predator
{...};
class Herbivorous
{...};
Button click processing
void MainWindow::on_pushButton_clicked()
{
int kol_grass = ui->lineEdit_11->text().toInt(), kol_weeks = 0, t, t_x, t_y, kol_H = ui->lineEdit->text().toInt(), kol_P = ui->lineEdit_2->text().toInt(), k = 0, kk = 0;
list<Herbivorous> lst_H;
list<Predator> lst_P;
list<Grass> lst_G;
int kol_die_P_hungry = 0, kol_die_H_hungry = 0, kol_die_P_age = 0, kol_die_H_age = 0, eaten = 0;
int years = 0;
qsrand(QDateTime::currentMSecsSinceEpoch());
//resize dynamic lists
lst_H.resize(kol_H);
lst_P.resize(kol_P);
lst_G.resize(kol_grass);
//creating list iterators
list<Herbivorous>::iterator i = lst_H.begin();
list<Herbivorous>::iterator j = lst_H.begin();
list<Predator>::iterator ii = lst_P.begin();
list<Predator>::iterator jj = lst_P.begin();
list<Grass>::iterator grass_it = lst_G.begin();
//creating grass objects
for (; grass_it != lst_G.end(); grass_it++)
{
...
}
grass_it = lst_G.begin();
//creating herbivore objects
for (; i != lst_H.end(); i++)
{
...
}
//creating predator objects
for (; ii != lst_P.end(); ii++)
{
...
}
i = lst_H.begin();
ii = lst_P.begin();
Then the same cycle
Apparently because of it hangs tightly application when you click on the button. What am I doing wrong?
//as long as both animal species are alive, simulate life
while (lst_H.size() > 0 && lst_P.size() > 0)
{
//grass rendering
for (; grass_it != lst_G.end(); grass_it++)
{
if (grass_it->GetValue() != .0)
{
QTableWidgetItem * grass = new QTableWidgetItem();
grass->setBackground(Qt::green);
ui->tableWidget->setItem(grass_it->GetY(), grass_it->GetX(), grass);
}
}
grass_it = lst_G.begin();
//once a month grass replenishment
if (kol_weeks % 4 == 0)
{
grass_it = lst_G.begin();
for(; grass_it != lst_G.end(); grass_it++)
{
grass_it->Recovery();
}
grass_it = lst_G.begin();
}
//once a month check for starvation
if (kol_weeks % 4 == 0)
{
i = lst_H.begin();
for (; i != lst_H.end(); i++)
{
if (i->GetHungry() <= 0)
{
i->SetInLife(false);//смерть от голода
kol_die_H_hungry++;
}
else
{
i->SetAge(i->GetAge() + 1);//+1 год
}
}
i = lst_H.begin();
ii = lst_P.begin();
for (; ii != lst_P.end(); ii++)
{
if (ii->GetHungry() <= 0)
{
ii->SetInLife(false);//смерть от голода
kol_die_P_hungry++;
}
else
{
ii->SetAge(ii->GetAge() + 1);//+1 год
}
}
ii = lst_P.begin();
}
//once a year old age check
if (kol_weeks == 48)
{
i = lst_H.begin();
kol_weeks = 0;
years++;
for (; i != lst_H.end(); i++)
{
if (i->GetAge() >= ui->lineEdit_3->text().toInt())
{
i->SetInLife(false);//смерть от старости
kol_die_H_age++;
}
else
{
i->SetAge(i->GetAge() + 1);//+1 год
}
}
i = lst_H.begin();
ii = lst_P.begin();
for (; ii != lst_P.end(); ii++)
{
if ((ii->GetAge() >= ui->lineEdit_10->text().toInt()) || (ii->GetHungry() <= 0))
{
ii->SetInLife(false);//смерть от старости или от голода
kol_die_P_age++;
}
else
{
ii->SetAge(ii->GetAge() + 1);//+1 год
}
}
ii = lst_P.begin();
}
//movement of herbivores
for (; i != lst_H.end(); i++)
{
QTableWidgetItem * noherbivorous = new QTableWidgetItem();
noherbivorous->setBackground(Qt::black);
ui->tableWidget->setItem(i->GetY(), i->GetX(), noherbivorous);
if (i->GetInLife() == true)
{
i->Direction();
i->Hungry();
QTableWidgetItem * herbivorous = new QTableWidgetItem();
herbivorous->setBackground(Qt::white);
ui->tableWidget->setItem(i->GetY(), i->GetX(), herbivorous);
}
}
//moving predators
for (; ii != lst_P.end(); ii++)
{
QTableWidgetItem * nopredator = new QTableWidgetItem();
nopredator->setBackground(Qt::black);
ui->tableWidget->setItem(ii->GetY(), ii->GetX(), nopredator);
if (ii->GetInLife() == true)
{
ii->Direction();
ii->Hungry();
QTableWidgetItem * predator = new QTableWidgetItem();
predator->setBackground(Qt::red);
ui->tableWidget->setItem(ii->GetY(), ii->GetX(), predator);
}
}
lst_H.remove_if([] (Herbivorous a) {return (a.GetInLife() == false);});//the removal of dead
lst_P.remove_if([] (Predator a) {return (a.GetInLife() == false);});//the removal of dead
i = lst_H.begin();
j = lst_H.begin();
//death of parents and birth of children (herbivores)
for (; i != lst_H.end(); i++)
{
for (; j != lst_H.end(); j++)
{
if ((i->GetNumber() != j->GetNumber()) && (i->GetInLife() == true) && (j->GetInLife() == true) && (i->GetAge() >= i->GetMin_Rep()) && (i->GetAge() <= i->GetMax_Rep()) && (j->GetAge() >= j->GetMin_Rep()) && (j->GetAge() <= j->GetMax_Rep()))
{
t_x = i->GetX() - j->GetX();
t_y = i->GetY() - j->GetY();
if (((t_x == 1) || (t_x == -1) || (t_x == 0)) && ((t_y == 1) || (t_y == -1) || (t_y == 0)))
{
i->SetInLife(false);
j->SetInLife(false);
//creating children
t = qrand() % (4 + 1);//от 0 до 4 детей
for (int u = 0; u < t; u++)
{
Herbivorous child;
child.SetInLife(true);
child.SetX(qrand() % (234 + 1));//рандом
child.SetY(qrand() % (144 + 1));//рандом
child.SetAge(1);
child.SetFood(ui->lineEdit_6->text().toDouble());
child.SetNumber(kk);
child.SetMax_Rep(ui->lineEdit_4->text().toInt());
child.SetMin_Rep(ui->lineEdit_5->text().toInt());
k++;
lst_H.push_back(child);
}
}
}
}
j = lst_H.begin();
}
ii = lst_P.begin();
jj = lst_P.begin();
//death of parents and birth of children (predators)
for (; ii != lst_P.end(); ii++)
{
for (; jj != lst_P.end(); jj++)
{
if ((ii->GetNumber() != jj->GetNumber()) && (ii->GetInLife() == true) && (jj->GetInLife() == true) && (ii->GetAge() >= ii->GetMin_Rep()) && (ii->GetAge() <= ii->GetMax_Rep()) && (jj->GetAge() >= jj->GetMin_Rep()) && (jj->GetAge() <= jj->GetMax_Rep()))
{
t_x = ii->GetX() - jj->GetX();
t_y = ii->GetY() - jj->GetY();
if (((t_x == 1) || (t_x == -1) || (t_x == 0)) && ((t_y == 1) || (t_y == -1) || (t_y == 0)))
{
ii->SetInLife(false);
jj->SetInLife(false);
//creating children
t = qrand() % (4 + 1);//от 0 до 4 детей
for (int u = 0; u < t; u++)
{
Predator child;
child.SetInLife(true);
child.SetX(qrand() % (234 + 1));//рандом
child.SetY(qrand() % (144 + 1));//рандом
child.SetAge(1);
child.SetFood(ui->lineEdit_8->text().toDouble());
child.SetNumber(k);
child.SetMax_Rep(ui->lineEdit_9->text().toInt());
child.SetMin_Rep(ui->lineEdit_7->text().toInt());
kk++;
lst_P.push_back(child);
}
}
}
}
jj = lst_P.begin();
}
ii = lst_P.begin();
j = lst_H.begin();
//hunting of predators on herbivores
for (; ii != lst_P.end(); ii++)
{
if (ii->GetHungry() < 1)
{
for (; j != lst_H.end(); j++)
{
if ((ii->GetInLife() == true) && (j->GetInLife() == true))
{
t_x = ii->GetX() - j->GetX();
t_y = ii->GetY() - j->GetY();
if (((t_x == 1) || (t_x == -1) || (t_x == 0)) && ((t_y == 1) || (t_y == -1) || (t_y == 0)))
{
j->SetInLife(false);
ii->SetHungry(ii->GetHungry() + 1);
eaten++;
}
}
}
}
j = lst_H.begin();
}
grass_it = lst_G.begin();
//hunting herbivores on the grass
for (; j != lst_H.end(); j++)
{
if (j->GetHungry() < 1)
{
for (; grass_it != lst_G.end(); grass_it++)
{
if ((grass_it->GetValue() > 0) && (j->GetInLife() == true) && (j->GetX() == grass_it->GetX()) && (j->GetY() == grass_it->GetY()))
{
if (grass_it->GetValue() > 0.5)
{
j->SetHungry(j->GetHungry() + 0.5);
grass_it->SetValue(grass_it->GetValue() - 0.5);
}
else
{
j->SetHungry(j->GetHungry() + grass_it->GetValue());
grass_it->SetValue(0);
}
}
}
}
j = lst_H.begin();
}
i = lst_H.begin();
ii = lst_P.begin();
grass_it = lst_G.begin();
//summary of interim results
ui->label_11->setText("The number of herbivores: " + QString::number(lst_H.size()));
...
QEventLoop loop;
QTimer::singleShot(100, &loop, SLOT(quit()));
loop.exec();
kol_weeks++;
}
}```
Aucun commentaire:
Enregistrer un commentaire