예제 #1
0
        //protected Position GenerateRobotShoot()
        //{
        //    Position robotShot=new Position();
        //    robotShot.X=randGenerator.Next(0, seas[0].GetBody().GetLength(0));
        //    robotShot.Y  = randGenerator.Next(0, seas[0].GetBody().GetLength(0));
        //    return robotShot;
        //}

        public void ConfigureGameField(Sea someSea, int shipMaxLength)
        {
            if (shipMaxLength > someSea.GetBody().GetLength(0) - 2)
            {
                throw new OutOfShipLengthException("Exceeded maximal ship length.", someSea.GetBody().GetLength(0) - 2);
            }

            for (int shipLength = shipMaxLength; shipLength >= 2; shipLength--)
            {
                while (true)
                {
                    Ship currentShip;
                    if (someSea is PlayerSea)
                    {
                        string message = string.Format("Ship`s length: {0}. Enter ships parameters in format: \r\nStart position: X Y:\r\nAlignment:", shipLength);
                        renderer.AddMessageToRender(message);
                        renderer.RenderAll();
                        renderer.ClearRenderObjects();
                        currentShip = this.userInput.ReadInputShip(shipLength);
                    }
                    else
                    {
                        currentShip = this.GenerateShip(shipLength, someSea.GetBody().GetLength(0));
                    }

                    Position[] currentShipBody = currentShip.GetBody() as Position[];
                    int        freePositions   = 0;

                    foreach (var position in currentShipBody)
                    {
                        if (someSea is PlayerSea)
                        {
                            if ((position.X < someSea.GetBody().GetLength(0) && position.Y < someSea.GetBody().GetLength(1)) && (someSea[position.X, position.Y] == State.Empty))
                            {
                                freePositions++;
                            }
                        }
                        else if (someSea[position.X, position.Y] == State.Empty)
                        {
                            freePositions++;
                        }
                    }

                    if (freePositions == shipLength)
                    {
                        foreach (var position in currentShipBody)
                        {
                            someSea[position.X, position.Y] = State.Ship;
                        }

                        if (someSea is PlayerSea)
                        {
                            this.AddPlayerShips(currentShip);
                            string message = string.Format("Ship with length {0} was created", shipLength);
                            renderer.AddMessageToRender(message);
                            renderer.RenderAll();
                            renderer.ClearRenderObjects();
                        }
                        else
                        {
                            this.AddRobotShips(currentShip);
                        }

                        break;
                    }
                    else if (someSea is PlayerSea)
                    {
                        string message = string.Format("Ship with length {0} was NOT created", shipLength);
                        renderer.AddMessageToRender(message);
                        renderer.RenderAll();
                        renderer.ClearRenderObjects();
                    }
                }
            }

            this.AddSea(someSea);
        }