Exemplo n.º 1
0
        private bool CheckForLastMissingPart(SumCell sumCell, int sum, GameCell[] parts, Direction direction)
        {
            var unSolvedCells = new List <GameCell>();

            foreach (var gameCell in parts)
            {
                if (gameCell.IsSolved)
                {
                    var gameCellValue = gameCell.Value;
                    sumCell.AddAlreadySolved(gameCellValue, direction);
                    sum -= gameCellValue;
                }
                else
                {
                    unSolvedCells.Add(gameCell);
                }
            }

            var unSolvedCount = unSolvedCells.Count;

            if (unSolvedCount == 1)
            {
                unSolvedCells[0].MarkAsSolved(sum);
                return(true);
            }

            if (unSolvedCount > 1)
            {
                if (EliminatePossibilitiesBasedOnRemainingUnSolvedSubset(sum, unSolvedCount, unSolvedCells))
                {
                    return(true);
                }

                var possibleSets  = NumberSets[NumberSet.KeyFrom(unSolvedCount, sum)].Sets;
                var alreadySolved = sumCell.GetAlreadySolved(direction);

                var remainingPossibleSets = possibleSets.Where(x => !x.Intersect(alreadySolved).Any());

                // foreach (var set in remainingPossibleSets)
                // {
                //     foreach (var value in set)
                //     {
                //         foreach (var unSolvedCell in unSolvedCells)
                //         {
                //             unSolvedCell.IsTheValuePossibleSolution(value);
                //         }
                //     }
                // }
            }

            return(false);
        }
Exemplo n.º 2
0
 private void BuildGameCellChainForSumCell(SumCell currentCell, Func <int, Cell> cellAt, int startPosition, int endPosition, Direction direction)
 {
     for (var i = startPosition; i < endPosition; i++)
     {
         if (cellAt(i) is GameCell gameCell)
         {
             currentCell.AddPartGameCell(gameCell, direction);
         }
         else
         {
             break;
         }
     }
 }
Exemplo n.º 3
0
        private bool EliminateNeverUsedNumbers(SumCell sumCell, string lookupKey, GameCell[] partCells, Direction direction)
        {
            foreach (var gameCell in partCells.Where(x => x.IsSolved))
            {
                sumCell.AddAlreadySolved(gameCell.Value, direction);
            }

            var neverUsed       = NumberSets[lookupKey].NeverUsed;
            var alreadySolved   = sumCell.GetAlreadySolved(direction);
            var eliminationList = neverUsed.Union(alreadySolved);

            var anythingSolved = false;

            foreach (var gameCell in partCells.Where(x => !x.IsSolved))
            {
                if (gameCell.EliminateTheNumbers(eliminationList.ToArray()))
                {
                    anythingSolved = true;
                }
            }
            return(anythingSolved);
        }
Exemplo n.º 4
0
        public static Cell NewCell(string initialValue, int xPosition, int yPosition)
        {
            if (initialValue.Equals("x", StringComparison.InvariantCultureIgnoreCase))
            {
                return(new RemovedCell(xPosition, yPosition));
            }

            if (initialValue.Contains(@"\"))
            {
                var sumCell = new SumCell(xPosition, yPosition);

                var parts = initialValue.Split(@"\");
                if (parts[0].Length == 0)
                {
                    sumCell.HasSumY = false;
                }
                else
                {
                    sumCell.HasSumY = true;
                    sumCell.SumY    = int.Parse(parts[0]);
                }

                if (parts[1].Length == 0)
                {
                    sumCell.HasSumX = false;
                }
                else
                {
                    sumCell.HasSumX = true;
                    sumCell.SumX    = int.Parse(parts[1]);
                }

                return(sumCell);
            }

            return(new GameCell(xPosition, yPosition));
        }