Exemple #1
0
            public void Should_Add_Battleship_On_Board_Vertical_Orientation()
            {
                var board = new Board();

                var battleship1 = new Battleship.Models.Battleship(5);

                board.AddBattleship(battleship1, BattleshipOrientation.Vertical, new Point(0, 0));

                var positions = board.GetBattleshipPositions(battleship1);

                Assert.IsNotNull(positions);
                Assert.AreEqual(5, positions.Count);
                for (int i = 0; i < 5; i++)
                {
                    Assert.AreEqual(true, positions.Contains(new Point(0, i)));
                }

                var battleship2 = new Battleship.Models.Battleship(5);

                board.AddBattleship(battleship2, BattleshipOrientation.Vertical, new Point(5, 0));

                positions = board.GetBattleshipPositions(battleship2);

                Assert.IsNotNull(positions);
                Assert.AreEqual(5, positions.Count);
                for (int i = 0; i < 5; i++)
                {
                    Assert.AreEqual(true, positions.Contains(new Point(5, i)));
                }
            }
Exemple #2
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());
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            var board = new Board();

            var battleship1 = new Battleship.Models.Battleship(5);

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

            var battleship2 = new Battleship.Models.Battleship(3);

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

            var battleship3 = new Battleship.Models.Battleship(5);

            board.AddBattleship(battleship3, BattleshipOrientation.Horizontal, new System.Drawing.Point(4, 0));

            Console.WriteLine(board.ToString());
        }
Exemple #7
0
            public void Should_Cannot_Add_Overlapping_Battleships()
            {
                var board = new Board();

                var battleship1 = new Battleship.Models.Battleship(5);

                board.AddBattleship(battleship1, BattleshipOrientation.Horizontal, new Point(2, 1));

                var positions = board.GetBattleshipPositions(battleship1);

                Assert.IsNotNull(positions);
                Assert.AreEqual(5, positions.Count);
                for (int i = 2; i < 7; i++)
                {
                    Assert.AreEqual(true, positions.Contains(new Point(i, 1)));
                }

                var battleship2 = new Battleship.Models.Battleship(3);

                board.AddBattleship(battleship2, BattleshipOrientation.Horizontal, new Point(6, 1));
            }
Exemple #8
0
        public void AddBattleship(Battleship battleship,
                                  BattleshipOrientation orientation,
                                  System.Drawing.Point startPosition)
        {
            if (battleship == null || startPosition == null)
            {
                throw new ValidationException("Battleship and start position is required");
            }

            List <Point> pointsOccupiedByShip = new List <Point>();

            for (int index = 0; index < battleship.Size; index++)
            {
                var nextPosition = new Point();
                if (orientation == BattleshipOrientation.Horizontal)
                {
                    nextPosition.X = startPosition.X + index;
                    nextPosition.Y = startPosition.Y;
                }
                else
                {
                    nextPosition.X = startPosition.X;
                    nextPosition.Y = startPosition.Y + index;
                }

                if (OccupiedCells.Contains(nextPosition) == true)
                {
                    throw new PositionConflictException("Battleship cannot be placed here as position is already occupied");
                }

                pointsOccupiedByShip.Add(nextPosition);
            }

            this.battleships.Add(battleship);
            this.OccupiedCells.AddRange(pointsOccupiedByShip);
            this.BattleshipPositions.Add(battleship, pointsOccupiedByShip);
        }
Exemple #9
0
 public List <Point> GetBattleshipPositions(Battleship battleship)
 {
     return(this.BattleshipPositions.ContainsKey(battleship) ?
            this.BattleshipPositions[battleship] : null);
 }