Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************************************
- * World class - defines a World object which consists
- * of an array of Cell objects.
- * INSTANCE Variables
- * board - the array of Cell objects
- * rows, cols - the number of rows and columns if the world
- ***********************************************************************************
- */
- import java.util.Scanner;
- import java.util.Random;
- public class World
- {
- private Cell[][] board;
- private int rows;
- private int cols;
- /*******************************************************************************
- * The constructor
- * Creates a world object
- * PARAMETERS
- * two integers indicating the number of rows and columns respectively
- * All cells are "dead" in a World created by this constructor
- *******************************************************************************
- */
- public World(int numOfRows, int numOfCols)
- {
- int row, col;
- rows = numOfRows;
- cols = numOfCols;
- board = new Cell[rows][cols];
- for(row = 0; row < rows; row++)
- for(col = 0; col < cols; col++)
- board[row][col] = new Cell(false);
- }
- /*******************************************************************************
- * nextGen()
- * modifies the board instance variable to the new configuration
- * based on Conway's rules.
- * Creates a local "world" to populate, then copies the status
- * of those cells to the existing instance board.
- *******************************************************************************
- */
- public void nextGen()
- {
- World nextGenerations = new World(rows, cols);
- int row, col, neighbors;
- for(row = 1; row < rows - 1; row++)
- for(col = 1; col < cols - 1; col++)
- {
- neighbors = countNeighbors(row, col);
- if(neighbors == 3)
- nextGenerations.makeAlive(row, col);
- else if(neighbors == 2)
- nextGenerations.setStatus(row, col, this.getStatus(row, col));
- }
- for(row=0; row < rows; row++)
- for(col = 0; col < cols; col++)
- board[row][col].setStatus(nextGenerations.board[row][col].getStatus());
- }
- /*********************************************************************************
- * countNeighbors(int, int)
- * returns an int representing the number of "live" neighbors within the instance
- * variable board, at the row and col coordinates provided as parameters.
- *********************************************************************************
- */
- private int countNeighbors(int curRow, int curCol)
- {
- int row, col, neighbors = 0;
- for(row = curRow - 1; row <= curRow + 1; row++)
- for(col = curCol - 1; col <= curCol + 1; col++)
- if(board[row][col].getStatus())
- neighbors++;
- if(board[curRow][curCol].getStatus())
- neighbors--;
- return neighbors;
- }
- /*******************************************************************************
- *
- *******************************************************************************
- */
- public void userChoice()
- {
- int row, col, generations, neighbors;
- Scanner input = new Scanner(System.in);
- System.out.println("Enter in an row value");
- row = input.nextInt();
- System.out.println("Enter in a column values.");
- col = input.nextInt();
- if(row > 10 && row < 10 && col > 10 && col < 10)
- {
- System.out.println("Please put in a valid row value.");
- row = input.nextInt();
- System.out.println("Please put in a valid column value.");
- col = input.nextInt();
- }
- else
- {
- board[col][row] = new Cell(true);
- }
- System.out.println("How many generations do you want to appear?");
- generations = input.nextInt();
- }
- /*******************************************************************************
- * toString()
- * overrides the default toString() to create (and return) a string version of
- * the Life board.
- *******************************************************************************
- */
- public String toString()
- {
- String boardPic = "";
- int row, col;
- for(row = 0; row < rows; row++)
- {
- for(col = 0; col < cols; col++)
- if(board[row][col].getStatus())
- boardPic = boardPic + "X";
- else
- boardPic = boardPic + ".";
- boardPic = boardPic + "\n";
- }
- return boardPic;
- }
- /*******************************************************************************
- * getStatus(int, int)
- * Expects the row and column coordinates of a cell.
- * Calls the Cell's getStatus() method of a Cell within the local board.
- * Returns true for a live cell and false for a dead cell.
- *******************************************************************************
- */
- public boolean getStatus(int row, int col)
- {
- return board[row][col].getStatus();
- }
- /*******************************************************************************
- * setStatus(int, int, boolean)
- * Expects the row and column coordinates of a cell and the status value.
- * Calls the Cell's setStatus() method sending it the status parameter.
- *******************************************************************************
- */
- public void setStatus(int row, int col, boolean status)
- {
- board[row][col].setStatus(status);
- }
- /*******************************************************************************
- * makeAlive(int, int)
- * Sends a true to the Cell's setStatus() method at the given coordinates.
- *******************************************************************************
- */
- public void makeAlive(int row, int col)
- {
- board[row][col].setStatus(true);
- }
- /*******************************************************************************
- * makeDead(int, int)
- * Sends a false to the Cell's setStatus() method at the given coordinates.
- *******************************************************************************
- */
- public void makeDead(int row, int col)
- {
- board[row][col].setStatus(false);
- }
- }
- /*Include your documentation here:
- *
- *
- *
- * Your assignment is to modify PlayLife so the user can enter a series of
- * row, col coordinates between 1 and 10 to make cells alive. Entering a value
- * less than 1 or greater than 10 will be the signal the user is done.
- * Then ask the user how many generations past the initial one they would like
- * to view.
- * Show then the initial generation and the successive ones up to the number the
- * user requested.
- *
- */
- public class PlayLife
- {
- public static void main(String[] args)
- {
- World myWorld = new World(10, 10);
- System.out.println(myWorld);
- System.out.println();
- myWorld.userChoice();
- System.out.println(myWorld);
- System.out.println();
- System.out.println();
- myWorld.nextGen();
- System.out.println(myWorld);
- System.out.println();
- }
- }
- public class Cell
- {
- private boolean status;
- public Cell(boolean aliveOrDead)
- {
- status = aliveOrDead;
- }
- public void setStatus(boolean aliveOrDead)
- {
- status = aliveOrDead;
- }
- public boolean getStatus()
- {
- return status;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement