Bonjour,
j'ai fait cette implémentation du Jeu de la Vie et je veux avoir votre avis si l'implémentation est vraie ou pas.
La modularité n'est pas au rendez-vous mais je travaille dessus
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
109
110
111
112
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifr;
 
import java.util.Random;
 
/**
 *
 * @author taserface
 */
public class GridLife {
    private static int generation = 1;
 
 
 
    int w = 8;
    int columns, rows;
 
    //Grille
    int[][] board;
 
 
    //Constructeur;
    public GridLife(int col, int row){
        this.columns = col;
        this.rows = row;
 
        board = new int[rows][columns];
        initialisation();
    }
 
    public void printGrid(){
        int count = 0;
        for(int i = 0; i < rows; i = i+1){
            for(int j = 0; j < columns; j = j+1){
                //System.out.print(this.board[i][j]+"   ");
                if(board[i][j] == 1){
                    count++;
                    System.out.print("# ");
                }else
                    System.out.print(". ");
            }
            System.out.println();
        }
        System.out.println("Le nombre de cellule vivante est: "+count);
    }
 
    public void initialisation(){
        Random r = new Random();
        for(int i = 0; i < this.board.length; i = i+1){
            for(int j = 1; j < this.board.length; j = j+1){
                board[i][j] = (int)r.nextInt(2); //generate number between 0 and 1
 
            }
        }
    }
 
    //compter le nombre de voisin en vie
    public int countNeighborsLive(int[][] tab, int x, int y){
        int sum = 0;
        for(int i = -1; i < 2; i = i+1){
            for(int j = -1; j < 2; j = j+1){
                //sum += tab[x+i][y+j];
                int c = (y+j+this.columns) %this.columns;
                int r = (x+i+this.rows) % this.rows;
                sum += tab[r][c];
            }
        }
 
        //ne pas compter la cellule active
        sum -= board[x][y];
        return sum;
    }
 
    //Création de la nouvelle génération
    public void nextGen() throws InterruptedException{
        int[][] next = new int[this.rows][this.columns];
        int neighbors;
        while(true){
            Thread.sleep(5000);
            printGrid();
            for(int x = 1; x < columns-1; x=x+1){
                for(int y = 1; y < rows-1; y = y+1){
 
                   /* if(x == 0 || x == this.rows-1 || y == 0 || y == this.columns-1){
                        next[x][y] = board[x][y];
                    }else{*/
                    neighbors = countNeighborsLive(this.board,x,y);
 
                    //Règle de vie
                    if((board[x][y] == 1) && (neighbors < 2 || neighbors > 3)){ //solitude
                        next[x][y] = 0;
 
                    }else if((board[x][y] == 0) && (neighbors == 3)){ //reproduction
                        next[x][y] = 1;
                    }else{
                        next[x][y] = board[x][y];
                    }
 
                //}
                }
            }
            board = next;
            System.out.println("Génération: "+GridLife.generation);
            GridLife.generation++;
        }
    }
 
}
et là la fonction main
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
public static void main(String[] args) throws InterruptedException {
        // TODO code application logic here
        GridLife gl = new GridLife(27,27);
        //System.out.println(gl);
        System.out.println(" ===============  Starting Game Of Life  ===============");
        System.out.println();
        gl.nextGen();
    }