예제 #1
0
        private int _evaluatePositionForToad(EvaluationData <FrogsAndToadsPosition> evaluationData)
        {
            (Maybe <EvaluationRecord> toad, Maybe <EvaluationRecord> frog)cached
                = _cache.Lookup(evaluationData.Position);
            if (cached.toad.HasValue && cached.toad.Value.IsComplete)
            {
                return(cached.toad.Value.Value);
            }

            MoveRecord moveRecord = new MoveRecord(
                evaluationData.Position.GetLeftMoves().Select(x => x as FrogsAndToadsMove),
                int.MinValue,
                cached.toad);

            if (moveRecord.NoPossibleMoves)
            {
                return(EvaluateEndPositionForRight(evaluationData.Position));
            }

            EvaluationRecord record = _updateEvaluation(
                evaluationData,
                moveRecord,
                _evaluatePositionForFrog,
                (x, y) => Math.Max(x, y),
                x => (Math.Min(evaluationData.BestToad, x), evaluationData.BestFrog));


            _cache.Store(evaluationData.Position, (record.ToMaybe(), cached.frog));
            return(record.Value);
        }
예제 #2
0
        private EvaluationRecord _updateEvaluation(
            EvaluationData <FrogsAndToadsPosition> evaluationData,
            MoveRecord moveRecord,
            _evaluator nextEvaluator,
            _bestValueUpdater bestValueUpdater,
            _bestPairUpdater bestPairUpdater)
        {
            FrogsAndToadsPosition resultingPosition;
            EvaluationData <FrogsAndToadsPosition> resultingEvaluationData;

            int bestToad            = evaluationData.BestToad;
            int bestFrog            = evaluationData.BestFrog;
            int moveEvaluationcount = 0;
            int bestValue           = moveRecord.BestValueSoFar;
            List <FrogsAndToadsMove> evaluatedMoves =
                moveRecord.EvaluatedMoves;

            foreach (FrogsAndToadsMove move in moveRecord.MovesToEvaluate)
            {
                moveEvaluationcount++;
                evaluatedMoves.Add(move);
                resultingPosition       = evaluationData.Position.PlayMove(move);
                resultingEvaluationData = new EvaluationData <FrogsAndToadsPosition>(
                    resultingPosition,
                    evaluationData.Depth + 1,
                    evaluationData.BestToad,
                    evaluationData.BestFrog);

                bestValue =
                    bestValueUpdater(
                        bestValue,
                        nextEvaluator(resultingEvaluationData));

                (bestToad, bestFrog) = bestPairUpdater(bestValue);
                if (bestToad > bestFrog)
                {
                    break;
                }
            }

            return
                (moveEvaluationcount == moveRecord.MovesToEvaluate.Count
                ? new EvaluationRecord(bestValue)
                : new EvaluationRecord(bestValue, evaluatedMoves));
        }