Advertisement
Pyo514

Game of Life

Jul 25th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.91 KB | None | 0 0
  1. /***********************************************************************************
  2.  * World class - defines a World object which consists
  3.  * of an array of Cell objects.
  4.  *  INSTANCE Variables
  5.  *      board - the array of Cell objects
  6.  *      rows, cols - the number of rows and columns if the world
  7.  ***********************************************************************************
  8.  */
  9. import java.util.Scanner;
  10. import java.util.Random;
  11. public class World
  12. {
  13.     private Cell[][] board;
  14.     private int rows;
  15.     private int cols;
  16.  
  17.     /*******************************************************************************
  18.      *  The constructor
  19.      *  Creates a world object
  20.      *  PARAMETERS
  21.      *      two integers indicating the number of rows and columns respectively
  22.      *  All cells are "dead" in a World created by this constructor
  23.      *******************************************************************************
  24.      */
  25.     public World(int numOfRows, int numOfCols)
  26.     {
  27.         int row, col;
  28.  
  29.         rows = numOfRows;
  30.         cols = numOfCols;
  31.         board = new Cell[rows][cols];
  32.         for(row = 0; row < rows; row++)
  33.             for(col = 0; col < cols; col++)
  34.             board[row][col] = new Cell(false); 
  35.     }
  36.  
  37.     /*******************************************************************************
  38.      * nextGen()
  39.      * modifies the board instance variable to the new configuration
  40.      * based on Conway's rules.
  41.      * Creates a local "world" to populate, then copies the status
  42.      * of those cells to the existing instance board.
  43.      *******************************************************************************
  44.      */
  45.     public void nextGen()
  46.     {
  47.         World nextGenerations = new World(rows, cols);
  48.         int row, col, neighbors;
  49.  
  50.         for(row = 1; row < rows - 1; row++)
  51.             for(col = 1; col < cols - 1; col++)
  52.             {
  53.                 neighbors = countNeighbors(row, col);
  54.                 if(neighbors == 3)
  55.                     nextGenerations.makeAlive(row, col);
  56.                 else if(neighbors == 2)
  57.                     nextGenerations.setStatus(row, col, this.getStatus(row, col));
  58.             }
  59.         for(row=0; row < rows; row++)
  60.             for(col = 0; col < cols; col++)
  61.                 board[row][col].setStatus(nextGenerations.board[row][col].getStatus());
  62.     }
  63.  
  64.     /*********************************************************************************
  65.      *  countNeighbors(int, int)
  66.      *  returns an int representing the number of "live" neighbors within the instance
  67.      *  variable board, at the row and col coordinates provided as parameters.
  68.      *********************************************************************************
  69.      */
  70.     private int countNeighbors(int curRow, int curCol)
  71.     {
  72.         int row, col, neighbors = 0;
  73.         for(row = curRow - 1; row <= curRow + 1; row++)
  74.             for(col = curCol - 1; col <= curCol + 1; col++)
  75.                 if(board[row][col].getStatus())
  76.                     neighbors++;
  77.         if(board[curRow][curCol].getStatus())
  78.             neighbors--;
  79.         return neighbors;
  80.     }
  81.     /*******************************************************************************
  82.      *
  83.      *******************************************************************************
  84.      */
  85.     public void userChoice()
  86.     {
  87.         int row, col, generations, neighbors;
  88.         Scanner input = new Scanner(System.in);
  89.         System.out.println("Enter in an row value");
  90.         row = input.nextInt();
  91.         System.out.println("Enter in a column values.");
  92.         col = input.nextInt();
  93.         if(row > 10 && row < 10 && col > 10 && col < 10)
  94.         {
  95.             System.out.println("Please put in a valid row value.");
  96.             row = input.nextInt();
  97.             System.out.println("Please put in a valid column value.");
  98.             col = input.nextInt();
  99.         }
  100.         else
  101.         {  
  102.             board[col][row] = new Cell(true);
  103.         }
  104.         System.out.println("How many generations do you want to appear?");
  105.         generations = input.nextInt();
  106.        
  107.     }
  108.        
  109.        
  110.     /*******************************************************************************
  111.      * toString()
  112.      * overrides the default toString() to create (and return) a string version of
  113.      * the Life board.
  114.      *******************************************************************************
  115.      */
  116.     public String toString()
  117.     {
  118.         String boardPic = "";
  119.         int row, col;
  120.  
  121.         for(row = 0; row < rows; row++)
  122.         {
  123.             for(col = 0; col < cols; col++)
  124.                 if(board[row][col].getStatus())
  125.                     boardPic = boardPic + "X";
  126.                 else
  127.                     boardPic = boardPic + ".";
  128.             boardPic = boardPic + "\n";
  129.         }
  130.         return boardPic;
  131.     }
  132.     /*******************************************************************************
  133.      * getStatus(int, int)
  134.      * Expects the row and column coordinates of a cell.
  135.      * Calls the Cell's getStatus() method of a Cell within the local board.
  136.      * Returns true for a live cell and false for a dead cell.
  137.      *******************************************************************************
  138.      */
  139.     public boolean getStatus(int row, int col)
  140.     {
  141.         return board[row][col].getStatus();
  142.     }
  143.     /*******************************************************************************
  144.      * setStatus(int, int, boolean)
  145.      * Expects the row and column coordinates of a cell and the status value.
  146.      * Calls the Cell's setStatus() method sending it the status parameter.
  147.      *******************************************************************************
  148.      */
  149.     public void setStatus(int row, int col, boolean status)
  150.     {
  151.         board[row][col].setStatus(status);
  152.     }
  153.     /*******************************************************************************
  154.      * makeAlive(int, int)
  155.      * Sends a true to the Cell's setStatus() method at the given coordinates.
  156.      *******************************************************************************
  157.      */
  158.     public void makeAlive(int row, int col)
  159.     {
  160.         board[row][col].setStatus(true);
  161.     }
  162.     /*******************************************************************************
  163.      * makeDead(int, int)
  164.      * Sends a false to the Cell's setStatus() method at the given coordinates.
  165.      *******************************************************************************
  166.      */
  167.     public void makeDead(int row, int col)
  168.     {
  169.         board[row][col].setStatus(false);
  170.     }
  171.  
  172.  
  173. }
  174.  
  175. /*Include your documentation here:
  176.  *
  177.  *
  178.  *
  179.  * Your assignment is to modify PlayLife so the user can enter a series of
  180.  * row, col coordinates between 1 and 10 to make cells alive. Entering a value
  181.  * less than 1 or greater than 10 will be the signal the user is done.
  182.  * Then ask the user how many generations past the initial one they would like
  183.  * to view.
  184.  * Show then the initial generation and the successive ones up to the number the
  185.  * user requested.
  186.  *
  187.  */
  188.  public class PlayLife
  189. {
  190.     public static void main(String[] args)
  191.     {
  192.         World myWorld = new World(10, 10);
  193.         System.out.println(myWorld);
  194.         System.out.println();
  195.         myWorld.userChoice();
  196.         System.out.println(myWorld);
  197.         System.out.println();
  198.         System.out.println();
  199.         myWorld.nextGen();
  200.         System.out.println(myWorld);
  201.         System.out.println();
  202.     }
  203. }
  204.  
  205. public class Cell
  206. {
  207.     private boolean status;
  208.  
  209.     public Cell(boolean aliveOrDead)
  210.     {
  211.         status = aliveOrDead;
  212.     }
  213.  
  214.     public void setStatus(boolean aliveOrDead)
  215.     {
  216.         status = aliveOrDead;
  217.     }
  218.  
  219.     public boolean getStatus()
  220.     {
  221.         return status;
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement