Exemplo n.º 1
0
        public void PlaceShip(Submarine submarine, Battleship battleship, AircraftCarrier aircraftCarrier)
        {
            locationSet = false;
            location    = ClearLocation(location);
            Console.WriteLine($"Where would you like to place your {name}?");
            Console.WriteLine($"This ship requires {size} spots.  Pleae give the first spots x coordinate:");
            string x1 = Console.ReadLine();

            Console.WriteLine($"Now, pleae give the first spots y coordinate:");
            string y1 = Console.ReadLine();

            Console.WriteLine($"This ship requires {size} spots.  Pleae give the second spots x coordinate:");
            string x2 = Console.ReadLine();

            Console.WriteLine($"Now, pleae give the second spots y coordinate:");
            string y2 = Console.ReadLine();

            if (!GoodCoordinates(x1, y1, x2, y2, submarine, battleship, aircraftCarrier))
            {
                Console.WriteLine($"I'm sorry, it doesn't seem like those coordinates work for the {name}.  Let's try again.");
                location = ClearLocation(location);
                PlaceShip(submarine, battleship, aircraftCarrier);
            }
            if (!locationSet)
            {
                SetCoordinates(x1, y1, x2, y2);
            }
        }
Exemplo n.º 2
0
 public Board()
 {
     GetBoardSize();
     destroyer       = new Destroyer();
     submarine       = new Submarine();
     battleship      = new Battleship();
     aircraftCarrier = new AircraftCarrier();
 }
