Beispiel #1
0
        private static void PlayGame(PatienceField field)
        {
            field.Stock.JustMoveTop = true;
            string input;

            do
            {
                field.DumpToConsole();
                input = Console.ReadLine();
                if (input.Length < 2)
                {
                    field = field.NextCard();
                }
                else if (input.Length == 2)
                {
                    var from = field.GetStack(input[0]);
                    var to   = field.GetStack(input[1]);
                    field = Move(field, from, to);
                    if (field.IsDone())
                    {
                        break;
                    }
                }
            } while (input != "exit");
        }
Beispiel #2
0
        private static int SolveField(int currentFieldNumber)
        {
            while (true)
            {
                currentFieldNumber++;

                var field = PatienceField.FillWithRandomCards(new Random(currentFieldNumber));
                field.DumpToConsole();
                TimeSpan timeout   = TimeSpan.FromSeconds(10);
                var      stopwatch = Stopwatch.StartNew();
                var      solution  = TrySolve(field, timeout);
                stopwatch.Stop();
                if (solution == null)
                {
                    Console.WriteLine("No solution found for field {0} in {1} seconds :(", currentFieldNumber, stopwatch.Elapsed.TotalSeconds);
                }
                else
                {
                    Console.WriteLine("Solution found for field {0} in {1} seconds :)({2} steps) ",
                                      currentFieldNumber, stopwatch.Elapsed.TotalSeconds, solution.GetSequence().Count());
                    File.AppendAllText(solvableFieldsFile, String.Format("{0};{1};{2}\r\n",
                                                                         currentFieldNumber,
                                                                         stopwatch.Elapsed.TotalMilliseconds,
                                                                         String.Join(",", solution.WinningMoves().ToArray())));
                }
            }
        }
Beispiel #3
0
        private static CardStack GetStack(this PatienceField field, char p)
        {
            switch (p)
            {
            case '1': return(field.PlayStacks.Take(1).Last());

            case '2': return(field.PlayStacks.Take(2).Last());

            case '3': return(field.PlayStacks.Take(3).Last());

            case '4': return(field.PlayStacks.Take(4).Last());

            case '5': return(field.PlayStacks.Take(5).Last());

            case '6': return(field.PlayStacks.Take(6).Last());

            case '7': return(field.PlayStacks.Take(7).Last());

            case 'a': return(field.FinishStacks.Take(1).Last());

            case 'b': return(field.FinishStacks.Take(2).Last());

            case 'c': return(field.FinishStacks.Take(3).Last());

            case 'd': return(field.FinishStacks.Take(4).Last());

            case '0': return(field.Stock);
            }
            Console.WriteLine("Unknown stack {0}", p);
            return(null);
        }
Beispiel #4
0
 public Solver(PatienceField field, bool silent = false)
 {
     _knownFields = new HashSet <PatienceField>();
     _toTry       = new Stack <SolverEntry>();
     _startfield  = field;
     _silent      = silent;
 }
Beispiel #5
0
        private static SolverEntry TrySolve(PatienceField field, TimeSpan timeout)
        {
            Console.WriteLine(timeout);
            var         solver = new Solver(field, silent: false);
            SolverEntry result = solver.Solve(timeout);

            return(result);
        }
Beispiel #6
0
 private int GetIndex(PatienceField field, CardStack stack)
 {
     if (stack == field.Stock)
     {
         return(0);
     }
     return(field.GetDestinationStacks().ToList().IndexOf(stack) + 1);
 }
Beispiel #7
0
 private Move GetMove(PatienceField field, CardStack from, CardStack dest, Card c)
 {
     return(new Move
     {
         From = GetIndex(field, from),
         To = GetIndex(field, dest),
         Card = c,
     });
 }
Beispiel #8
0
        private void TryAddWork(SolverEntry currentEntry, PatienceField newField, Move move)
        {
            if (!_knownFields.Contains(newField))
            {
                var newFieldm = newField.DoTrivialMoves();

                if (_knownFields.Add(newFieldm))
                {
                    _knownFields.Add(newField);
                    _toTry.Push(new SolverEntry {
                        Field = newFieldm, Previous = currentEntry, Move = move
                    });
                }
            }
        }
Beispiel #9
0
 static void Main(string[] args)
 {
     if (args.FirstOrDefault() == "play")
     {
         int field;
         if (!int.TryParse(args.LastOrDefault(), out field))
         {
             field = new Random().Next();
         }
         Console.WriteLine("Playing field {0}", field);
         PlayGame(PatienceField.FillWithRandomCards(new Random(field)));
     }
     else
     {
         SolveField(GetLastField());
     }
 }
Beispiel #10
0
        private static PatienceField Move(PatienceField field, CardStack from, CardStack to)
        {
            if (from == null || to == null)
            {
                Console.WriteLine("Invalid move: unknown stack");
                return(field);
            }

            var cardToMove = from.GetMovableCards().FirstOrDefault(c => to.CanAccept(c, from));

            if (cardToMove == null)
            {
                Console.WriteLine("Invalid move: none of the cards on from can enter destination");
                return(field);
            }
            return(field.Move(cardToMove, from, to));
        }