private void RandomWorld() { int tmpDim = rand.Next(2, 6); rows = tmpDim; columns = tmpDim; columns = rows; rows = columns; DimValue.Text = rows.ToString(); PValue.Text = p.ToString(); world = new GridWorld(tmpDim, tmpDim, mainFrame.Width, mainFrame.Height, p); int[] coord = world.GetGoalCoord(); GoalSpaceValue.Text = ((coord[0] * rows) + coord[1]).ToString(); world.SetGoal(coord[0], coord[1]); coord = world.GetStartCoord(); StartSpaceValue.Text = ((coord[0] * rows) + coord[1]).ToString(); world.SetStart(coord[0], coord[1]); mainFrame.Child = world; TimeStepsButton.IsEnabled = false; CostLabel.Content = "Total Cost: "; SolvableLabel.Content = ""; PathText.Text = ""; timeStep = 0; }
// Sets the start space private void StartSpaceValue_TextChanged(object sender, TextChangedEventArgs e) { int startIndex; Int32.TryParse(StartSpaceValue.Text, out startIndex); int startIndex_row = startIndex / columns; int startIndex_col = startIndex % columns; world.SetStart(startIndex_row, startIndex_col); }
// If you want to run the same map repeatedly without having to put it in every time, // put the index numbers of the nontraversible spaces in the map array and set the // goal and start spaces here. // ex. you have a 5x5 with spaces A1 and B4 nontraversible, put { 0, 8 } in the map array public void InitialWorld() { //int[] map = { 2, 3, 5}; int[] map = { }; if (map.Length == 0) { RandomWorld(); } else { rows = 3; columns = 3; world = new GridWorld(rows, columns, mainFrame.Width, mainFrame.Height, map); world.SetStart(0, 0); world.SetGoal(2, 2); } }
private List <GridWorldSpace> RepeatedAStar() { int timeSteps = 0; GridWorld currWorld = new GridWorld(world.Rows, world.Rows, world.Width, world.Height); currWorld.SetStart(0, 0); currWorld.SetGoal(world.Rows - 1, world.Rows - 1); int size = (world.Rows * world.Rows) + 1; Node currNode = world.GetSpaceByCoord(world.GetStartCoord()).TreePointer; Node goalNode = world.GetSpaceByCoord(world.GetGoalCoord()).TreePointer; List <GridWorldSpace> repeatedPath = new List <GridWorldSpace>(); List <GridWorldSpace> tmpPath = new List <GridWorldSpace>(); CoordHeap openList = new CoordHeap(); while (currNode.key != goalNode.key) { repeatedPath.Add(currNode.key); currNode.key.G = 0; currNode.key.S = timeSteps; goalNode.key.G = size; goalNode.key.S = timeSteps; currNode.key.F = currNode.key.G + currNode.key.H; openList.Insert(currNode.key); tmpPath = GetPath(currWorld); currNode = openList.GetLeastF().TreePointer; timeSteps++; } cost = timeSteps; return(repeatedPath); }