예제 #1
0
        public static void Play()
        {
            Console.Write("What is your name? : ");
            string name = Console.ReadLine();
            Sea PlayerSea = new Sea();
            Player player1 = new Player(PlayerSea, name);
            Sea ComputerSea = new Sea();
            Computer computer = new Computer(ComputerSea);

            while (true)
            {
                try
                {
                    //player1.Shoot(computer);
                    computer.ComputerShoot(player1);

                    if (player1.DestroyedShips >= 3 || computer.DestroyedShips >= 3)
                    {
                        Console.WriteLine("You Won! Bye!");
                        break;
                    }

                }
                catch (OutOfBoundsException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (FormatException)
                {
                    Console.WriteLine("This is not valid input");
                }
            }
        }
        public Computer(Sea sea)
        {
            Sea = sea;

            frigate.ComputerShipPlacement(Sea);
            submarine.ComputerShipPlacement(Sea);
            destroyer.ComputerShipPlacement(Sea);
        }
        public Player(Sea sea, string name)
        {
            Sea  = sea;
            Name = name;

            frigate.ShipPlacment(sea);
            submarine.ShipPlacment(sea);
            destroyer.ShipPlacment(sea);
        }
예제 #4
0
        public static void Play()
        {
            Console.Write("What is Player 1 name? : ");
            string name1 = Console.ReadLine();

            Console.Write("What is Player 2 name? : ");
            string name2 = Console.ReadLine();

            Sea Player1Sea = new Sea();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write($"{name1}");
            Console.ResetColor();
            Console.WriteLine(" Please place battle ships on the sea.");

            Player player1 = new Player(Player1Sea, name1);

            Sea Player2Sea = new Sea();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write($"{name2}");
            Console.ResetColor();
            Console.WriteLine(" Please place battle ships on the sea.");
            Player player2 = new Player(Player2Sea, name2);

            while (true)
            {
                try
                {
                    player1.Shoot(player2);
                    player2.Shoot(player1);

                    if (player1.DestroyedShips >= 3 || player2.DestroyedShips >= 3)
                    {
                        Console.WriteLine("You Won! Bye!");

                        break;
                    }
                }
                catch (OutOfBoundsException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (FormatException)
                {
                    Console.WriteLine("This is not valid input");
                }
            }
        }
예제 #5
0
        public void ShipPlacment(Sea sea)
        {
            bool placed = false;

            while (placed == false)
            {
                try
                {
                    Console.WriteLine($"Select location of your {Name} between 1-10:");
                    Console.Write("Row Location: ");
                    int x = int.Parse(Console.ReadLine()) - 1;
                    Console.Write("Column location: ");
                    int y = int.Parse(Console.ReadLine()) - 1;

                    Console.Write("Enter 1 for Horizontal or 2 Vertical :");
                    string orient = Console.ReadLine();

                    if (!sea.OnSea(x, y))
                    {
                        throw new OutOfBoundsException($"{x - 1}, {y - 1} is outside the boundaries of the sea.");
                    }

                    string notEmpty = $"You already got battleship on {x + 1}, {y + 1} location, please select other location.";



                    if (sea.GameSea[x, y].Name != "Empty")
                    {
                        Console.WriteLine(notEmpty);
                        continue;
                    }
                    else if (sea.GameSea[x, y].Name == "Empty")
                    {
                        if (orient == "1")
                        {
                            for (int i = 0; i < Health; i++, x++)
                            {
                                if (sea.GameSea[x, y].Name != "Empty")
                                {
                                    Console.WriteLine(notEmpty);
                                    continue;
                                }
                                else
                                {
                                    sea.GameSea[x, y] = this;
                                    placed            = true;
                                }
                            }
                        }
                        else if (orient == "2")
                        {
                            for (int i = 0; i < Health; i++, y++)
                            {
                                if (sea.GameSea[x, y].Name != "Empty")
                                {
                                    Console.WriteLine(notEmpty);
                                    continue;
                                }
                                else
                                {
                                    sea.GameSea[x, y] = this;
                                    placed            = true;
                                }
                            }
                        }
                        else
                        {
                            throw new FormatException();
                        }
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    Console.WriteLine("Battleship placed out of sea, please try again.");
                }
                catch (OutOfBoundsException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (FormatException)
                {
                    Console.WriteLine("This is not valid input select 1 or 2");
                }
            }
        }
예제 #6
0
        public void ComputerShipPlacement(Sea sea)
        {
            bool placed = false;

            while (placed == false)
            {
                try
                {
                    int x      = random.Next(9);
                    int y      = random.Next(9);
                    int orient = random.Next(2);

                    if (sea.GameSea[x, y].Name != "Empty")
                    {
                        continue;
                    }
                    else if (sea.GameSea[x, y].Name == "Empty")
                    {
                        if (orient == 1)
                        {
                            for (int i = 0; i < Health; i++, x++)
                            {
                                if (sea.GameSea[x, y].Name != "Empty")
                                {
                                    continue;
                                }
                                else
                                {
                                    sea.GameSea[x, y] = this;
                                    placed            = true;
                                }
                            }
                        }
                        else if (orient == 2)
                        {
                            for (int i = 0; i < Health; i++, y++)
                            {
                                if (sea.GameSea[x, y].Name != "Empty")
                                {
                                    continue;
                                }
                                else
                                {
                                    sea.GameSea[x, y] = this;
                                    placed            = true;
                                }
                            }
                        }
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    continue;
                }
                catch (OutOfBoundsException)
                {
                    continue;
                }
                catch (FormatException)
                {
                    continue;
                }
            }
        }