Ejemplo n.º 1
0
        private static Winner FindWinner(IBattleshipsBot playerOneBot, IBattleshipsBot playerTwoBot)
        {
            var playerOneShipsPlacement = new ShipsPlacement(playerOneBot);
            var playerTwoShipsPlacement = new ShipsPlacement(playerTwoBot);

            if (!playerOneShipsPlacement.IsValid())
            {
                throw new Exception("Player One Ship Placement Invalid");
            }

            if (!playerTwoShipsPlacement.IsValid())
            {
                throw new Exception("Player Two Ship Placement Invalid");
            }

            while (true)
            {
                MakeMove(playerOneBot, playerTwoBot, playerTwoShipsPlacement);

                if (playerTwoShipsPlacement.AllHit())
                {
                    return(Winner.Player);
                }

                MakeMove(playerTwoBot, playerOneBot, playerOneShipsPlacement);

                if (playerOneShipsPlacement.AllHit())
                {
                    return(Winner.Computer);
                }
            }
        }
Ejemplo n.º 2
0
        private static void MakeMove(IBattleshipsBot attacker, IBattleshipsBot defender, ShipsPlacement defendingShips)
        {
            var target         = attacker.SelectTarget();
            var defendingIsHit = defendingShips.IsHit(target);

            attacker.HandleShotResult(target, defendingIsHit);
            defender.HandleOpponentsShot(target);
        }
Ejemplo n.º 3
0
 public ShipsPlacement(IBattleshipsBot bot)
 {
     try
     {
         ShipPositions = bot.GetShipPositions();
         ships         = ShipPositions.Select(ship => new Ship(ship)).ToList();;
     }
     catch (Exception)
     {
         ships = null;
     }
 }