-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjeuvie.cpp
110 lines (87 loc) · 2.21 KB
/
jeuvie.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
#include "jeuvie.h"
jeuVie::jeuVie(int height, int width, int nbMinVoisins, int nbMaxVoisins, QString name) : AutoCell(width, height, cellStates,3, name), nbMinVoisins(nbMinVoisins), nbMaxVoisins(nbMaxVoisins)
{
}
void jeuVie::changeCellState(const QPoint point)
{
if(point.x() < width && point.y() < height)
{
int x = point.x();
int y = point.y();
if(etats[currentState%nbMaxEtats].getMatrice()[y][x]==1)
etats[currentState%nbMaxEtats].setValue(y,x,0);
else{
//etats.last().getMatrice()[(point.x())/cellWidth][(point.y())/cellHeight]=1;
etats[currentState%nbMaxEtats].setValue(y,x,1);
}
}
}
void jeuVie::nextState()
{
if(currentState == 0)
etatInitial = etats[0];
Etat etat(height, width);
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
etat.setValue(i,j,willBorn(i,j,etats[(currentState)%nbMaxEtats]));
}
}
currentState++;
if(currentState<nbMaxEtats)
etats.push_back(etat);
else
etats[currentState%nbMaxEtats]=etat;
}
void jeuVie::generateRandomly()
{
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
etats[currentState%nbMaxEtats].setValue(i,j,rand()%2);
}
}
}
bool jeuVie::willBorn(int x, int y, Etat etat)
{
int n = 0;
if(y > 0){
n += etat.getMatrice()[x][y-1];
if(x > 0)
n += etat.getMatrice()[x-1][y-1];
if(x < height-1)
n += etat.getMatrice()[x+1][y-1];
}
if(y < width-1){
n += etat.getMatrice()[x][y+1];
if(x > 0)
n += etat.getMatrice()[x-1][y+1];
if(x < height-1)
n += etat.getMatrice()[x+1][y+1];
}
if(x > 0)
n += etat.getMatrice()[x-1][y];
if(x < height-1)
n += etat.getMatrice()[x+1][y] ;
//return((etat.getMatrice()[x][y]==1 && (n == 2 || n == 3)) || (etat.getMatrice()[x][y] == 0 && n == 3));
return((etat.getMatrice()[x][y]==1 && (n >= nbMinVoisins && n <= nbMaxVoisins)) || (etat.getMatrice()[x][y]==0 && n == nbMaxVoisins));
}
int jeuVie::getNbMaxVoisins() const
{
return nbMaxVoisins;
}
void jeuVie::setNbMaxVoisins(int value)
{
nbMaxVoisins = value;
}
void jeuVie::previous()
{
if(currentState>1)
currentState--;
}
int jeuVie::getNbMinVoisins() const
{
return nbMinVoisins;
}
void jeuVie::setNbMinVoisins(int value)
{
nbMinVoisins = value;
}