示例#1
0
        public void BindAStar()
        {
            // Made this way so it runs on the UI Thread, always.
            XNAWindow.BeginInvoke(new UIDelegate(() =>
            {
                XNAWindow.AStar = _astar;

                if (_astar != null)
                {
                    ListOpen.DataSource  = null;
                    ListOpen.DataSource  = _astar.Open;
                    ListClose.DataSource = null;
                    ListClose.DataSource = _astar.Close;
                    if (_astar.Current != null)
                    {
                        TextCurrent.Text = _astar.Current.ToString();
                    }
                    else
                    {
                        TextCurrent.Text = String.Empty;
                    }

                    this.Invalidate();
                }
            }));
        }
示例#2
0
        public void LoadMap()
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            dialog.Filter           = "Text Files (*.txt)|*.txt";
            dialog.RestoreDirectory = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (Stream fileStream = dialog.OpenFile())
                    {
                        if (fileStream != null)
                        {
                            TextReader reader = new StreamReader(fileStream);
                            string     text   = reader.ReadToEnd();
                            string[]   split  = text.Split('\n');

                            char[,] result = new char[split[0].Length - 1, split.Length]; //Not counting \r

                            for (int i = 0; i < split.Length; i++)
                            {
                                char[] characters = split[i].TrimEnd('\r').ToCharArray();
                                for (int j = 0; j < characters.Length; j++)
                                {
                                    result[j, i] = characters[j];
                                }
                            }

                            _astar = new Algorithm.AStar(result.GetLength(0), result.GetLength(1));
                            _astar.LoadFromArray(result);

                            _astar.OnStepChanged = new StepChangedDelegate((state) =>
                            {
                                if (_stopAtStep)
                                {
                                    _semaphore.WaitOne();
                                }
                                else
                                {
                                    Thread.Sleep(_sleepInterval);
                                }

                                XNAWindow.BeginInvoke(new UIDelegate(() =>
                                {
                                    ListNextStep.SelectedIndex = (int)state;
                                    BindAStar();
                                }));
                            });

                            BindAStar();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error reading file from disk. Error: " + ex.Message);
                }
            }
        }