Question

I am writing a program to play the game "Game of Life". I am hoping someone...

I am writing a program to play the game "Game of Life". I am hoping someone can help me edit my sad program so that it will print the first board as well as the next generation. The first board is working just fine. It is the next generation that is not working and i'm not sure what I am doing wrong. Thanks in advance.

public class Life {
  
public char FirstBoard[][];
  
public Life()
  
{
FirstBoard = new char[19][19];
  
for(int i = 0; i < 19; i++)
for(int j = 0; j < 19; j++)
  
//print dead cells
FirstBoard[i][j] = '.';
//print live cells
FirstBoard[2][3] = 'O';
FirstBoard[2][12] = 'O';
FirstBoard[2][13] = 'O';
FirstBoard[3][3] = 'O';
FirstBoard[3][12] = 'O';
FirstBoard[3][13] = 'O';
FirstBoard[7][6] = 'O';
FirstBoard[7][7] = 'O';
FirstBoard[8][5] = 'O';
FirstBoard[8][8] = 'O';
FirstBoard[9][6] = 'O';
FirstBoard[9][7] = 'O';
FirstBoard[12][12] = 'O';
FirstBoard[12][13] = 'O';
FirstBoard[12][14] = 'O';
FirstBoard[14][5] = 'O';
FirstBoard[15][3] = 'O';
FirstBoard[15][5] = 'O';
FirstBoard[16][4] = 'O';
FirstBoard[16][5] = 'O';
}
  
public String toString() {
String boardString = "";
for(int i = 0; i < FirstBoard.length; i++) {
for(int j = 0; j < FirstBoard[i].length - 1; j++) {
boardString += FirstBoard[i][j] + " ";
}
boardString += "\n";
if(i != FirstBoard.length - 1)
boardString += FirstBoard[i][FirstBoard[i].length - 1] + " ";
else
boardString += FirstBoard[i][FirstBoard[i].length - 1];
}
return boardString;
  
}
  
public static void nextGeneration(char FirstBoard[][], int row, int col) {
char[][] future = new char[19][19];
  
for(int l = 1; l < 19; l++)
{
for(int m = 1; m < 19 - 1; m++)
{
int aliveCells = 0;
for(int i = -1; i <= 1; i++)
for(int j = -1; j <= 1; j++)
aliveCells += FirstBoard[l + i][m + j];
aliveCells -= FirstBoard[l][m];
  
//loneliness
if((FirstBoard[l][m] == 1) && (aliveCells < 2))
future[l][m] = 0;
//crowding
else if((FirstBoard[l][m] == 1) && (aliveCells > 3))
future[l][m] =0;
//birth
else if ((FirstBoard[l][m] == 0) && (aliveCells == 3))
future[l][m] = 1;
//stays dead
else
future[l][m] = FirstBoard[l][m];
}
  
}
}
  
public static void main(String[] args) {
Life game = new Life();
System.out.println(game);
  
  
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

public class Life {

private char board[][];
  
public Life()

{
board = new char[19][19];

for(int i = 0; i < 19; i++)
for(int j = 0; j < 19; j++)
//print dead cells
board[i][j] = '.';
//print live cells
board[2][3] = 'O';
board[2][12] = 'O';
board[2][13] = 'O';
board[3][3] = 'O';
board[3][12] = 'O';
board[3][13] = 'O';
board[7][6] = 'O';
board[7][7] = 'O';
board[8][5] = 'O';
board[8][8] = 'O';
board[9][6] = 'O';
board[9][7] = 'O';
board[12][12] = 'O';
board[12][13] = 'O';
board[12][14] = 'O';
board[14][5] = 'O';
board[15][3] = 'O';
board[15][5] = 'O';
board[16][4] = 'O';
board[16][5] = 'O';
}

public String toString() {
String boardString = "";
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length ; j++) {
boardString += board[i][j] + " ";
}
boardString += "\n";
}
return boardString;

}
  
private boolean isValid(int row, int col){
if(row >= 0 && col >= 0 && row < board.length && col < board[0].length)
return true;
else
return false;
}
  
private int liveNeighbors(int row, int col){
int count = 0;
for(int i = row-1; i <= row + 1; i++){
for(int j = col -1; j <= col + 1; j++){
if(i == row && j == col) // do not count the reference cell itself
continue;
if(isValid(i, j) && board[i][j] == 'O')//live
count++;
}
}
return count;
}
  
public void nextGeneration() {
int m = board.length, n = board[0].length;
char[][] future = new char[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
int count = liveNeighbors(i, j);
if(board[i][j] == 'O'){ //liviing
if(count < 2 ) //loneliness
future[i][j] = '.';
else if(count > 3) //overcrowded
future[i][j] = '.';
else
future[i][j] = 'O';
}
else{ //non living
if( count == 3) //dead with 3 neighbors will come alive
future[i][j] = 'O';
else
future[i][j] = '.';
}
}
}
  
//copy over to board
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
board[i][j] = future[i][j];
}
  

