Exemple #1
0
        public static Battleship BuildBattleShip(Coordinate[] coordinates)
        {
            if (coordinates == null)
            {
                throw new ArgumentNullException(nameof(coordinates));
            }
            if (coordinates.Length == 0)
            {
                throw new ArgumentOutOfRangeException($"A battleship can not be of zero length.");
            }

            var battleshipSize    = coordinates.Length;
            var battleshipSquares = new BattleshipSquare[battleshipSize];
            var counter           = 0;

            foreach (var coord in coordinates)
            {
                var bPart = new BattleshipSquare(coord);

                battleshipSquares[counter] = bPart;
                counter++;
            }

            var battleship = new Battleship(battleshipSquares);

            return(battleship);
        }
        /// <summary>
        /// Shoots the opponents board
        /// </summary>
        /// <param name="coord"></param>
        public void Shoot(Coordinate coord)
        {
            if (coord == null)
            {
                throw new ArgumentNullException(nameof(coord));
            }

            if (HasShotThereBefore(coord))
            {
                Console.WriteLine("You have shot there before. You can't win the game by shooting the same place twice.");
                return;
            }
            else
            {
                var response = (ShotResponse)int.Parse(Console.ReadLine());

                //store the opponents shot result on the opponents board
                var seaSquare = GetSeaSquare(_OpponentsBoard, coord);

                switch (response)
                {
                case ShotResponse.Miss:
                    seaSquare.HasBeenShot = true;
                    break;

                case ShotResponse.Hit:

                    seaSquare.HasBeenShot = true;
                    if (seaSquare.BattleshipSquare == null)
                    {
                        var battleshipSquare = new BattleshipSquare(coord);
                        battleshipSquare.IsShot = true;

                        seaSquare.AddBattleshipSquare(battleshipSquare);
                    }
                    else
                    {
                        // if it falls in here, it has been shot before.
                        seaSquare.BattleshipSquare.IsShot = true;
                    }
                    break;

                case ShotResponse.Sunk:

                    if (seaSquare.Battleship != null)
                    {
                        seaSquare.Battleship.SunkBattleship();
                    }
                    break;

                default:
                    Console.WriteLine("Unknown response.");
                    Console.WriteLine("Appropriate values: [1] Miss, [2] Hit, [3] Sunk");
                    break;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Initializes a battleship on the board
        /// </summary>
        /// <param name="battleship"></param>
        public void LinkBattleshipSquare(BattleshipSquare battleshipSquare)
        {
            if (battleshipSquare == null)
            {
                throw new ArgumentNullException(nameof(battleshipSquare));
            }

            BattleshipSquare = battleshipSquare;
        }
Exemple #4
0
        /// <summary>
        /// Adds a battleship square to the sea square to track opponents pieces
        /// </summary>
        /// <param name="battleshipSquare"></param>
        public void AddBattleshipSquare(BattleshipSquare battleshipSquare)
        {
            if (battleshipSquare == null)
            {
                throw new ArgumentNullException(nameof(battleshipSquare));
            }

            BattleshipSquare = battleshipSquare;

            if (HasBattleship)
            {
                Battleship.AddBattleshipPart(battleshipSquare);
            }
            else
            {
                var battleship = new Battleship(new BattleshipSquare[] { battleshipSquare });
                Battleship = battleship;
            }
        }