Exemple #1
0
        public void IfOverlapInShips()
        {
            Board board = new Board();

            // let's put a carrier at (6,10), (5,10), (4,10), (3,10), (2,10)
            var carrierRequest = new ShipCoordinates()
            {
                Coordinate = new Coordinates(6, 10),
                Direction  = ShipDirections.Left,
                ShipType   = ShipType.Carrier
            };

            var carrierResponse = board.PlaceShip(carrierRequest);

            Assert.AreEqual(ShipPlacements.Ok, carrierResponse);

            // Destroyer at (4,10)
            var destroyerRequest = new ShipCoordinates()
            {
                Coordinate = new Coordinates(4, 10),
                Direction  = ShipDirections.Down,
                ShipType   = ShipType.Battleship
            };

            var destroyerResponse = board.PlaceShip(destroyerRequest);

            Assert.AreEqual(ShipPlacements.Overlap, destroyerResponse);
        }
 private void AssertShipCoordinatesAreEqual(ShipCoordinates actual, int x1, int y1, int x2, int y2)
 {
     Assert.AreEqual(x1, actual.Begin.X);
     Assert.AreEqual(y1, actual.Begin.Y);
     Assert.AreEqual(x2, actual.End.X);
     Assert.AreEqual(y2, actual.End.Y);
 }
Exemple #3
0
 public void MarkNeighboringCells(ShipCoordinates shipCoords)
 {
     MarkAboveNeighboringCells(shipCoords);
     MarkBelowNeighboringCells(shipCoords);
     MarkRightNeighboringCells(shipCoords);
     MarkLeftNeighboringCells(shipCoords);
 }
Exemple #4
0
        public static ShipCoordinates GetLocation(string location)
        {
            string strX, strY, strDirection; int x, y;

            if (location.Split(',').Length == 2)
            {
                if (location.Split(',')[0].Trim().Length > 1)
                {
                    strX         = location.Split(',')[0].Trim().Substring(0, 1);
                    strY         = location.Split(',')[0].Trim().Substring(1);
                    strDirection = location.Split(',')[1].ToUpper().Trim();

                    x = GetNumberFromLetter(strX);
                    if (x > 0 && x < 11 && int.TryParse(strY, out y) && y > 0 && y < 11 &&
                        (strDirection == "L" ||
                         strDirection == "R" ||
                         strDirection == "U" ||
                         strDirection == "D"))
                    {
                        ShipCoordinates ShipToPlace = new ShipCoordinates();
                        ShipToPlace.Direction  = getDirection(strDirection);
                        ShipToPlace.Coordinate = new Coordinates(x, y);
                        return(ShipToPlace);
                    }
                }
            }
            return(null);
        }
Exemple #5
0
        public ShipPlacements PlaceShip(ShipCoordinates request)
        {
            if (_currentShipIndex > 4)
            {
                throw new Exception("You can not add another ship, 5 is the limit!");
            }

            if (!IsValidCoordinate(request.Coordinate))
            {
                return(ShipPlacements.NotEnoughSpace);
            }

            Ship newShip = CreateShip.CreateShips(request.ShipType);

            switch (request.Direction)
            {
            case ShipDirections.Down:
                return(PlaceShipDown(request.Coordinate, newShip));

            case ShipDirections.Up:
                return(PlaceShipUp(request.Coordinate, newShip));

            case ShipDirections.Left:
                return(PlaceShipLeft(request.Coordinate, newShip));

            default:
                return(PlaceShipRight(request.Coordinate, newShip));
            }
        }
Exemple #6
0
        public static ShipCoordinates GetLocationFromComputer()
        {
            ShipCoordinates ShipToPlace = new ShipCoordinates();

            ShipToPlace.Direction  = getDirection(GetRandom.GetDirection());
            ShipToPlace.Coordinate = new Coordinates(GetRandom.GetLocation(), GetRandom.GetLocation());
            return(ShipToPlace);
        }
Exemple #7
0
        private void MarkAboveNeighboringCells(ShipCoordinates shipCoords)
        {
            if (shipCoords.Begin.X == 0)
            {
                return;
            }
            var x = shipCoords.Begin.X - 1;

            MarkVerticalNeighboringCells(shipCoords, x);
        }
