Esempio n. 1
0
        //Gets the player to place their ships on the board
        public void SetUp()
        {
            int  x;
            int  y;
            char orientation;
            bool shipAdded = false;
            bool setUpDone = false;

            do
            {
                for (int i = 0; i < 5; i++)
                {
                    UI.DisplaySetUpInstructions(i);
                    UI.DisplayShipsBoard(ships);

                    do
                    {
                        x = UI.GetCoordinateFromUser('x');
                        y = UI.GetCoordinateFromUser('y');

                        orientation = UI.GetOrientationFromUser();

                        try
                        {
                            List <Ship> shipsCopy = new List <Ship>(ships);
                            Ship        ship      = ShipFactory.Build(i, new Point(x, y), orientation, shipsCopy);
                            ship.SetShipSunkListener(this);
                            ships.Add(ship);

                            shipAdded = true;
                        }
                        catch (PointOutOfBoundsException)
                        {
                            UI.DisplayErrorMessage("The ship did not fit inside the board. Try again.");
                            shipAdded = false;
                        }
                        catch (ShipOverLapException)
                        {
                            UI.DisplayErrorMessage("The ship overlaps with one or more ships on the borad. Try again.");
                            shipAdded = false;
                        }
                    } while (!shipAdded);
                }

                if (UI.PlayerReadyToStartGame(ships))
                {
                    setUpDone = true;
                }
                else
                {
                    ships.Clear();
                }
            } while (!setUpDone);
        }