Example #1
0
        private Space[] findValidField(CancellationToken?token, Point?solverOrigin = null)
        {
            Random          rnd    = new Random(new Random().Next(999999999));
            MinefieldSolver solver = new MinefieldSolver();

            Space[] fieldToCalculate = new Space[cWidth * cHeight];
            do
            {
                fieldToCalculate = new Space[cWidth * cHeight];

                for (int i = 0; i < Bombcount; i++)
                {
                    int x = rnd.Next(0, cWidth);
                    int y = rnd.Next(0, cHeight);
                    if (fieldToCalculate[x + y * cWidth] == null)
                    {
                        fieldToCalculate[x + y * cWidth] = new Space(true, 0);
                    }
                    else
                    {
                        i--;
                    }
                }

                for (int x = 0; x < cWidth; x++)
                {
                    for (int y = 0; y < cHeight; y++)
                    {
                        if (fieldToCalculate[y * cWidth + x] == null)
                        {
                            fieldToCalculate[y * cWidth + x] = new Space(false, GenerateNumber(x, y, fieldToCalculate));
                        }
                    }
                }
            } while (
                solverOrigin.HasValue && !(token.HasValue && token.Value.IsCancellationRequested) && !(
                    fieldToCalculate[solverOrigin.Value.X + solverOrigin.Value.Y * cWidth].number == 0 &&
                    !fieldToCalculate[solverOrigin.Value.X + solverOrigin.Value.Y * cWidth].isMine &&
                    solver.tryToSolve(ref fieldToCalculate, FieldSize, solverOrigin.Value)
                    ));
            return(fieldToCalculate);
        }
Example #2
0
        public void runVisualSolver()
        {
            OnStartGenerating(new EventArgs());
            MinefieldSolver mfs = new MinefieldSolver();

            this.disabled = true;
            Task.Factory.StartNew(() =>
            {
                mfs.tryToSolve(ref FieldSpaces, FieldSize, Point.Empty, this);
                this.MainThreadInvoke(() =>
                {
                    highlightedField = -1;
                    disabled         = false;
                    OnEndGenerating(new EventArgs());
                    if (CheckWin())
                    {
                        RevealBoard(true);
                    }
                });
            });
        }