public MainWindow() { InitializeComponent(); //Create the map mapSpace = new Map(54, 54); //Create asynchornous task worker.DoWork += worker_DoWork; drawMap(); }
//Runs one iteration of the algorithm public void run(ref Map map) { //Get end position coordinates if (endPos == null) { for (int x = 0; x < map.width; x++) { for (int y = 0; y < map.height; y++) { if (map.cellArray[x, y].state == Cell.CellStates.OBJECTIVE) { endPos = new Tuple<int, int>(x, y); } } } } //Insert new possible cells for (int x = 0; x < map.width; x++) { for (int y = 0; y < map.height; y++) { if (map.cellArray[x, y].state == Cell.CellStates.ROBOT) { currPos = new Tuple<int, int>(x, y); if (x - 1 >= 0 && map.cellArray[x - 1, y].state == Cell.CellStates.FREE && !inBag(x - 1, y)) possibleCells.Add(map.cellArray[x - 1, y]); if (x + 1 <= map.cellArray.GetUpperBound(0) && map.cellArray[x + 1, y].state == Cell.CellStates.FREE && !inBag(x + 1, y)) possibleCells.Add(map.cellArray[x + 1, y]); if (y - 1 >= 0 && map.cellArray[x, y - 1].state == Cell.CellStates.FREE && !inBag(x, y - 1)) possibleCells.Add(map.cellArray[x, y - 1]); if (y + 1 <= map.cellArray.GetUpperBound(1) && map.cellArray[x, y + 1].state == Cell.CellStates.FREE && !inBag(x, y + 1)) possibleCells.Add(map.cellArray[x, y + 1]); } } } //weight cells //heuristic function is: distanceToTarget + moveThereCost weigthCells.Clear(); foreach (Cell c in possibleCells) { int distanceToTarget = Map.manhattanCellToPos(c, endPos); int moveThereCost = Map.manhattanCellToPos(c, currPos); weigthCells.Add(new Tuple<Cell, int>(c, distanceToTarget + moveThereCost)); } //select next int minimamWeight = 10000; Cell selectedCell = null; foreach (Tuple<Cell, int> c in weigthCells) { if (c.Item2 < minimamWeight) { minimamWeight = c.Item2; selectedCell = c.Item1; } } map.cellArray[selectedCell.x, selectedCell.y].state = Cell.CellStates.ROBOT; possibleCells.Remove(selectedCell); map.cellArray[currPos.Item1, currPos.Item2].state = Cell.CellStates.USED; }