Пример #1
0
        public void AddShip_RightShipPlacementInfo(int pointX, int pointY, ShipRotation shipRotation, ShipType shipType)
        {
            var shipStorage = new ShipsStorage();

            shipStorage.AddShip(shipType, 1);

            _builder.SetDimension(5, 5);
            _builder.SetShipsStorage(shipStorage);

            var shipPlacementInfo = new ShipPlacementInfo
            {
                Point        = new Point(pointX, pointY),
                ShipRotation = shipRotation,
                ShipType     = shipType
            };


            _builder.AddShip(shipPlacementInfo);

            var listOfCells = GetListOfCellsWhereShipShouldBe(shipPlacementInfo);

            foreach (var cell in listOfCells)
            {
                Assert.True(cell.CellState == CellState.Ship);
            }
        }
Пример #2
0
        public void AddShip_SurroundShipWithClosedCells(int pointX, int pointY, ShipRotation shipRotation, ShipType shipType)
        {
            var shipStorage = new ShipsStorage();

            shipStorage.AddShip(shipType, 1);

            _builder.SetDimension(5, 5);
            _builder.SetShipsStorage(shipStorage);

            var shipPlacementInfo = new ShipPlacementInfo
            {
                Point        = new Point(pointX, pointY),
                ShipRotation = shipRotation,
                ShipType     = shipType
            };


            _builder.AddShip(shipPlacementInfo);

            var listOfCells = GetListOfCellsAroundShip(shipPlacementInfo);

            foreach (var cell in listOfCells)
            {
                Assert.False(cell.CanPlaceShip);
            }
        }
Пример #3
0
        private List <Cell> GetListOfCellsWhereShipShouldBe(ShipPlacementInfo shipPlacementInfo)
        {
            var listOfCells = new List <Cell>();
            var filed       = _builder.GetResult();

            int x          = shipPlacementInfo.Point.X;
            int y          = shipPlacementInfo.Point.Y;
            int iterations = (int)shipPlacementInfo.ShipType + 1;

            //main iteration
            for (int i = 0; i < iterations; i++)
            {
                if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    listOfCells.Add(filed.CellsMatrix[y, x]);
                }
                else
                {
                    listOfCells.Add(filed.CellsMatrix[y, x]);
                }

                //increment
                if (shipPlacementInfo.ShipRotation == ShipRotation.Horizontal)
                {
                    x++;
                }
                else if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    y++;
                }
            }


            return(listOfCells);
        }
Пример #4
0
        public void AddShip_ThrowsShipPlacementCollisionException(int pointX, int pointY, ShipRotation shipRotation)
        {
            var shipStorage = new ShipsStorage();

            shipStorage.AddShip(ShipType.FourDecks, 1);
            shipStorage.AddShip(ShipType.TwoDecks, 1);

            _builder.SetDimension(5, 5);
            _builder.SetShipsStorage(shipStorage);

            var shipPlacementInfoFirst = new ShipPlacementInfo
            {
                Point        = new Point(3, 3),
                ShipRotation = ShipRotation.Horizontal,
                ShipType     = ShipType.TwoDecks
            };

            _builder.AddShip(shipPlacementInfoFirst);

            var shipPlacementInfoSecond = new ShipPlacementInfo
            {
                Point        = new Point(pointX, pointY),
                ShipRotation = shipRotation,
                ShipType     = ShipType.FourDecks
            };


            void Action() => _builder.AddShip(shipPlacementInfoSecond);


            Assert.Throws <ShipPlacementCollisionException>(Action);
        }
Пример #5
0
        public void AddShip(ShipPlacementInfo shipPlacementInfo)
        {
            CheckShipPlacement(shipPlacementInfo);
            SurroundShipWithClosedCells(shipPlacementInfo);

            //CHECKS COMPLETE, ADDING SHIP
            int x          = shipPlacementInfo.Point.X;
            int y          = shipPlacementInfo.Point.Y;
            int iterations = (int)shipPlacementInfo.ShipType + 1;

            // MAIN ITERATION
            for (int i = 0; i < iterations; i++)
            {
                _field.CellsMatrix[y, x].CellState    = CellState.Ship;
                _field.CellsMatrix[y, x].CanPlaceShip = false;

                //increment
                if (shipPlacementInfo.ShipRotation == ShipRotation.Horizontal)
                {
                    x++;
                }
                else if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    y++;
                }
            }
        }
