/** Creates an instance of this class. Provides convenient execution. */
 public static void main(String[] args) throws InterruptedException {
   GameOfLife game = new GameOfLife(false);
   Thread.sleep(1000);
   // A for loop that runs through many generations
   for (int i = 0; i < 10000; i++) {
     Thread.sleep(100);
     game.createNextGeneration();
   }
 }
Example #2
0
File: Ex1.java Project: bigman3/cdp
  public static void main(String[] argv) {
    if (argv.length != 6) {
      printUsage();
    }

    String filename = argv[0];
    int hSplit = Integer.parseInt(argv[1]);
    int vSplit = Integer.parseInt(argv[2]);
    int nGenerations = Integer.parseInt(argv[3]);
    int xSize = Integer.parseInt(argv[4]);
    int ySize = Integer.parseInt(argv[5]);

    GameFieldReader g = new GameFieldReader(filename, xSize, ySize);
    boolean[][] field = null;
    try {
      field = g.parseData();
    } catch (Exception e) {
      System.err.println("Error while initializing the field:" + e.getMessage());
      // e.printStackTrace();
      System.exit(-1);
    }
    // printArray(field);
    System.out.println(":END of inital array:");

    if (vSplit > field.length || hSplit > field[0].length) {
      System.err.println("Wrong number of splits specified");
      System.exit(-1);
    }
    GameOfLife sGol = new SerialGameOfLife();
    GameOfLife pGol = new ParallelGameOfLife();

    boolean[][][] resultSerial = sGol.invoke(field, hSplit, vSplit, nGenerations);

    // printArray(resultSerial);
    try {
      printArray(resultSerial[0], "output.txt");
      printArray(resultSerial[1], "output1.txt");
    } catch (Exception e) {
      System.err.println("Failed to write the output to file");
      e.printStackTrace();
      System.exit(-1);
    }

    boolean[][][] resultParallel = pGol.invoke(field, hSplit, vSplit, nGenerations);

    if (compareArrays(resultParallel[0], resultSerial[0])
        && compareArrays(resultParallel[1], resultSerial[1])) {
      System.out.println("Success!");
    } else {
      System.out.println("Parallel version results do not match!!");
      System.exit(-1);
    }
    System.exit(0);
  }
  @Test
  public void testFinalState() {
    /* verify that the actual pattern matches the expected pattern after 3 generations         *
     */
    /* expected pattern for final state
     *  (X: alive; -: dead)
     *
     *    0 1 2 3 4
     *  0 - - X - -
     *  1 - X - X -
     *  2 X - - - X
     *  3 - X - X -
     *  4 - - X - -
     *
     */

    GameOfLife game = new GameOfLife(true);
    game.createNextGeneration();
    game.createNextGeneration();
    final int ROWS = game.getNumRows();
    final int COLS = game.getNumCols();

    for (int row = 0; row < ROWS; row++) {
      for (int col = 0; col < COLS; col++) {
        // in this example, an alive cell has a non-null actor and a dead cell has a null actor
        Actor cell = game.getActor(row, col);

        // if the cell at the current row and col should be alive, assert that the actor is not null
        if ((row == 0 && col == 2)
            || (row == 1 && col == 1)
            || (row == 1 && col == 3)
            || (row == 2 && col == 0)
            || (row == 2 && col == 4)
            || (row == 3 && col == 1)
            || (row == 3 && col == 3)
            || (row == 4 && col == 2)) {
          assertNotNull("expected alive cell at (" + row + ", " + col + ")", cell);
        } else // else, the cell should be dead; assert that the actor is null
        {
          assertNull("expected dead cell at (" + row + ", " + col + ")", cell);
        }
      }
    }

    // ...
  }
  @Test
  public void testInitialState() {
    /* expected pattern for initial state
     *  (X: alive; -: dead)
     *
     *    0 1 2 3 4
     *  0 X - - - X
     *  1 - X - X -
     *  2 - - X - -
     *  3 - X - X -
     *  4 X - - - X
     *
     */

    GameOfLife game = new GameOfLife(true);
    final int ROWS = game.getNumRows();
    final int COLS = game.getNumCols();

    for (int row = 0; row < ROWS; row++) {
      for (int col = 0; col < COLS; col++) {
        // in this example, an alive cell has a non-null actor and a dead cell has a null actor
        Actor cell = game.getActor(row, col);

        // if the cell at the current row and col should be alive, assert that the actor is not null
        if ((row == 0 && col == 0)
            || (row == 0 && col == 4)
            || (row == 1 && col == 1)
            || (row == 1 && col == 3)
            || (row == 2 && col == 2)
            || (row == 3 && col == 1)
            || (row == 3 && col == 3)
            || (row == 4 && col == 0)
            || (row == 4 && col == 4)) {
          assertNotNull("expected alive cell at (" + row + ", " + col + ")", cell);
        } else // else, the cell should be dead; assert that the actor is null
        {
          assertNull("expected dead cell at (" + row + ", " + col + ")", cell);
        }
      }
    }
  }
 private void pause() {
   lifeVisual.enable();
   run_pause.setText("Run");
   runTimer.stop();
   myLife.pause();
 }