Exemple #1
0
        public static void Randomize(MyGrid grid, List <Ship> list)
        {
            Random        rnd          = new Random();
            List <string> listOfFields = new List <string> {
            };

            int shipCounter = 0;

            while (shipCounter < 5)
            {
                string value          = $"{grid.fields[rnd.Next(0, 8)][rnd.Next(0, 8)]}";
                char[] seperateValues = value.ToCharArray();

                if (rnd.Next(100) < 50 ? true : false)
                {
                    list[shipCounter].HorizontalOrientation = true;
                }
                else
                {
                    list[shipCounter].HorizontalOrientation = false;
                }

                listOfFields.Add(value);

                if (list[shipCounter].HorizontalOrientation == true)
                {
                    for (int i = 1, k = 1; i < list[shipCounter].Length; ++i)
                    {
                        if ((seperateValues[0] + i) >= (int)'A' && (seperateValues[0] + i) <= (int)'H')
                        {
                            listOfFields.Add($"{(char)(seperateValues[0] + i)}{seperateValues[1]}");
                        }
                        else
                        {
                            listOfFields.Add($"{(char)(seperateValues[0] - k)}{seperateValues[1]}");
                            k++;
                        }
                    }
                }
                else
                {
                    for (int i = 1, k = 1; i < list[shipCounter].Length; ++i)
                    {
                        if ((seperateValues[1] + i) >= (int)'1' && (seperateValues[1] + i) <= (int)'8')
                        {
                            listOfFields.Add($"{seperateValues[0]}{(char)(seperateValues[1] + i)}");
                        }
                        else
                        {
                            listOfFields.Add($"{seperateValues[0]}{(char)(seperateValues[1] - k)}");
                            k++;
                        }
                    }
                }

                if (grid.availableLocation(listOfFields))
                {
                    list[shipCounter].OcupiedFields = new List <string>(listOfFields);
                    grid.addShip(list[shipCounter++]);
                }
                else
                {
                    listOfFields.Clear();
                }
            }
        }
Exemple #2
0
        public string ComputerAttack(MyGrid g)
        {
            string value = computer.strategy.Execute(g);

            return(value);
        }