Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GiocoDellaVita
- {
- public static void main(String[] args)
- {
- int[][] test = { { 1,2,3,5,4 }, { 1, 2 }, { 1, 2, 3 }, { 1, 2, 4, 5 }, { 1 } };
- System.out.println(new GiocoDellaVita(test).toString());
- }
- private int[][] mat;
- public GiocoDellaVita(int[][] a)
- {
- this.mat = new int[a.length][a[0].length];
- for (int i = 0; i < a.length; i++)
- for (int j = 0; j < a[0].length; j++)
- this.mat[i][j] = a[i][j];
- }
- public void prossimaGenerazione()
- {
- int countZ = 0; // contatore di individui adiacenti ad una cella con Zeri
- int countO = 0; // contatori uno
- for (int i = 0; i < this.mat.length; i++)
- for (int j = 0; j < this.mat[0].length; j++)
- {
- if (this.mat[i][j] == 1 && this.mat[i][j + 1] == 1)
- countO++;
- else if (this.mat[i][j] == 0 && this.mat[i][j + 1] == 1)
- countZ++;
- if (this.mat[i][j] == 1 && this.mat[i + 1][j] == 1)
- countO++;
- else if (this.mat[i][j] == 0 && this.mat[i + 1][j] == 1)
- countZ++;
- if (this.mat[i][j] == 1 && this.mat[i + 1][j + 1] == 1)
- countO++;
- else if (this.mat[i][j] == 0 && this.mat[i + 1][j + 1] == 1)
- countZ++;
- if (countZ == 3 || countO == 2 || countO == 3)
- this.mat[i][j] = 1;
- else
- this.mat[i][j] = 0;
- }
- }
- // conversione gioco-matrice
- @Override
- public String toString()
- {
- String s = "";
- for (int i = 0; i < this.mat.length; i++)
- {
- for (int j = 0; j < this.mat[i].length; j++)
- s += this.mat[i][j] + " ";
- s += "\n";
- }
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement