internal void Mark(ShotResultEnum shotResult)
        {
            if (shotResult == ShotResultEnum.Miss)
            {
                _status = new Missed();
            }

            if (shotResult == ShotResultEnum.Hit || shotResult == ShotResultEnum.Sunk)
            {
                _status = new Hit();
            }
        }
Example #2
0
        /// <summary>
        /// Shoots given target
        /// </summary>
        /// <param name="targetSquare">Target square</param>
        /// <param name="player">Is player shooting?</param>
        /// <returns></returns>
        public Shot Shoot(Square?targetSquare, bool isPlayerShooting)
        {
            Ship[]      targetShips;
            List <Shot> shotsCollection;

            targetShips     = isPlayerShooting ? this.NPCShips : this.PlayerShips;
            shotsCollection = isPlayerShooting ? this.PlayerShots : this.NPCShots;

            if (!targetSquare.HasValue)
            {
                var alreadyShotSquares = shotsCollection.Select(s => s.Square);
                int shotsCount         = alreadyShotSquares.Count();

                Square randomSquare = GetRandomSquare();
                while (shotsCount < 100 && alreadyShotSquares.Contains(randomSquare))
                {
                    randomSquare = GetRandomSquare();
                }
                targetSquare = randomSquare;
            }

            ShotResultEnum shotResult = ShotResultEnum.Miss;

            foreach (var ship in targetShips)
            {
                if (ship.HealthySquares.Contains(targetSquare.Value))
                {
                    ship.Destroy(targetSquare.Value);
                    shotResult = ship.Destroyed ? ShotResultEnum.Sink : ShotResultEnum.Hit;
                    break;
                }
            }

            var shot = new Shot(targetSquare.Value, shotResult);

            shotsCollection.Add(shot);

            return(shot);
        }
Example #3
0
 public Shot(Square square, ShotResultEnum result)
 {
     Square = square;
     Result = result;
 }
 public TargetGridMark(Coordinate coordinate, ShotResultEnum shotResult)
 {
     Coordinate = coordinate;
     ShotResult = shotResult;
 }