Ejemplo n.º 1
0
        private bool RandomShot()
        {
            if (Elems.Count() == 0)
            {
                return(false);//this = 0 when all cells are used
            }
            Random        rnd  = new Random();
            int           n    = rnd.Next(Elems.Count());
            FieldsElement elem = Elems[n];

            Elems.RemoveAt(n);
            bool ret = UserField.Move(elem.GetY(), elem.GetX());

            if (ret)
            {
                Goal = new Goal(elem, UserField);
            }

            List <FieldsElement> state = UserField.NewState;

            foreach (FieldsElement item in state)
            {
                Elems.Remove(item);
            }

            return(ret);
        }
Ejemplo n.º 2
0
        private bool CheckEdge(FieldsElement elem1, FieldsElement elem2)
        {//вызывает CheckEdge(y,x) исходя от направления цели
            switch (GoalDirection)
            {
            case 1:
                return(CheckEdge(elem2.GetY(), elem2.GetX()));

            case 2:
                return(CheckEdge(elem2.GetY(), elem2.GetX()));

            case 3:
                return(CheckEdge(elem1.GetY(), elem1.GetX()));

            case 4:
                return(CheckEdge(elem1.GetY(), elem1.GetX()));

            default:
                return(false);
            }
        }
Ejemplo n.º 3
0
        public int[] RandomShot()
        {
            List <FieldsElement> elems = new List <FieldsElement>();

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (!Field[i, j].IsFired)
                    {
                        elems.Add(Field[i, j]);
                    }
                }
            }

            int           x   = rnd.Next(elems.Count - 1);
            FieldsElement aim = elems[x];

            return(new int[] { aim.GetY(), aim.GetX() });
        }
Ejemplo n.º 4
0
        private void FullDisplayField(GameLogic Field, object Side, bool IsUser)
        {
            StackPanel panel = (StackPanel)Side;

            for (int i = 0; i < 10; i++)
            {
                StackPanel row = new StackPanel()
                {
                    Orientation = Orientation.Horizontal
                };
                panel.Children.Add(row);
                for (int j = 0; j < 10; j++)
                {
                    FieldsElement elem = Field.Field[i, j];
                    string        tag  = $"{elem.GetY()},{elem.GetX()},{elem.Ship}";

                    Image img = null;
                    if (elem.IsFired)
                    {
                        if (elem.IsShip())
                        {
                            img = new Image {
                                Source = new BitmapImage(new Uri("RedCross.png", UriKind.Relative))
                            }
                        }
                        ;
                        else
                        {
                            img = new Image {
                                Source = new BitmapImage(new Uri("BlueCircle.png", UriKind.Relative))
                            }
                        };
                    }

                    Button btn = new Button()
                    {
                        Width      = BtnSize,
                        Height     = BtnSize,
                        FontWeight = FontWeights.UltraBold,
                        Background = new SolidColorBrush(Colors.White),
                        Tag        = tag,
                        Content    = img
                    };
                    if (!IsUser)
                    {
                        if (elem.IsShip())
                        {
                            btn.BorderBrush     = new SolidColorBrush(Colors.Blue);
                            btn.BorderThickness = new Thickness(2);
                            btn.Background      = (SolidColorBrush)(new BrushConverter().ConvertFrom("#f0f8ff"));
                        }
                    }
                    else
                    {
                        btn.Click += Button_Click;
                    }

                    row.Children.Add(btn);
                }
            }
        }
Ejemplo n.º 5
0
        public int[] GetNextShotPosition()
        {
            if (Elems.Count() == 1)
            {
                FieldsElement elem = Elems[0];
                int           y    = elem.GetY();
                int           x    = elem.GetX();
                for (int i = 0; i < 4; i++)
                {
                    if (CheckEdge(y, x))
                    {
                        return(CFD(y, x));
                    }
                    else
                    {
                        ChangeGoalDirection();
                    }
                }

                //ship destoyed
                return(new int[] { -1, -1 });
            }
            else
            {
                int minY = 9, minX = 9;
                int maxY = 0, maxX = 0;
                foreach (FieldsElement item in Elems)
                {
                    int y = item.GetY();
                    int x = item.GetX();
                    if (y < minY)
                    {
                        minY = y;
                    }
                    if (y > maxY)
                    {
                        maxY = y;
                    }
                    if (x < minX)
                    {
                        minX = x;
                    }
                    if (x > maxX)
                    {
                        maxX = x;
                    }
                }

                FieldsElement elem1 = UserField.Field[minY, minX];                 //начало открытого коробля
                FieldsElement elem2 = UserField.Field[maxY, maxX];                 //конец открытого коробля
                ShipDirection = Elems[0].GetY() == Elems[1].GetY() ? true : false; //определение направления коробля //true is horizontal

                for (int i = 0; i < 2; i++)
                {
                    if (CheckEdge(elem1, elem2))
                    {
                        if (GoalDirection == 1 || GoalDirection == 2)
                        {
                            return(CFD(elem2.GetY(), elem2.GetX()));
                        }
                        else
                        {
                            return(CFD(elem1.GetY(), elem1.GetX()));
                        }
                    }
                    else
                    {
                        ChangeGoalDirection(true);
                    }
                }
                //ship destoyed
                return(new int[] { -1, -1 });
            }
        }