Exemple #8
0
        private void MarkBelowNeighboringCells(ShipCoordinates shipCoords)
        {
            if (shipCoords.End.X == _game.Matrix.Size - 1)
            {
                return;
            }
            var x = shipCoords.End.X + 1;

            MarkVerticalNeighboringCells(shipCoords, x);
        }
Exemple #9
0
        private void MarkRightNeighboringCells(ShipCoordinates shipCoords)
        {
            if (shipCoords.End.Y == _game.Matrix.Size - 1)
            {
                return;
            }
            var y = shipCoords.End.Y + 1;

            MarkHorizontalNeighboringCells(shipCoords, y);
        }
Exemple #10
0
        private void MarkLeftNeighboringCells(ShipCoordinates shipCoords)
        {
            if (shipCoords.Begin.Y == 0)
            {
                return;
            }
            var y = shipCoords.Begin.Y - 1;

            MarkHorizontalNeighboringCells(shipCoords, y);
        }
Exemple #11
0
        private void Submarine(Board board)
        {
            var placingTheShip = new ShipCoordinates()
            {
                Coordinate = new Coordinates(4, 5),
                Direction  = ShipDirections.Right,
                ShipType   = ShipType.Submarine
            };

            board.PlaceShip(placingTheShip);
        }
Exemple #12
0
        private void Battleship(Board board)
        {
            var placingTheShip = new ShipCoordinates()
            {
                Coordinate = new Coordinates(10, 6),
                Direction  = ShipDirections.Right,
                ShipType   = ShipType.Battleship
            };

            board.PlaceShip(placingTheShip);
        }
Exemple #13
0
        private void Destroyer(Board board)
        {
            var placingTheShip = new ShipCoordinates()
            {
                Coordinate = new Coordinates(1, 8),
                Direction  = ShipDirections.Down,
                ShipType   = ShipType.Destroyer
            };

            board.PlaceShip(placingTheShip);
        }
Exemple #14
0
        private void MarkHorizontalNeighboringCells(ShipCoordinates shipCoords, int y)
        {
            var xStart = shipCoords.Begin.X;
            var xEnd   = shipCoords.End.X;

            for (var i = xStart; i <= xEnd; i++)
            {
                var cell = _game.Matrix[i, y] ?? (_game.Matrix[i, y] = new Cell());
                cell.IsNextToShip = true;
            }
        }