Пример #6
0
        private void CheckShipPlacement(ShipPlacementInfo shipPlacementInfo)
        {
            int x          = shipPlacementInfo.Point.X;
            int y          = shipPlacementInfo.Point.Y;
            int iterations = (int)shipPlacementInfo.ShipType + 1;

            bool tempCanPlaceShipMainPoint;

            try
            {
                tempCanPlaceShipMainPoint = _field.CellsMatrix[shipPlacementInfo.Point.Y, shipPlacementInfo.Point.X].CanPlaceShip;
            }
            catch
            {
                throw new ShipPlacementOutOfBoundsException("Ship Out of bounds");
            }

            if (tempCanPlaceShipMainPoint == false)
            {
                throw new ShipPlacementCollisionException("Can't place ship here");
            }


            for (int i = 0; i < iterations; i++)
            {
                try
                {
                    var temp = _field.CellsMatrix[y, x].CellState;
                }
                catch
                {
                    throw new ShipPlacementOutOfBoundsException("Ship Out of bounds");
                }

                if (_field.CellsMatrix[y, x].CanPlaceShip == false)
                {
                    throw new ShipPlacementCollisionException("Can't place ship here");
                }


                //increment
                if (shipPlacementInfo.ShipRotation == ShipRotation.Horizontal)
                {
                    x++;
                }
                else if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    y++;
                }
            }
        }
Пример #7
0
        public override Field GetPlayerField(GameRules gameRules)
        {
            IFieldBuilder fieldBuilder = new FieldBuilder();

            fieldBuilder.SetDimension(gameRules.FieldHeight, gameRules.FieldWidth);
            fieldBuilder.SetShipsStorage(gameRules.ShipsStorage);

            View.ShowConcreteCellsMatrix(fieldBuilder.GetResult().CellsMatrix);

            foreach (var shipAvailable in gameRules.ShipsStorage.ShipsAvailable)
            {
                int i = 0;
                while (i < shipAvailable.Value)
                {
                    View.ShowInfo("Place " + shipAvailable.Key + " ship:");

                    var shipPlacementInfo = new ShipPlacementInfo
                    {
                        ShipType     = shipAvailable.Key,
                        ShipRotation = View.GetRotationInput(),
                        Point        = View.GetPointInput()
                    };

                    try
                    {
                        fieldBuilder.AddShip(shipPlacementInfo);
                    }
                    catch (Exception e)
                    {
                        View.ShowInfo(e.Message);
                        continue;
                    }

                    View.Clear();

                    View.ShowConcreteCellsMatrix(fieldBuilder.GetResult().CellsMatrix);

                    i++;
                }
            }

            return(fieldBuilder.GetResult());
        }
Пример #8
0
        public void AddShip_ThrowsShipPlacementOutOfBoundsException(int pointX, int pointY, ShipRotation shipRotation)
        {
            var shipStorage = new ShipsStorage();

            shipStorage.AddShip(ShipType.FourDecks, 1);


            _builder.SetDimension(5, 5);
            _builder.SetShipsStorage(shipStorage);

            var shipPlacementInfo = new ShipPlacementInfo
            {
                Point        = new Point(pointX, pointY),
                ShipRotation = shipRotation,
                ShipType     = ShipType.FourDecks
            };


            void Action() => _builder.AddShip(shipPlacementInfo);


            Assert.Throws <ShipPlacementOutOfBoundsException>(Action);
        }
Пример #9
0
        public ShipPlacementPatch PlaceShip([FromBody] ShipPlacementInfo info)
        {
            using (var connection = new SqlConnection(connectionString))
                using (var command = new SqlCommand("usp_PlaceShip", connection))
                {
                    var table = new DataTable();
                    command.CommandType = CommandType.StoredProcedure;

                    command.Parameters.Add(new SqlParameter("@userid", info.UserId));
                    command.Parameters.Add(new SqlParameter("@x", info.X));
                    command.Parameters.Add(new SqlParameter("@y", info.Y));
                    command.Parameters.Add(new SqlParameter("@o", info.Orientation));
                    command.Parameters.Add(new SqlParameter("@type", info.ShipType));
                    command.Parameters.Add(new SqlParameter("@gameid", info.GameId));

                    var dataAdapter = new SqlDataAdapter(command);

                    connection.Open();
                    dataAdapter.Fill(table);
                    connection.Close();

                    var dataRow = table.Rows[0];

                    if (int.Parse(dataRow["StatusCode"].ToString()) == 0)
                    {
                        return(new ShipPlacementPatch()
                        {
                            X = int.Parse(dataRow["X"].ToString()),
                            Y = int.Parse(dataRow["Y"].ToString()),
                            Orientation = dataRow["Orientation"].ToString()[0],
                            Size = int.Parse(dataRow["Size"].ToString())
                        });
                    }

                    return(default(ShipPlacementPatch));
                }
        }
