private static LMRecord2D_Retangular Match_Internal(SlotWrapper2D[,] slots,
                                                        int x,
                                                        int y,
                                                        RuleMatchBasic2D_Rectangular matchRule)
    {
        var sample = slots[y + matchRule.scatters[0].y, x + matchRule.scatters[0].x];

        if (!sample.IsTarget)
        {
            return(null);
        }
        for (int i = 1; i < matchRule.scatters.Length; ++i)
        {
            var sniff = slots[y + matchRule.scatters[i].y, x + matchRule.scatters[i].x];
            if (!sniff.IsTarget)
            {
                return(null);
            }
            if (!sample.slotAttribute.trait.AbsoluteEqual(sniff.slotAttribute.trait))
            {
                return(null);
            }
        }
        var record = new LMRecord2D_Retangular();

        record.x    = x;
        record.y    = y;
        record.rule = matchRule;
        return(record);
    }
	private static LMRecord2D_Retangular Match_Internal(SlotWrapper2D[,] slots,
	                                                    int x,
	                                                    int y,
	                                                    RuleMatchBasic2D_Rectangular matchRule)
	{
		var sample = slots[y + matchRule.scatters[0].y, x + matchRule.scatters[0].x];
		if (!sample.IsTarget)
		{
			return null;
		}
		for (int i = 1; i < matchRule.scatters.Length; ++i)
		{
			var sniff = slots[y + matchRule.scatters[i].y, x + matchRule.scatters[i].x];
			if (!sniff.IsTarget)
			{
				return null;
			}
			if (!sample.slotAttribute.trait.AbsoluteEqual(sniff.slotAttribute.trait))
			{
				return null;
			}
		}
		var record = new LMRecord2D_Retangular();
		record.x = x;
		record.y = y;
		record.rule = matchRule;
		return record;
	}
Exemple #3
0
	public RuleMatchBasic2D_Rectangular SerializeData()
	{
		var ret = new RuleMatchBasic2D_Rectangular();
		ret.maskWidth = maskWidth;
		ret.maskHeight = maskHeight;
		ret.mask = selection.Clone() as bool[,];
		return ret;
	}
Exemple #4
0
    public RuleMatchBasic2D_Rectangular SerializeData()
    {
        var ret = new RuleMatchBasic2D_Rectangular();

        ret.maskWidth  = maskWidth;
        ret.maskHeight = maskHeight;
        ret.mask       = selection.Clone() as bool[, ];
        return(ret);
    }
Exemple #5
0
 private static void Seek_Internal(SeekContext ctx, RuleMatchBasic2D_Rectangular rule)
 {
     for (int y = 0; y < ctx.container.Height - rule.maskHeight + 1; y++)
     {
         for (int x = 0; x < ctx.container.Width - rule.maskWidth + 1; x++)
         {
             Match_Internal(ctx, x, y, rule, 0);
             Match_Internal(ctx, x, y, rule, rule.scatters.Length - 1);
         }
     }
 }
	private static void Seek_Internal(SeekContext ctx, RuleMatchBasic2D_Rectangular rule)
	{
		var slots = ctx.container.WrapperRect;

		for (int y = 0; y < ctx.container.Height - rule.maskHeight + 1; y++)
		{
			for (int x = 0; x < ctx.container.Width - rule.maskWidth + 1; x++)
			{
				var r = Match_Internal(slots, x, y, rule);
				if (null != r) ctx.result.Add(r);
			}
		}
	}
Exemple #7
0
	public static RuleMatchBasic StaticDeserialize(string str)
	{
		var tuple = JsonHelper.Deserialize<Tuple<string, string>>(str);
		switch (tuple.item1)
		{
		case RECTANGULAR_2D:
			var ret = new RuleMatchBasic2D_Rectangular();
			ret.Deserialize(tuple.item2);
			return ret;
		default:
			throw new NotSupportedException();
		}
	}
Exemple #8
0
    public static RuleMatchBasic StaticDeserialize(string str)
    {
        var tuple = JsonHelper.Deserialize <Tuple <string, string> >(str);

        switch (tuple.item1)
        {
        case RECTANGULAR_2D:
            var ret = new RuleMatchBasic2D_Rectangular();
            ret.Deserialize(tuple.item2);
            return(ret);

        default:
            throw new NotSupportedException();
        }
    }
