/// <summary> /// Event triggered by dragging over a cell. Cycles the dragged over cell between dead and alive /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DragOverCell(object sender, RoutedEventArgs e) { if (isMouseDown) { Cell c = sender as Cell; c.DeadAliveSwitch(); } }
/// <summary> /// Allows for cloning cells /// Useful for making non-destructive copies /// </summary> /// <returns></returns> public object Clone() { Cell clonedCell = new Cell(this.x, this.y) { Style = FindResource("CellView") as Style }; if (isAlive) { clonedCell.DeadAliveSwitch(); } return(clonedCell); }
/// <summary> /// Applies basic interaction logic for cell development /// If a cell has 2 or 3 live neighbors, survives to next iteration /// If a dead cell has exactly 3 live neighbors, it becomes alive /// Every other cell dies by next iteration /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Iterate(object sender, EventArgs e) { // Cells that will undergo changes over next iteration // Dead cells in this list will become alive and vice-versa ArrayList changedCells = new ArrayList(); for (int x = 0; x < 40; x++) { for (int y = 0; y < 40; y++) { Cell c = coreGrid[x, y]; // Count live neighbors int liveNeighbors = 0; for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { if (i < 0 || i >= 40 || j < 0 || j >= 40) { continue; } if (i == x && j == y) { continue; } if (coreGrid[i, j].isAlive) { liveNeighbors++; } } } //If the cell is still alive if (c.isAlive) { if (liveNeighbors == 2 || liveNeighbors == 3) { //Do nothing } //Kill the cell else { changedCells.Add(c); } } //If it's dead else { if (liveNeighbors == 3) { changedCells.Add(c); } } } } //Apply changes to cells for (int i = 0; i < changedCells.Count; i++) { Cell temp = changedCells[i] as Cell; temp.DeadAliveSwitch(); } }
/// <summary> /// Same interaction logic as DragOverCell but upon clicking a cell /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClickCell(object sender, MouseButtonEventArgs e) { Cell c = sender as Cell; c.DeadAliveSwitch(); }