Exemple #1
0
        public void convertAdjToMatrix()
        {
            matrix = new BitArray[adj.Count];
            for (int i = 0; i < matrix.Length; i++)
            {
                matrix[i] = new BitArray(columns * rows);
            }

            int k = 0, l = 0;

            for (int i = 0; i < adj.Count(); i++)
            {
                for (int j = 0; j < adj.Count(); j++)
                {
                    if (l < adj[k].Count())
                    {
                        if (i == k && j == adj[k][l] - 1)
                        {
                            matrix[i][j] = true;
                            l++;
                        }
                        else
                        {
                            matrix[i][j] = false;
                        }
                    }
                    else
                    {
                        matrix[i][j] = false;
                    }
                }

                k++;
                l = 0;
            }

            recalculateNodes(columns, rows);
            grid = new NodeGrid(columns, rows, nodes);
            grid.setWalls(matrix);

            matrixUpToDate = true;
        }
Exemple #2
0
        private void OpenFile(object sender, RoutedEventArgs e)
        {
            if (simInProgress)
            {
                MessageBox.Show("Nie można wykonać operacji w trakcie symulacji.", "Trwa symulacja", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".txt";
            dlg.Filter     = "Dokument tekstowy (*.txt)|*.txt";

            // Display OpenFileDialog by calling ShowDialog method
            Nullable <bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {
                // Open document
                string   filename = dlg.FileName;
                string[] readText = File.ReadAllLines(filename);
                adj = new List <List <int> >();

                for (int i = 0; i < readText.Count(); i++)
                {
                    if (i == 0)
                    {
                        string[] values = readText[0].Split(' ');
                        int.TryParse(values[0], out columns);
                        int.TryParse(values[1], out rows);
                        int.TryParse(values[2], out startNode);
                        int.TryParse(values[3], out endNode);
                    }
                    else
                    {
                        string[] values = readText[i].Split(' ');

                        List <int> ln = new List <int>(values.Count());

                        for (int j = 0; j < values.Count(); j++)
                        {
                            int temp;
                            int.TryParse(values[j], out temp);
                            ln.Add(temp);
                        }

                        adj.Add(ln);
                    }
                }


                nodes          = new List <List <Node> >();
                selectedNode   = null;
                editMode       = false;
                matrixUpToDate = true;
                simInProgress  = false;

                nodes = new List <List <Node> >();
                recalculateNodes(columns, rows);
                grid   = new NodeGrid(columns, rows, nodes);
                matrix = new BitArray[columns * rows];

                for (int i = 0; i < matrix.Length; i++)
                {
                    matrix[i] = new BitArray(columns * rows);
                }

                noData = false;

                convertAdjToMatrix();
                drawLabyrinth();
            }
        }
Exemple #3
0
        private void NewFile(object sender, RoutedEventArgs e)
        {
            if (simInProgress)
            {
                MessageBox.Show("Nie można wykonać operacji w trakcie symulacji.", "Trwa symulacja", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            NewLabyrinth dlg = new NewLabyrinth();

            dlg.Owner = this;

            if (dlg.ShowDialog() == true)
            {
                columns = dlg.columns;
                rows    = dlg.rows;
                bool random = dlg.random;

                if ((columns <= 0 || rows <= 0) || (columns <= 1 && rows <= 1))
                {
                    MessageBox.Show("Labirynt jest zbyt mały.", "Błąd dodawania", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                if ((columns * rows > sizeLimit && sizeLimit > 0) || rows > 9999 || columns > 9999)
                {
                    MessageBox.Show("Labirynt jest zbyt duży.", "Błąd dodawania", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                int    prog    = 0;
                double progmax = columns * rows;

                startNode = 1;
                endNode   = columns * rows;

                nodes = new List <List <Node> >();
                recalculateNodes(columns, rows);
                grid   = new NodeGrid(columns, rows, nodes);
                matrix = new BitArray[columns * rows];

                for (int i = 0; i < matrix.Length; i++)
                {
                    matrix[i] = new BitArray(columns * rows);
                }

                BarStatus.Value = 0;
                if (random)
                {
                    BarKroki.Text = "1/3";
                }
                else
                {
                    BarKroki.Text = "1/2";
                }
                BarOperacja.Text = "Generowanie macierzy";
                PopupBar.IsOpen  = true;

                for (int i = 0; i < rows * columns; i++)
                {
                    for (int j = 0; j < rows * columns; j++)
                    {
                        if (grid.areNeighbours(i + 1, j + 1))
                        {
                            matrix[i][j] = true;
                        }
                        else
                        {
                            matrix[i][j] = false;
                        }
                    }

                    prog = (int)(((i + 1) / progmax) * 100);
                    BarStatus.Dispatcher.Invoke(() => BarStatus.Value = prog, DispatcherPriority.Background);
                }

                PopupBar.IsOpen = false;

                if (random)
                {
                    BarStatus.Value  = 0;
                    BarKroki.Text    = "2/3";
                    BarOperacja.Text = "Losowanie ścian";
                    PopupBar.IsOpen  = true;

                    makeRandomMatrix();

                    PopupBar.IsOpen = false;
                    BarKroki.Text   = "3/3";
                }
                else
                {
                    BarKroki.Text = "2/2";
                }

                grid.setWalls(matrix);
                convertMatrixToAdj();

                selectedNode   = null;
                noData         = false;
                matrixUpToDate = false;
                simInProgress  = false;

                lblColumns.Content = columns.ToString();
                lblRows.Content    = rows.ToString();

                canvas1.Width  = columns * 20;
                canvas1.Height = rows * 20;
                drawLabyrinth();
            }
        }