Example #1
0
        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;
        }
Example #2
0
        /// <summary>
        /// Moves this agent in the grid world
        /// </summary>
        /// <param name="grid">The grid world</param>
        /// <param name="direction">The Direction to move towards</param>
        /// <returns>true if the move was successful; false otherwise</returns>
        public bool Move(GridWorld gridWorld, Direction direction)
        {
            // Move in the provided direction if there is no obstacle in that direction
            if (gridWorld.CanMove(direction, rowIndex, columnIndex))
            {
                switch (direction)
                {
                case Direction.UP:
                    rowIndex--;
                    break;

                case Direction.DOWN:
                    rowIndex++;
                    break;

                case Direction.LEFT:
                    columnIndex--;
                    break;

                case Direction.RIGHT:
                    columnIndex++;
                    break;
                }

                UpdatePosition();

                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Moves this agent in the grid world
        /// </summary>
        /// <param name="grid">The grid world</param>
        /// <param name="direction">The Direction to move towards</param>
        /// <returns>true if the move was successful; false otherwise</returns>
        public bool Move(GridWorld gridWorld, Direction direction)
        {
            // Move in the provided direction if there is no obstacle in that direction
            if (gridWorld.CanMove(direction, rowIndex, columnIndex))
            {
                switch (direction)
                {
                    case Direction.UP:
                        rowIndex--;
                        break;
                    case Direction.DOWN:
                        rowIndex++;
                        break;
                    case Direction.LEFT:
                        columnIndex--;
                        break;
                    case Direction.RIGHT:
                        columnIndex++;
                        break;
                }

                UpdatePosition();

                return true;
            }

            return false;
        }
        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;
        }