Exemplo n.º 1
0
        public object Evaluate(CubeState state)
        {
            IList value = this.Value.Evaluate(state) as IList;

            if (value is null)
            {
                throw new Exception("Tried to unpack from a Non-Enumerable value");
            }
            if (value.Count != this.Children.Length)
            {
                throw new Exception($"Tried to unpack into {this.Children.Length} variables from Enumerable with {value.Count} elements");
            }
            for (int i = 0; i < value.Count; i++)
            {
                state.VariableValues[this.Children[i].Identifier] = value[i];
            }
            return(value);
        }
Exemplo n.º 2
0
        public object Evaluate(CubeState state)
        {
            object inner = Value.Evaluate(state);

            if (inner is Node <List <PackCard> > board)
            {
                List <PackCard> cards = board.Evaluate(state);
                if (cards.Count == 0)
                {
                    throw new Exception("Tried to extract from an empty board");
                }
                int      ind  = rnd.Next(cards.Count);
                PackCard card = cards[ind];
                if (!this.Replacement)
                {
                    state.Boards[card.BoardName].Remove(card);
                }
                state.VariableValues[this.Identifier.Identifier] = card;
                return(card);
            }
            IList value = inner as IList;

            if (value is null)
            {
                throw new Exception("Tried to extract from a Non-Enumerable value");
            }
            if (value.Count == 0)
            {
                throw new Exception("Tried to extract from an empty List");
            }
            int    index = rnd.Next(value.Count);
            object val   = value[index];

            if (!this.Replacement)
            {
                value.RemoveAt(index);
            }

            state.VariableValues[this.Identifier.Identifier] = val;

            return(val);
        }
Exemplo n.º 3
0
        public override bool Evaluate(CubeState state)
        {
            bool res1 = this.Children[0].Evaluate(state);
            bool res2;

            switch (this.Function)
            {
            case PropositionOperator.And:
                res2 = this.Children[1].Evaluate(state);
                return(res1 && res2);

            case PropositionOperator.Or:
                res2 = this.Children[1].Evaluate(state);
                return(res1 || res2);

            case PropositionOperator.Not:
                return(!res1);
            }
            throw new NotImplementedException();
        }
Exemplo n.º 4
0
        public override bool Evaluate(CubeState state)
        {
            object[] values = this.Children.Select(n => n.Evaluate(state)).ToArray();
            switch (this.Function)
            {
            case PropositionFunction.ContainsAtLeast:
                int?tCount = values[1] as int?;
                if (tCount is null)
                {
                    throw new Exception("Tried to pass Non-Numeric as second argument to ContainsAtLeast");
                }
                int count = (int)tCount;
                if (values[0] is Node <List <PackCard> > board)
                {
                    return(board.Evaluate(state).Count >= count);
                }
                IList value = values[0] as IList;
                if (value is null)
                {
                    throw new Exception("Tried to pass Non-Enumerable as first argument to ContainsAtLeast");
                }
                return(value.Count >= count);

            case PropositionFunction.ContainsExact:
                int?tCount2 = values[1] as int?;
                if (tCount2 is null)
                {
                    throw new Exception("Tried to pass Non-Numeric as second argument to ContainsExact");
                }
                int count2 = (int)tCount2;
                if (values[0] is Node <List <PackCard> > board2)
                {
                    return(board2.Evaluate(state).Count == count2);
                }
                IList value2 = values[0] as IList;
                if (value2 is null)
                {
                    throw new Exception("Tried to pass Non-Enumerable as first argument to ContainsExact");
                }
                return(value2.Count == count2);

            case PropositionFunction.Contains:
                if (values[0] is Node <List <PackCard> > board3)
                {
                    return(board3.Evaluate(state).Contains(values[1]));
                }
                IList value3 = values[0] as IList;
                if (value3 is null)
                {
                    throw new Exception("Tried to pass Non-Enumerable as first argument to Contains");
                }
                return(value3.Contains(values[1]));

            case PropositionFunction.Intersects:
                if (values[0] is Node <List <PackCard> > lBoard && values[1] is Node <List <PackCard> > rBoard)
                {
                    return(lBoard.Evaluate(state).Intersect(rBoard.Evaluate(state)).Count() > 0);
                }
                IEnumerable left = values[0] as IEnumerable;
                if (left is null)
                {
                    throw new Exception("Tried to pass Non-Enumerable as first argument to Intersects");
                }
                IList right = values[1] as IList;
                if (right is null)
                {
                    throw new Exception("Tried to pass Non-Enumerable as second argument to Intersects");
                }
                return(left.Cast <object>().Intersect(right.Cast <object>()).Count() > 0);

            case PropositionFunction.Subset:
                if (values[0] is Node <List <PackCard> > lBoard2 && values[1] is Node <List <PackCard> > rBoard2)
                {
                    List <PackCard> lCards = lBoard2.Evaluate(state);
                    return(lCards.Intersect(rBoard2.Evaluate(state)).Count() > lCards.Count);
                }
                IList left1 = values[0] as IList;
                if (left1 is null)
                {
                    throw new Exception("Tried to pass Non-Enumerable as first argument to Subset");
                }
                IList right1 = values[1] as IList;
                if (right1 is null)
                {
                    throw new Exception("Tried to pass Non-Enumerable as second argument to Subset");
                }
                return(left1.Cast <object>().Intersect(right1.Cast <object>()).Count() == left1.Count);

            case PropositionFunction.Equals:
                return(values[0] == values[1]);
            }
            throw new NotImplementedException();
        }
