Example #1
0
 public bool PlayerCheckHitOrMiss(int longitude, int latitude)
 {
     NumberOfMoves++;
     for (int i = 0; i < ComputerShipsList.Count; i++)
     {
         if (ComputerShipsList[i].Longitude.Contains(longitude) && ComputerShipsList[i].Latitude.Contains(latitude))
         {
             ComputerShipsList[i].HitsTaken++;
             if (ComputerShipsList[i].HitsTaken == 3 && ComputerShipsList[i].ShipType == "Submarine")
             {
                 ComputerShipsList.RemoveAt(i);
             }
             else if (ComputerShipsList[i].HitsTaken == 2 && ComputerShipsList[i].ShipType == "BattleShip")
             {
                 ComputerShipsList.RemoveAt(i);
             }
             else if (ComputerShipsList[i].HitsTaken == 1 && ComputerShipsList[i].ShipType == "Destroyer")
             {
                 ComputerShipsList.RemoveAt(i);
             }
             return(true);
         }
     }
     return(false);
 }
Example #2
0
        public void FillComputerShips()
        {
            Random random = new Random();
            int    longitude;
            int    latitude;

            longitude = random.Next(0, 7);
            latitude  = random.Next(0, 7);
            ComputerShipsList.Add(new Destroyer(longitude, latitude));
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    longitude = random.Next(0, 6);
                    latitude  = random.Next(0, 7);
                    while (ComputerShipsList[i].Latitude.Contains(latitude) && ComputerShipsList[i].Longitude.Contains(longitude) ||
                           ComputerShipsList[i].Latitude.Contains(latitude) && ComputerShipsList[i].Longitude.Contains(longitude + 1))
                    {
                        longitude = random.Next(0, 6);
                        latitude  = random.Next(0, 7);
                    }
                    ComputerShipsList.Add(new BattleShip(longitude, latitude));
                }
                else
                {
                    longitude = random.Next(1, 6);
                    latitude  = random.Next(0, 7);
                    while (CheckIfShipCanBePlaced(longitude, latitude, ComputerShipsList) == true)
                    {
                        longitude = random.Next(1, 6);
                        latitude  = random.Next(0, 7);
                    }
                    ComputerShipsList.Add(new Submarine(longitude, latitude));
                }
            }
        }