Exemplo n.º 1
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            //memastikan richTextBox tidak ada isinya
            richTextBoxAnswer.Text = "";
            richTextBoxQuery.Text  = "";
            richTextBoxDFS.Text    = "";
            richTextBoxPath.Text   = "";

            MapReader Peta = new MapReader("Map.txt");

            Peta.Parse();

            QueryHandler Ask = new QueryHandler(textBoxQueryFile.Text);

            Ask.Parse();

            // memberi level pada semua rumah
            List <int> path = new List <int>();

            DFS_Graph.TentukanLevel(1, 0, path, Peta.Map);

            for (int i = 1; i <= Ask.Query_Count; i++)
            {
                richTextBoxQuery.Text += i + ") " + Ask.ResultString[i] + "\n";
                List <int> trimmedRoute;

                // DFS
                int        DekatJauh      = Ask.Queries[i - 1].Item1;
                int        TempatJose     = Ask.Queries[i - 1].Item2;
                int        TempatFerdiant = Ask.Queries[i - 1].Item3;
                List <int> route          = new List <int>();
                bool       Jawaban;

                if (DekatJauh == 0)
                {
                    if ((TempatJose <= Peta.House_Count) && (TempatFerdiant <= Peta.House_Count))
                    {
                        DFS_Graph.DFS(0, 1, TempatFerdiant, route, Peta.Map);
                    }
                    // mencari jawaban
                    trimmedRoute            = DFS_Graph.Trim(route);
                    Jawaban                 = trimmedRoute.Contains <int>(TempatJose);
                    richTextBoxAnswer.Text += i + ")";
                    if (Jawaban)
                    {
                        richTextBoxAnswer.Text += " YA\n";
                    }
                    else
                    {
                        richTextBoxAnswer.Text += " TIDAK\n";
                    }
                }
                else if (DekatJauh == 1)
                {
                    if ((TempatJose <= Peta.House_Count) && (TempatFerdiant <= Peta.House_Count))
                    {
                        foreach (var vert in Peta.Map.Vertices)
                        {
                            if (vert.NomorRumah == TempatFerdiant)
                            {
                                int startLevel = vert.Level;
                                DFS_Graph.DFS(startLevel, TempatFerdiant, TempatJose, route, Peta.Map);
                            }
                        }
                    }
                    // mencari jawaban
                    trimmedRoute            = DFS_Graph.Trim(route);
                    Jawaban                 = trimmedRoute.Contains <int>(TempatJose);
                    richTextBoxAnswer.Text += i + ")";
                    if (Jawaban)
                    {
                        richTextBoxAnswer.Text += " YA\n";
                    }
                    else
                    {
                        richTextBoxAnswer.Text += " TIDAK\n";
                    }
                }
                else
                {
                    richTextBoxAnswer.Text += ("Input tidak valid.\n");
                    return;
                }

                // print rute DFS
                richTextBoxDFS.Text += i + ")";
                if (route.Count() != 0)
                {
                    foreach (int member in route)
                    {
                        richTextBoxDFS.Text += " -> " + member.ToString();
                    }
                    richTextBoxDFS.Text += "\n\n";
                }
                else
                {
                    richTextBoxDFS.Text += " Tidak ada penelusuran DFS.\n\n";
                }


                // print rute jawaban yang benar jika ada
                richTextBoxPath.Text += i + ")";
                if (Jawaban)
                {
                    foreach (int member in trimmedRoute)
                    {
                        richTextBoxPath.Text += " -> " + member.ToString();
                    }
                    richTextBoxPath.Text += "\n\n";
                }
                else
                {
                    richTextBoxPath.Text += " Tidak ada jalur solusi\n\n";
                }
            }
        }
Exemplo n.º 2
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            //memastikan richTextBox kosong
            richTextBoxAnswer.Text = "";
            richTextBoxDFS.Text    = "";
            richTextBoxPath.Text   = "";

            //string dari input pada textbox
            String input = textBoxInput.Text;

            string[] temp = input.Split();

            MapReader Peta = new MapReader("Map.txt");

            Peta.Parse();

            // memberi level pada semua rumah
            List <int> path = new List <int>();

            DFS_Graph.TentukanLevel(1, 0, path, Peta.Map);

            // DFS
            int        DekatJauh      = int.Parse(temp[0]);
            int        TempatJose     = int.Parse(temp[1]);
            int        TempatFerdiant = int.Parse(temp[2]);
            List <int> route          = new List <int>();
            bool       Jawaban;

            if (DekatJauh == 0)
            {
                if ((TempatJose <= Peta.House_Count) && (TempatFerdiant <= Peta.House_Count))
                {
                    DFS_Graph.DFS(0, 1, TempatFerdiant, route, Peta.Map);
                }
            }
            else if (DekatJauh == 1)
            {
                if ((TempatJose <= Peta.House_Count) && (TempatFerdiant <= Peta.House_Count))
                {
                    foreach (var vert in Peta.Map.Vertices)
                    {
                        if (vert.NomorRumah == TempatFerdiant)
                        {
                            int startLevel = vert.Level;
                            DFS_Graph.DFS(startLevel, TempatFerdiant, TempatJose, route, Peta.Map);
                        }
                    }
                }
            }
            else
            {
                richTextBoxPath.Text = ("Input tidak valid.");
                return;
            }

            // mencari jawaban
            List <int> trimmedRoute = DFS_Graph.Trim(route);

            Jawaban = trimmedRoute.Contains <int>(TempatJose);
            if (Jawaban)
            {
                richTextBoxAnswer.Text = "YA";
            }
            else
            {
                richTextBoxAnswer.Text = "TIDAK";
            }

            // print rute DFS
            foreach (int member in route)
            {
                richTextBoxDFS.Text += " -> " + member.ToString();
            }

            // print rute jawaban yang benar jika ada
            if (Jawaban)
            {
                foreach (int member in trimmedRoute)
                {
                    richTextBoxPath.Text += " -> " + member.ToString();
                }
            }
        }