QuestionQuestion

The game of life takes place on a two dimensional board (a 2D array in Java terms). Each square on the board (element in the array) is cell, either dead or alive.
The game progresses in steps. Each step, each cell determines whether it will die or spring to life. It does counting the number of live neighbors it has. Neighbors are the up to 8 adjacent cells, including diagonals. The board does not wrap around, so cells in the corners and sides have fewer than 8 neighbors.1 After counting their living neighbors, if a live cell has < 2 living neighbors, it dies of loneliness. If a live cell has > 3 living neighbors, it dies of overpopulation. If the live cell has 2 or 3 neighbors, it lives on, content. If a dead cell has exactly 3 neighbors, it springs back to life. Each of these status changes happen each step, so if a live cell has 5 neighbors on step n, then its status changes to dead for step n + 1.
Using these simple rules, we can create complex and beautiful patterns.

Implementing the Game of Life
There is a skeleton provided to make things easier. To make our job easier, we are doing a completely text based version of the game of life. We will represent our board as a 2D array. We also have a second 2D array of the same size as a buer," essentially a workspace to create the next step of the game, as we'll explain in a bit.
Use the character `X' to represent a live cell and any other character of your choice to represent a dead one (I use the carat character).
Let's look at the constructor and each of the functions in turn.
1 The Constructor
In the constructor, build a new board of your desired size, and initialize your initial board state. Also initialize the nextBoard variable to be an empty board of the same size.

2 getNeighborCount
This method takes in the row and column location of a cell and returns the number of living neighbors. This will most likely be your most challenging method and I recommend writing and testing this method rst. Let me be perfectly clear.
Write and test this method before trying to write the whole program at once.
If you try to write the whole program in one go, your program will crash and you won't be able to gure out why.
The challenge of this function is that each of the 8 neighboring cells you need to check could be out of bounds.

3 generateNextStep
This method generates the next step of the board. For each cell in the board, get the number of neighbors and the status of the cell you are currently at.
Use this information to gure out whether the cell will be alive or dead in the next iteration of the board. Set the cell at the same row, column location in nextBoard to this status. To reiterate, you want to change the cells in nextBoard, not your current board, because if you changed the cells in board, you would lose your current board state.
Once that is done, make set board equal to nextBoard to advance to the next step and create a new 2D for nextBoard.

4 printBoard
This one is obvious. Print out the board.
2

5 main
Use main to create a GameOfLifeBoard, and then create a while loop. In the while loop do the following
Print out the board by calling printBoard() wait for user input if the user puts in quit, stop the game.
Otherwise, call generateNextStep() and keep the loop going.

6 Testing
To demo your code, create a glider and watch it go across the board.

Here is the skeleton. Make sure the codes follows the skelon properly.

import java.util.Scanner;

public class GameOfLifeBoard {

private char[][] board;
private char[][] nextBoard;
public static final char LIVE = 'X';
public static final char DEAD = '^';

public GameOfLifeBoard(){
}

public void generateNextStep(){
}

public int getNeighborCount(int row, int col){ int numNeighbors = 0;
return numNeighbors;
}

public void printBoard(){
}

public static void main(String[] args){
}
}

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

// Character array representing present game board
private char[][] board;
// Character array representing next game board
private char[][] nextBoard;
// Character representing LIVE cell
public static final char LIVE = 'X';
// Character representing DEAD cell
public static final char DEAD = '^';

// Board constructor
public GameOfLifeBoard(){
    // Default size
    int rows = 10;
    int cols = 10;
    // Create board
    board = new char[rows][cols];
    // Iterate over the board
    for (int i = 0; i < board.length; i++) {
      for (int j = 0; j < board[i].length; j++) {
       // Assign initial state of the board
       board[i][j] = DEAD;
       if ((i == 4 || i == 5 || i == 6) && (j == 4 || j == 5 || j == 6)) {
          board[i][j] = LIVE;
       }
      }
    }
    // Create next board
    nextBoard = new char[rows][cols];
}

// Method to identify next board state
public void generateNextStep(){
    // Iterate over the board
    for (int i = 0; i < board.length; i++) {
      for (int j = 0; j < board[i].length; j++) {
       // Get number of live neighbors of cell...

By purchasing this solution you'll be able to access the following files:
Solution.java.

$60.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats