public string Process(string text, XmlAttributeCollection attributes, RequestProcess process) { var fields = text.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries); var ruleSeed = int.Parse(fields[0]); var colours = (from s in fields.Skip(1) select(Colour) Enum.Parse(typeof(Colour), s, true)).ToArray(); var rules = GetRules(ruleSeed); foreach (var rule in rules[colours.Length - 3]) { var conditionResult = ConditionResult.FromBool(true); foreach (var condition in rule.Queries) { conditionResult = conditionResult && condition.Delegate(process, colours); } if (conditionResult) { var result = rule.Solution.Type.Delegate(process, rule.Solution.Colour ?? 0, colours); return(result.ToString()); } else if (conditionResult.Code == ConditionResultCode.Unknown) { return(conditionResult.Details !); } } throw new InvalidOperationException("No rules matched?!"); }
/// <param name="text">Usage: [rule seed] [colour] [label]?</param> public string Process(string text, XmlAttributeCollection attributes, RequestProcess process) { var fields = text.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries); var colour = (Colour)Enum.Parse(typeof(Colour), fields[1], true); var rules = GetRules(int.Parse(fields[0])); if (fields.Length > 2) { // Initial stage var label = (Label)Enum.Parse(typeof(Label), fields[2], true); InitialSolution?instruction = null; foreach (var rule in rules.InitialRules) { var result = ConditionResult.FromBool(true); foreach (var condition in rule.Conditions) { result = result && condition.Delegate(process, new ButtonData() { Colour = colour, Label = label }); } if (result.Code == ConditionResultCode.True) { instruction = rule.Solution; break; } else if (result.Code == ConditionResultCode.Unknown) { return(result.Details !); } } if (instruction == null) { throw new InvalidOperationException("No rules matched?!"); } return(instruction.Value.ToString()); } else { // Held stage var rule = rules.HeldRules.FirstOrDefault(r => r.Colour == colour) ?? rules.HeldRules.Single(r => r.Colour == null); return($"{rule.Solution.Type} {rule.Solution.Digit}"); } }
public ConditionType(string key, string text, bool colourAvailableForSolution, int wiresInvolved, Func <Colour, Colour[], bool> func, params InstructionType[] additionalSolutions) { this.Key = key; this.UsesColour = true; this.ColourAvailableForSolution = colourAvailableForSolution; this.WiresInvolved = wiresInvolved; this.AdditionalSolutions = additionalSolutions; this.Delegate = c => new WireCondition(this, string.Format(text, c.ToString().ToLower()), c, (p, w) => ConditionResult.FromBool(func(c, w))); }
private static Condition <ButtonData> ButtonLabel(Label label) => new Condition <ButtonData>("isButtonLabel", $"the button says '{label}'", (p, d) => ConditionResult.FromBool(d.Label == label));
private static Condition <ButtonData> ButtonColour(Colour colour) => new Condition <ButtonData>("isButtonColor", $"the button is {colour.ToString().ToLower()}", (p, d) => ConditionResult.FromBool(d.Colour == colour));