public void TestIfFieldPropertyReturnTheSameFieldItIsSetTo() { var field = new IBalloon[5, 10]; var gameModel = new GameModel(); gameModel.Field = field; Assert.AreSame(field, gameModel.Field); }
public void TestIfFieldPropertyReturnsTheSameValueItIsSetTo() { var game = new GameModel(); var field = new IBalloon[5, 10]; game.Field = field; Assert.AreEqual(field, game.Field); }
/// <summary> /// Generates a random matrix of balloons that are not popped. /// </summary> /// <param name="field">The field.</param> public void RandomizeBalloonField(IBalloon[,] field) { new QueryableMatrix<IBalloon>(field) .ForEach(x => { x.Number = this.rng.Next(MinBalloonValue, MaxBalloonValue + 1); x.IsPopped = false; }); }
/// <summary> /// Initializes a new instance of the <see cref="GameModel" /> class. /// </summary> /// <param name="balloonFiller"></param> public GameModel(IBalloon balloonFiller = null) { if (balloonFiller == null) { balloonFiller = new Balloon(); } this.field = new QueryableMatrix<IBalloon>(5, 10).Select(x => { var balloon = balloonFiller.Clone(); balloon.IsPopped = false; return balloon; }) .ToMatrix(5, 10); this.userMovesCount = 0; }
public void PrintField(IBalloon[,] matrix) { Console.Clear(); this.PrintColumnIndeces(); this.PrintDashedLine(1 + (matrix.GetLength(1) * 2)); for (byte i = 0; i < matrix.GetLongLength(0); i++) { this.PrintRowBeggining(i); for (byte j = 0; j < matrix.GetLongLength(1); j++) { this.PrintBalloon(matrix[i, j]); } this.SetConsoleColorToDefault(); this.PrintRowEnd(); } this.PrintDashedLine(1 + (matrix.GetLength(1) * 2)); }
/// <summary> /// Updates the view with information from the provided balloon matrix. /// </summary> /// <param name="matrix"></param> public void PrintField(IBalloon[,] matrix) { if (this.cachedField == null) { this.InitializeField(matrix); return; } int rows = matrix.GetLength(0); int columns = matrix.GetLength(1); int popped = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { var childToUpdate = this.cachedField[i, j]; childToUpdate.Style.Color = Colors[matrix[i, j].IsPopped ? 0 : matrix[i, j].Number]; if (matrix[i, j].IsPopped) { childToUpdate.Content = PoppedBalloonContent; popped++; } } } if (popped >= (rows * columns) - 1) { foreach (var element in this.cachedField) { element.Content = WholeBalloonContent; } } }
/// <summary> /// Initializes a new instance of the <see cref="Memento"/> class which stores the board's state. /// </summary> /// <param name="board">the object that is being stored for restoration at a later point.</param> public Memento(IBalloon[,] board) { this.Board = board; this.Rows = board.GetLength(0); this.Cols = board.GetLength(1); }
private void PrintBalloon(IBalloon balloon) { if (balloon.IsPopped) { Console.Write(EmptyCell); } else { this.SetConsoleColor(balloon.Number); Console.Write(CellPrintingFormat, balloon.Number); } }
/// <summary> /// Provides randomization of a balloon field represented as two-dimensional array. /// </summary> /// <param name="field">The array representation of a game field.</param> public void RandomizeBalloonField(IBalloon[,] field) { this.fieldRandomizer.RandomizeBalloonField(field); }
/// <summary> /// Provides popping logic based on field, row and column. /// </summary> /// <param name="field">Array representation of the game field.</param> /// <param name="row">The number of rows in the field.</param> /// <param name="column">The number of columns in the field.</param> public void PopBalloons(IBalloon[,] field, int row, int column) { this.balloonPopper.PopBalloons(field, row, column); }
/// <summary> /// Moves all non-popped balloons vertically to the bottom of the array, effectively simulating gravity. /// </summary> /// <param name="field">Array representation of the game field.</param> public void LetBalloonsFall(IBalloon[,] field) { this.balloonPopper.LetBalloonsFall(field); }
/// <summary> /// Determines whether a game is over. /// </summary> /// <param name="matrix">Array representation of the game field.</param> /// <returns>Returns whether there remains at least one unpopped balloon.</returns> public bool GameIsOver(IBalloon[,] matrix) { var fieldIsEmpty = new QueryableMatrix<IBalloon>(matrix).All(balloon => balloon.IsPopped); return fieldIsEmpty; }
public void BoardSaveMementoShouldReturnAMemento() { var board = new Board(5, 5, this.randomGenerator); var memento = board.SaveMemento().Board; var boardAsMatrix = new IBalloon[5, 5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { boardAsMatrix[i, j] = board[i, j]; } } var anotherMemento = new Memento(boardAsMatrix).Board; Assert.AreEqual(memento, anotherMemento); }
private void InitializeField(IBalloon[,] matrix) { int rows = matrix.GetLength(0); int columns = matrix.GetLength(1); this.cachedField = new TextElement[rows, columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { // TODO: extract in resource provider var childToAdd = new TextElement(WholeBalloonContent); childToAdd.Style = new Style() { PaddingLeft = j * BalloonPaddingLeftMultiplier, PaddingTop = i * BalloonPaddingTopMultiplier, Color = Colors[matrix[i, j].IsPopped ? 0 : matrix[i, j].Number] }; childToAdd.Id = i + " " + j; // cache this.cachedField[i, j] = childToAdd; this.View.AddChildToParent(this.field, childToAdd); // link to neighbors if (i > 0) { childToAdd.LinkTo(ConsoleKey.UpArrow, this.cachedField[i - 1, j]); } if (j > 0) { childToAdd.LinkTo(ConsoleKey.LeftArrow, this.cachedField[i, j - 1]); } } } for (int i = columns - 1; i >= 0; i--) { this.cachedField[rows - 1, i].LinkTo(ConsoleKey.DownArrow, this.View[RestartButtonId]); } }
/// <summary> /// Introduces updates on the view bases on the provided IBalloon two-dimensional array. /// </summary> /// <param name="matrix">The array which is used as a blueprint for introducing changes to the view.</param> public void PrintField(IBalloon[,] matrix) { var rowsCount = matrix.GetLength(0); var colsCount = matrix.GetLength(1); if (this.balloons == null) { this.InitializeBalloonImageMatrix(rowsCount, colsCount); } for (int row = 0; row < rowsCount; row++) { for (int col = 0; col < colsCount; col++) { var colorNumber = matrix[row, col].IsPopped ? 0 : matrix[row, col].Number; var sourcePath = string.Format(this.Resources.ImagePathTemplate, this.Resources.Colors[colorNumber]); this.balloons[row, col].SetSource(sourcePath); } } this.Window.UserMoves = 3.ToString(); }