Example #1
0
        //place all ships for player
        public void PlaceShips(Player player)
        {
            string[] directions = new string[4] {
                "UP", "DOWN", "LEFT", "RIGHT"
            };
            bool error = false;
            bool check = false;

            //For loop to run through all boats in boatNames[]
            for (int i = 0; i < boatNames.Length; i++)
            {
                player.DrawPlayerBoard();

                //User is asked to enter the first coordinate for the current boat
                string place;
                Console.WriteLine("\n\n" + player.PlayerName + ": Select first coordinate for " + boatNames[i] + " (" + boatLengths[i] + ") (Example: A1 to J10)\n");
                place = Console.ReadLine();
                string direction;
                //Letter is the first letter of the current boat. Used to mark the map
                string letter = " " + boatNames[i].Substring(0, 1) + " ";

                while (!CheckValidMove(place))
                {
                    Console.WriteLine("\nInvalid coordinates entered. Please enter a new coordinate (A1 to J10)\n");
                    place = Console.ReadLine();
                }

                int x = GetXCoord(place);
                int y = GetYCoord(place);

                //While loop to check if that square already contains another boat
                while (player.PlayerBoard[y, x] != " x ")
                {
                    Console.WriteLine("\nOverlapping boat, please enter a new starting coordinate: (A1 to J10)\n");

                    place = Console.ReadLine();

                    while (!CheckValidMove(place))
                    {
                        Console.WriteLine("\nInvalid coordinates entered. Please enter a new coordinate (A1 to J10)\n");
                        place = Console.ReadLine();
                    }

                    while (place == "")
                    {
                        Console.WriteLine("\nNo coordinate entered. Try again.\n");
                        place = Console.ReadLine();
                    }

                    x = GetXCoord(place);
                    y = GetYCoord(place);
                }

                //do while loop to determine the direction of the boat
                Console.WriteLine("\nPlease enter direction of boat. (UP/DOWN/LEFT/RIGHT)\n");
                do
                {
                    error     = false;
                    check     = false;
                    direction = Console.ReadLine().ToUpper();

                    //direction must be UP LEFT DOWN or RIGHT
                    while (direction != "UP" && direction != "DOWN" && direction != "LEFT" && direction != "RIGHT")
                    {
                        Console.WriteLine("\nInvalid input. Try again.\n");
                        direction = Console.ReadLine().ToUpper();
                    }

                    //for loop which runs through the length of the current boat selected
                    for (int l = 0; l < boatLengths[i]; l++)
                    {
                        switch (direction)
                        {
                        case "UP":
                            try
                            {
                                //if statement to check boatlengths in direction only once per choice
                                if (!check)
                                {
                                    for (int d = 0; d < boatLengths[i]; d++)
                                    {
                                        if (player.PlayerBoard[y - d, x] != " x ")
                                        {
                                            //checks all boatlengths in the current direction and makes error true if not all tiles are clear
                                            Console.WriteLine("\nBoats cannot be on top of eachother! Pick a new direction.\n");
                                            error = true;
                                            l     = boatLengths.Length;
                                            break;
                                        }
                                        check = true;
                                    }
                                }
                                else
                                {
                                    //places the boat tile for y - L (up)
                                    player.GameBoard(y - l, x, letter);
                                    player.GameBoard(y, x, letter);
                                }
                            }
                            catch (Exception)
                            {
                                if (!error)
                                {
                                    Console.WriteLine("\n" + boatNames[i] + " Fell off the map!\n\nPlease enter a new direction.\n");
                                    error = true;
                                    l     = boatLengths.Length;
                                }
                            }
                            break;

                        case "DOWN":
                            try
                            {
                                if (!check)
                                {
                                    for (int d = 0; d < boatLengths[i]; d++)
                                    {
                                        if (player.PlayerBoard[y + d, x] != " x ")
                                        {
                                            Console.WriteLine("\nBoats cannot be on top of eachother! Pick a new direction.\n");
                                            error = true;
                                            l     = boatLengths.Length;
                                            break;
                                        }
                                        check = true;
                                    }
                                }
                                else
                                {
                                    player.GameBoard(y + l, x, letter);
                                    player.GameBoard(y, x, letter);
                                }
                            }
                            catch (Exception)
                            {
                                if (!error)
                                {
                                    Console.WriteLine("\n" + boatNames[i] + " Fell off the map!\n\nPlease enter a new direction.\n");
                                    error = true;
                                    l     = boatLengths.Length;
                                }
                            }
                            break;

                        case "LEFT":
                            try
                            {
                                if (!check)
                                {
                                    for (int d = 0; d < boatLengths[i]; d++)
                                    {
                                        if (player.PlayerBoard[y, x - d] != " x ")
                                        {
                                            Console.WriteLine("\nBoats cannot be on top of eachother! Pick a new direction.\n");
                                            error = true;
                                            l     = boatLengths.Length;
                                            break;
                                        }
                                        check = true;
                                    }
                                }
                                else
                                {
                                    player.GameBoard(y, x - l, letter);
                                    player.GameBoard(y, x, letter);
                                }
                            }
                            catch (Exception)
                            {
                                if (!error)
                                {
                                    Console.WriteLine("\n" + boatNames[i] + " Fell off the map!\n\nPlease enter a new direction.\n");
                                    error = true;
                                    l     = boatLengths.Length;
                                }
                            }
                            break;

                        case "RIGHT":
                            try
                            {
                                if (!check)
                                {
                                    for (int d = 0; d < boatLengths[i]; d++)
                                    {
                                        if (player.PlayerBoard[y, x + d] != " x ")
                                        {
                                            Console.WriteLine("\nBoats cannot be on top of eachother! Pick a new direction.\n");
                                            error = true;
                                            l     = boatLengths.Length;
                                            break;
                                        }
                                        check = true;
                                    }
                                }
                                else
                                {
                                    player.GameBoard(y, x + l, letter);
                                    player.GameBoard(y, x, letter);
                                }
                            }
                            catch (Exception)
                            {
                                if (!error)
                                {
                                    Console.WriteLine("\n" + boatNames[i] + " Fell off the map!\n\nPlease enter a new direction.\n");
                                    error = true;
                                    l     = boatLengths.Length;
                                }
                            }
                            break;

                        default:
                            Console.WriteLine("Your boat was ravaged by sharks! You lost! (If you see this message please report it to a developer)");
                            break;
                        }
                    }
                }while (error == true);

                Console.Clear();
            }
        }