Пример #1
0
        public static void Main(string[] args)
        {
            Game game = new Game(); //Game class will control main program

            Printer.DisplayStartMenu();
            while (!game.GameOver()) //GameOver() = true when there are 5 ships destroyed
            {
                Console.Clear();
                game.DisplayUserBoard();
                UserCoordinates userTarget = GetUserTarget(); //collects user input for selecting a target, and feeds it to Game class for processing
                game.ProcessUserTarget(userTarget);
            }
            Printer.DisplayWin();
        }
Пример #2
0
        private static UserCoordinates GetUserTarget()
        {
            Console.WriteLine("\n\n\n\n"); //leading whitespace for formatting

            string userInput = "";
            bool   loop      = true;

            while (loop)
            {
                Console.WriteLine("Type the coordinates (column, row) of your target (ie. b3) and then press 'Enter'.");
                userInput = Console.ReadLine().ToUpper();

                if (string.IsNullOrEmpty(userInput))
                {
                    Console.WriteLine("\nYou must fire!\n"); continue;
                }                                                                                       //check if input is empty
                else if (userInput.Length > 3)
                {
                    Console.WriteLine("\nYour coordinates were too long!\n"); continue;
                }
                ;                                                                          //check if input is too long - string length 3 should be longest input
                Match match = Regex.Match(userInput, (@"(^[A-J][1-9]$)|(^[A-J][1][0]$)")); //regex match check for correct user input
                if (match.Success)
                {
                    loop = false;
                }
                else
                {
                    Console.WriteLine("\nThose weren't valid coordinates. Try again!\n");
                }
            }
            char tempUserCol = userInput.Substring(0, 1).ToCharArray()[0]; //collect column and row and turn both to ints
            int  userCol     = Convert.ToInt32(tempUserCol) - 64;
            int  userRow     = Convert.ToInt32(userInput.Substring(1));

            UserCoordinates userCoords = new UserCoordinates(userCol, userRow);

            return(userCoords);
        }
Пример #3
0
        public void ProcessUserTarget(UserCoordinates userTarget)
        {
            if (cpuBoard[userTarget.Row, userTarget.Col] != "░░░") //triggers when a user target is not water, meaning a hit
            {
                Printer.DisplayHit();
                Console.WriteLine("\n\n\n\n\n\n\n\n(Press any key to continue.)");
                Console.ReadKey(true);

                if (cpuBoard[userTarget.Row, userTarget.Col] == " P ")
                {
                    ships[0].Length--;
                }                                                                          //subtract life from the ships to know when they are sunk
                else if (cpuBoard[userTarget.Row, userTarget.Col] == " S ")
                {
                    ships[1].Length--;
                }
                else if (cpuBoard[userTarget.Row, userTarget.Col] == " D ")
                {
                    ships[2].Length--;
                }
                else if (cpuBoard[userTarget.Row, userTarget.Col] == " B ")
                {
                    ships[3].Length--;
                }
                else if (cpuBoard[userTarget.Row, userTarget.Col] == " C ")
                {
                    ships[4].Length--;
                }

                userBoard[userTarget.Row, userTarget.Col] = "HIT";
                cpuBoard[userTarget.Row, userTarget.Col]  = "░░░";

                foreach (var ship in ships)
                {
                    if (ship.Length == 0)
                    {
                        Printer.DisplaySink(ship.Id);
                        ship.Length--;
                        shipsDestroyed++;
                    }
                }
            }
            else if (cpuBoard[userTarget.Row, userTarget.Col] == "░░░" && userBoard[userTarget.Row, userTarget.Col] == " M ") //if user selects a miss again
            {
                Console.WriteLine("\n");
                Console.WriteLine("You already missed here, silly! Try again!".PadLeft(75));
                Console.WriteLine("\n\n(Press any key to continue.)");
                Console.ReadKey(true);
            }
            else if (cpuBoard[userTarget.Row, userTarget.Col] == "░░░" && userBoard[userTarget.Row, userTarget.Col] == "HIT") //if user selects a target they already hit
            {
                Console.WriteLine("\n");
                Console.WriteLine("You already hit this ship here. Try again!".PadLeft(75));
                Console.WriteLine("\n\n(Press any key to continue.)");
                Console.ReadKey(true);
            }
            else if (cpuBoard[userTarget.Row, userTarget.Col] == "░░░")
            {
                Console.WriteLine("\n");
                Console.WriteLine("Miss!".PadLeft(55));
                Console.WriteLine("\n\n(Press any key to continue.)");
                Console.ReadKey(true);

                userBoard[userTarget.Row, userTarget.Col] = " M ";
                cpuBoard[userTarget.Row, userTarget.Col]  = "░░░";
            }
        }