Ejemplo n.º 1
0
        public static Algorithm Parse(
            string baseName,
            IState initialState,
            IState finalState,
            XElement element,
            Solver solver)
        {
            var(algorithm, colors) = GetBaseAlgorithm(element, solver);

            algorithm = algorithm
                        .WithName(BuildName(baseName, element.Attribute(nameof(Algorithm.Name))?.Value))
                        .WithDescription(element.Element(nameof(Algorithm.Description))?.Value);

            var movesText = element.Element(nameof(Algorithm.Moves))?.Value;

            if (!string.IsNullOrWhiteSpace(movesText))
            {
                algorithm = algorithm.WithMoves(ParseMoves(movesText));
            }

            var myInitialState  = GetState(initialState, element.Element(nameof(Algorithm.InitialState)), solver);
            var myFinishedState = GetState(finalState, element.Element(nameof(Algorithm.FinishedState)), solver);

            return(algorithm
                   .WithInitialState(AndState.Combine(algorithm.InitialState, myInitialState))
                   .WithFinishedState(AndState.Combine(algorithm.FinishedState, myFinishedState))
                   .WithColors(colors));
        }
Ejemplo n.º 2
0
 public Algorithm WithInitialState(IState state)
 => state == this.InitialState
     ? this
     : new Algorithm(
     this.Name,
     this.Description,
     AndState.Combine(this.InitialState, state),
     this.FinishedState,
     this.Moves);
Ejemplo n.º 3
0
        public void Converts_to_OrState_with_each_part_negationed()
        {
            var state1 = SingleColorState.Create("Left", "Blue");
            var state2 = SingleColorState.Create("Front", "Red");
            var stete3 = SingleColorState.Create("Right", "Green");

            var andState     = new AndState(ImmutableArray.Create <IState>(state1, state2, stete3));
            var negatedState = Assert.IsType <OrState>(andState.Negate());

            Assert.Collection(
                negatedState.States,
                state => Assert.Equal("!Left Blue", state.ToString()),
                state => Assert.Equal("!Front Red", state.ToString()),
                state => Assert.Equal("!Right Green", state.ToString()));
        }
Ejemplo n.º 4
0
        public void False_if_all_parts_are_true()
        {
            var trueState1 = A.Fake <IState>();

            A.CallTo(() => trueState1.Matches(A <Puzzle> .Ignored)).Returns(true);
            var trueState2 = A.Fake <IState>();

            A.CallTo(() => trueState2.Matches(A <Puzzle> .Ignored)).Returns(true);
            var trueState3 = A.Fake <IState>();

            A.CallTo(() => trueState3.Matches(A <Puzzle> .Ignored)).Returns(true);

            var andState = new AndState(ImmutableArray.Create(trueState1, trueState2, trueState3));

            Assert.False(andState.Negate().Matches(null));
        }
Ejemplo n.º 5
0
        public void False_if_any_part_is_false(bool value1, bool value2, bool value3)
        {
            var state1 = A.Fake <IState>();

            A.CallTo(() => state1.Matches(A <Puzzle> .Ignored)).Returns(value1);
            var state2 = A.Fake <IState>();

            A.CallTo(() => state2.Matches(A <Puzzle> .Ignored)).Returns(value2);
            var state3 = A.Fake <IState>();

            A.CallTo(() => state3.Matches(A <Puzzle> .Ignored)).Returns(value3);

            var andState = new AndState(ImmutableArray.Create(state1, state2, state3));

            Assert.False(andState.Matches(null));
        }