Exemplo n.º 3
0
 public bool NoOtherShipConflict(Destroyer destroyer, Submarine submarine, AircraftCarrier aircraftCarrier)
 {
     if (destroyer.GetLocationIsSet())
     {
         for (int i = 0; i < destroyer.location.GetLength(0); i++)
         {
             if ((destroyer.location[i, 0] == location[0, 0] && destroyer.location[i, 1] == location[0, 0]) ||
                 (destroyer.location[i, 0] == location[1, 0] && destroyer.location[i, 1] == location[1, 1]) ||
                 (destroyer.location[i, 0] == location[2, 0] && destroyer.location[i, 1] == location[2, 1]) ||
                 (destroyer.location[i, 0] == location[3, 0] && destroyer.location[i, 1] == location[3, 1]))
             {
                 return(false);
             }
         }
     }
     if (submarine.GetLocationIsSet())
     {
         for (int i = 0; i < submarine.location.GetLength(0); i++)
         {
             if ((submarine.location[i, 0] == location[0, 0] && submarine.location[i, 1] == location[0, 0]) ||
                 (submarine.location[i, 0] == location[1, 0] && submarine.location[i, 1] == location[1, 1]) ||
                 (submarine.location[i, 0] == location[2, 0] && submarine.location[i, 1] == location[2, 1]) ||
                 (submarine.location[i, 0] == location[3, 0] && submarine.location[i, 1] == location[3, 1]))
             {
                 return(false);
             }
         }
     }
     if (aircraftCarrier.GetLocationIsSet())
     {
         for (int i = 0; i < aircraftCarrier.location.GetLength(0); i++)
         {
             if ((aircraftCarrier.location[i, 0] == location[0, 0] && aircraftCarrier.location[i, 1] == location[0, 0]) ||
                 (aircraftCarrier.location[i, 0] == location[1, 0] && aircraftCarrier.location[i, 1] == location[1, 1]) ||
                 (aircraftCarrier.location[i, 0] == location[2, 0] && aircraftCarrier.location[i, 1] == location[2, 1]) ||
                 (aircraftCarrier.location[i, 0] == location[3, 0] && aircraftCarrier.location[i, 1] == location[3, 1]))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Exemplo n.º 4
0
 public bool NoOtherShipConflict(Destroyer destroyer, Battleship battleship, AircraftCarrier aircraftCarrier)
 {
     if (destroyer.GetLocationIsSet())
     {
         for (int i = 0; i < destroyer.location.GetLength(0); i++)
         {
             if ((destroyer.location[i, 0] == location[0, 0] && destroyer.location[i, 1] == location[0, 0]) ||
                 (destroyer.location[i, 0] == location[1, 0] && destroyer.location[i, 1] == location[1, 1]) ||
                 (destroyer.location[i, 0] == location[2, 0] && destroyer.location[i, 1] == location[2, 1]))
             {
                 return(false);
             }
         }
     }
     if (battleship.GetLocationIsSet())
     {
         for (int i = 0; i < battleship.location.GetLength(0); i++)
         {
             if ((battleship.location[i, 0] == location[0, 0] && battleship.location[i, 1] == location[0, 0]) ||
                 (battleship.location[i, 0] == location[1, 0] && battleship.location[i, 1] == location[1, 1]) ||
                 (battleship.location[i, 0] == location[2, 0] && battleship.location[i, 1] == location[2, 1]))
             {
                 return(false);
             }
         }
     }
     if (aircraftCarrier.GetLocationIsSet())
     {
         for (int i = 0; i < aircraftCarrier.location.GetLength(0); i++)
         {
             if ((aircraftCarrier.location[i, 0] == location[0, 0] && aircraftCarrier.location[i, 1] == location[0, 0]) ||
                 (aircraftCarrier.location[i, 0] == location[1, 0] && aircraftCarrier.location[i, 1] == location[1, 1]) ||
                 (aircraftCarrier.location[i, 0] == location[2, 0] && aircraftCarrier.location[i, 1] == location[2, 1]))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Exemplo n.º 5
0
 public bool NoConflictWithOtherShips(Submarine Submarine, Battleship battleship, AircraftCarrier aircraftCarrier)
 {
     return(true);
 }
Exemplo n.º 6
0
 public bool NoOtherShipConflict(int x1, int y1, int x2, int y2, Submarine submarine, Battleship battleship, AircraftCarrier aircraftCarrier)
 {
     if (submarine.GetLocationIsSet())
     {
         for (int i = 0; i < submarine.location.GetLength(0); i++)
         {
             if ((submarine.location[i, 0] == x1 && submarine.location[i, 1] == y1) ||
                 (submarine.location[i, 0] == x2 && submarine.location[i, 1] == y2))
             {
                 return(false);
             }
         }
     }
     if (battleship.GetLocationIsSet())
     {
         for (int i = 0; i < battleship.location.GetLength(0); i++)
         {
             if ((battleship.location[i, 0] == x1 && battleship.location[i, 1] == y1) ||
                 (battleship.location[i, 0] == x2 && battleship.location[i, 1] == y2))
             {
                 return(false);
             }
         }
     }
     if (aircraftCarrier.GetLocationIsSet())
     {
         for (int i = 0; i < aircraftCarrier.location.GetLength(0); i++)
         {
             if ((aircraftCarrier.location[i, 0] == x1 && aircraftCarrier.location[i, 1] == y1) ||
                 (aircraftCarrier.location[i, 0] == x2 && aircraftCarrier.location[i, 1] == y2))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Exemplo n.º 7
0
        public bool GoodCoordinates(string stringx1, string stringy1, string stringx2, string stringy2, Submarine submarine, Battleship battleship, AircraftCarrier aircraftCarrier)
        {
            bool verifyCoordinates = false;
            int  x1;
            int  x2;
            int  y1;
            int  y2;

            if (CoordinateIsInt(stringx1) && CoordinateIsInt(stringx2) && CoordinateIsInt(stringy1) && CoordinateIsInt(stringy2))
            {
                x1 = Convert.ToInt32(stringx1);
                y1 = Convert.ToInt32(stringy1);
                x2 = Convert.ToInt32(stringx2);
                y2 = Convert.ToInt32(stringy2);
                if (ValidBoardCoordinate(x1, y1) && ValidBoardCoordinate(x2, y2))
                {
                    if (ValidShipOnBoard(x1, y1, x2, y2))
                    {
                        if (NoOtherShipConflict(x1, y1, x2, y2, submarine, battleship, aircraftCarrier))
                        {
                            verifyCoordinates = true;
                        }
                    }
                }
            }
            return(verifyCoordinates);
        }
Exemplo n.º 8
0
        public void AddShips(int frontRow, int frontColumn, int backRow, int backColumn, int choiceOfShip)
        {
            string[,] establishedPositions = new string[20, 20];
            string[,] proposedPosition     = new string[20, 20];
            bool isOverlapping = false;

            switch (choiceOfShip)
            {
            case 1:
                if (!destroyerIsChosen)
                {
                    if (frontRow == backRow || frontColumn == backColumn)     //this is a check for correct placement / not diagonal
                    {
                        if ((frontRow == backRow) && (frontColumn + 1 == backColumn || backColumn + 1 == frontColumn) ||
                            (frontColumn == backColumn) && (frontRow + 1 == backRow || backRow + 1 == frontRow)) // check for length
                        {
                            establishedPositions = GetPlacedShipPositions();                                     //overlapping logic begins
                            proposedPosition     = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn);
                            for (int i = 0; i < 20; i++)
                            {
                                for (int j = 0; j < 20; j++)
                                {
                                    if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null))
                                    {
                                        isOverlapping = true;
                                    }
                                }
                            }                   //overlapping logic ends
                            if (!isOverlapping) //overlapping check
                            {
                                Ship destroyer = new Destroyer();
                                destroyer.frontRow    = frontRow;
                                destroyer.frontColumn = frontColumn;
                                destroyer.backRow     = backRow;
                                destroyer.backColumn  = backColumn;
                                this.fleet.Add(destroyer);
                                destroyerIsChosen = true;
                            }
                            else
                            {
                                Console.WriteLine("The destroyer is overlapping.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("The destroyer is not the correct length.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("The destroyer is diagonal.");
                    }
                }
                else
                {
                    Console.WriteLine("The destroyer has already been chosen.");
                }
                break;

            case 2:
                if (!submarineIsChosen)
                {
                    if (frontRow == backRow || frontColumn == backColumn)     //this is a check for correct placement / not diagonal
                    {
                        if ((frontRow == backRow) && (frontColumn + 2 == backColumn || backColumn + 2 == frontColumn) ||
                            (frontColumn == backColumn) && (frontRow + 2 == backRow || backRow + 2 == frontRow))
                        {
                            establishedPositions = GetPlacedShipPositions();
                            proposedPosition     = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn);
                            for (int i = 0; i < 20; i++)
                            {
                                for (int j = 0; j < 20; j++)
                                {
                                    if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null))
                                    {
                                        isOverlapping = true;
                                    }
                                }
                            }
                            if (!isOverlapping)
                            {
                                Ship submarine = new Submarine();
                                submarine.frontRow    = frontRow;
                                submarine.frontColumn = frontColumn;
                                submarine.backRow     = backRow;
                                submarine.backColumn  = backColumn;
                                this.fleet.Add(submarine);
                                submarineIsChosen = true;
                            }
                            else
                            {
                                Console.WriteLine("The submarine is overlapping.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("The submarine is not the correct length.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("The submarine is diagonal.");
                    }
                }
                else
                {
                    Console.WriteLine("The submarine has already been chosen.");
                }
                break;

            case 3:
                if (!battleshipIsChosen)
                {
                    if (frontRow == backRow || frontColumn == backColumn)     //this is a check for correct placement / not diagonal
                    {
                        if ((frontRow == backRow) && (frontColumn + 3 == backColumn || backColumn + 3 == frontColumn) ||
                            (frontColumn == backColumn) && (frontRow + 3 == backRow || backRow + 3 == frontRow))
                        {
                            establishedPositions = GetPlacedShipPositions();
                            proposedPosition     = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn);
                            for (int i = 0; i < 20; i++)
                            {
                                for (int j = 0; j < 20; j++)
                                {
                                    if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null))
                                    {
                                        isOverlapping = true;
                                    }
                                }
                            }
                            if (!isOverlapping)
                            {
                                Ship battleship = new Battleship();
                                battleship.frontRow    = frontRow;
                                battleship.frontColumn = frontColumn;
                                battleship.backRow     = backRow;
                                battleship.backColumn  = backColumn;
                                this.fleet.Add(battleship);
                                battleshipIsChosen = true;
                            }
                            else
                            {
                                Console.WriteLine("The battleship is overlapping.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("The battleship is not the correct length.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("The battleship is diagonal.");
                    }
                }
                else
                {
                    Console.WriteLine("The battleship has already been chosen.");
                }
                break;

            case 4:
                if (!aircraftCarrierIsChosen)
                {
                    if (frontRow == backRow || frontColumn == backColumn)     //this is a check for correct placement / not diagonal
                    {
                        if ((frontRow == backRow) && (frontColumn + 4 == backColumn || backColumn + 4 == frontColumn) ||
                            (frontColumn == backColumn) && (frontRow + 4 == backRow || backRow + 4 == frontRow))
                        {
                            establishedPositions = GetPlacedShipPositions();
                            proposedPosition     = GetShipAboutToBePlacedsPosition(frontRow, frontColumn, backRow, backColumn);
                            for (int i = 0; i < 20; i++)
                            {
                                for (int j = 0; j < 20; j++)
                                {
                                    if ((establishedPositions[i, j] == proposedPosition[i, j]) && (establishedPositions[i, j] != null))
                                    {
                                        isOverlapping = true;
                                    }
                                }
                            }
                            if (!isOverlapping)
                            {
                                Ship aircraftCarrier = new AircraftCarrier();
                                aircraftCarrier.frontRow    = frontRow;
                                aircraftCarrier.frontColumn = frontColumn;
                                aircraftCarrier.backRow     = backRow;
                                aircraftCarrier.backColumn  = backColumn;
                                this.fleet.Add(aircraftCarrier);
                                aircraftCarrierIsChosen = true;
                            }
                            else
                            {
                                Console.WriteLine("The aircraft carrier is overlapping.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("The aircraft carrier is not the correct length.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("The aircraft carrier is diagonal.");
                    }
                }
                else
                {
                    Console.WriteLine("The aircraft carrier has already been chosen.");
                }
                break;

            default:
                Console.WriteLine("Error, please try again.");
                break;
            }
        }