| 12
 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
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 
 |  import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;
 
import javax.swing.JOptionPane;
 
public class Board{
	private int xg = 60;
	private int yg = 40;
	int gol[][] = new int [xg][yg];
	// private int golX[][] = new int [xg][yg];
	private int golY[][] = new int [xg][yg];
 
	// récupère les modifications effectuées sur l'interface graphique
	public void graphToBoard(int pos, int state){
		int x = pos % 60;
		int y = Math.abs(pos/60);
 
		this.gol[x][y] = state;
	}
 
	// initialise toutes les cellules à mortes
	public void initDef(){
		for(int y = 0 ; y < this.yg ; y++){
			for(int x = 0 ; x < this.xg ; x++){
				this.gol[x][y] = 0;
			}
		}
	}
 
	// définit une valeur aléatoire pour chaque cellule du board
	public void rdm(){		
		for (int y = 0; y < this.yg ; y++){
			for (int x = 0; x < this.xg ; x++){
				int valeurMax = 2;
				int valeurMin = 1;
				Random r = new Random();
				int target = valeurMin + r.nextInt(valeurMax);
 
				if(target == 1){
					this.gol[x][y] = 1;
				}
				else{
					this.gol[x][y] = 0;
				}
			}
		}
	}
 
	// initialise le tableau en fx d'une conf initiale (chargement fichier txt)
	public void conf(String res){
		int x, y = 0, z;
 
		try{
			InputStream is = new FileInputStream(res);
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
 
			String line = br.readLine();
 
			while(line != null){
				for(x = 0 ; x < 60 ; x++){
					z = Integer.parseInt("" + line.charAt(x));
					this.gol[x][y] = z;
				}
 
				line = br.readLine();
				y++;
			}
 
			br.close();
			isr.close();
			is.close();
		} catch (Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog (null, "Erreur lors du chargement de la partie", "Oops !", JOptionPane.ERROR_MESSAGE);
		}
	}
 
	// traitements pour une génération
	public void generation(){
 
		/*
		// pour golX : n-2, golY : n-1, gol : n
		for(int i = 0 ; i < this.yg ; i++){
			for(int j = 0 ; j < this.xg ; j++){
				this.golX[j][i] = this.golY[j][i];
			}
		}
		*/
 
		// effectue les operations necessaires sur la generation actuelle
		for(int i = 0 ; i < this.yg ; i++){
			for(int j = 0 ; j < this.xg ; j++){
				this.golY[j][i] = this.gol[j][i];
			}
		}
 
		// determine les cellules vivantes et mortes
		for(int y = 0 ; y < this.yg ; y++){
			for(int x = 0 ; x < this.xg ; x++){
				lifeDeath(x, y);
			}
		}	
 
 
 
		// determine la coloration des cellules
		for(int y = 0 ; y < this.yg ; y++){
			for(int x = 0 ; x < this.xg ; x++){
				lifeDeathC(x, y);
			}
		}
 
 
	}
 
	// selon le nbre de voisins, définit à vivant ou mort
	public void lifeDeath(int x, int y){
		int bnc = this.neighbourCount(x, y, this.golY);
 
		// verifie condition mort
		if((bnc < 2) || (bnc > 3)){
			this.gol[x][y] = 0;
		}
		// verifie condition vie
		else if(bnc == 3){
			this.gol[x][y] = 1;
		}
	}
 
	// reprend les cellules vivantes de la generation pour les colorer
	public void lifeDeathC(int x, int y){		
		int bnc = this.neighbourCount(x, y, this.golY);
 
		if(bnc == 3){
			this.coloration(x, y);
		}
	}
 
	// attribue des valeurs differentes aux cellules pour determiner la coloration
	public void coloration(int x, int y){
		int bnc = this.neighbourCount(x, y, this.gol);
 
		// vert : cellules naissantes (et qui ne meurt pas à la gen suivante)
 
		// 1 : ne passe pas à bleu si a déjà vécu une gén et a deux cellules voisines
		// 2 : parfois, passe vert et disparait sans passer par rouge
		// 3 : parfois, passe vert au lieu jaune (?)
		// 4 : parfois, passe bleu et disparait sans passer par rouge
 
		if(this.golY[x][y] == 0 && (bnc ==  3 || bnc == 2)){
			this.gol[x][y] = 1;
		}
 
		// bleu : cellules en cours de vie (si a vécu à la dernière gen et continue de vivre à la gen suivante
		if((this.golY[x][y] == 1 || this.golY[x][y] == 2) && (bnc ==  3 || bnc == 2)){
			this.gol[x][y] = 2;
		}
 
		// rouge : cellules mourantes (a survécu plus d'une gen et meurt gen suivante)
		if((this.golY[x][y] == 1 || this.golY[x][y] == 2) && ((bnc < 2) || (bnc > 3))){
			this.gol[x][y] = 3;
		}
 
		// jaune : cellule ne vivant qu'une génération
		if(this.golY[x][y] == 0 && ((bnc < 2) || (bnc > 3))){
			this.gol[x][y] = 4;
		}
	}
 
	// gestion des valeurs négatives
	public static int mod (int x, int m){
		// ex. 1 : mod(y-1, yg), y = 0, yg = 40
			// ((0-1) % 40 + 40) % 40 = 0
		// ex. 2 : mod(y-1, yg), y = 4, yg = 40
			// ((5-1) % 40 + 40) % 40 = 4
		return (x % m + m) % m;
	} 
 
	// compte le nbre de voisins pour une cellule(x, y)
	public int neighbourCount(int x, int y, int gol[][]){
		int nC = 0; 
 
		// haut
		if(y-1 >= 0 && checkValues(gol, x, mod(y-1, yg))){
			nC++;
		}
 
		// bas
		if(y+1 < 40 && checkValues(gol, x, mod(y+1, yg))){
			nC++;
		}
 
		// gauche
		if(x-1 >= 0 && checkValues(gol, mod(x-1, xg), y)){
			nC++;
		}
 
		// droite
		if(x+1 < 60 && checkValues(gol, mod(x+1, xg), y)){
			nC++;
		}
 
		// haut gauche
		if(x-1 >= 0 && y-1 >= 0 && checkValues(gol, mod(x-1, xg), mod(y-1, yg))){
			nC++;
		}
 
		// haut droite
		if(x+1 < 60 && y-1 >= 0 && checkValues(gol, mod(x+1, xg), mod(y-1, yg))){
			nC++;
		}
 
		// bas gauche
		if(x-1 >= 0 && y+1 < 40 && checkValues(gol, mod(x-1, xg), mod(y+1, yg))){
			nC++;
		}
 
		// bas droite
		if(x+1 < 60 && y+1 < 40 && checkValues(gol, mod(x+1, xg), mod(y+1, yg))){
			nC++;
		}
 
		return nC;
	}
 
	// renvoie vrai si la concernée est vivante (une des quatre couleurs)
	public boolean checkValues(int gol[][], int x, int y){
		boolean check = false;
 
		if(gol[x][y] == 1 || gol[x][y] == 2 || gol[x][y] == 3 || gol[x][y] == 4){
			check = true;
		}
 
		return check;
	}
} | 
Partager