Beispiel #1
0
        // generates a random starting position for a ship with a given size
        private ShipPlacement PlaceShip(int size)
        {
            bool          horizontal;
            bool          validPlacement = false;
            ShipPlacement trial          = new ShipPlacement();

            horizontal = (randomizer.Next(2) == 0);
            if (horizontal)
            {
                trial.startPosition = new Coords(randomizer.Next(10 - size), randomizer.Next(10));
                trial.horizontal    = true;
            }
            else
            {
                trial.startPosition = new Coords(randomizer.Next(10), randomizer.Next(10 - size));
                trial.horizontal    = false;
            }

            while (!validPlacement)
            {
                if (ValidatePlaces(trial, size))
                {
                    validPlacement = true;
                }
                else
                {
                    horizontal = (randomizer.Next(2) == 0);
                    if (horizontal)
                    {
                        trial.startPosition = new Coords(randomizer.Next(10 - size), randomizer.Next(10));
                        trial.horizontal    = true;
                    }
                    else
                    {
                        trial.startPosition = new Coords(randomizer.Next(10), randomizer.Next(10 - size));
                        trial.horizontal    = false;
                    }
                }
            }

            return(trial);
        }
Beispiel #2
0
        public void PlaceShipOnBoard(Player player)
        {
            bool IsPlaceBoardAuto = false;

            if (player.IsPC != true)
            {
                OutputHandler.ShowWhoseTurn(player);
                IsPlaceBoardAuto = InputHandler.IsPlaceBoardAuto();
                if (!IsPlaceBoardAuto)
                {
                    Console.WriteLine("Input the location and direction(l, r, u, d) of the ships. Ex:) a2, r:");
                }
            }
            for (ShipType s = ShipType.Destroyer; s <= ShipType.Carrier; s++)
            {
                ShipPlacement ShipToPlace = new ShipPlacement();
                ShipPlacing   result;
                do
                {
                    if (!player.IsPC && !IsPlaceBoardAuto)
                    {
                        ShipToPlace          = InputHandler.GetLocationFromUser(s.ToString());
                        ShipToPlace.ShipType = s;
                        result = player.PlayerBoard.PlaceShip(ShipToPlace);
                        if (result == ShipPlacing.NotEnoughSpace)
                        {
                            Console.WriteLine("Not Enough Space!");
                        }
                        else if (result == ShipPlacing.Overlap)
                        {
                            Console.WriteLine("Overlap placement!");
                        }
                    }
                    else
                    {
                        ShipToPlace          = InputHandler.GetLocationFromComputer();
                        ShipToPlace.ShipType = s;
                        result = player.PlayerBoard.PlaceShip(ShipToPlace);
                    }
                } while (result != ShipPlacing.Ok);
            }
        }
        public static ShipPlacement GetLocationFromUser(string ShipType)
        {
            ShipPlacement result = null;

            do
            {
                Console.Write("- " + ShipType + ": ");
                result = GetLocation(Console.ReadLine());
                if (result is null)
                {
                    ;
                }
                else
                {
                    return(result);
                }

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Invalid input. Please input location and direiction. Ex:) a2, r");
                Console.ForegroundColor = ConsoleColor.White;
            } while (result is null);
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Phase 2: shipPlacement 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void shipSetup(object sender, EventArgs e)
        {
            //Close setup
            grid.Children.Clear();

            //Resize window
            this.MinWidth = 460;
            this.MinHeight = 530;
            this.Width = 460;
            this.Height = 530;

            //Initialize ship placement phase
            shipPlacement = new ShipPlacement();

            //Add ship placement grid
            grid.Children.Add(shipPlacement);

            shipPlacement.play += new EventHandler(playGame);
        }
Beispiel #5
0
        /// <summary>
        /// Add ship to the board
        /// </summary>
        /// <param name="ship">Ship with x/y coordinate and length </param>
        /// <param name="placement"> Placement can be horizontal or vertical </param>
        /// <returns> True if ship is added successfully, false if not </returns>
        public bool addShip(Ship ship, ShipPlacement placement)
        {
            int endXCoordinate = ship.x;
            int endYCoordinate = ship.y;

            // Find the end coordinate of the ship depending on it's
            // placement
            switch (placement)
            {
            case ShipPlacement.HORIZONTAL:
                endXCoordinate += (ship.length - 1);
                break;

            case ShipPlacement.VERTICAL:
                endYCoordinate += (ship.length - 1);
                break;

            default:
                throw new Exception("Invalid ship placement");
            }



            // Check whether the end coordinate is out of bound
            // Start coordinate isn't checked here as it's coordinate
            // and length is checked in Ship

            if (endXCoordinate <= this.horizontalLength &&
                endYCoordinate <= this.verticalLength)
            {
                // If coordinates are within board's bound


                // Start by sssuming that Ship can be placed in the coordinate
                bool shipCanBePlaced = true;

                // Check each individual cell and mark shipCanBePlaced as false if
                // there's a ship in any of the coordinate
                switch (placement)
                {
                case ShipPlacement.HORIZONTAL:
                    for (int idx = ship.x; idx <= endXCoordinate; idx++)
                    {
                        if (this.hasShipAtCoordinate(idx, ship.y))
                        {
                            shipCanBePlaced = false;
                            break;
                        }
                    }
                    break;

                case ShipPlacement.VERTICAL:
                    for (int idx = ship.y; idx <= endYCoordinate; idx++)
                    {
                        if (this.hasShipAtCoordinate(ship.x, idx))
                        {
                            shipCanBePlaced = false;
                            break;
                        }
                    }
                    break;

                default:
                    // Exception - Indicates that Placement functionality was updated
                    // but not yet implemented here
                    throw new Exception("Invalid ship placement");
                }



                // If ship can be placed
                if (shipCanBePlaced)
                {
                    // Iterate through each cell and assign the ship to those cell.
                    switch (placement)
                    {
                    case ShipPlacement.HORIZONTAL:
                        for (int idx = ship.x; idx <= endXCoordinate; idx++)
                        {
                            this.addShipToCoordinate(ship, idx, ship.y);
                        }
                        break;

                    case ShipPlacement.VERTICAL:
                        for (int idx = ship.y; idx <= endYCoordinate; idx++)
                        {
                            this.addShipToCoordinate(ship, ship.x, idx);
                        }
                        break;

                    default:
                        // Exception - Indicates that Placement functionality was updated
                        // but not yet implemented here
                        throw new Exception("Invalid ship placement");
                    }
                }
                else
                {
                    //Ship cannot be placed - Notify caller
                    return(false);
                }

                // Ship is added - increment the number of ship
                this.totalShips++;

                return(true); // Notify caller
            }
            else
            {
                // Ship cannot be placed - Ship is Out of bound
                return(false); // Notify caller
            }
        }