/// <summary> /// Initializes a new instance of the <see cref="Game"/> class. /// </summary> /// <param name="rules">The rules which should be used.</param> /// <param name="lifeBoard">The life board on which the game is played.</param> /// <exception cref="ArgumentNullException"> /// rules /// or /// lifeBoard /// </exception> public Game(Rules rules, LifeBoard lifeBoard) { Rules = rules ?? throw new ArgumentNullException(nameof(rules)); _lifeBoard = lifeBoard ?? throw new ArgumentNullException(nameof(lifeBoard)); Initialize(); }
// Captures and releases capture of mouse for LifeBoard void ButtonLeave(object sender, RoutedEventArgs e) { if (LifeBoard.IsEnabled) { LifeBoard.CaptureMouse(); LifeBoard.ReleaseMouseCapture(); } }
private void SizeChangedHandler(object sender, Windows.UI.Xaml.SizeChangedEventArgs e) { int columnsN = NumberOfTilesPerDimension(e.NewSize.Width); int rowsN = NumberOfTilesPerDimension(e.NewSize.Height); if (board == null) { board = new LifeBoard(columnsN, rowsN); } else { board = new LifeBoard(board, columnsN, rowsN); } Rectangle[,] controlsN = new Rectangle[columnsN, rowsN]; double colSeparation = SeparationWidth(e.NewSize.Width, columnsN); double rowSeparation = SeparationWidth(e.NewSize.Height, rowsN); for (int r = 0; r < Math.Max(rows, rowsN); r++) { for (int c = 0; c < Math.Max(columns, columnsN); c++) { if (r < rows && c < columns) { if (r < rowsN && c < columnsN) { Rectangle rectangle = controls[c, r]; rectangle.SetModel(board, c, r); Canvas.SetLeft(rectangle, c * (RectangleFactory.tileDimension + colSeparation)); Canvas.SetTop(rectangle, r * (RectangleFactory.tileDimension + rowSeparation)); controlsN[c, r] = rectangle; } else { Children.Remove(controls[c, r]); } } else { Rectangle rectangle = RectangleFactory.CreateRectangle(board, c, r); Children.Add(rectangle); Canvas.SetLeft(rectangle, c * (RectangleFactory.tileDimension + colSeparation)); Canvas.SetTop(rectangle, r * (RectangleFactory.tileDimension + rowSeparation)); controlsN[c, r] = rectangle; } } } controls = controlsN; columns = columnsN; rows = rowsN; }
public LifeBoard(LifeBoard otherBoard, int width, int height) : this(width, height) { for (int y = 0; y < Math.Min(otherBoard.Height, Height); y++) { for (int x = 0; x < Math.Min(otherBoard.Width, Width); x++) { this[x, y] = otherBoard[x, y]; } } }
public static void SetModel(this Rectangle r, LifeBoard board, int column, int row) { r.Tag = new RectangleTag() { Board = board, Column = column, Row = row }; r.Fill = board[column, row] ? RectangleFactory.aliveBrush : RectangleFactory.deadBrush; }
public void CopyTo(LifeBoard lifeBoard) { int width = Math.Min(this.width, lifeBoard.width); int height = Math.Min(this.height, lifeBoard.height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { lifeBoard[i, j] = this[i, j]; } } }
public MainWindow() { InitializeComponent(); Dimension dim = new Dimension(50, 50); LifeBoard lifeBoard = new LifeBoard(dim); lifeBoard[1, 1] = true; Dimension dim2 = new Dimension(25, 25); LifeBoard lifeBoadr2 = new LifeBoard(dim2); lifeBoard.CopyTo(lifeBoadr2); lifeBoard[5,10] = true; int i = 0; }
public static Rectangle CreateRectangle(LifeBoard board, int column, int row) { Rectangle rectangle = new Rectangle() { Width = tileDimension, Height = tileDimension, Fill = new SolidColorBrush(deadColor), Tag = false }; rectangle.SetModel(board, column, row); rectangle.Tapped += (s, te) => { rectangle.Toggle(); }; return(rectangle); }
public void NextStep() { LifeBoard newLifeBoard = new LifeBoard(this.dim); for (int i = 0; i < dim.Width; i++) { for (int j = 0; j < dim.Height; j++) { if (IsCurrentCellAlive(i, j)) { int currCellAliveNeighbors = CalculateAliveNeighbors(i, j); if (currCellAliveNeighbors < 2) //less then 2 - become dead { newLifeBoard[i, j] = DEAD; } else if (currCellAliveNeighbors >= 4) // 4 or more - become dead { newLifeBoard[i, j] = DEAD; } else if (currCellAliveNeighbors == 2 || currCellAliveNeighbors == 3) //exactly 2 or 3 - become alive { newLifeBoard[i, j] = ALIVE; } } else { if (CalculateAliveNeighbors(i, j) == 3) { newLifeBoard[i, j] = ALIVE; } else { newLifeBoard[i, j] = DEAD; } } } } }
public LifeModel(Dimension dim) { this.dim = dim; lifeBoard = new LifeBoard(this.dim); }