Пример #10
0
        private List <Cell> GetListOfCellsAroundShip(ShipPlacementInfo shipPlacementInfo)
        {
            var listOfCells = new List <Cell>();
            var filed       = _builder.GetResult();

            int x          = shipPlacementInfo.Point.X;
            int y          = shipPlacementInfo.Point.Y;
            int iterations = (int)shipPlacementInfo.ShipType + 1;

            //before main iteration
            if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
            {
                for (int xIndexBefore = x - 1; xIndexBefore < x + 2; xIndexBefore++)
                {
                    listOfCells.Add(filed.CellsMatrix[y - 1, xIndexBefore]);
                }
            }
            else
            {
                for (int yIndexBefore = y - 1; yIndexBefore < y + 2; yIndexBefore++)
                {
                    listOfCells.Add(filed.CellsMatrix[yIndexBefore, x - 1]);
                }
            }


            //main iteration
            for (int i = 0; i < iterations; i++)
            {
                if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    listOfCells.Add(filed.CellsMatrix[y, x - 1]);
                    listOfCells.Add(filed.CellsMatrix[y, x + 1]);
                }
                else
                {
                    listOfCells.Add(filed.CellsMatrix[y - 1, x]);
                    listOfCells.Add(filed.CellsMatrix[y + 1, x]);
                }

                //increment
                if (shipPlacementInfo.ShipRotation == ShipRotation.Horizontal)
                {
                    x++;
                }
                else if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    y++;
                }
            }

            //after main iteration
            if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
            {
                for (int xIndexAfter = x - 1; xIndexAfter < x + 2; xIndexAfter++)
                {
                    listOfCells.Add(filed.CellsMatrix[y, xIndexAfter]);
                }
            }
            else
            {
                for (int yIndexAfter = y - 1; yIndexAfter < y + 2; yIndexAfter++)
                {
                    listOfCells.Add(filed.CellsMatrix[yIndexAfter, x]);
                }
            }

            return(listOfCells);
        }
Пример #11
0
        private void SurroundShipWithClosedCells(ShipPlacementInfo shipPlacementInfo)
        {
            int x          = shipPlacementInfo.Point.X;
            int y          = shipPlacementInfo.Point.Y;
            int iterations = (int)shipPlacementInfo.ShipType + 1;

            //before main iteration
            if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
            {
                for (int xIndexBefore = x - 1; xIndexBefore < x + 2; xIndexBefore++)
                {
                    try
                    {
                        _field.CellsMatrix[y - 1, xIndexBefore].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
            else
            {
                for (int yIndexBefore = y - 1; yIndexBefore < y + 2; yIndexBefore++)
                {
                    try
                    {
                        _field.CellsMatrix[yIndexBefore, x - 1].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }


            //main iteration
            for (int i = 0; i < iterations; i++)
            {
                if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    try
                    {
                        _field.CellsMatrix[y, x - 1].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                    try
                    {
                        _field.CellsMatrix[y, x + 1].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                }
                else
                {
                    try
                    {
                        _field.CellsMatrix[y - 1, x].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                    try
                    {
                        _field.CellsMatrix[y + 1, x].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                }

                //increment
                if (shipPlacementInfo.ShipRotation == ShipRotation.Horizontal)
                {
                    x++;
                }
                else if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
                {
                    y++;
                }
            }


            //after main iteration
            if (shipPlacementInfo.ShipRotation == ShipRotation.Vertical)
            {
                for (int xIndexAfter = x - 1; xIndexAfter < x + 2; xIndexAfter++)
                {
                    try
                    {
                        _field.CellsMatrix[y, xIndexAfter].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
            else
            {
                for (int yIndexAfter = y - 1; yIndexAfter < y + 2; yIndexAfter++)
                {
                    try
                    {
                        _field.CellsMatrix[yIndexAfter, x].CanPlaceShip = false;
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        }