Exemple #9
0
	public void LoadData(RuleMatchBasic2D_Rectangular rule)
	{
		plane.gameObject.SetActive(true);
		Draw(planeWidth, planeHeight, rule.maskWidth, rule.maskHeight);
		for (int y = 0; y < rule.maskHeight; y++)
		{
			for (int x = 0; x < rule.maskWidth; x++)
			{
				if (rule.mask[y, x])
				{
					Mark(x, y);
				}
			}
		}
	}
Exemple #10
0
 public void LoadData(RuleMatchBasic2D_Rectangular rule)
 {
     plane.gameObject.SetActive(true);
     Draw(planeWidth, planeHeight, rule.maskWidth, rule.maskHeight);
     for (int y = 0; y < rule.maskHeight; y++)
     {
         for (int x = 0; x < rule.maskWidth; x++)
         {
             if (rule.mask[y, x])
             {
                 Mark(x, y);
             }
         }
     }
 }
    private static void Seek_Internal(SeekContext ctx, RuleMatchBasic2D_Rectangular rule)
    {
        var slots = ctx.container.WrapperRect;

        for (int y = 0; y < ctx.container.Height - rule.maskHeight + 1; y++)
        {
            for (int x = 0; x < ctx.container.Width - rule.maskWidth + 1; x++)
            {
                var r = Match_Internal(slots, x, y, rule);
                if (null != r)
                {
                    ctx.result.Add(r);
                }
            }
        }
    }
    public PlayableEnv2DR Build_2DR_Hardcoded()
    {
        var env = new PlayableEnv2DR();

        env.MinimalPlayablePLM = 3;

        var canvasConfig = new CanvasConfig2DR();

        canvasConfig.mapWidth   = 10;
        canvasConfig.mapHeight  = 10;
        canvasConfig.insulators = new List <Pos2D>();
        var slotConfig = new SlotConfig_Hardcoded();

        env.Foreground = new Container2D_Rectangular(canvasConfig, slotConfig);
        env.Background = new Container2D_Rectangular(canvasConfig, slotConfig);

        var matchRules = new RuleMatchBasic2D_Rectangular[2]
        {
            new RuleMatchBasicHorizontal(),
            new RuleMatchBasicVertical()
        };

        foreach (var r in matchRules)
        {
            r.Compile();
        }
        env.MatchRules = matchRules;

        env.ExtensionRules = new RuleMatchExtension2D_Rectangular[0];



        env.RefillRule = new RuleRefill2DR_Downward();

        env.OperationRules = new RuleOperation2D_Rectangular[2]
        {
            new RuleOperationHorizontal(),
            new RuleOperationVertical()
        };

        env.ScoreRules = new RuleScore2D_Rectangular[0];

        return(env);
    }
