コード例 #1
0
        public CardPositions[] GetButtonsLocations()
        {
            uint buttons = CalculateButtons();

            CardPositions[] cardsPositions = new CardPositions[buttons];

            ushort i = 0;

            foreach (Button button in checkedForm.Controls.OfType <Button>())
            {
                cardsPositions[i] = new CardPositions(button.Location.X, button.Location.Y);
                ++i;
            }

            return(cardsPositions);
        }
コード例 #2
0
        private async Task GetNewPositions()
        {
            await Task.Run(() =>
            {
                Random r = new Random();

                int[] indexes = new int[buttonsNumber];

                for (uint i = 0; i < buttonsNumber; ++i)
                {
                    indexes[i] = cardPositions[i].GetIndex();                           //getting array of card positions indexes
                }


                int[] newIndexes = new int[buttonsNumber];
                List <int> list  = indexes.ToList();
                int iter         = 0;

                foreach (int i in UniqueRandom(list.Min(), list.Max(), list))
                {
                    newIndexes[iter] = i;
                    ++iter;
                }

                CardPositions[] shuffled = new CardPositions[buttonsNumber];
                for (uint i = 0; i < buttonsNumber; ++i)
                {
                    shuffled[i] = new CardPositions(cardPositions[i].X, cardPositions[i].Y, newIndexes[i]);
                }

                int succesIteration = 0;
                foreach (var position in cardPositions)
                {
                    foreach (var pos in shuffled)
                    {
                        if (position.GetIndex() == pos.GetIndex())
                        {
                            finalPosition[succesIteration] = pos;
                            ++succesIteration;
                        }
                    }
                }
            });
        }
コード例 #3
0
        public async void Shuffle()
        {
            uint buttonNo = CalculateButtons();

            RandomizePositions randomize = new RandomizePositions(buttonNo, ref positions);

            CardPositions[] cardPositions = new CardPositions[buttonNo];
            cardPositions = await randomize.ReturnNewPositions();

            uint i = 0;

            foreach (Button button in checkedForm.Controls.OfType <Button>())
            {
                int posX = cardPositions[i].X;
                int posY = cardPositions[i].Y;

                button.Location = new Point(posX, posY);
                button.Refresh();

                ++i;
            }
        }