public override RefillFlowRecord Apply (Container2D_Rectangular container)
	{
		var key = GetType().Name + "-" + "RefillInfoMap";

		FillInfo[,] fillInfoMap = null;
		if (!container.UserData.ContainsKey(key))
		{
			List<Pos2D> pores = new List<Pos2D>();
			container.ForeachSlot((x, y, slot)=>{
				if (null == slot){
					pores.Add(new Pos2D(x, y));
					container.WrapperRect[y, x] = new SlotWrapper2D();
					container.WrapperRect[y, x].pos = new Pos2D(x, y);
				}
			});

			fillInfoMap = GenerateRefillTrendMap(container);
			container.UserData.Add(key, fillInfoMap);
			GlobalDebug.refillInfoMap = fillInfoMap;

			foreach (var p in pores)
			{
				container.ClearSlot(p.x, p.y);
			}
		}
		else
		{
			fillInfoMap = container.UserData[key] as FillInfo[,];
		}

		return DoApply(container, fillInfoMap);
	}
Beispiel #2
0
    private OperationOutput.Episode DoRefill()
    {
        var ret = new OperationOutput.Episode(OperationOutput.EpisodeType.REFILL);

        ret.refillFlow  = refillRule.Apply(foreground);
        ret.refillSlots = new SlotAttribute[foreground.Height, foreground.Width];

        foreground.ForeachSlot((x, y, slot) => {
            if (null == foreground.WrapperRect[y, x])
            {
                background.ClearSlot(x, y);
                background.FillSlot(x, y);
            }
        });
        MakeContainerStable(background);
        background.ForeachSlot((x, y, slot) => {
            if (slot.slotAttribute.category != SlotAttribute.Category.INSULATOR)
            {
                foreground.SetSlot(x, y, slot);
            }
        });
        background.FlushAsFirmware();
        foreground.ForeachSlot((x, y, slot) => {
            ret.refillSlots[y, x] = slot.slotAttribute;
        });
        return(ret);
    }
    private List <Pos2D> CollectRefillEnds(Container2D_Rectangular container, FillInfo[,] fillInfoMap)
    {
        var slots = container.WrapperRect;
        var ret   = new List <Pos2D>();

        container.ForeachSlot((x, y, slot) => {
            if (null != slot)
            {
                return;
            }
            if (fillInfoMap[y, x].IsOnSpot)
            {
                return;
            }
            foreach (var t in surroundingOffsets)
            {
                if (!container.IsLegalPosition(x + t.item1, y + t.item2))
                {
                    continue;
                }
                if (null != slots[y + t.item2, x + t.item1])
                {
                    continue;
                }
                var fi = fillInfoMap[y + t.item2, x + t.item1];
                if (!fi.IsOnSpot && fi.ancestorPos.x == x && fi.ancestorPos.y == y)
                {
                    return;
                }
            }
            ret.Add(new Pos2D(x, y));
        });
        return(ret);
    }
    public override RefillFlowRecord Apply(Container2D_Rectangular container)
    {
        var key = GetType().Name + "-" + "RefillInfoMap";

        FillInfo[,] fillInfoMap = null;
        if (!container.UserData.ContainsKey(key))
        {
            List <Pos2D> pores = new List <Pos2D>();
            container.ForeachSlot((x, y, slot) => {
                if (null == slot)
                {
                    pores.Add(new Pos2D(x, y));
                    container.WrapperRect[y, x]     = new SlotWrapper2D();
                    container.WrapperRect[y, x].pos = new Pos2D(x, y);
                }
            });

            fillInfoMap = GenerateRefillTrendMap(container);
            container.UserData.Add(key, fillInfoMap);
            GlobalDebug.refillInfoMap = fillInfoMap;

            foreach (var p in pores)
            {
                container.ClearSlot(p.x, p.y);
            }
        }
        else
        {
            fillInfoMap = container.UserData[key] as FillInfo[, ];
        }

        return(DoApply(container, fillInfoMap));
    }
Beispiel #5
0
 public void Init(Container2D_Rectangular container, ViewLayout2DR layout)
 {
     this.layout = layout;
     cells       = new ViewCell[container.Height, container.Width];
     container.ForeachSlot((x, y, s) => {
         InsertCell(s.slotAttribute, new Pos2D(x, y));
     });
     if (drawCoordinate)
     {
         DrawCoordinates(container);
     }
 }
