예제 #1
0
 public void Render(IBattleshipGrid battleshipGrid)
 {
     _textWindow.Clear();
     _gridWindow.Clear();
     DrawGrid(battleshipGrid);
     _writer?.Flush();
 }
예제 #2
0
        private void DrawGrid(IBattleshipGrid grid)
        {
            var size      = grid.Size;
            var shipCells = grid.Ships.SelectMany(t => t.Cells);
            var line      = new Draw(_gridWindow);

            DrawHeaders(size);

            const int cellWidth = 4, cellHeight = 2;
            const int startingX = 3, startingY = 1;

            int x = startingX, y = startingY;

            for (int row = 1; row <= size; ++row)
            {
                for (int column = 1; column <= size; ++column)
                {
                    var color   = Colors.WhiteOnBlack;
                    var cell    = new GridCell(column, row);
                    var shipHit = shipCells.Any(t => t.Equals(cell) && t.Hit);
                    if (shipHit || grid.MissedShots.Contains(cell))
                    {
                        color = Colors.BlackOnWhite;
                    }

                    line.Box(x, y, x + cellWidth, y + cellHeight, string.Empty, color, Colors.BlackOnWhite);

                    if (shipHit)
                    {
                        var ship     = grid.Ships.Single(t => t.Cells.Contains(cell));
                        var hitColor = ship.HasSink ? ConsoleColor.Gray : ConsoleColor.Red;
                        _gridWindow.PrintAtColor(color.Foreground, x + 2, y + 1, "X", hitColor);
                    }

                    x += cellWidth + 1;
                }

                y += cellHeight + 1;
                x  = startingX;
            }
        }
예제 #3
0
 public void Setup()
 {
     _battleship = Freeze <IBattleshipGrid>();
     _ui         = Freeze <IGameUi>();
 }
예제 #4
0
파일: Game.cs 프로젝트: Yozer/Battleships
 public Game(IBattleshipGrid battleshipGrid, IGameUi ui)
 {
     _battleshipGrid = battleshipGrid;
     _ui             = ui;
 }