Ejemplo n.º 1
0
        public Player()
        {
            gameboard = new Gameboard();
            name      = Console.ReadLine();
            Battleship battleship = new Battleship();
            Carrier    carrier    = new Carrier();
            Submarine  submarine  = new Submarine();
            Destroyer  destroyer  = new Destroyer();

            allShips.Add(battleship);
            allShips.Add(carrier);
            allShips.Add(submarine);
            allShips.Add(destroyer);
        }
Ejemplo n.º 2
0
 public bool NoOtherShipConflict(Destroyer destroyer, Submarine submarine, Battleship battleship)
 {
     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]) ||
                 (destroyer.location[i, 0] == location[4, 0] && destroyer.location[i, 1] == location[4, 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]) ||
                 (submarine.location[i, 0] == location[4, 0] && submarine.location[i, 1] == location[4, 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]) ||
                 (battleship.location[i, 0] == location[3, 0] && battleship.location[i, 1] == location[3, 1]) ||
                 (battleship.location[i, 0] == location[4, 0] && battleship.location[i, 1] == location[4, 1]))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Ejemplo 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);
 }
Ejemplo n.º 4
0
        //MembVars

        //Constr
        public HumanPlayer(int playerNumber, int size)
        {
            PlayerNumber = playerNumber;
            BoardSize    = size;
            MyBoard      = new Board(true, size);
            MyEnemyBoard = new Board(false, size);
            MyShips      = new List <Ship>();
            ShipsIveSunk = new List <Ship>();
            MyDestroyer  = new Destroyer(PlayerNumber);
            MyShips.Add(MyDestroyer);
            MySub = new Submarine(PlayerNumber);
            MyShips.Add(MySub);
            MyBattleship = new Battleship(PlayerNumber);
            MyShips.Add(MyBattleship);
            MyCarrier = new Carrier(PlayerNumber);
            MyShips.Add(MyCarrier);
            MyBoard.FillBoard();
            MyEnemyBoard.FillBoard();
            HasUnplacedShips = true;
            HasShipsAfloat   = true;
        }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
0
        // randomly generates initial ship positions for the AI
        private void SetupShips()
        {
            ShipPlacement randomPosition;

            // place the carrier
            randomPosition = PlaceShip(5);
            Carrier carrier = new Carrier(randomPosition.startPosition, randomPosition.horizontal);

            ships.Add(carrier);

            // place the battleship
            randomPosition = PlaceShip(4);
            Battleship battleship = new Battleship(randomPosition.startPosition, randomPosition.horizontal);

            ships.Add(battleship);

            // place the destroyer
            randomPosition = PlaceShip(3);
            Destroyer destroyer = new Destroyer(randomPosition.startPosition, randomPosition.horizontal);

            ships.Add(destroyer);

            // place the submarine
            randomPosition = PlaceShip(3);
            Submarine submarine = new Submarine(randomPosition.startPosition, randomPosition.horizontal);

            ships.Add(submarine);

            // place the PT boat
            randomPosition = PlaceShip(2);
            PTBoat ptboat = new PTBoat(randomPosition.startPosition, randomPosition.horizontal);

            ships.Add(ptboat);

            shipsSunk = 0;
        }
Ejemplo n.º 7
0
        public bool GoodCoordinates(string stringx1, string stringy1, string stringx2, string stringy2, Destroyer destroyer, 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))
                    {
                        FillShipLocation(x1, y1, x2, y2);
                        if (NoOtherShipConflict(destroyer, battleship, aircraftCarrier))
                        {
                            verifyCoordinates = true;
                        }
                    }
                }
            }
            return(verifyCoordinates);
        }
Ejemplo 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;
            }
        }