示例#1
0
        public int AddValue(int x, int y, bool showMessage = true)
        {
            if (x > BoardValues.GetLength(0) || y > BoardValues.GetLength(1))
            {
                return(0);
            }
            if (BoardValues[x, y].IsGrain())
            {
                if (showMessage)
                {
                    MessageBox.Show("This pixel is already grained", "Error", MessageBoxButtons.OK);
                }
                return(0);
            }

            var randomColor = InitialValuesGenerator.RandomColor();

            BoardValues[x, y].GrainValue = true;
            BoardValues[x, y].Color      = randomColor;
            Grained.Add(new GamePixelWithCoordinates
            {
                Color      = randomColor,
                GrainValue = true,
                Id         = BoardValues[x, y].Id,
                X          = x,
                Y          = y
            });

            return(1);
        }
示例#2
0
        private void NeighborhoodsAction(GamePixel[,] temporaryBoard, GamePixelWithCoordinates actualPixel)
        {
            var x = actualPixel.X;
            var y = actualPixel.Y;

            var neighborhoods = new GamePixel[8];

            if (_neighborhoodsPositions[0])
            {
                if (x - 1 >= 0 && y - 1 >= 0)
                {
                    neighborhoods[0] = temporaryBoard[x - 1, y - 1];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        if (x == 0 && y == 0)
                        {
                            neighborhoods[0] = temporaryBoard[WidthElementsNumber - 1, HeightElementsNumber - 1];
                        }
                        else
                        {
                            if (x == 0 && y != 0)
                            {
                                neighborhoods[0] = temporaryBoard[x - 1 + WidthElementsNumber, y - 1];
                            }

                            if (y == 0 && x != 0)
                            {
                                neighborhoods[0] = temporaryBoard[x - 1, y - 1 + HeightElementsNumber];
                            }
                        }
                    }
                    else
                    {
                        neighborhoods[0] = null;
                    }
                }
            }

            if (_neighborhoodsPositions[1])
            {
                if (y - 1 >= 0)
                {
                    neighborhoods[1] = temporaryBoard[x, y - 1];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        neighborhoods[1] = temporaryBoard[x, y + HeightElementsNumber - 1];
                    }
                    else
                    {
                        neighborhoods[1] = null;
                    }
                }
            }

            if (_neighborhoodsPositions[2])
            {
                if (x + 1 < WidthElementsNumber && y - 1 >= 0)
                {
                    neighborhoods[2] = temporaryBoard[x + 1, y - 1];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        if (x == WidthElementsNumber - 1 && y == 0)
                        {
                            neighborhoods[2] = temporaryBoard[0, HeightElementsNumber - 1];
                        }
                        else
                        {
                            if (x == WidthElementsNumber - 1 && y != 0)
                            {
                                neighborhoods[2] = temporaryBoard[x + 1 - WidthElementsNumber, y - 1];
                            }

                            if (y == 0 && x != 0)
                            {
                                neighborhoods[2] = temporaryBoard[x + 1, y - 1 + HeightElementsNumber];
                            }
                        }
                    }
                    else
                    {
                        neighborhoods[2] = null;
                    }
                }
            }

            if (_neighborhoodsPositions[3])
            {
                if (x - 1 >= 0)
                {
                    neighborhoods[3] = temporaryBoard[x - 1, y];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        neighborhoods[3] = temporaryBoard[x + WidthElementsNumber - 1, y];
                    }
                    else
                    {
                        neighborhoods[3] = null;
                    }
                }
            }

            if (_neighborhoodsPositions[4])
            {
                if (x + 1 < WidthElementsNumber)
                {
                    neighborhoods[4] = temporaryBoard[x + 1, y];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        neighborhoods[4] = temporaryBoard[x - WidthElementsNumber + 1, y];
                    }
                    else
                    {
                        neighborhoods[4] = null;
                    }
                }
            }

            if (_neighborhoodsPositions[5])
            {
                if (x - 1 >= 0 && y + 1 < HeightElementsNumber)
                {
                    neighborhoods[5] = temporaryBoard[x - 1, y + 1];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        if (x == 0 && y == HeightElementsNumber - 1)
                        {
                            neighborhoods[5] = temporaryBoard[WidthElementsNumber - 1, 0];
                        }
                        else
                        {
                            if (x == 0 && y != 0)
                            {
                                neighborhoods[5] = temporaryBoard[x - 1 + WidthElementsNumber, y - 1];
                            }

                            if (y == 0 && x != 0)
                            {
                                neighborhoods[5] = temporaryBoard[x - 1, y + 1 + HeightElementsNumber];
                            }
                        }
                    }
                    else
                    {
                        neighborhoods[5] = null;
                    }
                }
            }

            if (_neighborhoodsPositions[6])
            {
                if (y + 1 < HeightElementsNumber)
                {
                    neighborhoods[6] = temporaryBoard[x, y + 1];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        neighborhoods[6] = temporaryBoard[x, y - HeightElementsNumber + 1];
                    }
                    else
                    {
                        neighborhoods[6] = null;
                    }
                }
            }

            if (_neighborhoodsPositions[7])
            {
                if (x + 1 < WidthElementsNumber && y + 1 < HeightElementsNumber)
                {
                    neighborhoods[7] = temporaryBoard[x + 1, y + 1];
                }
                else
                {
                    if (PeriodicValues)
                    {
                        if (x == WidthElementsNumber - 1 && y == HeightElementsNumber - 1)
                        {
                            neighborhoods[7] = temporaryBoard[0, 0];
                        }
                        else
                        {
                            if (x == WidthElementsNumber - 1 && y != 0)
                            {
                                neighborhoods[7] = temporaryBoard[x + 1 - WidthElementsNumber, y + 1];
                            }

                            if (y == HeightElementsNumber - 1 && x != WidthElementsNumber - 1)
                            {
                                neighborhoods[5] = temporaryBoard[x + 1, y + 1 - HeightElementsNumber];
                            }
                        }
                    }
                    else
                    {
                        neighborhoods[7] = null;
                    }
                }
            }

            var groupedByColors = neighborhoods
                                  .Where(e => e != null)
                                  .Where(e => e.GrainValue)
                                  .GroupBy(e => e.Color)
                                  .Select(e => new ColorCounter {
                Color = e.Key, Count = e.Count()
            })
                                  .OrderByDescending(e => e.Count)
                                  .ToList();

            if (groupedByColors.Count == 0)
            {
                return;
            }
            var max      = groupedByColors.First();
            var toRandom = groupedByColors.Where(e => e.Count == max.Count).ToList();

            if (toRandom.Count == 1)
            {
                BoardValues[x, y].Color = toRandom.First().Color;
                BoardValues[x, y].MakeGrain();
                actualPixel.GrainValue = true;
                actualPixel.Color      = toRandom.First().Color;
                Grained.Add(actualPixel);
                return;
            }

            var random = new Random();

            BoardValues[x, y].Color = toRandom[random.Next(0, toRandom.Count - 1)].Color;
            BoardValues[x, y].MakeGrain();
            actualPixel.GrainValue = true;
            actualPixel.Color      = toRandom.First().Color;
            Grained.Add(actualPixel);
        }