SHOW:
|
|
- or go back to the newest paste.
1 | #include <Grapic.h> | |
2 | #include <ctime> | |
3 | #include <cmath> | |
4 | #include <iostream> | |
5 | #include <math.h> | |
6 | #include <iostream> | |
7 | #include <stdlib.h> | |
8 | #include <stdio.h> | |
9 | #include <time.h> | |
10 | ||
11 | using namespace std; | |
12 | using namespace grapic; | |
13 | ||
14 | ||
15 | const int DIMW = 1000; | |
16 | const int MAXX = 100; | |
17 | const int MAXY = 100; | |
18 | ||
19 | struct cell | |
20 | { | |
21 | int etat; | |
22 | }; | |
23 | ||
24 | ||
25 | struct jeu_de_la_vie | |
26 | { | |
27 | struct cell tab[MAXX][MAXY]; | |
28 | int dx,dy; | |
29 | int alive; | |
30 | int dead; | |
31 | Image im; | |
32 | }; | |
33 | ||
34 | void init(jeu_de_la_vie &jeu) | |
35 | { | |
36 | int i,j; | |
37 | jeu.dx = 50; | |
38 | jeu.dy = 50; | |
39 | jeu.im = image("data/vert.jpg", true, 255, 255, 255, 255); | |
40 | for (i=0;i<jeu.dx;i++) | |
41 | { | |
42 | for (j=0;j<jeu.dy;j++) | |
43 | { | |
44 | jeu.tab[i][j].etat = 0; | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | void etat_initial(jeu_de_la_vie &jeu) | |
50 | { | |
51 | int i; | |
52 | int l,c; | |
53 | jeu.alive = 1000; | |
54 | jeu.dead = jeu.dx*jeu.dy-jeu.alive; | |
55 | for (i=0;i<jeu.alive;i++) | |
56 | { | |
57 | do{ | |
58 | l = rand()%(jeu.dx-2)+1; | |
59 | c = rand()%(jeu.dy-2)+1; | |
60 | }while (jeu.tab[l][c].etat != 0); | |
61 | jeu.tab[l][c].etat = 1; | |
62 | } | |
63 | } | |
64 | ||
65 | void etat_suivant(jeu_de_la_vie &jeu) | |
66 | { | |
67 | int i,j; | |
68 | int a; | |
69 | for (i=0;i<jeu.dx;i++) | |
70 | { | |
71 | for (j=0;j<jeu.dy;j++) | |
72 | { | |
73 | a = 0; | |
74 | if (jeu.tab[i-1][j-1].etat == 1) | |
75 | a=a+1; | |
76 | if (jeu.tab[i-1][j].etat == 1) | |
77 | a=a+1; | |
78 | if (jeu.tab[i-1][j+1].etat == 1) | |
79 | a=a+1; | |
80 | if (jeu.tab[i][j-1].etat == 1) | |
81 | a=a+1; | |
82 | if (jeu.tab[i][j+1].etat == 1) | |
83 | a=a+1; | |
84 | if (jeu.tab[i+1][j-1].etat == 1) | |
85 | a=a+1; | |
86 | if (jeu.tab[i+1][j].etat == 1) | |
87 | a=a+1; | |
88 | if (jeu.tab[i+1][j+1].etat == 1) | |
89 | a=a+1; | |
90 | if (jeu.tab[i][j].etat == 0 && a == 3) | |
91 | { | |
92 | jeu.tab[i][j].etat = 2; | |
93 | jeu.alive = jeu.alive+1; | |
94 | jeu.dead = jeu.dead-1; | |
95 | } | |
96 | if (jeu.tab[i][j].etat == 1 && a > 3) | |
97 | { | |
98 | jeu.tab[i][j].etat = 0; | |
99 | jeu.alive = jeu.alive-1; | |
100 | jeu.dead = jeu.dead+1; | |
101 | } | |
102 | if (jeu.tab[i][j].etat == 1 && a < 2) | |
103 | { | |
104 | jeu.tab[i][j].etat = 0; | |
105 | jeu.alive = jeu.alive-1; | |
106 | jeu.dead = jeu.dead+1; | |
107 | } | |
108 | } | |
109 | } | |
110 | for (i=0;i<jeu.dx;i++) | |
111 | { | |
112 | for (j=0;j<jeu.dy;j++) | |
113 | { | |
114 | if (jeu.tab[i][j].etat == 2) | |
115 | jeu.tab[i][j].etat = 1; | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | ||
121 | void draw (jeu_de_la_vie &jeu) | |
122 | { | |
123 | int i,j; | |
124 | for (i=0;i<jeu.dy;i++) | |
125 | { | |
126 | line(0,i*20,DIMW,i*20); | |
127 | line(i*20,0,i*20,DIMW); | |
128 | } | |
129 | for(i=0;i<jeu.dx;i++) | |
130 | { | |
131 | for(j=0;j<jeu.dy;j++) | |
132 | { | |
133 | if (jeu.tab[i][j].etat == 1) | |
134 | { | |
135 | image_draw(jeu.im,(20*i),(20*j),20,20); | |
136 | } | |
137 | } | |
138 | } | |
139 | if (isKeyPressed('s')) | |
140 | { | |
141 | delay(100); | |
142 | etat_suivant(jeu); | |
143 | } | |
144 | ||
145 | } | |
146 | ||
147 | ||
148 | int main(int , char** ) | |
149 | { | |
150 | bool stop=false; | |
151 | winInit("terrain", DIMW, DIMW); | |
152 | backgroundColor(255,255,255); | |
153 | jeu_de_la_vie jeu; | |
154 | init(jeu); | |
155 | etat_initial(jeu); | |
156 | setKeyRepeatMode(true); | |
157 | while(!stop){ | |
158 | winClear(); | |
159 | draw(jeu); | |
160 | stop=winDisplay(); | |
161 | } | |
162 | winDisplay(); | |
163 | pressSpace(); | |
164 | winQuit(); | |
165 | return 0; | |
166 | } |