Ejemplo n.º 1
0
        public override Move Execute()
        {
            var values    = _engine.values;
            var possibles = _engine.possibles;

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (values[i, j] > 0)
                    {
                        continue;
                    }

                    var array = possibles[i, j];
                    if (array.Count == 1)
                    {
                        var number = array.Single();

                        var move = new SolveNumberMove {
                            Value = number, Row = i, Column = j, Algorithm = this
                        };
                        return(move);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        public override Move Execute()
        {
            var values = _engine.values;
            var possibles = _engine.possibles;

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (values[i, j] > 0)
                        continue;

                    var array = possibles[i, j];
                    if (array.Count == 1)
                    {
                        var number = array.Single();

                        var move = new SolveNumberMove { Value = number, Row = i, Column = j, Algorithm = this };
                        return move;
                    }
                }
            }

            return null;
        }
        public override Move Execute()
        {
            var possibles = _engine.possibles;
            var values = _engine.values;

            bool contains = false;
            bool containsTwo = false;

            for (int number = 1; number < 10; number++)
            {
                for (int squareIndex = 0; squareIndex < 9; squareIndex++)
                {
                    int minX = (squareIndex / 3) * 3;
                    int maxX = minX + 3;

                    int minY = (squareIndex % 3) * 3;
                    int maxY = minY + 3;

                    contains = false;
                    containsTwo = false;
                    int indexI = 0;
                    int indexJ = 0;

                    for (int i = minX; i < maxX; i++)
                    {
                        for (int j = minY; j < maxY; j++)
                        {
                            if (values[i, j] > 0)
                                continue;

                            if (possibles[i, j].Contains(number))
                            {
                                if (!contains)
                                {
                                    contains = true;
                                    indexI = i;
                                    indexJ = j;
                                }
                                else
                                {
                                    containsTwo = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (contains && !containsTwo)
                    {
                        var move = new SolveNumberMove { Value = number, Row = indexI, Column = indexJ, Algorithm = this };
                        return move;
                    }
                }
            }

            return null;
        }
        public override Move Execute()
        {
            var possibles = _engine.possibles;
            var values    = _engine.values;

            bool contains        = false;
            bool containsTwo     = false;
            int  indexContaining = 0;

            // Check for a single possibility in row
            for (int number = 1; number < 10; number++)
            {
                for (int i = 0; i < 9; i++)
                {
                    contains        = false;
                    containsTwo     = false;
                    indexContaining = 0;

                    for (int j = 0; j < 9; j++)
                    {
                        if (values[i, j] > 0)
                        {
                            continue;
                        }

                        if (possibles[i, j].Contains(number))
                        {
                            if (!contains)
                            {
                                contains        = true;
                                indexContaining = j;
                            }
                            else
                            {
                                containsTwo = true;
                                break;
                            }
                        }
                    }

                    if (contains && !containsTwo)
                    {
                        var move = new SolveNumberMove {
                            Value = number, Row = i, Column = indexContaining, Algorithm = this
                        };
                        return(move);
                    }
                }
            }

            return(null);
        }
        public override Move Execute()
        {
            var possibles = _engine.possibles;
            var values = _engine.values;

            bool contains = false;
            bool containsTwo = false;
            int indexContaining = 0;

            // Check for a single possibility in row
            for (int number = 1; number < 10; number++)
            {
                for (int i = 0; i < 9; i++)
                {
                    contains = false;
                    containsTwo = false;
                    indexContaining = 0;

                    for (int j = 0; j < 9; j++)
                    {
                        if (values[i, j] > 0)
                            continue;

                        if (possibles[i, j].Contains(number))
                        {
                            if (!contains)
                            {
                                contains = true;
                                indexContaining = j;
                            }
                            else
                            {
                                containsTwo = true;
                                break;
                            }
                        }
                    }

                    if (contains && !containsTwo)
                    {
                        var move = new SolveNumberMove { Value = number, Row = i, Column = indexContaining, Algorithm = this };
                        return move;
                    }
                }
            }

            return null;
        }
Ejemplo n.º 6
0
        public Move PerformSolveNumberMove(SolveNumberMove move)
        {
            string message = string.Format("Performing a move on {0} with val {1}", CellFormat(move.Row, move.Column), move.Value);

            if (move.Algorithm != null)
            {
                string algoName = move.Algorithm.GetType().Name;
                algoName = algoName.Replace("Algorithm", "");
                message += " using algo " + algoName;
            }

            WriteLog(message);
            values[move.Row, move.Column] = move.Value;
            move.Possibles = new List <int>(possibles[move.Row, move.Column]);
            moves.Push(move);
            return(move);
        }
        public override Move Execute()
        {
            var possibles = _engine.possibles;
            var values    = _engine.values;

            bool contains    = false;
            bool containsTwo = false;

            for (int number = 1; number < 10; number++)
            {
                for (int squareIndex = 0; squareIndex < 9; squareIndex++)
                {
                    int minX = (squareIndex / 3) * 3;
                    int maxX = minX + 3;

                    int minY = (squareIndex % 3) * 3;
                    int maxY = minY + 3;

                    contains    = false;
                    containsTwo = false;
                    int indexI = 0;
                    int indexJ = 0;

                    for (int i = minX; i < maxX; i++)
                    {
                        for (int j = minY; j < maxY; j++)
                        {
                            if (values[i, j] > 0)
                            {
                                continue;
                            }

                            if (possibles[i, j].Contains(number))
                            {
                                if (!contains)
                                {
                                    contains = true;
                                    indexI   = i;
                                    indexJ   = j;
                                }
                                else
                                {
                                    containsTwo = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (contains && !containsTwo)
                    {
                        var move = new SolveNumberMove {
                            Value = number, Row = indexI, Column = indexJ, Algorithm = this
                        };
                        return(move);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 8
0
        public Move PerformSolveNumberMove(SolveNumberMove move)
        {
            string message = string.Format("Performing a move on {0} with val {1}", CellFormat(move.Row, move.Column), move.Value);
            if (move.Algorithm != null)
            {
                string algoName = move.Algorithm.GetType().Name;
                algoName = algoName.Replace("Algorithm", "");
                message += " using algo " + algoName;
            }

            WriteLog(message);
            values[move.Row, move.Column] = move.Value;
            move.Possibles = new List<int>(possibles[move.Row, move.Column]);
            moves.Push(move);
            return move;
        }
        public override Move Execute()
        {
            var possibles = _engine.possibles;
            var values    = _engine.values;

            int minPossibles = int.MaxValue;
            int numberJ      = -1;
            int numberI      = -1;

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (values[i, j] > 0)
                    {
                        continue;
                    }

                    int possiblesCount = possibles[i, j].Count;
                    if (possiblesCount < minPossibles)
                    {
                        minPossibles = possiblesCount;
                        numberI      = i;
                        numberJ      = j;
                    }
                }
            }

            if (numberI == -1 || numberJ == -1)
            {
                _engine.WriteLog("Bug in the algorithm. Couldn't find a minimum cell to randomize");
                return(null);
            }

            var poss = possibles[numberI, numberJ];

            if (poss.Count == 0)
            {
                _engine.WriteLog(string.Format("Cannot continue further. Found a spot with no possibles: {0}", _engine.CellFormat(numberI, numberJ)));
                return(null);
            }
            else if (poss.Count == 1)
            {
                int val = poss.Single();
                return(new SolveNumberMove {
                    Value = val, Row = numberI, Column = numberJ, Algorithm = this
                });
            }
            else
            {
                var guess = _engine.guesses[numberI, numberJ];
                if (guess == null)
                {
                    int val = GetRandom(poss);
                    _engine.guesses[numberI, numberJ] = new List <int> {
                        val
                    };
                    var move = new SolveNumberMove {
                        Value = val, Row = numberI, Column = numberJ, Algorithm = this
                    };
                    _engine.lastRandomMove = move;
                    return(move);
                }
                else
                {
                    while (poss.Any())
                    {
                        int val = GetRandom(poss);

                        if (guess.Contains(val))
                        {
                            poss.Remove(val);
                            continue;
                        }
                        else
                        {
                            guess.Add(val);
                            var move = new SolveNumberMove {
                                Value = val, Row = numberI, Column = numberJ, Algorithm = this
                            };
                            _engine.lastRandomMove = move;
                            return(move);
                        }
                    }

                    _engine.WriteLog("All guesses were exhausted on " + _engine.CellFormat(numberI, numberJ));
                    return(null);
                }
            }
        }