示例#1
0
        public void Should_Not_Win_Unless_All_Ships_Are_Sunk()
        {
            var board = new Models.Board();

            var battleship1 = new Models.Battleship(3);

            board.AddBattleship(battleship1,
                                Models.BattleshipOrientation.Horizontal,
                                new System.Drawing.Point(3, 3));

            board.TakeAttack(new System.Drawing.Point(3, 3));
            board.TakeAttack(new System.Drawing.Point(4, 3));
            board.TakeAttack(new System.Drawing.Point(5, 3));

            var battleship2 = new Models.Battleship(3);

            board.AddBattleship(battleship2,
                                Models.BattleshipOrientation.Vertical,
                                new System.Drawing.Point(0, 0));

            board.TakeAttack(new System.Drawing.Point(0, 0));
            board.TakeAttack(new System.Drawing.Point(0, 1));

            Assert.AreEqual(false, board.Won());
        }
示例#2
0
        public void Should_Not_Accept_Hit_At_InValid_Position()
        {
            var board = new Models.Board();

            var battleship1 = new Models.Battleship(5);

            board.AddBattleship(battleship1,
                                Models.BattleshipOrientation.Horizontal,
                                new System.Drawing.Point(3, 3));

            var wasHit = board.TakeAttack(new System.Drawing.Point(2, 3));

            Assert.AreEqual(false, wasHit);
        }
示例#3
0
        public void Ship_Should_Not_Sink_Unless_All_Positions_Are_hit()
        {
            var board = new Models.Board();

            var battleship1 = new Models.Battleship(4);

            board.AddBattleship(battleship1,
                                Models.BattleshipOrientation.Horizontal,
                                new System.Drawing.Point(3, 3));

            board.TakeAttack(new System.Drawing.Point(3, 3));
            board.TakeAttack(new System.Drawing.Point(3, 4));
            board.TakeAttack(new System.Drawing.Point(3, 5));

            Assert.AreEqual(false, battleship1.IsSunk);
        }
示例#4
0
        public void Ship_Should_Sink_If_All_Positions_hit()
        {
            var board = new Models.Board();

            var battleship1 = new Models.Battleship(3);

            board.AddBattleship(battleship1,
                                Models.BattleshipOrientation.Horizontal,
                                new System.Drawing.Point(3, 3));

            board.TakeAttack(new System.Drawing.Point(3, 3));
            board.TakeAttack(new System.Drawing.Point(4, 3));
            board.TakeAttack(new System.Drawing.Point(5, 3));

            Assert.AreEqual(true, battleship1.IsSunk);
        }
示例#5
0
        static void Main()
        {
            IInputValidation inputValidation = new S.InputValidation();
            var userInput = new M.UserInput();

            Console.WriteLine("Welcome to Battleship!");
            Console.WriteLine("Enter your ship's start position. Example: 3,4");

            var input = Console.ReadLine();

            if (!inputValidation.ValidatePosition(input))
            {
                Console.WriteLine(ErrorMessage);
            }
            else
            {
                userInput.StartRow    = Convert.ToInt32(input.Split(",")[0]);
                userInput.StartColumn = Convert.ToInt32(input.Split(",")[1]);
                Console.WriteLine("Enter length of your ship. 0 < Length <= 10.");
                var length2 = Console.ReadLine();

                if (!inputValidation.ValidateLength(length2))
                {
                    Console.WriteLine(ErrorMessage);
                }
                else
                {
                    userInput.Length = Convert.ToInt32(length2);
                    Console.WriteLine("Enter 0 for horizontal and 1 for vertical alignment of ship.");
                    var alignment2 = Console.ReadLine();

                    if (!inputValidation.ValidateAlignment(alignment2))
                    {
                        Console.WriteLine(ErrorMessage);
                    }
                    else
                    {
                        userInput.Alignment = Convert.ToInt32(alignment2);
                        _validState         = true;
                    }
                }
            }

            if (_validState)
            {
                IShip   ship   = new S.Ship();
                IBoard  board  = new S.Board();
                IWorker worker = new S.Worker(board, ship, inputValidation);

                var userboard = new M.Board
                {
                    BoardDimension = new int[10, 10]
                };
                var shipList = new List <M.Ship>();
                if (!worker.AddShips(userInput.StartRow, userInput.StartColumn, userInput.Length, userInput.Alignment,
                                     ref shipList, userboard))
                {
                    Console.WriteLine("ship too large or overlap. Hit any key to quit and retry.");
                }
                else
                {
                    worker.Attack(ref shipList, userboard);
                }
            }

            Console.ReadKey();
        }