Write a program titled \"YourLastName_GameOfLife\" which will implement Conway\'
ID: 3569716 • Letter: W
Question
Write a program titled "YourLastName_GameOfLife" which will implement Conway's Game of Life. Life is a simulation of simple one-celled organisms and therefore typically played on a 2D grid of square cells. Each cell has two possible states: dead or alive. To calculate each new generation of the board, use the following rules:
For spaces already "alive":
For spaces already "dead":
Note: Every cell has eight neighbors which are the cells that are diagonally, vertically, or horizontally adjacent.
For your program, use a 2D char array to represent the board. Use '0' to represent dead cells of the board and 'X' to represent cells which are alive. Your program will load game board data from a file to begin the game, with the first line of the input file consisting of two ints representing the number of columns and rows for the board.
For example, a sample input file may appear as follows:
10 9
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 X X X 0 0 0
0 0 X 0 0 0 0 X 0 0
0 0 X 0 0 0 0 X 0 0
0 0 X 0 0 0 0 X 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 X X X 0 0 0
0 0 0 0 0 0 0 0 0 0
Methods to implement:
The program should prompt the user for the file name and how many generations to compute, then print out each generation to the screen.
The following shows an example interaction captured in a file by the command
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class Sarin_GameOfLife
{
private char[][] board;
private int columns;
private int rows;
private int generation;
public static void main(String[] args) throws FileNotFoundException
{
Sarin_GameOfLife game = new Sarin_GameOfLife();
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter how many generations to compute: ");
int gen = keyboard.nextInt();
System.out.println("Generation 1:");
game.print();
for(int i = 2; i <= gen; i++)
{
System.out.println("Generation " + i);
game.computeNextGeneration(gen);
game.printNew();
}
}
public Sarin_GameOfLife() throws FileNotFoundException
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter file name: ");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
int columns = inputFile.nextInt();
int rows = inputFile.nextInt();
inputFile.nextLine();
board = new char[rows][columns];
for(int i = 0; i < rows; i++)
{
String line = inputFile.nextLine();
for(int j = 0; j < columns; j++)
{
board[i][j] = line.charAt(j);
}
}
}
public int getColumns()
{
return columns;
}
public int getRows()
{
return rows;
}
public int getCell(int rows, int columns)
{
if(board[rows][columns] == 'X' || board[rows][columns] == '0')
{
return board[rows][columns];
}
else
{
return 0;
}
}
public void setCell(int rows, int columns, int value)
{
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
value = board[rows][columns];
}
}
}
public void print()
{
for(int i = rows; i < board.length; i++)
{
for(int j = columns; j < board[i].length; j++)
{
System.out.print( board[i][j] );
}
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.