示例#1
0
        public void ExampleTree()
        {
            squareCoords temp = new squareCoords(100, 100, 1);

            TreeNode root = new TreeNode(temp);

            TreeNode node = new TreeNode(new squareCoords(200, 200, 2));

            root.child1 = node;
            node.parent = root;

            root.child2 = new TreeNode(new squareCoords(300, 300, 3));

            root.child2.parent = root;

            root.child3 = new TreeNode(new squareCoords(300, 300, 4));

            root.child3.parent = root;

            root.child4 = new TreeNode(new squareCoords(300, 300, 5));

            root.child4.parent = root;

            root.child1.child1 = new TreeNode(new squareCoords(300, 300, 6));

            root.child1.child1.parent = root.child1;



            this.root = root;
        }
示例#2
0
        //inisialisasi array puzzle
        private void initPuzzle()
        {
            int numCounter = 0;

            for (int i = 0; i < puzzleState.GetLength(0); i++)
            {
                for (int j = 0; j < puzzleState.GetLength(1); j++)
                {
                    //isi array puzzleState dengan objek squareCoords baru dengan isi x,y 0 dan angka dari kotak
                    puzzleState[i, j] = new squareCoords(0, 0, numCounter);
                    numCounter++;
                }
            }

            //check untuk setiap button dalam gbSolution untuk button dengan nama yang mengandung angka dalam var temp
            int temp = 0;

            foreach (Button btn in gbSolution.Controls)
            {
                while (!btn.Name.Contains(temp.ToString()))
                {
                    temp++;
                }

                //copy property Left dan Top dari button yang match dengan angka dalam var temp
                //ke dalam setiap objek yang ada dalam puzzleState
                foreach (squareCoords coords in puzzleState)
                {
                    if (coords.Number == temp)
                    {
                        coords.X = btn.Left;
                        coords.Y = btn.Top;

                        //rtbLog.Text += "puzzle coords= x: " + coords.X + " y: " + coords.Y + " number: " + coords.Number + " " + temp + " \n";
                    }
                }

                temp = 0;
            }

            //visualisasi array puzzleState sebagai sebuah 8 Puzzle
            for (int i = 0; i < puzzleState.GetLength(0); i++)
            {
                rtbLog.Text += "                 ";
                for (int j = 0; j < puzzleState.GetLength(1); j++)
                {
                    rtbLog.Text += puzzleState[i, j].Number + "       ";
                }
                rtbLog.Text += "\n\n";
            }

            rtbLog.Text          += "\n\n\n";
            rtbLog.SelectionStart = rtbLog.Text.Length;
            rtbLog.ScrollToCaret();

            startState = puzzleState;
        }
示例#3
0
        //visualisasi untuk setiap pergerakan yang dilakukan
        private void visualizeMove(squareCoords currSquare, squareCoords destSquare)
        {
            //Visualisasi menggunakan button (bugged)
            foreach (Button btn in gbSolution.Controls)
            {
                if (btn.Name.Contains(currSquare.Number.ToString()))
                {
                    btn.Left = destSquare.X;
                    btn.Top  = destSquare.Y;
                }
            }
            foreach (Button btn in gbSolution.Controls)
            {
                if (btn.Name.Contains(destSquare.Number.ToString()))
                {
                    btn.Left = currSquare.X;
                    btn.Top  = currSquare.Y;
                }
            }

            //visualisasi menggunakan richtextbox
            for (int i = 0; i < puzzleState.GetLength(0); i++)
            {
                rtbLog.Text += "                 ";
                for (int j = 0; j < puzzleState.GetLength(1); j++)
                {
                    rtbLog.Text += puzzleState[i, j].Number + "       ";
                }
                rtbLog.Text += "\n\n";
            }
            rtbLog.Text += "\n\n\n";
            //set posisi cursor ke huruf terakhir dan scroll ke cursor tersebut
            //digunakan agar rich text box auto scroll ke bagian visualisasi text.
            rtbLog.SelectionStart = rtbLog.Text.Length;
            rtbLog.ScrollToCaret();
        }
