Advertisement
Guest User

Untitled

a guest
Mar 27th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. // I have everything working except for the dead cell rule
  2. import java.util.Arrays;
  3. import java.util.Random;
  4. public class GOL {
  5.    public static void main(String[] args)
  6.    {
  7.       Cell   newcell   = new Cell(0);
  8.       int    board[][] = new int[5][5];
  9.       Random rand      = new Random();
  10.  
  11. //https://stackoverflow.com/questions/43314281/random-matrix-fill-between-1-and-0-in-java
  12.       for (int i = 0; i < 5; i++)
  13.       {
  14.          for (int j = 0; j < 5; j++)
  15.          {
  16.             // you could maybe do a random initialization of board before you start checking for the cells
  17.             // in that way it will be more obvious where the problem is defined and when you start to find the solution
  18.             board[i][j] = rand.nextInt(2);           
  19.             try{
  20.                // don't use this object(newcell) just in order to be able to call method of it's class,
  21.                // it would be better to make that method static so Cell.getNumberofNeighbors(),
  22.                // look on when to use static methods,
  23.                // or if you make class Board that consists of Cell's you could write it like this
  24.                // board.getNumberOfNeighborsForCurrentCell(cell. or i,j whatever),
  25.                // if you do it like this there is no need to pass the whole matrix everytime,
  26.                // but even then there is space for improvement in class Board make method isDying(cell),
  27.                // add your methods for every rule, that will improve readability, it will be more like a story...
  28.                if (newcell.getNumberofNeighbors(i, j, board) < 2)
  29.                {
  30.                   board[i][j] = 0;
  31.                }
  32.                if (newcell.getNumberofNeighbors(i, j, board) > 3)
  33.                {
  34.                   board[i][j] = 0;
  35.                }
  36.             }
  37.             catch (Exception e) {
  38.                //https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array
  39.                System.out.println(Arrays.deepToString(board).replace("], " + "[", "\n"));
  40.             }
  41.          }
  42.       }
  43.    }
  44. }
  45.  
  46.  
  47. import java.util.Arrays;
  48. import java.util.Random;
  49. public class Cell {
  50.    int numberofneighbors;
  51.    public Cell(int numberofneighbors)
  52.    {
  53.       this.numberofneighbors = numberofneighbors;
  54.    }
  55.  
  56.    // optimize your parameters, naming is not good, third arg. is really not position
  57.    // it is the whole board(your matrix), so this is rather confusing
  58.    public int getNumberofNeighbors(int x, int y, int[][] position) throws Exception
  59.    {
  60.       if ((x < 0) || (y < 0) || (x > 5) || (y > 5)) { throw new Exception(""); }
  61.       else
  62.       {
  63.          if (position[x][y] == 1)
  64.          {
  65.             if (position[x][y + 1] == 1) { numberofneighbors += 1; }
  66.             if (position[x + 1][y] == 1) { numberofneighbors += 1; }
  67.             if (position[x + 1][y + 1] == 1) { numberofneighbors += 1; }
  68.             if (position[x - 1][y] == 1) { numberofneighbors += 1; }
  69.             if (position[x][y - 1] == 1) { numberofneighbors += 1; }
  70.             if (position[x + 1][y - 1] == 1) { numberofneighbors += 1; }
  71.             if (position[x - 1][y + 1] == 1) { numberofneighbors += 1; }
  72.          }
  73.       }
  74.       return numberofneighbors;
  75.    }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement