Example #1
0
        public void AttemptShoot(Coordinates coordinates)
        {
            if (Status != GameStatus.InProgress)
            {
                return;
            }

            Ship ship = _radar.FindShip(coordinates);

            if (ship == null)
            {
                Board.Board[coordinates.Column, coordinates.Row].MarkAsMissed();
                ShotMissedEventHandler shotMissedEventHandler = ShotMissed;
                shotMissedEventHandler?.Invoke(this, new ShotMissedEventArgs());
                return;
            }

            ship.GetShot(coordinates);
            Board.Board[coordinates.Column, coordinates.Row].MarkAsHit();
            TargetHitEventHandler targetHitEventHandler = TargetHit;

            targetHitEventHandler?.Invoke(this, new TargetHitEventArgs());


            if (ship.Status == ShipStatus.Sunk)
            {
                ShipSunkEventHandler shipSunkEventHandler = ShipSunk;
                var shipSunkEventArgs = new ShipSunkEventArgs(ship.GetType().Name);
                shipSunkEventHandler?.Invoke(this, shipSunkEventArgs);
            }

            if (!_radar.AnyShipAlive())
            {
                UserVictoryEventHandler userVictoryEventHandler = UserVictory;
                userVictoryEventHandler?.Invoke(this, new UserVictoryEventArgs());
                Status = GameStatus.Victory;
                _timer.Stop();
            }
        }
Example #2
0
        public void OnShipSunk(object sender, ShipSunkEventArgs e)
        {
            RefreshConsole(e.Board);

            Console.WriteLine($"{e.ShipName} has been sunk by your shot! Great!");
        }