Exemple #13
0
    private static bool JudgeOperable(SlotTrait matchingTrait,
                                      Container2D_Rectangular container,
                                      int xMask, int yMask,
                                      int xInMask, int yInMask,
                                      RuleMatchBasic2D_Rectangular match,
                                      RuleOperation2D_Rectangular operation,
                                      bool inverse)
    {
        int xRelative = inverse ? -operation.xRelative : operation.xRelative;
        int yRelative = inverse ? -operation.yRelative : operation.yRelative;

        int xTouch = xInMask + xRelative;
        int yTouch = yInMask + yRelative;

        if (yTouch >= 0 && yTouch < match.maskHeight &&
            xTouch >= 0 && xTouch < match.maskWidth &&
            match.PeekMask(xTouch, yTouch))
        {
            return(false);
        }

        var sniffX = xMask + xInMask + xRelative;
        var sniffY = yMask + yInMask + yRelative;

        if (!container.IsLegalPosition(sniffX, sniffY))
        {
            return(false);
        }

        var sniff = container.GetSlot(sniffX, sniffY);

        if (!sniff.IsTarget)
        {
            return(false);
        }

        return(matchingTrait.AbsoluteEqual(sniff.slotAttribute.trait));
    }
    public PlayableEnv2DR Build_2DR(PlayableScheme scheme)
    {
        var env = new PlayableEnv2DR();

        env.MinimalPlayablePLM = scheme.minimalPlayablePLM;
        env.Foreground         = new Container2D_Rectangular(scheme.canvasConfig as CanvasConfig2DR,
                                                             scheme.slotConfig);
        env.Background = new Container2D_Rectangular(scheme.canvasConfig as CanvasConfig2DR,
                                                     scheme.slotConfig);

        env.MatchRules = scheme.matchRules.SchemeStyleMap <RuleMatchBasic, RuleMatchBasic2D_Rectangular>((r) => {
            var raw        = r as RuleMatchBasic2D_Rectangular;
            var ret        = new RuleMatchBasic2D_Rectangular();
            ret.maskWidth  = raw.maskWidth;
            ret.maskHeight = raw.maskHeight;
            ret.mask       = raw.mask;
            ret.Compile();
            return(ret);
        }).ToArray();

        env.ExtensionRules = scheme.extensionRules.SchemeStyleMap <RuleMatchExtension, RuleMatchExtension2D_Rectangular>((r) => {
            return(r as RuleMatchExtension2D_Rectangular);
        }).ToArray();

        env.RefillRule = scheme.refillRule as RuleRefill2D_Rectangular;

        env.OperationRules = scheme.operationRules.SchemeStyleMap <RuleOperation, RuleOperation2D_Rectangular>((r) => {
            return(r as RuleOperation2D_Rectangular);
        }).ToArray();

        env.ScoreRules = scheme.scoreRules.SchemeStyleMap <RuleScore, RuleScore2D_Rectangular>((r) => {
            return(r as RuleScore2D_Rectangular);
        }).ToArray();

        return(env);
    }
    private static bool DryRun(PlayableScheme scheme, out string reason)
    {
        reason = "";
        if (scheme.canvasConfig is CanvasConfig2DR)
        {
            var container = new Container2D_Rectangular(scheme.canvasConfig as CanvasConfig2DR,
                                                        scheme.slotConfig);
            container.InitBlocks();

            var count1 = 0;
            var count2 = 0;
            for (int i = 0; i < 20; i++)
            {
                container.RecreateSubjects(true);
                var ctx1 = new LMSeeker2D_Retangular.SeekContext();
                ctx1.container  = container;
                ctx1.matchRules = scheme.matchRules.SchemeStyleMap <RuleMatchBasic, RuleMatchBasic2D_Rectangular>((r) => {
                    var raw        = r as RuleMatchBasic2D_Rectangular;
                    var ret        = new RuleMatchBasic2D_Rectangular();
                    ret.mask       = raw.mask;
                    ret.maskWidth  = raw.maskWidth;
                    ret.maskHeight = raw.maskHeight;
                    ret.Compile();
                    return(ret);
                }).ToArray();
                ctx1.result = new List <LMRecord2D_Retangular>();
                LMSeeker2D_Retangular.Seek(ctx1);
                if (ctx1.result.Count > 0)
                {
                    count1++;
                }

                var ctx2 = new PLMSeeker2D_Retangular.SeekContext();
                ctx2.container  = container;
                ctx2.matchRules = scheme.matchRules.SchemeStyleMap <RuleMatchBasic, RuleMatchBasic2D_Rectangular>((r) => {
                    var raw        = r as RuleMatchBasic2D_Rectangular;
                    var ret        = new RuleMatchBasic2D_Rectangular();
                    ret.mask       = raw.mask;
                    ret.maskWidth  = raw.maskWidth;
                    ret.maskHeight = raw.maskHeight;
                    ret.Compile();
                    return(ret);
                }).ToArray();
                ctx2.operationRules = scheme.operationRules.SchemeStyleMap <RuleOperation, RuleOperation2D_Rectangular>((r) => {
                    return(r as RuleOperation2D_Rectangular);
                }).ToArray();
                ctx2.result = new List <PLMRecord2D_Retangular>();
                PLMSeeker2D_Retangular.Seek(ctx2);
                if (ctx2.result.Count >= scheme.minimalPlayablePLM)
                {
                    count2++;
                }
            }

            if (count1 < 2 || count2 < 2)
            {
                reason = "Scheme is NOT playable!";
                return(false);
            }

            return(true);
        }
        throw new NotImplementedException();
    }
	private static bool DryRun(PlayableScheme scheme, out string reason)
	{
		reason = "";
		if (scheme.canvasConfig is CanvasConfig2DR)
		{
			var container = new Container2D_Rectangular(scheme.canvasConfig as CanvasConfig2DR,
			                                            scheme.slotConfig);
			container.InitBlocks();

			var count1 = 0;
			var count2 = 0;
			for (int i = 0; i < 20; i++)
			{
				container.RecreateSubjects(true);
				var ctx1 = new LMSeeker2D_Retangular.SeekContext();
				ctx1.container = container;
				ctx1.matchRules = scheme.matchRules.SchemeStyleMap<RuleMatchBasic, RuleMatchBasic2D_Rectangular>((r)=>{
					var raw = r as RuleMatchBasic2D_Rectangular;
					var ret = new RuleMatchBasic2D_Rectangular();
					ret.mask = raw.mask;
					ret.maskWidth = raw.maskWidth;
					ret.maskHeight = raw.maskHeight;
					ret.Compile();
					return ret;
				}).ToArray();
				ctx1.result = new List<LMRecord2D_Retangular>();
				LMSeeker2D_Retangular.Seek(ctx1);
				if (ctx1.result.Count > 0)
				{
					count1++;
				}

				var ctx2 = new PLMSeeker2D_Retangular.SeekContext();
				ctx2.container = container;
				ctx2.matchRules = scheme.matchRules.SchemeStyleMap<RuleMatchBasic, RuleMatchBasic2D_Rectangular>((r)=>{
					var raw = r as RuleMatchBasic2D_Rectangular;
					var ret = new RuleMatchBasic2D_Rectangular();
					ret.mask = raw.mask;
					ret.maskWidth = raw.maskWidth;
					ret.maskHeight = raw.maskHeight;
					ret.Compile();
					return ret;
				}).ToArray();
				ctx2.operationRules = scheme.operationRules.SchemeStyleMap<RuleOperation, RuleOperation2D_Rectangular>((r)=>{
					return r as RuleOperation2D_Rectangular;
				}).ToArray();
				ctx2.result = new List<PLMRecord2D_Retangular>();
				PLMSeeker2D_Retangular.Seek(ctx2);
				if (ctx2.result.Count >= scheme.minimalPlayablePLM)
				{
					count2++;
				}
			}

			if (count1 < 2 || count2 < 2)
			{
				reason = "Scheme is NOT playable!";
				return false;
			}

			return true;
		}
		throw new NotImplementedException();
	}
