This isn't really an answer, but more of a hyper-extended comment. I have made a program that simulates the infection of a board in JavaScript, to check on any particular setups.
TL;DR
Right-click on any HTML page and select “Inspect Element” (It's the bottom option in Firefox and Chrome. I don't know where it is otherwise, probably the same place.) Navigate to your browsers console. Copy the minified code and paste it into the command line into the console. (On Firefox, you will need to type “Allow Pasting” before being able to paste it.)
To create a new board, simply choose a name for your board (let's say you choose bob
) and type var bob = new Board();
.
To insert an infected cell, type at position $(x,y)$ bob.infect(x,y);
. NOTE: The counting is zero-based. I have furnished a table in the commented code within the Board
class that tells you which cell is which.
To see what the board looks like, type either bob.print()
or console.log(bob.print());
. The former only displays if it is the last line you have typed, so I'd suggest the latter.
To advance to the next day/stage of infection, type bob.nextDay()
.
To see what the value at position $(x,y)$ is, type console.log(bob.board[y][x]);
.
To see what day/stage of the infection, type bob.day
. There is a zeroeth day, and this means that this is the day before anything has happened; the insertion of infected cells. You can still insert cells on any other day, however.
Code
Commented Code
function Board(){
this.width = this.height = 8; // an 8x8 chess board
var x = new Array(); // creating the 2d array
for(var Zi=0;Zi<this.height;Zi++){
x[Zi] = new Array(); // creating the y-axis of the 2d array
for(var Zj=0;Zj<this.width;Zj++){
x[Zi][Zj] = 0; // populating the y-axis with 0
}
}
this.board = x; // setting board to the 2d array
this.day = 0; // starting the board out on the first day
/*
0 1 2 3 4 5 6 7 } i
+---------------
0 |0 0 0 0 0 0 0 0
1 |0 0 0 0 0 0 0 0
2 |0 0 0 0 0 0 0 0
3 |0 0 0 0 0 0 0 0
4 |0 0 0 0 0 0 0 0
5 |0 0 0 0 0 0 0 0
6 |0 0 0 0 0 0 0 0
7 |0 0 0 0 0 0 0 0
v
j
*/
}
Board.prototype.print = function(){
var string = "\n"; // for display purposes
for(var i=0;i<this.height;i++){
for(var j=0;j<this.width;j++){
if(this.board[i][j]) // displaying if occupied
string += this.board[i][j] + " "; // displaying the (j,i)th entry
else
string += " ";
}
string += "\n"; // spacing for the next array
}
return string;
}
Board.prototype.infect = function(x,y){
// origin = top-left, (0,0)
this.board[y][x] = 2; // setting the (x,y)th entry to be “newly infected”
}
Board.prototype.nextDay = function(){ // advances the day, spreads infection
// getting rid of any 2's from the last day.
for(var y=0;y<this.height;y++){
for(var x=0;x<this.width;x++){
// !x return 0 if x != 0 and 1 if x == 0, so
// !!x returns 1 if x !=0 and 0 if x == 0
// ~~x simply converts x to a number
this.board[y][x] = ~~!!this.board[y][x];
}
}
// creating the board to be the next day's
var nextDayBoard = Array.from(this.board);
for(var y=0;y<this.height;y++){
for(var x=0;x<this.width;x++){
// no need to act on the cell if its already infected
if(!this.board[y][x]){
// establishing “null” value if not present
var topCell = 0;
// checking if there is a cell orthogonally above
if(y>0){
// get the top cell's coordinate
var topCell = this.board[y-1][x];
}
// establishing “null” value
var leftCell = 0;
// checking if there is a cell orthogonally left
if(x>0){
// get the left cell's coordinates
var leftCell = this.board[y][x-1];
}
// establishing “null” value
var rightCell = 0;
// checking for cell orthogonally right
if(x<this.width-1){ // minus 1 due to zero-based indexing
var rightCell = this.board[y][x+1];
}
// establishing “null” value
var bottomCell = 0;
// checking for cell orthogonally below
if(y<this.height-1){ // minus 1 due to zero-based indexing
var bottomCell = this.board[y+1][x];
}
// creating array of adjacent cells
var adjacentCells = [topCell,leftCell,rightCell,bottomCell];
/*
Now, the current cell is infected iff
there are two orthogonally adjacent
cells. Therefore, taking the sum of
adjacentCells will give us how many
cells are infected that are adjacent
to the current cell.
*/
// taking the sum of adjacentCells
var infected = adjacentCells.reduce(function(previousCell,currentCell){
if(currentCell != 2) return previousCell + currentCell;
return previousCell;
},0);
// if the cell has become infected:
if(infected>=2){
// setting the current sell to newly infected state,
// so there is now no way for it to effect other
// cells
nextDayBoard[y][x] = 2;
}
// we do not need to change the value otherwise,
// so an else statement is not required
}
}
}
// incrementing the day.
this.day++;
// setting the board to the next-day board.
this.board = nextDayBoard;
}
// different variables in for loops to makes sure
// no variable conflicts happen
// ----
// creating test board
var board = new Board();
board.infect(1,1);
board.infect(3,1);
console.log(board.print());
board.nextDay();
console.log(board.print());
// success!
// creating example board
var example = new Board();
// group 1
example.infect(1,1);
example.infect(2,1);
// group 2
example.infect(0,4);
example.infect(0,5);
example.infect(0,6);
example.infect(1,5);
// group 3 (last)
example.infect(3,4);
example.infect(4,3);
example.infect(5,4);
example.infect(5,6);
example.infect(6,5);
example.infect(6,7);
example.infect(7,6);
console.log(example.print());
example.nextDay();
console.log(example.print());
example.nextDay();
console.log(example.print());
Minified Code
function Board(){this.width=this.height=8;var x=new Array();for(var Zi=0;Zi<this.height;Zi++){x[Zi]=new Array();for(var Zj=0;Zj<this.width;Zj++){x[Zi][Zj]=0;}} this.board=x;this.day=0;} Board.prototype.print=function(){var string="n";for(var i=0;i<this.height;i++){for(var j=0;j<this.width;j++){if(this.board[i][j]) string+=this.board[i][j]+" ";else string+=" ";} string+="n";} return string;} Board.prototype.infect=function(x,y){this.board[y][x]=2;} Board.prototype.nextDay=function(){for(var y=0;y<this.height;y++){for(var x=0;x<this.width;x++){this.board[y][x]=~~!!this.board[y][x];}} var nextDayBoard=Array.from(this.board);for(var y=0;y<this.height;y++){for(var x=0;x<this.width;x++){if(!this.board[y][x]){var topCell=0;if(y>0){var topCell=this.board[y-1][x];} var leftCell=0;if(x>0){var leftCell=this.board[y][x-1];} var rightCell=0;if(x<this.width-1){var rightCell=this.board[y][x+1];} var bottomCell=0;if(y<this.height-1){var bottomCell=this.board[y+1][x];} var adjacentCells=[topCell,leftCell,rightCell,bottomCell];var infected=adjacentCells.reduce(function(previousCell,currentCell){if(currentCell!=2)return previousCell+currentCell;return previousCell;},0);if(infected>=2){nextDayBoard[y][x]=2;}}}} this.day++;this.board=nextDayBoard;}