Conway’s Game of Life Complete the todos in model.java. Complete the todos in vi
ID: 3595492 • Letter: C
Question
Conway’s Game of Life
Complete the todos in model.java.
Complete the todos in view.java.
Add code to the body tag to call the paint method when the doc (life.html) is loaded, and whenever the window size changes.
Add buttons called reset and random to the doc. When reset is pressed, the initializeBoard method for the CGoL instance should be called followed by a call to paint. Reset may initialize the board to any pattern you wish. When random is pressed, a random pattern should be created (by a call to initializeBoard followed by a call to paint).
Add a button next to the doc. When next is pressed, the nextGeneration method for the CGoL instance should be called followed by a call to paint.
Use only the wikipedia page as a reference for your implementation. Other sources may use slightly different rules which will yield different results (and lower your grade). Also, don't submit code from the internet as your own (which will result in a grade of 0).
It should contain the following –
HTML
Javascript
MVC – Model View Controller
Documentation
Unit testing – including assert
Any live cell with fewer than 2 live neighbors dies. (as if caused by underpopulation)
Any live cell with 2/3 live neighbors lives on to the next generation.
Any live cell with more than 3 live neighbors dies. (as if by overpopulation)
Any dead cell with exactly 3 live neighbors becomes a live cell (as if by reproduction).
Model.js
View.js – contains functions (no classes) to paint the board.
Controller.js - contains functions (no classes) to paint the board.
Life.html
Javascript “strict mode” . Declare turn on via : use strict. Restriction in strict mode.
Using a variable without declaring it.
Writing to a read only property.
Adding a property to an object whose extensible attribute is set to “false”
Defining a property more than once in an object literal.
Jsdoc Rules :
Required Jsdoc comments at the top of every file , before every class and before every method.
Qunit
Obtains a Qunit.css and qunit.js
Create the tests(test.js)
Create an html file (run-tests.html) to run the tests.
Explanation / Answer
Code:
import java.util.*;
import edu.princeton.cs.introcs.StdDraw;
public class GameOfLife
{
public static void main(String[] args)
{
final int ROWS_NUM = 100;
final int COLS_NUM = 100;
Boolean[][] curGen = new Boolean[ROWS_NUM][COLS_NUM];
for (int row = 0; row < ROWS_NUM; row++)
{
Arrays.fill(curGen[row], false);
}
Random random = new Random();
for (int row = 0; row < ROWS_NUM; row++)
{
for (int col = 0; col < COLS_NUM; col++)
{
curGen[row][col] = random.nextBoolean();
}
}
StdDraw.setCanvasSize(COLS_NUM, ROWS_NUM);
StdDraw.setYscale(0, ROWS_NUM);
StdDraw.setXscale(0, COLS_NUM);
StdDraw.setPenRadius(0);
StdDraw.setPenColor(StdDraw.BLACK);
while (true)
{
curGen = countNextGen(curGen, ROWS_NUM, COLS_NUM);
StdDraw.clear();
for (int row = 0; row < ROWS_NUM; row++)
{
for (int col = 0; col < COLS_NUM; col++)
{
if (curGen[row][col] == true)
{
StdDraw.point(col, row);
}
}
}
}
}
public static Boolean[][] countNextGen(Boolean[][] curGen, int rowsNum, int colsNum)
{
Boolean[][] nextGen = new Boolean[rowsNum][];
for (int row = 0; row < rowsNum; row++)
{
nextGen[row] = Arrays.copyOf(curGen[row], colsNum);
}
for (int row = 0; row < rowsNum; row++)
{
for (int col = 0; col < colsNum; col++)
{
int numOfNeighbours = countCellNeighbours(curGen, rowsNum, colsNum, row, col);
if ((numOfNeighbours < 2) || (numOfNeighbours > 3))
{
nextGen[row][col] = false;
}
if (numOfNeighbours == 2)
{
nextGen[row][col] = curGen[row][col];
}
if (numOfNeighbours == 3)
{
nextGen[row][col] = true;
}
}
}
return nextGen;
}
public static int countCellNeighbours(Boolean[][] curGen, int rowsNum, int colsNum, int row, int col) {
int numOfNeighbours = 0;
if ((row > 0) && (row < rowsNum - 1) && (col > 0) && (col < colsNum - 1))
{
if (curGen[row - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][col + 1])
{
numOfNeighbours++;
}
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][col + 1])
{
numOfNeighbours++;
}
}
else if (row == 0)
{
if (col == 0)
{
if (curGen[rowsNum - 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col + 1])
{
numOfNeighbours++;
}
if (curGen[row][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][col + 1])
{
numOfNeighbours++;
}
}
else if (col == colsNum - 1)
{
if (curGen[rowsNum - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][col])
{
numOfNeighbours++;
}
if (curGen[rowsNum - 1][0])
{
numOfNeighbours++;
}
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][0])
{
numOfNeighbours++;
}
if (curGen[row + 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][0])
{
numOfNeighbours++;
}
}
else if (row == rowsNum - 1)
{
if (col == 0)
{
if (curGen[row - 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][col + 1])
{
numOfNeighbours++;
}
if (curGen[row][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
if (curGen[0][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[0][col])
{
numOfNeighbours++;
}
if (curGen[0][col + 1])
{
numOfNeighbours++;
}
}
else if (col == colsNum - 1)
{
if (curGen[row - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][0])
{
numOfNeighbours++;
}
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][0])
{
numOfNeighbours++;
}
// below
if (curGen[0][col - 1])
{
numOfNeighbours++;
}
if (curGen[0][col])
{
numOfNeighbours++;
}
if (curGen[0][0])
{
numOfNeighbours++;
}
}
else if (col == 0)
{
if (curGen[row - 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][col + 1])
{
numOfNeighbours++;
}
if (curGen[row][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row][col + 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][colsNum - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][col + 1])
{
numOfNeighbours++;
}
}
else if (col == colsNum - 1)
{
if (curGen[row - 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row - 1][col])
{
numOfNeighbours++;
}
if (curGen[row - 1][0])
{
numOfNeighbours++;
}
if (curGen[row][col - 1])
{
numOfNeighbours++;
}
if (curGen[row][0])
{
numOfNeighbours++;
}
if (curGen[row + 1][col - 1])
{
numOfNeighbours++;
}
if (curGen[row + 1][col])
{
numOfNeighbours++;
}
if (curGen[row + 1][0])
{
numOfNeighbours++;
}
}
return numOfNeighbours;
}