예제 #1
0
 private void AddShipCategory(int lengthOfShip, int quantity)
 {
     for (int i = 0; i < quantity; i++)
     {
         Ships.Add(new Ship(Field, FieldLength, lengthOfShip, Random));
     }
 }
예제 #2
0
        public EnumShotResult ProcessShot(Coords fireLocation)
        {
            try
            {
                //Call the block the player specificed via coordinates
                var block = PlayBoard.Blocks.Where(x => x.Coordinates.Row == fireLocation.Row && x.Coordinates.Column == fireLocation.Column).First();

                if (!block.IsOccupied)
                {
                    //if the block had no ship in it, return a miss message, and change enum to miss
                    Console.WriteLine("Your shot did not hit any battleships.");
                    block.ShipType = EnumShipType.Miss;
                    return(EnumShotResult.Miss);
                }
                //if the block had a ship in it, return a hit message, and change enum to hit
                //increment the hit value to track how many times a ship has been hit
                var ship = Ships.First(Z => Z.ShipType == block.ShipType);
                ship.Hits++;
                Console.WriteLine("Hit: " + ship.ShipType.ToString() + "!");
                if (ship.IsSunk)
                {
                    Console.WriteLine("Your shot sunk a " + ship.ShipType.ToString());
                }
                block.ShipType = EnumShipType.Hit;
                return(EnumShotResult.Hit);
            }
            catch (Exception ex)
            {
                Console.WriteLine("The selected coordinate was not found in the array");
                return(EnumShotResult.Miss);
            }
        }
예제 #3
0
        private void PlaceShip(int numOfShips, Ships shipType)
        {
            for (int i = 0; i < numOfShips; i++)
            {
                IShip ship = null;

                switch (shipType)
                {
                case Ships.Battleship:
                    ship = new Battleship();
                    break;

                case Ships.Destroyer:
                    ship = new Destroyer();
                    break;

                default:
                    break;
                }

                while (true)
                {
                    if (ship != null)
                    {
                        if (PlaceShipOnBoard(ship))
                        {
                            WinAmount += ship.Health;
                            break;
                        }
                    }
                }
            }
        }
예제 #4
0
        public override void ShipSelect()
        {
            if (RandomSelection == true)
            {
                string   chosen = "";
                string   ab, bc, cd, de, ef = "";
                int      size = 0;
                string[] shipArray;
                int      i    = 1;
                Random   rGen = new Random();
                int      rNum = rGen.Next(7);

                shipArray = new string[7];
                do
                {
                    switch (rNum)
                    {
                    case 1:
                    {
                        Ships sub = new Ships();
                        shipArray[i] = sub.ShipName;
                        break;
                    }

                    case 2:
                    {
                        Ships Frigate = new Ships(ab);
                        shipArray[i] = Frigate.ShipName;
                        break;
                    }

                    case 3:
                    {
                        Ships Dest = new Ships(ab, bc);
                        shipArray[i] = Dest.ShipName;
                        break;
                    }

                    case 4:
                    {
                        break;
                    }

                    case 5:
                    {
                        break;
                    }

                    case 6:
                    {
                        break;
                    }
                    }


                    i++;
                }while(i == size);
            }
        }
예제 #5
0
 public bool IsDefeated()
 {
     if (Ships.Exists(ship => ship.Destroyed == false))
     {
         return(false);
     }
     return(true);
 }
예제 #6
0
 private void BoatChoice(Ships boat)
 {
     if (numSelected <= numAllowed)
     {
         shipChoices[numSelected] = boat;
         statlable.Text          += boat.ShipName + "\n";
         numSelected++;
     }
     if (numSelected == numAllowed)
     {
         MessageBox.Show("Max Ships Chosen");
         btnDestroyer.Enabled = false; btnSubmarine.Enabled = false; btnMedical.Enabled = false; btnFrigate.Enabled = false; btnBattleship.Enabled = false; btnAircraft.Enabled = false;
     }
 }
예제 #7
0
        public Boolean PlaceShip(int x1, int y1, int x2, int y2, int size)
        {
            if (x1 > 10 || y1 > 10 || x2 > 10 || y2 > 10 || x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0 || Abs(x1 - x2) + Abs(y1 - y2) + 1 != size || !IsFreeSpace(x1, y1, x2, y2))
            {
                return(false);
            }
            List <ShipPart> parts = new List <ShipPart>();

            for (int i = 0; i <= Abs(x1 - x2); i++)
            {
                for (int j = 0; j <= Abs(y1 - y2); j++)
                {
                    int x = Min(x1, x2) + i;
                    int y = Min(y1, y2) + j;
                    parts.Add(new ShipPart(x, y));
                    Board[x, y] = TileStatus.Ship;
                }
            }
            Ships.Add(new Ship(parts, this));
            return(true);
        }