public Cell AStarMove2Goal(Cell goal) { List <Point> pathGoal = null; List <Point> pathTail = null; pathGoal = GetPath2Goal(goal); if (pathGoal.Count > 0) { Serpent vsp = new Serpent(this); vsp.VirtualMoveByPath(pathGoal); Cell vtail = vsp.GetTail(); pathTail = vsp.GetPath2Goal(vtail); if (pathTail.Count == 0) { return(AStarMove2Tail(goal)); } else { return(Point2MapCell(pathGoal[0])); } } else { return(AStarMove2Tail(goal)); } }
private SerpentState CheckSerpentMove(Cell cell) { if (!serpent.IsMovedCellValid(cell)) { return(SerpentState.Error); } //serpent.PrintAllCells(); serpent.InsertCell(cell); if (Global.DisplaySerpentItemNumber) { map.DrawSerpentCells(serpent); } else { map.DrawSerpentCell(serpent, 0); map.DrawSerpentCell(serpent, 1); //map.DrawSerpentCell(serpent, 2); } if (!cell.IsFood()) { Cell tail = serpent.GetTail(); if (!tail.IsSamePostion(cell)) { map.RedrawMapCell(tail.Row, tail.Col); } else { map.DrawSerpentCell(serpent, 0); } serpent.RemoveTail(); map.DrawSerpentCell(serpent, serpent.Body.Count - 1); //map.DrawSerpentCells(serpent.Body); } else { lblSerpentCount.BeginInvoke(new MethodInvoker(delegate() { this.lblSerpentCount.Text = (Convert.ToInt32(this.lblSerpentCount.Text.Trim()) + 1).ToString(); })); cell.Kind = CellKind.Normal; //Console.WriteLine("When eat food"); //serpent.PrintAllCells(); if (!map.SetFood(serpent)) { return(SerpentState.Finish); } //Console.WriteLine("\nafter set food"); //serpent.PrintAllCells(); } return(SerpentState.Moving); }
public Cell AStarMove2Tail(Cell goal) { Cell head = GetHead(); // Get the list of movable cells List <Cell> nextCells = GetMovableCells(head.Row, head.Col); if (nextCells.Count == 0) { return(null); } List <Cell> tails = new List <Cell>(); // If moved cell can find path to tail, add to the tail list foreach (Cell cell in nextCells) { Serpent vsp = new Serpent(this); vsp.VirtualMoveCell(cell); Cell tail = vsp.GetTail(); if (vsp.GetPath2Goal(tail).Count > 0) { tails.Add(cell); } } // If not cell can find tail then check the nextcells if (tails.Count == 0) { tails.AddRange(nextCells); } // Get farthest cell to goal int dis0 = GetCellsDistance(tails[0], goal); Cell cellGot = tails[0]; for (int i = 1; i < tails.Count; i++) { int disloop = GetCellsDistance(tails[i], goal); if (disloop > dis0) { dis0 = disloop; cellGot = tails[i]; } } return(cellGot); }