示例#1
0
        /// <summary>
        /// Places the ships in the grid
        /// </summary>
        public void PlaceShips()
        {
            //Place each ship horizontally or vertically
            foreach (Ship ship in _gridShips)
            {
                //Keep trying to find a valid spot to place the ship
                while (ship.shipPlaced == false)
                {
                    int gridSeed = seedGen.getGridSeed();

                    //Random roll to place the ships vertically or horizontally
                    int orientation = seedGen.getShipOrientation();

                    //Horizontal
                    if (orientation == (int)SHIP_ORIENTATION.Horizontal && TryHorizontalPlacement(gridSeed, ship))
                    {
                        //For horizontal placement we take the starting square and place in each consecutive right square
                        for (int i = gridSeed; i < gridSeed + ship.shipSize; i++)
                        {
                            _gridSquares[i].squareContents = ship.shipType;
                            _gameGrid[i].Tag = ship.shipType;

                            //Keep computer squares hidden
                            if (!this.isComputer)
                            {
                                _gameGrid[i].Background = ship.shipColor;
                            }
                        }
                    }
                    //Vertical
                    else if (orientation == (int)SHIP_ORIENTATION.Vertical && TryVerticalPlacement(gridSeed, ship))
                    {
                        //For vertical placement we need to go down one row and place up to down, so we have to add 10 to the index
                        //The loop index also must go from the initial seed number to shipSize*10 to accomodate this range
                        for (int i = gridSeed; i < gridSeed + (ship.shipSize * 10); i = i + 10)
                        {
                            _gridSquares[i].squareContents = ship.shipType;
                            _gameGrid[i].Tag = ship.shipType;

                            //Keep computer squares hidden
                            if (!this.isComputer)
                            {
                                _gameGrid[i].Background = ship.shipColor;
                            }
                        }
                    }
                }
            }
        }