Esempio n. 1
0
        private void InitializeGrid()
        {
            //TODO:
            Random rand = new Random(DateTime.Now.Millisecond + DateTime.Now.Second * 1337);

            //Determine our needle cordinates before hand.
            List <Coordinate> needleCordinates = new List <Coordinate>();
            List <int>        randomValues     = new List <int>();

            for (int i = 0; i < settings.numOfNeedles; i++)
            {
                Coordinate buffer = new Coordinate(rand.Next(1, 6), rand.Next(1, 9));
                while (needleCordinates.Find(x => x.CompareTo(buffer) == 1) != null)
                {
                    buffer = new Coordinate(rand.Next(1, 6), rand.Next(1, 9));
                }

                needleCordinates.Add(buffer);

                if (settings.allowRepeats)
                {
                    randomValues.Add(rand.Next(1, 10));
                }
            }

            if (!settings.allowRepeats)
            {
                for (int i = 1; i < 10; i++)
                {
                    randomValues.Add(i);
                }

                randomValues.Shuffle();
                randomValues = randomValues.GetRange(0, settings.numOfNeedles);
            }


            for (int row = 1; row < 6; row++)
            {
                for (int col = 1; col < 9; col++)
                {
                    //Initialize a new Cell object with the random value and the current row,col
                    Cell cell = new Cell(randomValues.Count > 0 ? randomValues.Last() : 0);
                    cell.coord = new Coordinate(row, col);

                    if (needleCordinates.Find(x => x.CompareTo(cell.coord) == 1) != null)
                    {
                        cell.cellType = Cell.CellType.NEEDLE;

                        if (randomValues.Count > 0)
                        {
                            randomValues.RemoveAt(randomValues.Count - 1);
                        }
                    }
                    else
                    {
                        cell.cellType = Cell.CellType.HAYSTACK;
                    }

                    //Add the cell to its respected list.
                    if (cell.cellType == Cell.CellType.HAYSTACK)
                    {
                        if (settings.difficulty == GameSettings.Difficulty.EASY || settings.difficulty == GameSettings.Difficulty.NORMAL)
                        {
                            continue;
                        }

                        Haystack.Add(cell);
                    }
                    else
                    {
                        Needles.Add(cell);
                    }

                    cell.num.Click += CellBtn_Click;
                    cell.num.Tag    = cell;

                    //Add the Cell to the GameGrid UI.
                    Grid.SetRow(cell, row - 1);
                    Grid.SetColumn(cell, col - 1);

                    this.grid.Children.Add(cell);
                }
            }

            //Sort the Needles
            Needles.Sort(delegate(Cell x, Cell y)
            {
                if (x == null && y == null)
                {
                    return(0);
                }
                else if (x.value == y.value)
                {
                    return(0);
                }
                else if (x == null)
                {
                    return(-1);
                }
                else if (y == null)
                {
                    return(1);
                }
                else
                {
                    int retVal = x.value.CompareTo(y.value);

                    if (retVal == 0)
                    {
                        return(x.coord.CompareTo(y.coord));
                    }
                    else
                    {
                        return(retVal);
                    }
                }
            });
        }