Exemplo n.º 5
0
 public object Evaluate(CubeState state)
 {
     // CodeReview: Should this assert that all children are of the same type?
     return(this.Children.Select(c => c.Evaluate(state)).ToList());
 }
Exemplo n.º 6
0
        public object Evaluate(CubeState state)
        {
            object[] values = this.Children.Select(n => n.Evaluate(state)).ToArray();
            switch (this.Function)
            {
            case ValueFunction.Zip:
                IList[] realValues = values.Select(v => v as IList).ToArray();
                foreach (object tvalue in realValues)
                {
                    if (tvalue is null)
                    {
                        throw new Exception("Non-Enumerable type passed to Zip");
                    }
                }
                int maxSize = realValues.Select(e => e.Count).Max();
                return(Enumerable.Range(0, maxSize)
                       .Select(i => Enumerable.Range(0, realValues.Length)
                               .Select(j => realValues[j][i])
                               .ToArray())
                       .ToList());

            case ValueFunction.Rotate:
                IList realValue = values[0] as IList;
                if (realValue is null)
                {
                    throw new Exception("Non-Enumerable variable passed as first argument to Rotate");
                }

                int?tempCount = values[1] as int?;
                if (tempCount is null)
                {
                    throw new Exception("Non-Integer variable passed as second argument to Rotate");
                }
                int count = (int)tempCount;

                return(Enumerable.Range(0, realValue.Count).Select(i => new[] { realValue[i], realValue[(i + count) % realValue.Count] }).ToList());

            case ValueFunction.GetBoard:
                string value = values[0] as string;
                if (value is null)
                {
                    throw new Exception("Non-String variable passed as argument to GetBoard");
                }
                return(new BoardList(value));

            case ValueFunction.Concat:
                Node <List <PackCard> >[] boards = values.Select(v => v as Node <List <PackCard> >).ToArray();
                bool useBoard = true;
                foreach (Node <List <PackCard> > board in boards)
                {
                    useBoard &= !(board is null);
                }
                if (useBoard)
                {
                    return(new BoardConcat(boards));
                }

                IList[] realValues2 = values.Select(v => v as IList).ToArray();
                foreach (object tvalue in realValues2)
                {
                    if (tvalue is null)
                    {
                        throw new Exception("Non-Enumerable type passed to Concat");
                    }
                }
                IList result = new ArrayList();
                for (int i = 1; i < realValues2.Length; i++)
                {
                    foreach (object o in realValues2[i])
                    {
                        result.Add(o);
                    }
                }
                return(result);

            case ValueFunction.GetProperty:
                if (values[0] is PackCard card)
                {
                    return(card.GetProperty((string)values[1]));
                }
                else
                {
                    return(values[0].GetType().GetProperty((string)values[1]).GetValue(values[0]));
                }

            case ValueFunction.Following:
                IList realValue2 = values[1] as IList;
                if (realValue2 is null)
                {
                    throw new Exception("Non-Enumerable type passed as second argument to Following");
                }
                int index = realValue2.IndexOf(values[0]);
                if (index < 0)
                {
                    throw new Exception("Value was not found in list in Following");
                }
                return(realValue2[(index + 1) % realValue2.Count]);
            }
            throw new NotImplementedException();
        }
Exemplo n.º 7
0
 public override T Evaluate(CubeState state)
 {
     return(this.Value);
 }
Exemplo n.º 8
0
 object INode.Evaluate(CubeState state) => Evaluate(state);
Exemplo n.º 9
0
 public abstract T Evaluate(CubeState state);
Exemplo n.º 10
0
        public override List <PackCard> Evaluate(CubeState state)
        {
            ArrayList innerValue = (ArrayList)Inner.Evaluate(state);

            return(innerValue.Cast <PackCard>().ToList());
        }
Exemplo n.º 11
0
 public override List <PackCard> Evaluate(CubeState state)
 {
     return(state.Boards[this.BoardName]);
 }