예제 #1
0
        private void bttnPlace_Click(object sender, EventArgs e)
        {
            try
            {
                int    controlRow;    // gets starting row for ship placement
                int    controlColumn; // gets starting column for ship placement
                string direction;     //gets direction for ship placement
                string shipType;      //gets shiptype for ship placement
                bool   shipValid;     //keeps track of whether ship placement is valid

                //LINQ to find which radio is checked
                var checkedRadio = tableLayoutPanel2.Controls.OfType <RadioButton>()
                                   .FirstOrDefault(r => r.Checked);

                controlRow           = tableLayoutPanel2.GetRow(checkedRadio);    //gets radiobutton row
                controlColumn        = tableLayoutPanel2.GetColumn(checkedRadio); //gets radiobutton column
                checkedRadio.Checked = false;

                //Linq to find which direction is selected
                checkedRadio = gbDirection.Controls.OfType <RadioButton>()
                               .FirstOrDefault(r => r.Checked);
                direction            = checkedRadio.Text;
                checkedRadio.Checked = false;

                //Linq to find out type of ship selected
                checkedRadio = gbShip.Controls.OfType <RadioButton>()
                               .FirstOrDefault(r => r.Checked);
                shipType = checkedRadio.Tag.ToString();

                //sends user data to isShipPlaceValid() to see if ship placement is valid
                shipValid = GameMechanics.PlacingShips(ref playerShips, controlColumn, controlRow, direction, shipType);

                //if ship placement is valid sets ship placement in object list
                if (shipValid == true)
                {
                    foreach (Ship s in playerShips.Where(s => s.ShipType == shipType)) //LINQ to get right ship to set
                    {
                        s.IsPlaced = true;
                    }

                    checkedRadio.Enabled = false; //disables set ship radio button
                    checkedRadio.Checked = false; // unselects ship
                }
                else //if ships placement is not valid sends message to user
                {
                    MessageBox.Show("Ship placement invalid.");
                }
                tableLayoutPanel2.Refresh();
            }
            catch (ArgumentNullException)//catch statement to catch unselected radio buttons
            {
                MessageBox.Show("Please select a ship location on the grid.");
            }
            catch (NullReferenceException)//catch statement to catch unselected radio buttons
            {
                MessageBox.Show("Please choose a ship or a direction for the ship.");
            }
        }
예제 #2
0
        public static void SettingAIShips(ref List <Ship> computerShips)
        {
            //sets the ships for the computer in random places

            //Direction Constants
            const string UP    = "Up";
            const string DOWN  = "Down";
            const string LEFT  = "Left";
            const string RIGHT = "Right";

            Random r         = new Random();
            bool   shipValid = false;
            int    startingColumn;
            int    startingRow;
            int    directionNum;
            string direction = UP;


            foreach (Ship s in computerShips) //setting location for each ship
            {
                while (shipValid == false)    // repeat until ship placement is valid
                {
                    // getting new random numbers
                    startingColumn = r.Next(12);
                    startingRow    = r.Next(12);
                    directionNum   = r.Next(4);
                    //switch case to convert number to direction
                    switch (directionNum)
                    {
                    case 0:
                        direction = UP;
                        break;

                    case 1:
                        direction = LEFT;
                        break;

                    case 2:
                        direction = RIGHT;
                        break;

                    case 3:
                        direction = DOWN;
                        break;
                    }
                    //send to check if placement is valid
                    shipValid = GameMechanics.PlacingShips(ref computerShips, startingColumn, startingRow, direction, s.ShipType);
                }
                s.IsPlaced = true;  // sets ship property to ship is placed
                shipValid  = false; // reseting shipValid
            }
        }