Пример #1
0
        // -------------------------------------------------------------
        // -------------------------------------        EVENTS
        // -------------------------------------------------------------

        private void Cell_Click(object sender, EventArgs e)
        {
            int[]      current_cell_index = new int[2];
            Image_Cell current_cell       = (Image_Cell)sender;

            // find index of cell was clicked
            for (int i = 0; i < max; i++)
            {
                bool check = false;
                for (int j = 0; j < max; j++)
                {
                    if (image_cell_list[i][j] == current_cell)
                    {
                        current_cell_index[0] = i;
                        current_cell_index[1] = j;
                        check = true;
                        break;
                    }
                }
                if (check == true)
                {
                    break;
                }
            }
            // if cell clicked is near null cell ->> swap two cells and increase count of move
            if ((Math.Abs(current_cell_index[0] - null_cell_index[0]) + Math.Abs(current_cell_index[1] - null_cell_index[1]) == 1))
            {
                Swap_Two_Cell(ref current_cell, ref null_cell, current_cell_index);
            }
        }
Пример #2
0
        // ----------------------------------------------------------
        // ---------------------Function to swap location of two cell
        // ----------------------------------------------------------
        private void Swap_Two_Cell(ref Image_Cell current_cell, ref Image_Cell null_cell, int[] current_cell_index)
        {
            // swap test tuple
            int tmp = this.test[null_cell_index[0], null_cell_index[1]];

            this.test[null_cell_index[0], null_cell_index[1]]       = this.test[current_cell_index[0], current_cell_index[1]];
            this.test[current_cell_index[0], current_cell_index[1]] = tmp;
            // swap cell list
            null_cell.Image    = current_cell.Image;
            current_cell.Image = null;
            null_cell.Value    = current_cell.Value;
            current_cell.Value = -1;
            null_cell          = current_cell;
            null_cell_index[0] = current_cell_index[0];
            null_cell_index[1] = current_cell_index[1];
            this.move_count++;
            this.move_label.Text = "MOVED: " + this.move_count.ToString();
            // when resolve the puzzle ->> you win
            if (Check_Win() == true)
            {
                timer.Stop();
                timerAutoRun.Stop();
                MessageBox.Show("You are Winner!");
                Show_Start_Hide_Play();
            }
        }
Пример #3
0
 private void MoveRight()
 {
     if (this.canMove == true)
     {
         if (null_cell_index[1] > 0)
         {
             int[] current_cell_index = new int[2];
             current_cell_index[0] = null_cell_index[0];
             current_cell_index[1] = null_cell_index[1] - 1;
             Image_Cell current_cell = image_cell_list[current_cell_index[0]][current_cell_index[1]];
             Swap_Two_Cell(ref current_cell, ref null_cell, current_cell_index);
         }
     }
 }
Пример #4
0
 // --------------------------------------
 // -------------move when press arow keys
 // --------------------------------------
 private void MoveUp()
 {
     if (this.canMove == true)
     {
         if (null_cell_index[0] < this.max - 1)
         {
             int[] current_cell_index = new int[2];
             current_cell_index[0] = null_cell_index[0] + 1;
             current_cell_index[1] = null_cell_index[1];
             Image_Cell current_cell = image_cell_list[current_cell_index[0]][current_cell_index[1]];
             Swap_Two_Cell(ref current_cell, ref null_cell, current_cell_index);
         }
     }
 }
Пример #5
0
        //--------------------------------------------------
        //--------------------------------------------------
        //--------------------------------------------------
        private void InitializeCellArray()
        {
            timerAutoRun.Stop();
            Random_For_Test();
            // assign file from path to image
            image = Image.FromFile(path);
            int    size_cell = CONST.SIZE_ORGINAL_IMAGE / max;
            Bitmap bmp       = image as Bitmap;

            this.image_cell_list = new List <List <Image_Cell> >();
            this.move_count      = 0;
            //
            // cells
            //
            Image_Cell tmp = new Image_Cell();

            for (int i = 0; i < max; i++)
            {
                List <Image_Cell> image_cell_row = new List <Image_Cell>();
                for (int j = 0; j < max; j++)
                {
                    // assign the value for cell
                    tmp       = new Image_Cell();
                    tmp.Value = test[i, j];

                    // save index of null cell
                    if (tmp.Value == -1)
                    {
                        null_cell_index[0] = i;
                        null_cell_index[1] = j;
                        null_cell          = tmp;
                    }
                    // cut the image and assign for cell
                    else
                    {
                        int loca_x = (tmp.Value - 1) % max;
                        int loca_y = Convert.ToInt32((tmp.Value - 1) / max);
                        tmp.Image = bmp.Clone(new Rectangle(size_cell * loca_x, size_cell * loca_y, size_cell, size_cell), bmp.PixelFormat);
                    }
                    tmp.BorderStyle = BorderStyle.Fixed3D;
                    tmp.Size        = new Size(CONST.SIZE_ORGINAL_IMAGE / max, CONST.SIZE_ORGINAL_IMAGE / max);
                    tmp.Click      += new System.EventHandler(Cell_Click);
                    controls_list2.Add(tmp);
                    image_cell_row.Add(tmp);


                    // set location for cell
                    if (i == 0)
                    {
                        if (j == 0)
                        {
                            tmp.Location = new Point(CONST.SIZE_DOCK, CONST.SIZE_DOCK);
                        }
                        else
                        {
                            tmp.Location = new Point(image_cell_row[j - 1].Location.X + CONST.SIZE_ORGINAL_IMAGE / max, image_cell_row[j - 1].Location.Y);
                        }
                    }
                    else
                    {
                        tmp.Location = new Point(image_cell_list[i - 1][j].Location.X, image_cell_list[i - 1][j].Location.Y + CONST.SIZE_ORGINAL_IMAGE / max);
                    }
                }
                image_cell_list.Add(image_cell_row);
            }
            image_cell_list[max - 1][max - 1].Image = null;

            // add to this.controls
            for (int i = 0; i < max; i++)
            {
                for (int j = 0; j < max; j++)
                {
                    this.Controls.Add(image_cell_list[i][j]);
                }
            }

            // refresh some attritbute
            this.orginal_image.Image = image;
            this.move_label.Text     = "MOVED: " + move_count.ToString();
            this.time_label.Text     = "TIME: 00:00:00";
            this.timer.Start();
            this.start_time = DateTime.Now;
        }