Exemplo n.º 1
0
        public void MovePiece(Tile t)
        {
            int row = t.Row;
            int col = t.Col;

            if (t.Row == missingPiece / cols)
            {
                // Move all pieces in same row with column less than missing piece to the right
                if (t.Col < missingPiece % cols)
                {
                    for (int i = 0; i < pieces; i++)
                    {
                        if (t.Row == tiles[i].Row && tiles[i].Col < missingPiece % cols && tiles[i].Col >= t.Col)
                        {
                            tiles[i].Col++;
                            tiles[i].Location = new Point(tiles[i].Location.X + imgSize.Width / cols, tiles[i].Location.Y);
                        }
                    }
                }
                
                // Move all pieces in same row with column greater than missing piece to the left
                else 
                {
                    for (int i = 0; i < pieces; i++)
                    {
                        if (t.Row == tiles[i].Row && tiles[i].Col > missingPiece % cols && tiles[i].Col <= t.Col)
                        {
                            tiles[i].Col--;
                            tiles[i].Location = new Point(tiles[i].Location.X - imgSize.Width / cols, tiles[i].Location.Y);
                        }
                    }
                }

                missingPiece = row * cols + col;
            }

            else if (t.Col == missingPiece % cols)
            {
                // Move all pieces in same col with row less than missing piece down
                if (t.Row < missingPiece / cols)
                {
                    for (int i = 0; i < pieces; i++)
                    {
                        if (t.Col == tiles[i].Col && tiles[i].Row < missingPiece / cols && tiles[i].Row >= t.Row)
                        {
                            tiles[i].Row++;
                            tiles[i].Location = new Point(tiles[i].Location.X, tiles[i].Location.Y + imgSize.Height / rows);
                        }
                    }
                }

                // Move all pieces in same col with row greater than missing piece up
                else
                {
                    for (int i = 0; i < pieces; i++)
                    {
                        if (t.Col == tiles[i].Col && tiles[i].Row > missingPiece / cols && tiles[i].Row <= t.Row)
                        {
                            tiles[i].Row--;
                            tiles[i].Location = new Point(tiles[i].Location.X, tiles[i].Location.Y - imgSize.Height / rows);
                        }
                    }
                }

                missingPiece = row * cols + col;
            }
        }        
Exemplo n.º 2
0
        public void SetBackgroundImage()
        {
            solver.Abort();
            buttonTimer.Stop();
            solveButton.Text = "Solve";

            for (int i = 0; i < pieces; i++)
            {
                tiles[i].Parent = null;
            }

            Bitmap bmpImage = new Bitmap(puzzleImage, ResizeImage());
            Rectangle cropArea = new Rectangle(0, 0, bmpImage.Width / cols * cols, bmpImage.Height / rows * rows);
            bmpImage = bmpImage.Clone(cropArea, bmpImage.PixelFormat);            
            
            imgSize = bmpImage.Size;
            Size = new Size(imgSize.Width + 20, imgSize.Height + 62);
            pieces = rows * cols - 1;
            missingPiece = pieces; // last square is empty initially
            tiles = new Tile[pieces];

            for (int i = 0; i < pieces; i++)
            {
                tiles[i] = new Tile(this);
                tiles[i].Parent = this;
                tiles[i].Row = i / cols;
                tiles[i].Col = i % cols;
                tiles[i].Location = new Point(i % cols * (imgSize.Width / cols) + 2, i / cols * (imgSize.Height / rows) + 22); 
                tiles[i].Size = new Size(imgSize.Width / cols, imgSize.Height / rows);
                cropArea = new Rectangle(i % cols * (imgSize.Width / cols), i / cols * (imgSize.Height / rows), imgSize.Width / cols, imgSize.Height / rows);
                tiles[i].BackgroundImage = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
            }

            Random rand = new Random();

            for (int i = 0; i < 200; i++)
            {
                MovePiece(tiles[rand.Next(pieces)]);
            }
        }