Пример #1
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);
        }
Пример #2
0
 private void DoMoves(SolverEntry currentEntry)
 {
     // Try moves, from least likely to lead to a solution to most likely
     TryMoveFinishPlay(currentEntry);
     TryMovePlayPlay(currentEntry);
     TryMoveStock(currentEntry);
     TryMovePlaytoFinish(currentEntry);
 }
Пример #3
0
        private void TryMove(SolverEntry currentEntry, Card card, CardStack from, CardStack dest)
        {
            var field = currentEntry.Field;
            var move  = GetMove(field, from, dest, card);

            if (move.From == currentEntry.Move.To && move.To == currentEntry.Move.From)
            {
                // this is the reverse move from the origin, does not make sense.
                return;
            }
            var newField = field.Move(card, from, dest);

            TryAddWork(currentEntry, newField, move);
        }
Пример #4
0
        private void TryMoveFinishPlay(SolverEntry currentEntry)
        {
            var current = currentEntry.Field;

            // Possibly, move cards back from finish=>play
            var min = current.FinishStacks.Min(s => s.GetTopValue()) + 1;

            foreach (var stack in current.FinishStacks.Where(s => s.GetTopValue() > min))
            {
                foreach (var dest in current.PlayStacks.Where(s => s.CanAccept(stack.Top, stack)))
                {
                    TryMove(currentEntry, stack.Top, stack, dest);
                }
            }
        }
Пример #5
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
                    });
                }
            }
        }
Пример #6
0
        private PatienceField TryMovePlayPlay(SolverEntry currentEntry)
        {
            var current = currentEntry.Field;

            // Play => Play, partial stacks
            foreach (var stack in current.PlayStacks)
            {
                foreach (var card in stack.GetMovableCards())
                {
                    foreach (var dest in current.PlayStacks.Where(s => s != stack && s.CanAccept(card, stack)))
                    {
                        TryMove(currentEntry, card, stack, dest);
                    }
                }
            }
            return(current);
        }
Пример #7
0
        private PatienceField TryMovePlaytoFinish(SolverEntry currentEntry)
        {
            var current = currentEntry.Field;

            // Play => finish, just top
            foreach (var stack in current.PlayStacks.Where(s => s.Count > 0))
            {
                foreach (var dest in current.FinishStacks.Where(s => s.CanAccept(stack.Top, stack)))
                {
                    TryMove(currentEntry, stack.Top, stack, dest);
                    if (stack.Top.Value == Value.Ace)
                    {
                        break;
                    }
                }
            }
            return(current);
        }
Пример #8
0
        private PatienceField TryMoveStock(SolverEntry currentEntry)
        {
            var current = currentEntry.Field;

            // Stock => everywhere
            foreach (var card in current.Stock.GetMovableCards())
            {
                foreach (var dest in current.GetDestinationStacks().Where(s => s.CanAccept(card, current.Stock)))
                {
                    TryMove(currentEntry, card, current.Stock, dest);
                    if (card.Value == Value.Ace || card.Value == Value.King)
                    {
                        break;
                    }
                }
            }
            return(current);
        }