示例#4
0
 public TreeNode(squareCoords data)
 {
     this.data = data;
     childs    = new TreeNode[4];
 }
示例#5
0
        //Method yang digunakan untuk menggerakan square 0 dalam array puzzleState
        //int direction -> 0=atas, 1=kanan, 2=bawah, 3=kiri
        private void move(int direction)
        {
            //dokumentasi untuk method ini hanya ada pada direction==0 karena cara kerja sama pada yang lain.
            step++;
            labelStepCount.Text = step + "";
            bool found = false;

            if (direction == 0)
            {//atas
                //var temp untuk index objek yang memiliki Property 'Number'
                //dengan value 0
                int tempIndexI = 0, tempIndexJ = 0;

                //cari square dengan angka 0 dalam array puzzleState
                for (int i = 0; i < puzzleState.GetLength(0); i++)
                {
                    for (int j = 0; j < puzzleState.GetLength(1); j++)
                    {
                        //check apakah setiap objek yang dilewati memiliki Number=0
                        if (puzzleState[i, j].Number == 0)
                        {
                            //copy index nya lalu hentikan pencarian
                            found      = true;
                            tempIndexI = i;
                            tempIndexJ = j;
                            if (debug)
                            {
                            }
                            break;
                        }
                    }
                }

                //jika square dengan Number=0 ditemukan maka found=true
                if (found)
                {
                    //check apakah objek tersebut ada di row teratas (row 0)
                    if (tempIndexI == 0)
                    {
                        if (debug)
                        {
                            //MessageBox.Show("this square is top most, cannot move up");
                            //do something
                        }
                        else
                        {
                            //do something
                        }
                    }
                    else
                    {
                        if (debug)
                        {
                            //copy isi dari kotak yang sekarang
                            squareCoords tempCurrSquare = new squareCoords(puzzleState[tempIndexI, tempIndexJ].X, puzzleState[tempIndexI, tempIndexJ].Y, puzzleState[tempIndexI, tempIndexJ].Number);

                            //copy isi dari kotak destinasi yang akan dipindah, karena ke atas maka index I (axis Y) di -1
                            squareCoords tempDestSquare = new squareCoords(puzzleState[tempIndexI - 1, tempIndexJ].X, puzzleState[tempIndexI - 1, tempIndexJ].Y, puzzleState[tempIndexI - 1, tempIndexJ].Number);

                            //tukar isi dari objek tersebut
                            puzzleState[tempIndexI, tempIndexJ]     = tempDestSquare;
                            puzzleState[tempIndexI - 1, tempIndexJ] = tempCurrSquare;

                            //jalankan visualisasi
                            visualizeMove(tempCurrSquare, tempDestSquare);
                        }
                        else
                        {
                        }
                    }
                }
            }
            else if (direction == 1)
            {//kanan
                int tempIndexI = 0, tempIndexJ = 0;

                //cari square dengan angka 0 dalam array puzzleState
                for (int i = 0; i < puzzleState.GetLength(0); i++)
                {
                    for (int j = 0; j < puzzleState.GetLength(1); j++)
                    {
                        if (puzzleState[i, j].Number == 0)
                        {
                            found      = true;
                            tempIndexI = i;
                            tempIndexJ = j;
                            if (debug)
                            {
                            }
                            break;
                        }
                    }
                }

                //jika square dengan angka 0 ditemukan maka found=true
                if (found)
                {
                    if (tempIndexJ == 2)
                    {
                        if (debug)
                        {
                            //MessageBox.Show("this square is right most, cannot move right");
                            //do something
                        }
                        else
                        {
                            //do something
                        }
                    }
                    else
                    {
                        if (debug)
                        {
                            squareCoords tempCurrSquare = new squareCoords(puzzleState[tempIndexI, tempIndexJ].X, puzzleState[tempIndexI, tempIndexJ].Y, puzzleState[tempIndexI, tempIndexJ].Number);

                            squareCoords tempDestSquare = new squareCoords(puzzleState[tempIndexI, tempIndexJ + 1].X, puzzleState[tempIndexI, tempIndexJ + 1].Y, puzzleState[tempIndexI, tempIndexJ + 1].Number);

                            puzzleState[tempIndexI, tempIndexJ]     = tempDestSquare;
                            puzzleState[tempIndexI, tempIndexJ + 1] = tempCurrSquare;

                            visualizeMove(tempCurrSquare, tempDestSquare);
                        }
                        else
                        {
                        }
                    }
                }
            }
            else if (direction == 2)
            {//bawah
                int tempIndexI = 0, tempIndexJ = 0;

                //cari square dgn angka 0
                for (int i = 0; i < puzzleState.GetLength(0); i++)
                {
                    for (int j = 0; j < puzzleState.GetLength(1); j++)
                    {
                        if (puzzleState[i, j].Number == 0)
                        {
                            found      = true;
                            tempIndexI = i;
                            tempIndexJ = j;
                            if (debug)
                            {
                                //rtbLog.Text += "square 0 is found! index X square 0: " + tempIndexI + " index Y square 0: " + tempIndexJ+" \n";
                            }
                            break;
                        }
                    }
                }

                if (found)
                {
                    if (tempIndexI == 2)
                    {
                        if (debug)
                        {
                            //MessageBox.Show("this square is bottom most, cannot move down");
                            //do something
                        }
                        else
                        {
                            //do something
                        }
                    }
                    else
                    {
                        if (debug)
                        {
                            squareCoords tempCurrSquare = new squareCoords(puzzleState[tempIndexI, tempIndexJ].X, puzzleState[tempIndexI, tempIndexJ].Y, puzzleState[tempIndexI, tempIndexJ].Number);

                            squareCoords tempDestSquare = new squareCoords(puzzleState[tempIndexI + 1, tempIndexJ].X, puzzleState[tempIndexI + 1, tempIndexJ].Y, puzzleState[tempIndexI + 1, tempIndexJ].Number);

                            puzzleState[tempIndexI + 1, tempIndexJ] = tempCurrSquare;
                            puzzleState[tempIndexI, tempIndexJ]     = tempDestSquare;

                            visualizeMove(tempCurrSquare, tempDestSquare);
                        }
                        else
                        {
                        }
                    }
                }
            }
            else if (direction == 3)
            {//kiri
                int tempIndexI = 0, tempIndexJ = 0;

                //cari square dengan angka 0 dalam array puzzleState
                for (int i = 0; i < puzzleState.GetLength(0); i++)
                {
                    for (int j = 0; j < puzzleState.GetLength(1); j++)
                    {
                        if (puzzleState[i, j].Number == 0)
                        {
                            found      = true;
                            tempIndexI = i;
                            tempIndexJ = j;
                            if (debug)
                            {
                            }
                            break;
                        }
                    }
                }

                //jika square dengan angka 0 ditemukan maka found=true
                if (found)
                {
                    if (tempIndexJ == 0)
                    {
                        if (debug)
                        {
                            //MessageBox.Show("this square is left most, cannot move left");
                            //do something
                        }
                        else
                        {
                            //do something
                        }
                    }
                    else
                    {
                        if (debug)
                        {
                            squareCoords tempCurrSquare = new squareCoords(puzzleState[tempIndexI, tempIndexJ].X, puzzleState[tempIndexI, tempIndexJ].Y, puzzleState[tempIndexI, tempIndexJ].Number);

                            squareCoords tempDestSquare = new squareCoords(puzzleState[tempIndexI, tempIndexJ - 1].X, puzzleState[tempIndexI, tempIndexJ - 1].Y, puzzleState[tempIndexI, tempIndexJ - 1].Number);

                            puzzleState[tempIndexI, tempIndexJ]     = tempDestSquare;
                            puzzleState[tempIndexI, tempIndexJ - 1] = tempCurrSquare;

                            visualizeMove(tempCurrSquare, tempDestSquare);
                        }
                        else
                        {
                        }
                    }
                }
            }
        }