Exemple #15
0
        private void Cruiser(Board board)
        {
            var placingTheShip = new ShipCoordinates()
            {
                Coordinate = new Coordinates(1, 1),
                Direction  = ShipDirections.Right,
                ShipType   = ShipType.Cruiser
            };

            board.PlaceShip(placingTheShip);
        }
        public void BattleServiceShould_DestroyShipAndFinishGame()
        {
            var oneCellShipCoords = new Coordinates(0, 0, "1A");
            var shipCoords        = new ShipCoordinates(oneCellShipCoords, oneCellShipCoords);

            _creationService.CreateShips(new [] { shipCoords });
            var result = _battleService.TakeShot(oneCellShipCoords);

            Assert.That(result.IsDestroyed, Is.True);
            Assert.That(result.IsKnocked, Is.True);
            Assert.That(result.IsEndOfTheGame, Is.True);
        }
        public void BattleServiceShould_ReturnMissResult_WhenMiss()
        {
            var oneCellShipCoords = new Coordinates(0, 0, "1A");
            var emptyCell         = new Coordinates(1, 1, "2B");
            var shipCoords        = new ShipCoordinates(oneCellShipCoords, oneCellShipCoords);

            _creationService.CreateShips(new[] { shipCoords });
            var result = _battleService.TakeShot(emptyCell);

            Assert.That(result.IsDestroyed, Is.False);
            Assert.That(result.IsKnocked, Is.False);
            Assert.That(result.IsEndOfTheGame, Is.False);
        }
 private bool IsBuildingImpossible(ShipCoordinates shipCoords)
 {
     for (var x = shipCoords.Begin.X; x <= shipCoords.End.X; x++)
     {
         for (var y = shipCoords.Begin.Y; y <= shipCoords.End.Y; y++)
         {
             var cell = _game.Matrix[x, y];
             if (cell != null && (cell.IsPartOfShip || cell.IsNextToShip))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        private void CreateShip(ShipCoordinates shipCoords)
        {
            var ship = new Ship();

            for (var x = shipCoords.Begin.X; x <= shipCoords.End.X; x++)
            {
                for (var y = shipCoords.Begin.Y; y <= shipCoords.End.Y; y++)
                {
                    var cell = new Cell();
                    ship.AddCell(cell);
                    _game.Matrix[x, y] = cell;
                }
            }
            _game.Ships.Add(ship);
        }
Exemple #20
0
        public void CheckIf50_50ShipOutofIndex()
        {
            Board b = new Board();

            ShipCoordinates placingTheShip = new ShipCoordinates()
            {
                Coordinate = new Coordinates(10, 10),
                Direction  = ShipDirections.Right,
                ShipType   = ShipType.Carrier
            };

            var response = b.PlaceShip(placingTheShip);

            Assert.AreEqual(ShipPlacements.NotEnoughSpace, response);
        }
Exemple #21
0
        public void CheckIfShipOutofIndex()
        {
            Board b = new Board();

            ShipCoordinates placingTheShip = new ShipCoordinates()
            {
                Coordinate = new Coordinates(23, 12),
                Direction  = ShipDirections.Up,
                ShipType   = ShipType.Destroyer
            };

            var response = b.PlaceShip(placingTheShip);

            Assert.AreEqual(ShipPlacements.NotEnoughSpace, response);
        }
Exemple #22
0
        private void MarkVerticalNeighboringCells(ShipCoordinates shipCoords, int x)
        {
            var yStart = shipCoords.Begin.Y;

            if (shipCoords.Begin.Y > 0)
            {
                yStart--;
            }
            var yEnd = shipCoords.End.Y;

            if (shipCoords.End.Y < _game.Matrix.Size - 1)
            {
                yEnd++;
            }
            for (var j = yStart; j <= yEnd; j++)
            {
                var cell = _game.Matrix[x, j] ?? (_game.Matrix[x, j] = new Cell());
                cell.IsNextToShip = true;
            }
        }
Exemple #23
0
        public void PlaceShipOnBoard(Player player)
        {
            bool IfManuallyPlaceTheShips = false;

            if (player.IsPC != true)
            {
                IfManuallyPlaceTheShips = Inputs.IfManuallyPlaceTheShips();
                if (!IfManuallyPlaceTheShips)
                {
                    Console.WriteLine("Input the location and direction(L, R, U, D) of the ships. Ex:) A6, L:");
                }
            }
            for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++)
            {
                ShipCoordinates ShipToPlace = new ShipCoordinates();
                ShipPlacements  result;
                do
                {
                    if (!player.IsPC && !IfManuallyPlaceTheShips)
                    {
                        ShipToPlace          = Inputs.GetCoordinatesForPlayer1(s.ToString());
                        ShipToPlace.ShipType = s;
                        result = player.PlayerBoard.PlaceShip(ShipToPlace);
                        if (result == ShipPlacements.NotEnoughSpace)
                        {
                            Console.WriteLine("Not Enough Space!");
                        }
                        else if (result == ShipPlacements.Overlap)
                        {
                            Console.WriteLine("Overlap placement!");
                        }
                    }
                    else
                    {
                        ShipToPlace          = Inputs.GetLocationFromComputer();
                        ShipToPlace.ShipType = s;
                        result = player.PlayerBoard.PlaceShip(ShipToPlace);
                    }
                } while (result != ShipPlacements.Ok);
            }
        }
Exemple #24
0
        public static ShipCoordinates GetCoordinatesForPlayer1(string ShipType)
        {
            ShipCoordinates result = null;

            do
            {
                Console.Write("- " + ShipType + ": ");
                result = GetLocation(Console.ReadLine());

                if (result is null)
                {
                }
                else
                {
                    return(result);
                }

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Invalid input. Please Enter correct Directions?. Ex:) B3, R");
                Console.ForegroundColor = ConsoleColor.White;
            } while (result is null);
            return(result);
        }