Exemple #17
0
    private static void Match_Internal(SeekContext ctx,
                                       int x,
                                       int y,
                                       RuleMatchBasic2D_Rectangular matchRule,
                                       int scatterIndex)
    {
        intermediate.Clear();
        var slots  = ctx.container.WrapperRect;
        var sample = slots[y + matchRule.scatters[scatterIndex].y, x + matchRule.scatters[scatterIndex].x];

        if (!sample.IsTarget)
        {
            return;
        }
        var operationUsed = false;

        for (int i = 0; i < matchRule.scatters.Length; ++i)
        {
            if (i == scatterIndex)
            {
                continue;
            }
            var targetX = x + matchRule.scatters[i].x;
            var targetY = y + matchRule.scatters[i].y;
            var sniff   = slots[targetY, targetX];
            if (!sniff.IsTarget)
            {
                return;
            }
            if (!sample.slotAttribute.trait.AbsoluteEqual(sniff.slotAttribute.trait))
            {
                if (operationUsed)
                {
                    return;
                }

                foreach (var operable in ctx.operationRules)
                {
                    if (JudgeOperable(sample.slotAttribute.trait,
                                      ctx.container,
                                      x, y,
                                      matchRule.scatters[i].x, matchRule.scatters[i].y,
                                      matchRule, operable, false))
                    {
                        var record = new PLMRecord2D_Retangular();
                        record.operation = operable;
                        record.rule      = matchRule;
                        record.x1        = targetX;
                        record.y1        = targetY;
                        record.x2        = targetX + operable.xRelative;
                        record.y2        = targetY + operable.yRelative;
                        intermediate.Add(record);
                        operationUsed = true;
                    }
                    if (JudgeOperable(sample.slotAttribute.trait,
                                      ctx.container,
                                      x, y,
                                      matchRule.scatters[i].x, matchRule.scatters[i].y,
                                      matchRule, operable, true))
                    {
                        var record = new PLMRecord2D_Retangular();
                        record.operation = operable;
                        record.rule      = matchRule;
                        record.x1        = targetX;
                        record.y1        = targetY;
                        record.x2        = targetX - operable.xRelative;
                        record.y2        = targetY - operable.yRelative;
                        intermediate.Add(record);
                        operationUsed = true;
                    }
                }
                if (!operationUsed)
                {
                    return;
                }
            }
        }

        // A pure match!
        if (!operationUsed)
        {
            return;
        }

        ctx.result.AddRange(intermediate);
        intermediate.Clear();
    }