コード例 #1
0
ファイル: Form1.cs プロジェクト: chandoni/PicturePuzzle
        /* Initialize the board uusing the ImageBlock[,] Positions 2d array
         *
         */
        private void BoardInit()
        {
            Positions = new ImageBlock[M_ROWS, M_COLS];
            int y = 0, IncrementValue = 100;

            for (int row = 0; row < M_ROWS; row++)
            {
                for (int col = 0; col < M_COLS; col++)
                {
                    Positions[row, col] = new ImageBlock(
                        new Rectangle(IncrementValue * col, y, 100, 100),
                        new Rectangle(0, 0, 100, 100),
                        src_img
                        );
                    Positions[row, col].Location = new Point(
                        IncrementValue * col, y);
                    Positions[row, col].Size = new Size(100, 100);

                    // set initial x and y values NO OFFSET VALUES
                    // this is important because of how another method will
                    // test for a compelete puzzle
                    Positions[row, col].InitialX = IncrementValue * col;
                    Positions[row, col].InitialY = y;

                    this.Controls.Add(Positions[row, col]);
                    Positions[row, col].MouseClick += mouseClick;
                }

                y += IncrementValue;
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: chandoni/PicturePuzzle
        /* Swaps the locations of the two
         * selected instances of ImageBlock
         * within the Positions 2d array
         *
         */
        public void Swap(ImageBlock selected1, ImageBlock selected2)
        {
            // if blocks too far
            if (selected1.Location.X > selected2.Location.X + 100 ||
                selected1.Location.X < selected2.Location.X - 100 ||
                selected1.Location.Y > selected2.Location.Y + 100 ||
                selected1.Location.Y < selected2.Location.Y - 100)
            {
                // too far
                return;
            }

            // confirm that there is no diagonal, if there is then return
            // from the method immediately
            if ((selected1.Location.X + 100 == selected2.Location.X ||
                 selected1.Location.X - 100 == selected2.Location.X) &&
                (selected1.Location.Y + 100 == selected2.Location.Y ||
                 selected1.Location.Y - 100 == selected2.Location.Y))
            {
                // diagonal so return from method
                return;
            }

            // first check if blocks are adjacent
            if (selected1.Location.X + 100 == selected2.Location.X ||
                selected1.Location.X - 100 == selected2.Location.X ||
                selected1.Location.Y + 100 == selected2.Location.Y ||
                selected1.Location.Y - 100 == selected2.Location.Y)
            {
                // is adjacent
                // so swap
                selected1.IsSelected = false;
                selected2.IsSelected = false;
                Point pos1 = selected1.Location;
                Point pos2 = selected2.Location;
                selected1.Location = pos2;
                selected2.Location = pos1;

                // if upon swap you completed the puzzle then tell the user that
                // and then load the next puzzle with NextPicture()
                if (PuzzleComplete())
                {
                    MessageBox.Show("Good job! Picture is sorted!");
                    NextPicture();
                }
            }
            else
            {
                return; // is not adjacent
            }
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: chandoni/PicturePuzzle
        /* when one of the ImageBlock's are clicked on the screen
         * selected that block
         *
         * user must deselect block manually
         * ONLY ADJACENT BLOCKS SWAP!
         */
        private void mouseClick(object sender, MouseEventArgs e)
        {
            List <ImageBlock> Elements = new List <ImageBlock>();

            foreach (ImageBlock block in Positions)
            {
                Elements.Add(block);
            }
            var SelectedBlocks = Elements.Where(a => a.IsSelected).ToList();

            if (SelectedBlocks.Count == 2)
            {
                // exactly two blocks selected
                Swap(SelectedBlocks[0], SelectedBlocks[1]);
            }
            else if (SelectedBlocks.Count > 2)
            {
                ImageBlock block = (ImageBlock)sender;
                block.IsSelected = false;
            }
        }