//create a grid of cells public void generateCells(int seed) { squares = new Rectangle[gridRows, gridColumns]; //create a grid of rectangles Random r = new Random(seed); int move; for (int i = 0; i < gridRows; i++) { for (int j = 0; j < gridColumns; j++) { //provides a random number between 0 and 7 move = r.Next(numStates); //creates a new cell with the state number cells[i, j] = new Cell(move, palette[move]); cells[i, j].setStates(); } } }
//calculate the orthogonal rule for the next generation public void orthogonal() { //create a blank grid of cells Cell[,] nextCells = new Cell[gridRows, gridColumns]; //loop through each cell for (int i = 0; i < gridRows; i++) { for (int j = 0; j < gridColumns; j++) { //method passes in the orthogonal neighbours //to the current cell and returns a new state State state = calculateCell(cells[i, j], cells[i, getLeft(j)], cells[i, getRight(j)], cells[getTop(i), j], cells[getBottom(i), j]); //create a new cell with the state and color nextCells[i, j] = new Cell(state, this.palette[(int)state]); nextCells[i, j].setStates(); } } cells = nextCells; //update the cells to the new generation }
//calculates the state of the cell of the next generation private State calculateCell(Cell current, Cell one, Cell two, Cell three, Cell four) { State s = new State(); //create a blank state //if any of the cell's four neighbours //has the next state of the current cell if (current.getNext == one.getState || current.getNext == two.getState || current.getNext == three.getState || current.getNext == four.getState) { s = current.getNext; } else { s = current.getState; } return s; }