示例#1
0
        public void Setup()
        {
            _response = new Response();
            var mockLoggerBattleShip = new Mock <ILogger <BattleshipService> >();

            _loggerBattleShipService = mockLoggerBattleShip.Object;

            var mockLoggerServiceHelper = new Mock <ILogger <ServiceHelper> >();

            _loggerServiceHelper = mockLoggerServiceHelper.Object;
            _battleshipBoardGame = new BattleshipBoardGame();

            _serviceHelper = new ServiceHelper(_loggerServiceHelper, _battleshipBoardGame, _response);

            var mockIServiceHelper = new Mock <IServiceHelper>();

            mockIServiceHelper.Setup(x => x.AddShipHorizontally(It.IsAny <ShipAddRequest>(), It.IsAny <Board>())).Returns(Task.FromResult(It.IsAny <bool>()));

            mockIServiceHelper.Setup(x => x.AddShipVertically(It.IsAny <ShipAddRequest>(), It.IsAny <Board>())).Returns(Task.FromResult(It.IsAny <bool>()));

            mockIServiceHelper.Setup(x => x.IsShipPlacementHorizontallyPossible(It.IsAny <ShipAddRequest>(), It.IsAny <List <List <Cell> > >())).Returns(Task.FromResult(It.IsAny <bool>()));

            mockIServiceHelper.Setup(x => x.IsShipPlacementVerticallyPossible(It.IsAny <ShipAddRequest>(), It.IsAny <List <List <Cell> > >())).Returns(Task.FromResult(It.IsAny <bool>()));


            _battleshipService = new BattleshipService(_loggerBattleShipService, _battleshipBoardGame, _serviceHelper, _response);
        }
示例#2
0
        static void Main(string[] args)
        {
            Console.WriteLine("How many games do you want to play?");
            int n = int.Parse(Console.ReadLine());
            BattleshipService service = new BattleshipService();

            for (int i = 0; i < n; i++)
            {
                service.StartGame();
                Console.WriteLine(service.DisplayBoards());

                while (!service.IsGameOver())
                {
                    // Uncomment following two-lines if you want to take charge
                    //Console.WriteLine("Press any key to play next turn.......");
                    //Console.Read();
                    service.PlayTurn();
                    Console.WriteLine(service.DisplayBoards());

                    // Grab popcorn and enjoy the battle
                    //Thread.Sleep(500);
                }
                Console.WriteLine(service.DisplayBoards());
                Console.WriteLine(service.GetWinnerStatistics());
            }

            Console.Read();
        }
        public void InitializeTests()
        {
            repository = new BattleshipFakeRepository();
            service    = new BattleshipService(repository);

            CreateFirstPlayerFleet();
        }
        public void Setup()
        {
            var mShip = new Mock <Ship>(It.IsAny <Constraints.ShipType>());

            mShip.Object.Coordinates = new List <Coordinates>();
            ship = new Lazy <Ship>(() => mShip.Object);

            var miShip = new Mock <IShip>();

            miShip.Setup(s => s.Attack(It.IsAny <string>(), It.IsAny <IMissile>())).Returns(It.IsAny <bool>);

            Ship shipMock = new Ship(It.IsAny <Constraints.ShipType>());

            shipMock.Coordinates = new List <Coordinates>();

            miShip.Setup(s => s.Ship).Returns(() => shipMock).Callback(() => { shipMock = ship.Value; });

            var iShip = new Lazy <IShip>(() => miShip.Object);

            ships = new List <IShip>()
            {
                iShip.Value, iShip.Value
            };

            player = new Mock <Player>(It.IsAny <string>()).Object;
            IShip intShip = new ShipP();

            intShip.Ship = shipMock;

            var shipFactory = new Mock <IShipFactory>();

            shipFactory.Setup(s => s.Create(It.IsAny <Constraints.ShipType>())).Returns(() => intShip).Callback(() => { intShip = iShip.Value; });
            this.ShipFactory = shipFactory;

            var playerService = new Mock <IPlayerService>();

            Players = new List <Player>();

            var missileService = new Mock <IMissileFactory>();

            playerService.SetupGet(p => p.Players).Returns(Players);
            playerService.Setup(p => p.CreatePlayer(It.IsAny <string>(), It.IsAny <IEnumerable <IShip> >(), It.IsAny <string[]>(), It.IsAny <IMissile>()))
            .Callback((string Name, IEnumerable <IShip> paramShips, string[] targets) =>
            {
                Players.Add(new Player(Name, missileService.Object.CreateMissile(Constraints.MissileType.Range))
                {
                    Ships   = paramShips,
                    Targets = targets
                });;
            });
            playerService.Setup(p => p.Play()).Returns("Player-2 won the battle");

            playerService.Setup(p => p.PlayerTurn(player, It.IsAny <int>(), ships));
            this.PlayerService = playerService;

            var battleshipService = new BattleshipService(PlayerService.Object, ShipFactory.Object, null);

            this.BattleshipService = battleshipService;
        }
        public void StartTheGame_WhenCalled_PlayerService_Play_ShouldCalled_1_Times()
        {
            //Act
            BattleshipService.StartTheGame(args);

            //assert
            PlayerService.Verify(p => p.Play(), Times.Exactly(1));
        }
        public void StartTheGame_WhenCalled_ShipFactory_Create_ShouldCalled_4_Times()
        {
            //Act
            BattleshipService.StartTheGame(args);

            //assert
            ShipFactory.Verify(p => p.Create(It.IsAny <Constraints.ShipType>()), Times.Exactly(4));
        }
        public void StartTheGame_WhenCalled_ShouldReturnPlayer2Won()
        {
            //Act
            var message = BattleshipService.StartTheGame(args);

            //assert
            Assert.AreEqual(message, "Player-2 won the battle");
        }
