Пример #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);
        }
Пример #2
0
        public GameLogic(string propName)
        {
            this.propName = propName;
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    Field[i, j] = new FieldsElement(i, j);
                }
            }

            AddShipInRandomPosition(4);

            AddShipInRandomPosition(3);
            AddShipInRandomPosition(3);

            AddShipInRandomPosition(2);
            AddShipInRandomPosition(2);
            AddShipInRandomPosition(2);

            AddShipInRandomPosition(1);
            AddShipInRandomPosition(1);
            AddShipInRandomPosition(1);
            AddShipInRandomPosition(1);
        }
Пример #3
0
        public bool Move(int y, int x)
        {
            FieldsElement elem = this.Field[y, x];

            elem.IsFired = true;
            NewState.Add(elem);
            if (elem.IsShip())
            {
                Score       += 5;
                Enemy.Score -= 1;

                elem.ShipRef.Decks -= 1;//hit ship
                if (elem.ShipRef.IsDead())
                {
                    Explosion(elem.ShipRef);
                }

                return(true);
            }
            else
            {
                Score--;
                return(false);
            }
        }
Пример #4
0
        private bool CheckEdge(int y, int x)
        {//возвращает true, если следующий элемент по направлению доступен для выстрела
            int[] pos = CFD(y, x);
            if (pos == null)
            {
                return(false);
            }
            FieldsElement elem = UserField.Field[pos[0], pos[1]];

            return(!elem.IsFired);
        }
Пример #5
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() });
        }
Пример #6
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);
            }
        }
Пример #7
0
        public bool Move()
        {
            if (Goal == null)
            {
                return(RandomShot());
            }

            int[] pos = Goal.GetNextShotPosition();
            if (pos[0] == -1)
            {
                Goal = null;
                return(RandomShot());
            }

            FieldsElement elem = UserField.Field[pos[0], pos[1]];

            if (elem.IsShip())
            {
                Goal.Elems.Add(elem);
            }

            return(UserField.Move(pos[0], pos[1]));
        }
Пример #8
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);
                }
            }
        }
Пример #9
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 });
            }
        }
Пример #10
0
 public Goal(FieldsElement elem, GameLogic userField)
 {
     this.Elems.Add(elem);
     this.UserField = userField;
 }