Exemple #1
0
        public override Grid Solve()
        {
            while (true)
            {
                if (!possibilitiesGrid.IsFinished())
                {
                    tempGrid = new Grid(problem);

                    ComputePossibilities();

                    for (int row = 0; row < 7; row++)
                    {
                        for (int col = 0; col < 7; col++)
                        {
                            PossibilitiesContainer possibContainer = (PossibilitiesContainer)possibilitiesGrid.GetValueAt(row, col);

                            // Make the move
                            Possibilities posib = (Possibilities)possibContainer.GetMove();
                            if (posib != null)
                            {
                                tempGrid.SetValueAt(posib.GetFromSlot(), 0);
                                tempGrid.SetValueAt(posib.GetToSlot(), 1);
                                tempGrid.SetValueAt(posib.GetHoleSlot(), 0);
                            }
                            possibContainer.IncrementCounter();

                            // Fillup the single possibilities and forget them.
                            // They are not required for the snapshot
                            if (possibContainer.GetCount() == 1)
                            {
                                possibilitiesGrid.SetValueAt(row, col, 0);
                            }
                        }
                    }

                    // Take snapshot
                    Snapshot snap = new Snapshot();
                    snap.SetGame(tempGrid);
                    snap.SetPossibilitiesGrid(possibilitiesGrid);
                    snapshots.SaveSnapshot(snap);

                    // Set the temp grid as the main grid for the next iteration
                    problem = new Grid(tempGrid);
                }
                else
                {
                    if (problem.GetResult() == 1)
                    {
                        break;
                    }

                    // take the top most snapshot from the stack and set it as the main problem
                }
            }
            return(problem);
        }
Exemple #2
0
        public override Grid Solve()
        {
            //// Do the initial snapshot (Initializing the solver)
            ComputePossibilities();

            //// Create Snapshot of the initial state
            //Snapshot ss = new Snapshot();
            //ss.SetGame(problem);
            //ss.SetPossibilities(possibilities);

            //// Add the snapshot to the stack
            //snapshots.SaveSnapshot(ss);

            //Snapshot sslatest = ss;

            Snapshot sslatest;

            // Do untill solution is reached
            while (true)
            {
                if (IsFinished())
                {
                    Console.WriteLine(problem.GetResult());
                    if (problem.GetResult() == 1)
                    {
                        break; // We are done
                    }
                    else
                    {
                        while (true)
                        {
                            // Make top of stack item as current problem
                            sslatest = snapshots.GetLatestSnapshot();
                            SetProblem(sslatest.GetGame());
                            SetPossibilities(sslatest.GetPossibilitiesGrid());
                            sslatest.IncrementIndex();

                            if (snapshots.GetLatestSnapshot().ExhaustedPossibilities())
                            {
                                snapshots.RemoveLatestSnapshot();
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    possibList.Clear();
                    ComputePossibilities();

                    // Create Snapshot
                    Snapshot ssNew = new Snapshot();
                    ssNew.SetGame(problem);
                    ssNew.SetPossibilities(possibList);

                    // Add the snapshot to the stack
                    snapshots.SaveSnapshot(ssNew);

                    sslatest = ssNew;
                }

                // Make a move from the list of possibilities
                Possibilities posib = (Possibilities)possibList[sslatest.GetCurrentIndex()];
                problem.SetValueAt(posib.GetFromSlot(), 0);
                problem.SetValueAt(posib.GetToSlot(), 1);
                problem.SetValueAt(posib.GetHoleSlot(), 0);

                // Reset the Possibilities as we have now made a move
                // and disturbed the earlier state
                possibList.Clear();
                ComputePossibilities();
            }
            return(this.problem);
        }