public static void main(String[] args) {
Life game = new Life();
int numGenerations = 5;
for(int i = 1; i <= 5;i++){
System.out.println("Generation " + i);
System.out.println(game);
game.nextGeneration();
}
}
}

output
=====
Generation 1
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . O . . . . . . . . O O . . . . .
. . . O . . . . . . . . O O . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . O . . O . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . O O O . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . O . . . . . . . . . . . . .
. . . O . O . . . . . . . . . . . . .
. . . . O O . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .

Generation 2
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . O . . O . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . O . . . . .
. . . . . . . . . . . . . O . . . . .
. . . . . . . . . . . . . O . . . . .
. . . . O . . . . . . . . . . . . . .
. . . . . O O . . . . . . . . . . . .
. . . . O O . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .

Generation 3
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . O . . O . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . O O O . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . O . . . . . . . . . . . . .
. . . . . . O . . . . . . . . . . . .
. . . . O O O . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .

Generation 4
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . O . . O . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . O . . . . .
. . . . . . . . . . . . . O . . . . .
. . . . . . . . . . . . . O . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . O . O . . . . . . . . . . . .
. . . . . O O . . . . . . . . . . . .
. . . . . O . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .

Generation 5
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . O O . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . O . . O . . . . . . . . . .
. . . . . . O O . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . O O O . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .
. . . . . . O . . . . . . . . . . . .
. . . . O . O . . . . . . . . . . . .
. . . . . O O . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . .

Add a comment
Know the answer?
Add Answer to:
I am writing a program to play the game "Game of Life". I am hoping someone...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However,...

    I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?: public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats; public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k]...

  • I am having problems getting the insertion sort function to output in descending order. Also, the...

    I am having problems getting the insertion sort function to output in descending order. Also, the initialize array function is not being called when I test it in the main function. I am wondering why it prints out the original array instead of initializing. Thank you. #include <stdio.h> void print_array(int *array, int length){ int i; for(i=0; i<length;i++) printf("%d"",",array[i]); printf("\n"); } void initialize_array(int *array, int length){ int i; for(i=0, i<length; i++;){ if (i%2 ==0) array[i]= 5; else array[i]= 0; } }...

  • Task: Simulate the growth of a biological population. The Game of Life, invented by John H....

    Task: Simulate the growth of a biological population. The Game of Life, invented by John H. Conway, is supposed to model the genetic laws for birth, survival, and death. Our implementation of the game will use a 2-dimensional array with 12 rows and 12 columns. Each cell of the array will hold either a 1 (an organism is present) or a 0 (no organism there). You will calculate 4 generations of the organisms’ birth, survival, and death. An organism’s neighbors...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • I am working on the divide/conquer algorithm. I am having a trouble with a print for...

    I am working on the divide/conquer algorithm. I am having a trouble with a print for output from reading the file. Here is my work. When you see I put the comment with TODO. that one I am struck with readfile. I wonder if you'd able to help me to fix the readfile to find a single number. Here is the input3.txt (1 12 13 24 35 46 57 58 69). after that, the output should be 0. int mergeInversion(int...

  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • I am going to show you my code and for some reason when I addDomino to...

    I am going to show you my code and for some reason when I addDomino to front, my program gives me a null value, I need help figuring out why.   public void addDominoFront(String d){         String temp1 = Domino[0];         String temp2;         Domino[0] = d;         for(int i = 1; i< numDominoes; i++){             temp2 = Domino[i];             Domino[i] = temp1;             temp1 = temp2;         }         numDominoes = numDominoes +1;     } The I have the following...

  • So I am working on an assignment where we make a rudimentary browser history with c#....

    So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT