Esempio n. 1
0
        public void CheckShipCoordinatesX_ShipCoordinatesGreaterThan0_ReturnsFalse(int length, int x, int y)
        {
            matrix = DummyShips(matrix);

            var check = Coordinates.CheckShipCoordinatesX(matrix, length, x, y);

            check.Should().BeFalse();
        }
Esempio n. 2
0
        public void CheckShipCoordinatesX_ShipCoordinatesEqual0_ReturnsTrue(int length, int x, int y)
        {
            matrix = DummyShips(matrix);

            var check = Coordinates.CheckShipCoordinatesX(matrix, length, x, y);

            check.Should().BeTrue();
        }
Esempio n. 3
0
        private static int[,] RandomShipAllocation(int[,] matrix, int rows, int shipLength, int shipNumber)
        {
            var coordinates = Coordinates.RandomCoordinates(matrix);

            var x = coordinates.Item1;
            var y = coordinates.Item2;

            // run till ship gets allocated coordinates
            while (true)
            {
                // horizontal
                // right
                if (y + shipLength <= rows)
                {
                    if (Coordinates.CheckShipCoordinatesY(matrix, shipLength, x, y))
                    {
                        Coordinates.WriteShipCoordinatesY(matrix, shipLength, x, y, shipNumber);

                        return(matrix);
                    }
                }

                // left
                if (y - (shipLength - 1) >= 0)
                {
                    if (Coordinates.CheckShipCoordinatesY(matrix, shipLength, x, y - (shipLength - 1)))
                    {
                        Coordinates.WriteShipCoordinatesY(matrix, shipLength, x, y - (shipLength - 1), shipNumber);

                        return(matrix);
                    }
                }

                // vertically
                // down
                if (x + shipLength <= rows)
                {
                    if (Coordinates.CheckShipCoordinatesX(matrix, shipLength, x, y))
                    {
                        Coordinates.WriteShipCoordinatesX(matrix, shipLength, x, y, shipNumber);

                        return(matrix);
                    }
                }

                // up
                if (x - (shipLength - 1) >= 0)
                {
                    if (Coordinates.CheckShipCoordinatesX(matrix, shipLength, x - (shipLength - 1), y))
                    {
                        Coordinates.WriteShipCoordinatesX(matrix, shipLength, x - (shipLength - 1), y, shipNumber);

                        return(matrix);
                    }
                }

                // new coordinates need to be set up
                var newCoordinates = Coordinates.RandomCoordinates(matrix);

                x = newCoordinates.Item1;
                y = newCoordinates.Item2;
            }
        }