public GridWorld(Grid grid, Cell[,] cells, int cellHeight, int cellWidth) { ROWS = cells.GetLength(0); COLUMNS = cells.GetLength(1); this.cells = cells; // Build the cells for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { // Check if this is an agent/reward if (this.cells[i, j].IsAgentStartingCell()) { this.agentStartingPosition = new int[2]; this.agentStartingPosition[0] = i; this.agentStartingPosition[1] = j; } if (this.cells[i, j].IsRewardCell()) { this.rewardPosition = new int[2]; this.rewardPosition[0] = i; this.rewardPosition[1] = j; } // Make a rectangle for the grid Rectangle rectangle = new Rectangle(); rectangle.Name = "cell" + i + j; rectangle.Height = cellHeight; rectangle.Width = cellWidth; rectangle.VerticalAlignment = VerticalAlignment.Top; rectangle.HorizontalAlignment = HorizontalAlignment.Left; rectangle.Margin = new Thickness(j * cellWidth, i * cellHeight, 0, 0); rectangle.Stroke = CELL_STROKE_COLOR; if (this.cells[i, j].IsObstacle()) { rectangle.Fill = OBSTACLE_CELL_BACKGROUND_COLOR; } else { rectangle.Fill = UNOCCUPIED_CELL_BACKGROUND_COLOR; } this.cells[i, j].SetRectangle(rectangle); grid.Children.Add(this.cells[i, j].GetRectangle()); } } }
public static void Save(Cell[,] cells) { // Verify the directory exists if (!Directory.Exists(EXPORT_DIRECTORY)) { Directory.CreateDirectory(EXPORT_DIRECTORY); } // Save a CSV file for f, g, and h using (StreamWriter file = new StreamWriter(EXPORT_DIRECTORY + @"\f.csv")) { for (int rowIndex = 0; rowIndex < cells.GetLength(0); rowIndex++) { for (int columnIndex = 0; columnIndex < cells.GetLength(1); columnIndex++) { file.Write(cells[rowIndex, columnIndex].GetFScore()); if (columnIndex == cells.GetLength(1) - 1) { file.WriteLine(); } else { file.Write(","); } } } } using (StreamWriter file = new StreamWriter(EXPORT_DIRECTORY + @"\g.csv")) { for (int rowIndex = 0; rowIndex < cells.GetLength(0); rowIndex++) { for (int columnIndex = 0; columnIndex < cells.GetLength(1); columnIndex++) { file.Write(cells[rowIndex, columnIndex].GetGScore()); if (columnIndex == cells.GetLength(1) - 1) { file.WriteLine(); } else { file.Write(","); } } } } using (StreamWriter file = new StreamWriter(EXPORT_DIRECTORY + @"\h.csv")) { for (int rowIndex = 0; rowIndex < cells.GetLength(0); rowIndex++) { for (int columnIndex = 0; columnIndex < cells.GetLength(1); columnIndex++) { file.Write(cells[rowIndex, columnIndex].GetHScore()); if (columnIndex == cells.GetLength(1) - 1) { file.WriteLine(); } else { file.Write(","); } } } } }
public void SetParent(Cell parent) { this.parent = parent; }
/// <summary> /// Gets the cell in the specified direction from the provided starting cell /// </summary> /// <param name="startingCell">The starting cell</param> /// <param name="direction">The direction to get the cell</param> /// <returns></returns> public Cell GetCell(Cell startingCell, Direction direction) { if (direction == Direction.UP) { return GetCells()[startingCell.GetRowIndex() - 1, startingCell.GetColumnIndex()]; } else if (direction == Direction.DOWN) { return GetCells()[startingCell.GetRowIndex() + 1, startingCell.GetColumnIndex()]; } else if (direction == Direction.LEFT) { return GetCells()[startingCell.GetRowIndex(), startingCell.GetColumnIndex() - 1]; } else if (direction == Direction.RIGHT) { return GetCells()[startingCell.GetRowIndex(), startingCell.GetColumnIndex() + 1]; } else { throw new Exception("Unknown direction encountered."); } }
private void LoadGridButton_Click(object sender, RoutedEventArgs e) { // Create OpenFileDialog OpenFileDialog fileDialog = new OpenFileDialog(); // Set the filter to CSV fileDialog.DefaultExt = ".csv"; fileDialog.Filter = "CSV Files (*.csv)|*.*"; Nullable<bool> result = fileDialog.ShowDialog(); // Get the selected filename if (result == true) { // Setup the grid world gridWorld = new GridWorld(grid, GridMapParser.Parse(fileDialog.FileName), CELL_HEIGHT, CELL_WIDTH); // Add click events for the rectangles foreach (Cell cell in gridWorld.GetCells()) { cell.GetRectangle().MouseLeftButtonDown += new MouseButtonEventHandler(MoveAgent); } // Setup the agent if (gridWorld.GetAgentStartingPosition() != null && gridWorld.GetAgentStartingPosition().Length == 2) { int agentRowIndex = gridWorld.GetAgentStartingPosition()[0], agentColumnIndex = gridWorld.GetAgentStartingPosition()[1]; gridWorld.SetAgent(new Agent(grid, CELL_HEIGHT, CELL_WIDTH, agentRowIndex, agentColumnIndex)); // Get the starting cell startingCell = gridWorld.GetCells()[agentRowIndex, agentColumnIndex]; } else { MessageBox.Show("Error: The agent starting position must be specified in the grid map file with the number 1. Please correct and try again."); return; } // Setup the reward if (gridWorld.GetRewardPosition() != null && gridWorld.GetRewardPosition().Length == 2) { int rewardRowIndex = gridWorld.GetRewardPosition()[0], rewardColumnIndex = gridWorld.GetRewardPosition()[1]; gridWorld.SetReward(new Reward(grid, CELL_HEIGHT, CELL_WIDTH, rewardRowIndex, rewardColumnIndex)); // Get the reward cell rewardCell = gridWorld.GetCells()[rewardRowIndex, rewardColumnIndex]; } else { MessageBox.Show("Error: The reward starting position must be specified in the grid map file with the number 2. Please correct and try again."); return; } } // Make the start button active StartButton.IsEnabled = true; ResetButton.IsEnabled = true; }
/// <summary> /// Parses a grid map CSV file /// </summary> /// <param name="fileName">The full or relative path of the well-formed CSV file</param> /// <returns>The cell 2D array</returns> public static Cell[,] Parse(string fileName) { Cell[,] cells = null; try { int rowCounter = 0; int previousLineCellCount = 0; // Change the extension to CSV and read the lines string[] lines = File.ReadAllLines(System.IO.Path.ChangeExtension(fileName, ".csv")); cells = new Cell[lines.GetLength(0), lines.GetLength(0)]; foreach (string line in lines) { string[] splitLine = line.Split(','); for (int i = 0; i < splitLine.Length; i++) { bool isObstacle = false; bool isAgentStartingCell = false; bool isRewardCell = false; if (splitLine[i].Contains(((int)GridMapCodes.OBSTACLE).ToString())) { isObstacle = true; } else if (splitLine[i].Contains(((int)GridMapCodes.AGENT).ToString())) { isAgentStartingCell = true; } else if (splitLine[i].Contains(((int)GridMapCodes.REWARD).ToString())) { isRewardCell = true; } cells[rowCounter, i] = new Cell(rowCounter, i, isObstacle, isAgentStartingCell, isRewardCell); } if (rowCounter > 0 && splitLine.Length != previousLineCellCount && rowCounter <= previousLineCellCount) { throw new FormatException("Error: Uneven row/column size(s) were found in the CSV file. Please correct and try again."); } else { rowCounter++; previousLineCellCount = splitLine.Length; } } } catch (FormatException e) { MessageBox.Show(e.Message); } catch (Exception e) { MessageBox.Show("Unable to parse the grid map. Please verify the file is correct and accessible."); } return cells; }