private void ClearPreviousAgents(UIGridTile currentAgentPosition)
        {
            int r = 0;

            foreach (ObservableCollection <UIGridTile> row in LearningGrid.ItemsSource)
            {
                int c = 0;
                foreach (UIGridTile tile in row)
                {
                    if (tile.Agent)
                    {
                        if (tile != currentAgentPosition)
                        {
                            tile.Agent = false;
                        }
                        else
                        {
                            agentPos = new int[] { c, r };
                        }
                    }
                    c++;
                }
                r++;
            }
        }
        private void SetExit(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;

            UIGridTile tile = ((ObservableCollection <UIGridTile>)button.DataContext)
                              [((button.Parent as System.Windows.Controls.Grid).TemplatedParent as DataGridCell).TabIndex];

            tile.Exit = !tile.Exit;

            button.Background = tile.Exit ? Brushes.DarkSeaGreen : Brushes.LightGray;

            if (tile.Exit)
            {
                if (lastAgentPositionIndicator != null)
                {
                    this.train.IsEnabled = true;
                }
                exitPositionIndicator.Add(button);
            }
            else
            {
                exitPositionIndicator.Remove(button);
                this.train.IsEnabled = exitPositionIndicator.ToArray().Length != 0;
            }
        }
        private void SetAgent(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;

            UIGridTile tile = ((ObservableCollection <UIGridTile>)button.DataContext)
                              [((button.Parent as System.Windows.Controls.Grid).TemplatedParent as DataGridCell).TabIndex];

            tile.Agent = !tile.Agent;

            button.Background = tile.Agent ? Brushes.DarkSlateBlue : Brushes.LightGray;

            if (tile.Agent)
            {
                ClearPreviousAgents(tile);
                if (lastAgentPositionIndicator != null)
                {
                    lastAgentPositionIndicator.Background = Brushes.LightGray;
                }

                if (exitPositionIndicator != null)
                {
                    this.train.IsEnabled = true;
                }
                lastAgentPositionIndicator = button;
            }
            else
            {
                lastAgentPositionIndicator = null;
                this.train.IsEnabled       = false;
            }
        }
        private void ToggleWall(Button cellButton, PossibleDirections wallPosition)
        {
            this.act.IsEnabled = false;
            UIGridTile tile = ((ObservableCollection <UIGridTile>)cellButton.DataContext)
                              [((cellButton.Parent as System.Windows.Controls.Grid).TemplatedParent as DataGridCell).TabIndex];

            if (tile.Wall == wallPosition)
            {
                tile.Wall             = null;
                cellButton.Background = Brushes.LightGray;
            }
            else
            {
                tile.Wall             = wallPosition;
                cellButton.Background = Brushes.IndianRed;
                ClearWallsInCell((System.Windows.Controls.Grid)cellButton.Parent, cellButton);
                ClearWallsInCellsAround((System.Windows.Controls.Grid)cellButton.Parent);
            }
        }