コード例 #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);
                }
            }
        }
コード例 #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);
        }