示例#8
0
        private static void DrawField(BattleshipService service, Guid?gameId, string player, string caption)
        {
            var sb = new StringBuilder()
                     .AppendLine()
                     .AppendLine(caption)
                     .AppendLine()
                     .Append("  1 2 3 4 5 6 7 8 9 10");

            for (char c = 'a'; c <= 'j'; c++)
            {
                sb
                .AppendLine()
                .AppendFormat("{0} ", char.ToUpper(c));

                for (int i = 1; i <= 10; i++)
                {
                    var result = service.CheckCell(gameId.Value, player, i, c);

                    switch (result)
                    {
                    case CellState.Empty:
                        sb.Append("* ");
                        break;

                    case CellState.Destroyed:
                        sb.Append("# ");
                        break;

                    case CellState.HasShip:
                        sb.Append("+ ");
                        break;

                    case CellState.Unknown:
                        sb.Append("? ");
                        break;

                    case CellState.HasMiss:
                        sb.Append("@ ");
                        break;

                    default:
                        break;
                    }
                }
            }

            sb.AppendLine();
            System.Console.WriteLine(sb.ToString());
        }
示例#9
0
 public void SetUp()
 {
     _battleshipService = new BattleshipService();
     _player1           = new Player {
         Id        = 1,
         HitsGiven = new List <Point>(),
         SuccessfulShotsReceived = 0,
         Board = _battleshipService.CreateBoard()
     };
     _player2 = new Player {
         Id        = 2,
         HitsGiven = new List <Point>(),
         SuccessfulShotsReceived = 0,
         Board = _battleshipService.CreateBoard()
     };
 }
示例#10
0
        private static void CreateFleetForPlayer(BattleshipService service, string playerName, Guid?gameId)
        {
            while (!service.IsFleetFull(gameId.Value, playerName).Value)
            {
                var    shipInfo = service.SuggestNextShipToAdd(gameId.Value, playerName);
                string coordinates;

                do
                {
                    //System.Console.Clear();
                    System.Console.WriteLine(string.Format("{2}, please enter coordinates for a {0} size[{1}].", shipInfo.ShipType, shipInfo.ShipSize, playerName));
                    coordinates = System.Console.ReadLine();
                }while (!service.AddShipToPlayersFleet(gameId.Value, playerName, coordinates, shipInfo));

                DrawField(service, gameId, playerName, "Ship was successfully created!");
            }
        }
示例#11
0
        static void Main(string[] args)
        {
            var repository = new BattleshipFakeRepository();
            var service    = new BattleshipService(repository);

            System.Console.WriteLine("Please, enter a name for the game:");
            string GameName = System.Console.ReadLine();

            System.Console.WriteLine("Please, enter a name for the player one:");
            string Player1 = System.Console.ReadLine();

            System.Console.WriteLine("Please, enter a name for the player two:");
            string Player2 = System.Console.ReadLine();


            Guid?gameId = null;

            if (!service.CreateGame(GameName, Player1, Player2))
            {
                System.Console.WriteLine("By some reason the game was not created!");
                System.Console.WriteLine("The game is over");
                System.Console.ReadKey();
                return;
            }

            gameId = service.FindGameByName(GameName);
            System.Console.WriteLine("Game successfully created!");
            System.Console.WriteLine(String.Format("Game id is: {0}", gameId.Value));
            System.Console.WriteLine();

            // creating fleets
            foreach (var player in new string[] { Player1, Player2 })
            {
                DrawField(service, gameId, player, string.Format("{0} - create your fleet!", player)); // <--- add player parameter!!!
                CreateFleetForPlayer(service, player, gameId);
            }

            // rolling dices
            // the winner takes the first step
            string currentPlayer = Player1;

            // the game cycle
            while (!service.IsGameEnded(gameId.Value))
            {
                System.Console.WriteLine(string.Format("{0} - it's your turn!", currentPlayer));
                Info <ShotResult> shotInfo;
                var misses = new[] { ShotResult.Miss, ShotResult.SecondHit };

                do
                {
                    System.Console.WriteLine(string.Format("{0}, please enter coordinates for a shot!", currentPlayer));
                    string coordinates = System.Console.ReadLine();
                    shotInfo = service.TakeTurn(gameId.Value, currentPlayer, coordinates);

                    if (shotInfo == null)
                    {
                        continue;
                    }

                    DrawField(service, gameId, currentPlayer, shotInfo.InfoString);
                }while (misses.Contains(shotInfo.Value)); // while you miss

                // next player
                currentPlayer = service.GetNextPlayerToTurn(gameId.Value, currentPlayer);
            }

            System.Console.WriteLine("The end!");
            System.Console.ReadKey();
        }