BattleShip.Core.Game.TakeShot C# (CSharp) Метод

TakeShot() публичный Метод

public TakeShot ( Player playerTakingShot, GeoCoordinate shotLocation ) : ShotResult
playerTakingShot Player
shotLocation GeoCoordinate
Результат ShotResult
        public ShotResult TakeShot(Player playerTakingShot, GeoCoordinate shotLocation)
        {
            if (playerTakingShot != NextPlayerToTakeShot)
            {
                return ShotResult.IllegalPlayer;
            }

            if (LastShotWasHit())
            {
                return ShotResult.GameAlreadyOver;
            }

            var targetPlayer = NextPlayerToTakeShot == Player1 ? Player2 : Player1;
            if (targetPlayer.Location == null)
            {
                return ShotResult.TargetHasNoLocation;
            }

            if (ShotOutsideTargetZone(targetPlayer, shotLocation))
            {
                return ShotResult.OutsideTargetZone;
            }

            //Take shot
            var shot = ShotOnTarget(targetPlayer, shotLocation) ? new Shot(playerTakingShot, shotLocation, ShotResult.Hit) : new Shot(playerTakingShot, shotLocation, ShotResult.Miss);

            //Push to stack
            shots.Push(shot);

            NextPlayerToTakeShot = targetPlayer;

            return shot.ShotResult;
        }

Usage Example

Пример #1
0
        public static void Main()
        {
            var p1 = new Player("*****@*****.**", "A");
            var p2 = new Player("*****@*****.**", "B");

            const double playerTargetZoneRadius = 2.0;
            const double shotBlastRadius = 1.0;

            var game = new Game("Test", p1, p2, playerTargetZoneRadius, shotBlastRadius);

            p1.UpdateLocation(new GeoCoordinate(0.0, 0.0), playerTargetZoneRadius);
            p2.UpdateLocation(new GeoCoordinate(5.0, 5.0), playerTargetZoneRadius);

            var result = game.TakeShot(p2, p1.Location);
            Console.WriteLine(result);

            result = game.TakeShot(p2, p1.Location);
            Console.WriteLine(result);

            result = game.TakeShot(p1, p1.Location);
            Console.WriteLine(result);

            Console.ReadKey();
        }