Beispiel #1
0
 private Fleet TryToMakeFleet()
 {
     m_grid = new Grid();
     Fleet fleet = new Fleet();
     foreach (int length in Fleet.ShipLengths)
     {
         Ship ship = MakeShip(length);
         if (ship == null)
             return null;
         fleet.AddShip(ship);
     }
     return fleet;
 }
Beispiel #2
0
        public void GetStartingSquaresTest()
        {
            Grid grid = new Grid();
            int length = 3;
            Grid.Orientation orientation = Grid.Orientation.Horizontal;
            Square[] actual = grid.GetFreeStartingSquares(length, orientation);
            Assert.AreEqual(80, actual.Length);

            Square startingSquare = GetSquare(actual, 3, 3);
            grid.MakeShip(startingSquare, length, orientation);

            actual = grid.GetFreeStartingSquares(length, orientation);
            Assert.AreEqual(59, actual.Length);
            Assert.IsNull(GetSquare(actual, 3, 3));
            Assert.IsNull(GetSquare(actual, 3, 4));
            Assert.IsNull(GetSquare(actual, 3, 5));
            Assert.IsNull(GetSquare(actual, 2, 2));
            Assert.IsNull(GetSquare(actual, 4, 6));
        }
Beispiel #3
0
        public void GetInitialStartingSquaresTest()
        {
            Grid grid = new Grid();
            int length = 3;
            Grid.Orientation orientation = Grid.Orientation.Horizontal;
            Square[] actual = grid.GetFreeStartingSquares(length, orientation);
            Assert.AreEqual(80, actual.Length);

            Assert.IsNotNull(GetSquare(actual, 3, 3));
            Assert.IsNull(GetSquare(actual, 4, 8));

            length = 4;
            orientation = Grid.Orientation.Vertical;
            actual = grid.GetFreeStartingSquares(length, orientation);
            Assert.AreEqual(70, actual.Length);

            Assert.IsNotNull(GetSquare(actual, 5, 2));
            Assert.IsNull(GetSquare(actual, 7, 0));
        }
Beispiel #4
0
        public void FireTest()
        {
            Grid grid = new Grid();
            int length = 3;
            Grid.Orientation orientation = Grid.Orientation.Horizontal;
            Square[] squares = grid.GetFreeStartingSquares(length, orientation);

            Square startingSquare = squares[10];
            Ship ship = grid.MakeShip(startingSquare, length, orientation);

            int row = startingSquare.Row;
            int column = startingSquare.Column;

            Assert.AreEqual(HitResults.Hit, ship.Fire(row, column));
            Assert.AreEqual(HitResults.Hit, ship.Fire(row, column + 1));
            Assert.AreEqual(HitResults.Sunk, ship.Fire(row, column + 2));
            Assert.AreEqual(HitResults.Missed, ship.Fire(row - 1, column));

            grid = new Grid();
            length = 5;
            orientation = Grid.Orientation.Vertical;
            squares = grid.GetFreeStartingSquares(length, orientation);

            startingSquare = squares[10];
            ship = grid.MakeShip(startingSquare, length, orientation);

            row = startingSquare.Row;
            column = startingSquare.Column;

            Assert.AreEqual(HitResults.Missed, ship.Fire(row, column - 1));
            Assert.AreEqual(HitResults.Hit, ship.Fire(row + length - 1, column));
            Assert.AreEqual(HitResults.Hit, ship.Fire(row + length - 2, column));
            Assert.AreEqual(HitResults.Hit, ship.Fire(row + length - 3, column));
            Assert.AreEqual(HitResults.Hit, ship.Fire(row + length - 3, column));
            Assert.AreEqual(HitResults.Hit, ship.Fire(row + length - 4, column));
            Assert.AreEqual(HitResults.Sunk, ship.Fire(row + length - 5, column));
            Assert.AreEqual(HitResults.Sunk, ship.Fire(row + length - 5, column));
            Assert.AreEqual(HitResults.Sunk, ship.Fire(row + length - 4, column));
        }