///////////////////// Virtual move function //////////////////////////// private bool[,] CreateAstarMap() { bool[,] mp = new bool[MapPtr.RowCount, MapPtr.ColCount]; Cell tail = this.GetTail(); for (int row = 0; row < MapPtr.RowCount; row++) { for (int col = 0; col < MapPtr.ColCount; col++) { Cell cell = MapPtr.GetCell(row, col); // the Tail is can PASSED! mp[row, col] = IsMovedCellValid(cell); } } return(mp); }
public string PrintAllCells() { StringBuilder sb = new StringBuilder(); sb.Append("\r\n"); for (int row = 0; row < MapPtr.RowCount; row++) { for (int col = 0; col < MapPtr.ColCount; col++) { Cell cell = MapPtr.GetCell(row, col); if (InBody(cell)) { if (cell.IsSamePostion(GetHead())) { sb.Append(" @"); } else if (cell.IsSamePostion(GetTail())) { sb.Append(" o"); } else { sb.Append(" #"); } } else if (cell.IsFood()) { sb.Append(" F"); } else if (cell.IsHinder()) { sb.Append(" H"); } else { sb.Append(" _"); } } sb.Append("\r\n"); } Console.Write(sb.ToString()); return(sb.ToString()); }
public List <Cell> GetMovableCells(int row, int col) { List <Cell> cells = new List <Cell>(); // left Cell left = MapPtr.GetCell(row, col - 1); if (IsMovedCellValid(left)) { cells.Add(left); } // right Cell right = MapPtr.GetCell(row, col + 1); if (IsMovedCellValid(right)) { cells.Add(right); } // up Cell up = MapPtr.GetCell(row - 1, col); if (IsMovedCellValid(up)) { cells.Add(up); } // down Cell down = MapPtr.GetCell(row + 1, col); if (IsMovedCellValid(down)) { cells.Add(down); } return(cells); }
private Cell Point2MapCell(Point pt) { return(MapPtr.GetCell(pt.X, pt.Y)); }
public List <Cell> GetGoalMovableCells(Cell cur, Cell goal) { List <Cell> cells = new List <Cell>(); Cell left, right, up, down; int cntLeft = 0; int cntRight = 0; int cntUp = 0; int cntDown = 0; int step = 5; int disLeft = int.MaxValue; int disRight = int.MaxValue; int disUp = int.MaxValue; int disDown = int.MaxValue; // left left = MapPtr.GetCell(cur.Row, cur.Col - 1); if (IsMovedCellValid(left)) { cntLeft = GetMovableCellsCountStep(left.Row, left.Col, step); disLeft = GetCellsDistance(left, goal); } // right right = MapPtr.GetCell(cur.Row, cur.Col + 1); if (IsMovedCellValid(right)) { cntRight = GetMovableCellsCountStep(right.Row, right.Col, step); disRight = GetCellsDistance(right, goal); } // up up = MapPtr.GetCell(cur.Row - 1, cur.Col); if (IsMovedCellValid(up)) { cntUp = GetMovableCellsCountStep(up.Row, up.Col, step); disUp = GetCellsDistance(up, goal); } // down down = MapPtr.GetCell(cur.Row + 1, cur.Col); if (IsMovedCellValid(down)) { cntDown = GetMovableCellsCountStep(down.Row, down.Col, step); disDown = GetCellsDistance(down, goal); } int nMax = Math.Max(cntLeft, cntRight); nMax = Math.Max(nMax, cntUp); nMax = Math.Max(nMax, cntDown); if (nMax == 0) { return(cells); } int nMin = Math.Min(disLeft, disRight); nMin = Math.Min(nMin, disUp); nMin = Math.Min(nMin, disDown); if (disLeft == nMin && cntLeft == nMax) { cells.Add(left); } if (disRight == nMin && cntRight == nMax) { cells.Add(right); } if (disUp == nMin && cntUp == nMax) { cells.Add(up); } if (disDown == nMin && cntDown == nMax) { cells.Add(down); } if (cells.Count == 0) { if (disLeft == nMin) { cells.Add(left); } if (disRight == nMin) { cells.Add(right); } if (disUp == nMin) { cells.Add(up); } if (disDown == nMin) { cells.Add(down); } } if (cells.Count == 0) { if (cntLeft == nMax) { cells.Add(left); } if (cntRight == nMax) { cells.Add(right); } if (cntUp == nMax) { cells.Add(up); } if (cntDown == nMax) { cells.Add(down); } } return(cells); }
public List <Cell> GetWidelyMovableCells(int row, int col) { List <Cell> cells = new List <Cell>(); Cell left, right, up, down; int cntLeft = 0; int cntRight = 0; int cntUp = 0; int cntDown = 0; int step = 5; // left left = MapPtr.GetCell(row, col - 1); if (IsMovedCellValid(left)) { cntLeft = GetMovableCellsCountStep(left.Row, left.Col, step); } // right right = MapPtr.GetCell(row, col + 1); if (IsMovedCellValid(right)) { cntRight = GetMovableCellsCountStep(right.Row, right.Col, step); } // up up = MapPtr.GetCell(row - 1, col); if (IsMovedCellValid(up)) { cntUp = GetMovableCellsCountStep(up.Row, up.Col, step); } // down down = MapPtr.GetCell(row + 1, col); if (IsMovedCellValid(down)) { cntDown = GetMovableCellsCountStep(down.Row, down.Col, step); } int nMax = Math.Max(cntLeft, cntRight); nMax = Math.Max(nMax, cntUp); nMax = Math.Max(nMax, cntDown); if (cntLeft == nMax) { cells.Add(left); } if (cntRight == nMax) { cells.Add(right); } if (cntUp == nMax) { cells.Add(up); } if (cntDown == nMax) { cells.Add(down); } return(cells); }