Beispiel #6
0
	public void Init(Container2D_Rectangular container, ViewLayout2DR layout)
	{
		this.layout = layout;
		cells = new ViewCell[container.Height,container.Width];
		container.ForeachSlot((x, y, s)=>{
			InsertCell(s.slotAttribute, new Pos2D(x, y));
		});
		if (drawCoordinate)
		{
			DrawCoordinates(container);
		}
	}
Beispiel #7
0
    private void MakeContainerStable(Container2D_Rectangular container)
    {
        var lmRecords = SeekContainerLM(container);

        if (lmRecords.Count <= 0)
        {
            return;
        }
        var sandbox = CollectContainerEliminate(container, lmRecords);

        container.ForeachSlot((x, y, slot) => {
            if (sandbox[y, x])
            {
                container.ClearSlot(x, y);
                container.FillSlot(x, y);
            }
        });
        MakeContainerStable(container);
    }
Beispiel #8
0
    public OperationOutput PerformOperation(OperationInput op)
    {
        var ret = new OperationOutput();

        ret.IsRejected = true;
        foreach (var plm in plmRecords)
        {
            if ((plm.x1 == op.x1 && plm.y1 == op.y1 && plm.x2 == op.x2 && plm.y2 == op.y2) ||
                (plm.x1 == op.x2 && plm.y1 == op.y2 && plm.x2 == op.x1 && plm.y2 == op.y1))
            {
                ret.IsRejected = false;
                break;
            }
        }
        if (ret.IsRejected)
        {
            return(ret);
        }
        else
        {
            ret.episodes = new List <OperationOutput.Episode>();
        }

        foreground.SwapSlot(op.x1, op.y1, op.x2, op.y2);

        var lmRecords = new List <LMRecord2D_Retangular>();

        lmRecords = SeekContainerLM(foreground);

        do
        {
            var elimination = new OperationOutput.Episode(OperationOutput.EpisodeType.ELIMINATION);
            elimination.elimination = new List <Pos2D>();
            var sandbox = CollectContainerEliminate(foreground, lmRecords);
            foreground.ForeachSlot((x, y, slot) => {
                if (sandbox[y, x])
                {
                    elimination.elimination.Add(new Pos2D(x, y));
                    foreground.ClearSlot(x, y);
                }
            });
            ret.episodes.Add(elimination);

            foreach (var sc in scoreRules)
            {
                sc.Apply(sandbox);
            }

            ret.episodes.Add(DoRefill());

            lmRecords = SeekContainerLM(foreground);
            if (0 == lmRecords.Count)
            {
                plmRecords = SeekContainerPLM(foreground);
                if (plmRecords.Count >= minimalPlayablePLM)
                {
                    break;
                }
                else
                {
                    foreground.RecreateSubjects(true);
                    MakeContainerStable(foreground);
                    MakeContainerPlayable(foreground);
                    var shuffle = new OperationOutput.Episode(OperationOutput.EpisodeType.SHUFFLE);
                    shuffle.shuffle = new SlotAttribute[foreground.Height, foreground.Width];
                    foreground.ForeachSlot((x, y, slot) => {
                        shuffle.shuffle[y, x] = slot.slotAttribute;
                    });
                    ret.episodes.Add(shuffle);
                    plmRecords = SeekContainerPLM(foreground);
                    break;
                }
            }
        }while (true);

        return(ret);
    }
	private FillInfo[,] GenerateRefillTrendMap(Container2D_Rectangular container)
	{
		var ret = new FillInfo[container.Height, container.Width];
		var slots = container.WrapperRect;
		Action<int, int, List<SlotWrapper2D>> picker = (x, y, list)=>{
			if (x < 0 || x >= container.Width) return;
			if (y < 0 || y >= container.Height) return;
			if (null != slots[y, x].slotAttribute && slots[y, x].slotAttribute.category == SlotAttribute.Category.INSULATOR)
			{
				return;
			}
			list.Add(slots[y, x]);
		};
		Action<int, int> exitMarker = (x, y)=>{
			if (null == ret[y, x])
			{
				ret[y, x] = new FillInfo();
				ret[y, x].ancestorPos = new Pos2D(x, y + 1);
			}
		};

		container.ForeachSlot((x, y, slot)=>{
			if (null != ret[y, x]) 
				return;
			if (null != slot.slotAttribute && slot.slotAttribute.category == SlotAttribute.Category.INSULATOR)
				return;
			var ctx = new AStar.Context<SlotWrapper2D>();
			ctx.start = slots[y, x];
			ctx.procTrait = (s)=>{
				return s.Trait;
			};
			ctx.procWeight = (SlotWrapper2D from, SlotWrapper2D to)=>{
				foreach (var t in surroundingOffsets){
					if (to.pos.x == from.pos.x + t.item1 && to.pos.y == from.pos.y + t.item2){
						return t.item3;
					}
				}
				throw new NotImplementedException();
			};
			ctx.procTermination = (SlotWrapper2D s)=>{
				return s.pos.y == container.Height - 1;
			};
			ctx.procDistanceEstimator = (SlotWrapper2D s)=>{
				return (container.Height - 1 - s.pos.y) * 1.0001f;
			};
			ctx.procAdjacencies = (SlotWrapper2D s)=>{
				var list = new List<SlotWrapper2D>();
				foreach (var t in surroundingOffsets){
					picker.Invoke(s.pos.x + t.item1, s.pos.y + t.item2, list);
				}
				return list;
			};

			if (AStar.Evaluate(ctx))
			{
				var result = ctx.path;
				if (result.Count <= 0)
				{
					exitMarker.Invoke(x, y);
				}
				else
				{
					result.Reverse();
					for (int i = 0, len = result.Count - 1; i < len; i++)
					{
						if (null != ret[result[i].pos.y, result[i].pos.x]) continue;
						var t = new FillInfo();
						t.ancestorPos = new Pos2D(result[i + 1].pos.x, result[i + 1].pos.y);
						ret[result[i].pos.y, result[i].pos.x] = t;
					}
					exitMarker(result[result.Count - 1].pos.x, result[result.Count - 1].pos.y);
				}
			}
			else
			{
				ret[y, x] = new FillInfo();
			}
		});

		for (int y = 0; y < container.Height; y++)
		{
			for (int x = 0; x < container.Width; x++)
			{
				var fi = ret[y, x];
				if (null == fi) continue;
				if (fi.IsOnSpot) continue;
				fi.childrenPos = new List<Pos2D>();
				foreach (var t in surroundingOffsets)
				{
					var sx = x + t.item1;
					var sy = y + t.item2;

					if (!container.IsLegalPosition(sx, sy))
					{
						continue;
					}
					var slot = container.GetSlot(sx, sy);
					if (null != slot && null != slot.slotAttribute && slot.slotAttribute.category == SlotAttribute.Category.INSULATOR)
					{
						continue;
					}

					var touch = ret[sy, sx];
					if (null != touch.ancestorPos && touch.ancestorPos.x == x && touch.ancestorPos.y == y)
					{
						fi.childrenPos.Add(new Pos2D(sx, sy));
					}
				}
			}
		}

		return ret;
	}
	private List<Pos2D> CollectRefillEnds(Container2D_Rectangular container, FillInfo[,] fillInfoMap)
	{
		var slots = container.WrapperRect;
		var ret = new List<Pos2D>();

		container.ForeachSlot((x, y, slot)=>{
			if (null != slot) return;
			if (fillInfoMap[y, x].IsOnSpot) return;
			foreach (var t in surroundingOffsets){
				if (!container.IsLegalPosition(x + t.item1, y + t.item2)) continue;
				if (null != slots[y + t.item2, x + t.item1]) continue;
				var fi = fillInfoMap[y + t.item2, x + t.item1];
				if (!fi.IsOnSpot && fi.ancestorPos.x == x && fi.ancestorPos.y == y)
				{
					return;
				}
			}
			ret.Add(new Pos2D(x, y));
		});
		return ret;
	}
	private RefillFlowRecord DoApply(Container2D_Rectangular container, FillInfo[,] fillInfoMap)
	{
		var ret = new RefillFlowRecord();
		var slots = container.WrapperRect;

		container.ForeachSlot((x, y, slot)=>{
			if (null != slot) return;
			var fi = fillInfoMap[y, x];
			if (fi.IsOnSpot){
				ret.OnSpotList.Add(new Pos2D(x, y));
			}
		});
		var ends = CollectRefillEnds(container, fillInfoMap);
		var dir = new Dictionary<int, Tuple<SlotWrapper2D, int>>();
		foreach (var e in ends)
		{
			var pos = fillInfoMap[e.y, e.x].ancestorPos;
			int inverseDepth = 0;
			while (container.IsLegalPosition(pos.x, pos.y))
			{
				var s = slots[pos.y, pos.x];
				if (null != s)
				{
					inverseDepth++;
					if (!dir.ContainsKey(s.Trait))
					{
						dir.Add(s.Trait, new Tuple<SlotWrapper2D, int>(s, inverseDepth));
					}
					else if (dir[s.Trait].item2 < inverseDepth)
					{
						dir[s.Trait].item2 = inverseDepth;
					}
				}
				pos = fillInfoMap[pos.y, pos.x].ancestorPos;
			}
		}
		var list = new List<Tuple<SlotWrapper2D, int>>();
		foreach (var kvp in dir)
		{
			list.Add(kvp.Value);
		}
		list.Sort((lhr, rhr)=>{
			return (lhr.item2 < rhr.item2) ? -1 : 1;
		});

		foreach (var t in list)
		{
			var src = t.item1.pos;
			var moveTo = DepthFirstSearch(slots, fillInfoMap, src.x, src.y);
			var dst = moveTo.item1;

			var path = new RefillFlowRecord.Path();
			path.src = src.Clone();
			path.dst = dst.Clone();

			container.SwapSlot(src.x, src.y, dst.x, dst.y);
			var cur = path.dst;
			do {
				path.movements.Add(cur);
				cur = fillInfoMap[cur.y, cur.x].ancestorPos;
			} while (cur != path.src);

			path.movements.Add(path.src);
			path.movements.Reverse();

			ret.NonFillMovements.Add(path);
		}

		ret.FillMovements = CollectFillPathList(container, fillInfoMap);

		return ret;
	}
    private FillInfo[,] GenerateRefillTrendMap(Container2D_Rectangular container)
    {
        var ret   = new FillInfo[container.Height, container.Width];
        var slots = container.WrapperRect;
        Action <int, int, List <SlotWrapper2D> > picker = (x, y, list) => {
            if (x < 0 || x >= container.Width)
            {
                return;
            }
            if (y < 0 || y >= container.Height)
            {
                return;
            }
            if (null != slots[y, x].slotAttribute && slots[y, x].slotAttribute.category == SlotAttribute.Category.INSULATOR)
            {
                return;
            }
            list.Add(slots[y, x]);
        };
        Action <int, int> exitMarker = (x, y) => {
            if (null == ret[y, x])
            {
                ret[y, x]             = new FillInfo();
                ret[y, x].ancestorPos = new Pos2D(x, y + 1);
            }
        };

        container.ForeachSlot((x, y, slot) => {
            if (null != ret[y, x])
            {
                return;
            }
            if (null != slot.slotAttribute && slot.slotAttribute.category == SlotAttribute.Category.INSULATOR)
            {
                return;
            }
            var ctx       = new AStar.Context <SlotWrapper2D>();
            ctx.start     = slots[y, x];
            ctx.procTrait = (s) => {
                return(s.Trait);
            };
            ctx.procWeight = (SlotWrapper2D from, SlotWrapper2D to) => {
                foreach (var t in surroundingOffsets)
                {
                    if (to.pos.x == from.pos.x + t.item1 && to.pos.y == from.pos.y + t.item2)
                    {
                        return(t.item3);
                    }
                }
                throw new NotImplementedException();
            };
            ctx.procTermination = (SlotWrapper2D s) => {
                return(s.pos.y == container.Height - 1);
            };
            ctx.procDistanceEstimator = (SlotWrapper2D s) => {
                return((container.Height - 1 - s.pos.y) * 1.0001f);
            };
            ctx.procAdjacencies = (SlotWrapper2D s) => {
                var list = new List <SlotWrapper2D>();
                foreach (var t in surroundingOffsets)
                {
                    picker.Invoke(s.pos.x + t.item1, s.pos.y + t.item2, list);
                }
                return(list);
            };

            if (AStar.Evaluate(ctx))
            {
                var result = ctx.path;
                if (result.Count <= 0)
                {
                    exitMarker.Invoke(x, y);
                }
                else
                {
                    result.Reverse();
                    for (int i = 0, len = result.Count - 1; i < len; i++)
                    {
                        if (null != ret[result[i].pos.y, result[i].pos.x])
                        {
                            continue;
                        }
                        var t         = new FillInfo();
                        t.ancestorPos = new Pos2D(result[i + 1].pos.x, result[i + 1].pos.y);
                        ret[result[i].pos.y, result[i].pos.x] = t;
                    }
                    exitMarker(result[result.Count - 1].pos.x, result[result.Count - 1].pos.y);
                }
            }
            else
            {
                ret[y, x] = new FillInfo();
            }
        });

        for (int y = 0; y < container.Height; y++)
        {
            for (int x = 0; x < container.Width; x++)
            {
                var fi = ret[y, x];
                if (null == fi)
                {
                    continue;
                }
                if (fi.IsOnSpot)
                {
                    continue;
                }
                fi.childrenPos = new List <Pos2D>();
                foreach (var t in surroundingOffsets)
                {
                    var sx = x + t.item1;
                    var sy = y + t.item2;

                    if (!container.IsLegalPosition(sx, sy))
                    {
                        continue;
                    }
                    var slot = container.GetSlot(sx, sy);
                    if (null != slot && null != slot.slotAttribute && slot.slotAttribute.category == SlotAttribute.Category.INSULATOR)
                    {
                        continue;
                    }

                    var touch = ret[sy, sx];
                    if (null != touch.ancestorPos && touch.ancestorPos.x == x && touch.ancestorPos.y == y)
                    {
                        fi.childrenPos.Add(new Pos2D(sx, sy));
                    }
                }
            }
        }

        return(ret);
    }
    private RefillFlowRecord DoApply(Container2D_Rectangular container, FillInfo[,] fillInfoMap)
    {
        var ret   = new RefillFlowRecord();
        var slots = container.WrapperRect;

        container.ForeachSlot((x, y, slot) => {
            if (null != slot)
            {
                return;
            }
            var fi = fillInfoMap[y, x];
            if (fi.IsOnSpot)
            {
                ret.OnSpotList.Add(new Pos2D(x, y));
            }
        });
        var ends = CollectRefillEnds(container, fillInfoMap);
        var dir  = new Dictionary <int, Tuple <SlotWrapper2D, int> >();

        foreach (var e in ends)
        {
            var pos          = fillInfoMap[e.y, e.x].ancestorPos;
            int inverseDepth = 0;
            while (container.IsLegalPosition(pos.x, pos.y))
            {
                var s = slots[pos.y, pos.x];
                if (null != s)
                {
                    inverseDepth++;
                    if (!dir.ContainsKey(s.Trait))
                    {
                        dir.Add(s.Trait, new Tuple <SlotWrapper2D, int>(s, inverseDepth));
                    }
                    else if (dir[s.Trait].item2 < inverseDepth)
                    {
                        dir[s.Trait].item2 = inverseDepth;
                    }
                }
                pos = fillInfoMap[pos.y, pos.x].ancestorPos;
            }
        }
        var list = new List <Tuple <SlotWrapper2D, int> >();

        foreach (var kvp in dir)
        {
            list.Add(kvp.Value);
        }
        list.Sort((lhr, rhr) => {
            return((lhr.item2 < rhr.item2) ? -1 : 1);
        });

        foreach (var t in list)
        {
            var src    = t.item1.pos;
            var moveTo = DepthFirstSearch(slots, fillInfoMap, src.x, src.y);
            var dst    = moveTo.item1;

            var path = new RefillFlowRecord.Path();
            path.src = src.Clone();
            path.dst = dst.Clone();

            container.SwapSlot(src.x, src.y, dst.x, dst.y);
            var cur = path.dst;
            do
            {
                path.movements.Add(cur);
                cur = fillInfoMap[cur.y, cur.x].ancestorPos;
            } while (cur != path.src);

            path.movements.Add(path.src);
            path.movements.Reverse();

            ret.NonFillMovements.Add(path);
        }

        ret.FillMovements = CollectFillPathList(container, fillInfoMap);

        return(ret);
    }