Matrix class
package gameoflife;
public class Matrix {
Boolean[][] matrix;
int numRows;
int numCols;
public Matrix(int rows, int cols) {
matrix = new Boolean[rows][cols];
numRows = rows;
numCols = cols;
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
matrix[j][i] = false;
}
}
}
//constructor with all values false
public Matrix(int size) {
matrix = new Boolean[size][size];
numRows = size;
numCols = size;
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
matrix[j][i] = false;
}
}
}
//constructor
void set(int x, int y, Boolean state){
matrix[x][y] = state;
}
//sets a value within the matrix
Boolean state(int x, int y){
return matrix[x][y];
}
//gets a value within the matrix
int rows(){
return numRows;
}
//gets the number of rows in the matrix
int cols(){
return numCols;
}
//gets the number of columns in the matrix
}
GameGrid class
package gameoflife;
public class GameGrid extends Matrix{
public GameGrid(int rows, int cols){
//gameGrid = new Boolean[rows][cols];
super(rows+2, cols+2);
}
//creates a gamegrid with set rows and cols
public GameGrid(int size){
//gameGrid = new Boolean[size][size];
super(size+2) /*me*/;
}
//creates a new gamegrid which is size x size
Boolean isAlive(int x, int y){
return matrix[x][y];
}
//returns the state of a coordinate
short numNeighbors(int x, int y) {
short numNeighbors;
numNeighbors = 0;
if (matrix[x+1][y] == true) numNeighbors++;
if (matrix[x][y+1] == true) numNeighbors++;
if (matrix[x+1][y+1] == true) numNeighbors++;
if (matrix[x][y-1] == true) numNeighbors++;
if (matrix[x-1][y] == true) numNeighbors++;
if (matrix[x+1][y-1] == true) numNeighbors++;
if (matrix[x-1][y+1] == true) numNeighbors++;
if (matrix[x-1][y-1] == true) numNeighbors++;
return numNeighbors;
}
//returns the number of neighbours that a coordinate has
void deactivate(int x, int y){
matrix[x][y] = false;
}
//turns a coord to false
void activate(int x, int y) {
matrix[x][y] = true;
}
//turns a coord to true
void nextGen(){
Boolean[][] newMatrix = new Boolean[rows()][cols()];
for (int i = 1; i < cols()-1; i++){
for (int j = 1; j < rows()-1; j++){
//if a cell has 3 neighbours, become or stay true
if (numNeighbors(j, i) == 3) newMatrix[j][i] = true;
//if it doesn't have 3 neighbours, become or stay false
else newMatrix[j][i] = false;
}
}
matrix = newMatrix;
}
//makes matrix represent the next generation
String showGrid(){
String arrayOutput = "";
//writes each row as a line, then goes to the next and does the same.
for(int k = 1; k < cols()-1; k++) {
//opens a square bracket on every row of the array
arrayOutput += "[ ";
//lists the row in the square brackets with the correct state in each slot
for(int l = 1; l < rows()-1; l++){
if (state(l, k))
{arrayOutput += " * ";}
else
{arrayOutput += " . ";}
}
//closes the square brackets and then starts a new line for the next row
arrayOutput += "]\n";
}
return arrayOutput;
}
//outputs the gamegrid
}
GameOfLife class
package gameoflife;
import javax.swing.JOptionPane;
public class GameOfLife {
public static void main(String args[]) {
int width = Integer.parseInt(JOptionPane.showInputDialog("How many spaces wide do you want your grid to be?"));
int height = Integer.parseInt(JOptionPane.showInputDialog("How many spaces high do you want your grid to be?"));
GameGrid myGrid = new GameGrid(width, height);
int numOrganisms = Integer.parseInt(JOptionPane.showInputDialog("How many organisms do you want to plot?"));
int xcoord;
int ycoord;
for (int i = 0; i < numOrganisms; i++){
xcoord = (1+Integer.parseInt(JOptionPane.showInputDialog("What is the x coordinate of organism #"+(i+1)+"?")));
ycoord = (1+Integer.parseInt(JOptionPane.showInputDialog("What is the y coordinate of organism #"+(i+1)+"?")));
myGrid.activate(xcoord, ycoord);
}
int numGenerations = Integer.parseInt(JOptionPane.showInputDialog("How many generations do you want to be shown?"));
for (int j = 0; j < numGenerations; j++){
JOptionPane.showMessageDialog(null,"generation " + (j+1) + ":\n\n" + myGrid.showGrid());
myGrid.nextGen();
}
}
}