/// <summary> /// 在尾巴到苹果的路径找到后,寻找头到苹果 /// </summary> public void FromAppleToHead(ContainerDictionary dict, LabelContainer apple, List <Coordinate> road) { var newSnake = new List <Coordinate>(); newSnake.Add(new Coordinate { X_ = Tail.X, Y_ = Tail.Y }); foreach (var body in Body) { newSnake.Add(new Coordinate { X_ = body.X, Y_ = body.Y }); } foreach (var r in road) { newSnake.Add(new Coordinate { X_ = r.X_, Y_ = r.Y_ }); } dict.SetContainerType(ContainerType.Start, Head.X, Head.Y); dict.SetContainerType(ContainerType.End, apple.X, apple.Y); foreach (var r in newSnake) { dict.SetContainerType(ContainerType.Wall, r.X_, r.Y_); } }
public void TestDictionarySerializationWithDeleted() { ShadowObject.Enable = true; var obj = new ContainerDictionary("Root") { Strings = { { GuidGenerator.Get(200), "aaa" }, { GuidGenerator.Get(100), "bbb" } }, Objects = { { "key3", new ContainerCollection("obj1") }, { "key4", new ContainerCollection("obj2") } }, }; var stringIds = CollectionItemIdHelper.GetCollectionItemIds(obj.Strings); stringIds[GuidGenerator.Get(200)] = IdentifierGenerator.Get(8); stringIds[GuidGenerator.Get(100)] = IdentifierGenerator.Get(5); stringIds.MarkAsDeleted(IdentifierGenerator.Get(3)); stringIds.MarkAsDeleted(IdentifierGenerator.Get(1)); var objectIds = CollectionItemIdHelper.GetCollectionItemIds(obj.Objects); objectIds["key3"] = IdentifierGenerator.Get(3); objectIds["key4"] = IdentifierGenerator.Get(4); objectIds.MarkAsDeleted(IdentifierGenerator.Get(1)); objectIds.MarkAsDeleted(IdentifierGenerator.Get(6)); var yaml = SerializeAsString(obj); Assert.Equal(YamlDictionaryWithDeleted, yaml); }
/// <summary> /// 寻找当前头到当前尾的路径 /// </summary> public void FromHeadToTail(ContainerDictionary dict) { dict.SetContainerType(ContainerType.Start, Head.X, Head.Y); dict.SetContainerType(ContainerType.End, Tail.X, Tail.Y); foreach (var d in Body) { dict.SetContainerType(ContainerType.Wall, d.X, d.Y); } }
/// <summary> /// 寻找当前尾到当前苹果的路径 /// </summary> public void FromTailToApple(ContainerDictionary dict, LabelContainer apple) { dict.SetContainerType(ContainerType.Start, Tail.X, Tail.Y); dict.SetContainerType(ContainerType.End, apple.X, apple.Y); dict.SetContainerType(ContainerType.Wall, Head.X, Head.Y); foreach (var d in Body) { dict.SetContainerType(ContainerType.Wall, d.X, d.Y); } }
/// <summary> /// 寻找走到下一的时候,头到尾的路径 /// </summary> public void FromNextHeadToTail(ContainerDictionary dict, LabelContainer next) { dict.SetContainerType(ContainerType.Start, next.X, next.Y); dict.SetContainerType(ContainerType.Wall, Head.X, Head.Y); foreach (var i in this.Body) { dict.SetContainerType(ContainerType.Wall, i.X, i.Y); } if (next.SnakeType != SnakeTypeEnum.Apple) { dict.SetContainerType(ContainerType.End, this.Body[0].X, this.Body[0].Y); } else { dict.SetContainerType(ContainerType.End, this.Tail.X, this.Tail.Y); } }
//初始化界面 private void InitializeUI() { dict = new ContainerDictionary(col - 1, row - 1); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(259, 210); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.MaximizeBox = false; this.Name = "Form1"; this.Text = "寻路"; this.PerformLayout(); this.Size = new Size(32 + col * howbig, 140 + row * howbig); int id = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { LabelContainer lab = new LabelContainer(); lab.CanPass = true; lab.AutoSize = false; lab.ID = id++; lab.X = j; lab.Y = i; lab.Width = howbig; lab.Height = howbig; lab.Location = new Point(howbig * j + 10, 60 + howbig * (row - i)); //lab.BorderStyle = BorderStyle.FixedSingle; lab.BorderStyle = BorderStyle.None; lab.BackColor = Color.White; lab.ContainerColor = Color.White; //lab.Text = j + "" + i; lab.Click += lab_Click; lab.MouseMove += lab_MouseMove; dict.AddOrUpdate(lab); this.Controls.Add(lab); } } }
/// <summary> /// 尾=》苹果=》头 /// </summary> /// <returns></returns> public async Task <bool> TryFindWayFromTailToAppleToHead() { var con = new ContainerDictionary(dict.TotalX, dict.TotalY); snake.FromTailToApple(con, Apple); var finder = CreateNewFinder(); var str = finder.Execute(con); if (str == "成功") { var road = finder.WayResult; var con2 = new ContainerDictionary(dict.TotalX, dict.TotalY); snake.FromAppleToHead(con2, this.Apple, road); finder = CreateNewFinder(); str = finder.Execute(con2); if (str == "成功") { road = finder.WayResult; await Task.Run(() => { road.Add(new Coordinate { X_ = con.EndContainer.X, Y_ = con.EndContainer.Y }); snake.MoveOneStep(dict[road[0].X_, road[0].Y_]); }); //苹果被吃掉了 if (road.Count == 1) { choiseDict = new Dictionary <Tuple <int, int>, List <MapHelper> >(); this.Apple = null; } return(true); } } return(false); }
/// <summary> /// 寻找吃掉苹果之后,头到尾的路径 /// </summary> public void FromAppleToTail(ContainerDictionary dict, LabelContainer apple, List <Coordinate> road) { var newSnake = new List <Coordinate>(); newSnake.Add(new Coordinate { X_ = Tail.X, Y_ = Tail.Y }); foreach (var body in Body) { newSnake.Add(new Coordinate { X_ = body.X, Y_ = body.Y }); } newSnake.Add(new Coordinate { X_ = Head.X, Y_ = Head.Y }); foreach (var r in road) { newSnake.Add(new Coordinate { X_ = r.X_, Y_ = r.Y_ }); } var snakeLength = Body.Count + 2; //蛇长 var roadLength = newSnake.Count; //总长 //移除多余的 newSnake.RemoveRange(0, roadLength - snakeLength); dict.SetContainerType(ContainerType.Start, apple.X, apple.Y); dict.SetContainerType(ContainerType.End, newSnake[0].X_, newSnake[0].Y_); newSnake.RemoveAt(0); foreach (var r in newSnake) { dict.SetContainerType(ContainerType.Wall, r.X_, r.Y_); } }
private List <MapHelper> ManagerMapHelperList() { var choiseList = new List <MapHelper>(); var head = snake.Head; foreach (var dir in Direction.DirectionArray) { var curX = head.X + dir.X_; var curY = head.Y + dir.Y_; if (dict[curX, curY] != null && ( dict[curX, curY].SnakeType == SnakeTypeEnum.None || dict[curX, curY].SnakeType == SnakeTypeEnum.Tail || dict[curX, curY].SnakeType == SnakeTypeEnum.Apple ) )//是空格子 { var containerDict = new ContainerDictionary(dict.TotalX, dict.TotalY); snake.FromNextHeadToTail(containerDict, dict[curX, curY]);//判断走到下一格之后,是否有路找到尾 var finder = CreateNewFinder(); if (finder.Execute(containerDict) == "成功") { //判断格子到苹果的距离 var dis = 0; if (this.LaterPeriod) { if (Math.Abs(snake.Tail.X - curX) > Math.Abs(snake.Tail.Y - curY)) { dis = (Math.Abs(snake.Tail.X - curX) * 3 + Math.Abs(snake.Tail.Y - curY)) * 2; } else { dis = (Math.Abs(snake.Tail.X - curX) + Math.Abs(snake.Tail.Y - curY) * 3) * 2; } } else { if (Math.Abs(this.Apple.X - curX) > Math.Abs(this.Apple.Y - curY)) { dis = (Math.Abs(this.Apple.X - curX) * 3 + Math.Abs(this.Apple.Y - curY)) * 2; } else { dis = (Math.Abs(this.Apple.X - curX) + Math.Abs(this.Apple.Y - curY) * 3) * 2; } } //dis += ((Math.Abs(snake.Tail.X - curX) + Math.Abs(snake.Tail.Y - curY)) * 2); //判断格子四周是边界的个数(身体或者边界) var con = 10; foreach (var dirr in Direction.DirectionArray) { var nX = curX + dirr.X_; var nY = curY + dirr.Y_; var nDict = dict[nX, nY]; if (nDict == null) { con++; } else if (nDict.SnakeType == SnakeTypeEnum.Body) { con += 2; } } //判断蛇方向 var di = 0; var nextX = head.X - snake.Body.Last().X; var nextY = head.Y - snake.Body.Last().Y; if (curX == head.X + nextX && curY == head.Y + nextY) { di = 2; } //添加多选项 choiseList.Add(new MapHelper() { Coor = new Coordinate() { X_ = curX, Y_ = curY }, Weight = dis + con + di }); } } } choiseList = choiseList.OrderByDescending(i => i.Weight).ToList(); if (choiseList.Count > 1) { choiseList.RemoveRange(1, choiseList.Count - 1); } return(choiseList); }
private MapHelper ManagerMapHelper() { MapHelper choise = null; var head = snake.Head; var distance = 0; foreach (var dir in Direction.DirectionArray) { var curX = head.X + dir.X_; var curY = head.Y + dir.Y_; if (dict[curX, curY] != null && ( dict[curX, curY].SnakeType == SnakeTypeEnum.None || dict[curX, curY].SnakeType == SnakeTypeEnum.Tail || dict[curX, curY].SnakeType == SnakeTypeEnum.Apple ) )//是空格子 { var containerDict = new ContainerDictionary(dict.TotalX, dict.TotalY); snake.FromNextHeadToTail(containerDict, dict[curX, curY]);//判断走到下一格之后,是否有路找到尾 var finder = CreateNewFinder(); if (finder.Execute(containerDict) == "成功") { //判断格子到苹果的距离 var dis = 0; //if (this.LaterPeriod) { if (Math.Abs(snake.Tail.X - curX) > Math.Abs(snake.Tail.Y - curY)) { dis = (Math.Abs(snake.Tail.X - curX) * 3 + Math.Abs(snake.Tail.Y - curY)) * 2; } else { dis = (Math.Abs(snake.Tail.X - curX) + Math.Abs(snake.Tail.Y - curY) * 3) * 2; } } //else //{ // if (Math.Abs(this.Apple.X - curX) > Math.Abs(this.Apple.Y - curY)) // { // dis = (Math.Abs(this.Apple.X - curX) * 3 + Math.Abs(this.Apple.Y - curY)) * 2; // } // else // dis = (Math.Abs(this.Apple.X - curX) + Math.Abs(this.Apple.Y - curY) * 3) * 2; //} //dis += ((Math.Abs(snake.Tail.X - curX) + Math.Abs(snake.Tail.Y - curY)) * 2); //判断格子四周是边界的个数(身体或者边界) var con = 10; foreach (var dirr in Direction.DirectionArray) { var nX = curX + dirr.X_; var nY = curY + dirr.Y_; var nDict = dict[nX, nY]; if (nDict == null) { con++; } else if (nDict.SnakeType == SnakeTypeEnum.Body) { con += 2; } } //判断蛇方向 var di = 0; var nextX = head.X - snake.Body.Last().X; var nextY = head.Y - snake.Body.Last().Y; if (curX == head.X + nextX && curY == head.Y + nextY) { di = 2; } if (dict[curX, curY].SnakeType == SnakeTypeEnum.Apple) { //dis += 1000; } //添加多选项 if (dis + con + di > distance) { distance = dis + con + di; choise = new MapHelper() { Coor = new Coordinate() { X_ = curX, Y_ = curY } }; } } } } return(choise); }
public void TestIdsGeneration() { ShadowObject.Enable = true; CollectionItemIdentifiers ids; var obj1 = new ContainerCollection("Root") { Strings = { "aaa", "bbb", "ccc" }, Objects = { new ContainerCollection("obj1"), new ContainerCollection("obj2") } }; var hashSet = new HashSet <ItemId>(); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Objects, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Objects, out ids)); AssetCollectionItemIdHelper.GenerateMissingItemIds(obj1); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Strings, out ids)); Assert.Equal(3, ids.KeyCount); Assert.Equal(0, ids.DeletedCount); Assert.True(ids.ContainsKey(0)); Assert.True(ids.ContainsKey(1)); Assert.True(ids.ContainsKey(2)); hashSet.Add(ids[0]); hashSet.Add(ids[1]); hashSet.Add(ids[2]); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Objects, out ids)); Assert.Equal(2, ids.KeyCount); Assert.Equal(0, ids.DeletedCount); Assert.True(ids.ContainsKey(0)); Assert.True(ids.ContainsKey(1)); hashSet.Add(ids[0]); hashSet.Add(ids[1]); Assert.Equal(5, hashSet.Count); var obj2 = new ContainerDictionary("Root") { Strings = { { GuidGenerator.Get(200), "aaa" }, { GuidGenerator.Get(100), "bbb" }, { GuidGenerator.Get(300), "ccc" } }, Objects = { { "key3", new ContainerCollection("obj1") }, { "key4", new ContainerCollection("obj2") } }, }; hashSet.Clear(); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Objects, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Objects, out ids)); AssetCollectionItemIdHelper.GenerateMissingItemIds(obj2); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Strings, out ids)); Assert.Equal(3, ids.KeyCount); Assert.Equal(0, ids.DeletedCount); Assert.True(ids.ContainsKey(GuidGenerator.Get(200))); Assert.True(ids.ContainsKey(GuidGenerator.Get(100))); Assert.True(ids.ContainsKey(GuidGenerator.Get(300))); hashSet.Add(ids[GuidGenerator.Get(200)]); hashSet.Add(ids[GuidGenerator.Get(100)]); hashSet.Add(ids[GuidGenerator.Get(300)]); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Objects, out ids)); Assert.Equal(2, ids.KeyCount); Assert.Equal(0, ids.DeletedCount); Assert.True(ids.ContainsKey("key3")); Assert.True(ids.ContainsKey("key4")); hashSet.Add(ids["key3"]); hashSet.Add(ids["key4"]); Assert.Equal(5, hashSet.Count); }
public void TestIdsGeneration() { ShadowObject.Enable = true; CollectionItemIdentifiers ids; var obj1 = new ContainerCollection("Root") { Strings = { "aaa", "bbb", "ccc" }, Objects = { new ContainerCollection("obj1"), new ContainerCollection("obj2") } }; var hashSet = new HashSet<ItemId>(); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Objects, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Objects, out ids)); AssetCollectionItemIdHelper.GenerateMissingItemIds(obj1); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Strings, out ids)); Assert.AreEqual(3, ids.KeyCount); Assert.AreEqual(0, ids.DeletedCount); Assert.True(ids.ContainsKey(0)); Assert.True(ids.ContainsKey(1)); Assert.True(ids.ContainsKey(2)); hashSet.Add(ids[0]); hashSet.Add(ids[1]); hashSet.Add(ids[2]); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj1.Objects, out ids)); Assert.AreEqual(2, ids.KeyCount); Assert.AreEqual(0, ids.DeletedCount); Assert.True(ids.ContainsKey(0)); Assert.True(ids.ContainsKey(1)); hashSet.Add(ids[0]); hashSet.Add(ids[1]); Assert.AreEqual(5, hashSet.Count); var obj2 = new ContainerDictionary("Root") { Strings = { { GuidGenerator.Get(200), "aaa" }, { GuidGenerator.Get(100), "bbb" }, { GuidGenerator.Get(300), "ccc" } }, Objects = { { "key3", new ContainerCollection("obj1") }, { "key4", new ContainerCollection("obj2") } }, }; hashSet.Clear(); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Strings, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Objects, out ids)); Assert.False(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Objects, out ids)); AssetCollectionItemIdHelper.GenerateMissingItemIds(obj2); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Strings, out ids)); Assert.AreEqual(3, ids.KeyCount); Assert.AreEqual(0, ids.DeletedCount); Assert.True(ids.ContainsKey(GuidGenerator.Get(200))); Assert.True(ids.ContainsKey(GuidGenerator.Get(100))); Assert.True(ids.ContainsKey(GuidGenerator.Get(300))); hashSet.Add(ids[GuidGenerator.Get(200)]); hashSet.Add(ids[GuidGenerator.Get(100)]); hashSet.Add(ids[GuidGenerator.Get(300)]); Assert.True(CollectionItemIdHelper.TryGetCollectionItemIds(obj2.Objects, out ids)); Assert.AreEqual(2, ids.KeyCount); Assert.AreEqual(0, ids.DeletedCount); Assert.True(ids.ContainsKey("key3")); Assert.True(ids.ContainsKey("key4")); hashSet.Add(ids["key3"]); hashSet.Add(ids["key4"]); Assert.AreEqual(5, hashSet.Count); }
public void TestDictionarySerializationWithDeleted() { ShadowObject.Enable = true; var obj = new ContainerDictionary("Root") { Strings = { { GuidGenerator.Get(200), "aaa" }, { GuidGenerator.Get(100), "bbb" } }, Objects = { { "key3", new ContainerCollection("obj1") }, { "key4", new ContainerCollection("obj2") } }, }; var stringIds = CollectionItemIdHelper.GetCollectionItemIds(obj.Strings); stringIds[GuidGenerator.Get(200)] = IdentifierGenerator.Get(8); stringIds[GuidGenerator.Get(100)] = IdentifierGenerator.Get(5); stringIds.MarkAsDeleted(IdentifierGenerator.Get(3)); stringIds.MarkAsDeleted(IdentifierGenerator.Get(1)); var objectIds = CollectionItemIdHelper.GetCollectionItemIds(obj.Objects); objectIds["key3"] = IdentifierGenerator.Get(3); objectIds["key4"] = IdentifierGenerator.Get(4); objectIds.MarkAsDeleted(IdentifierGenerator.Get(1)); objectIds.MarkAsDeleted(IdentifierGenerator.Get(6)); var yaml = SerializeAsString(obj); Assert.AreEqual(YamlDictionaryWithDeleted, yaml); }