2017-10-09 53 views
-4

我有一个图像被切成4件(2x2)。我以随机顺序画他们,如果小照片的顺序是正确的,写出“你赢了”。 如果你点击一个图像,那么它应该与旁边的另一个交换。 如果用第三个按钮(右按钮2-4)单击左按钮第一个更改位置。代码有什么问题?益智与PictureBoxes bug

[![此处输入图像的描述] [1] [1]

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.Collections; 


    namespace SimplePuzzle 
    { 
     public partial class Form1 : Form 
     { 
      public Form1() 
      { 
       InitializeComponent(); 

      } 

      private void buttonVéletlen_Click(object sender, EventArgs e) 
      { 

       Mix(); 
       pictureBox1.Image = Image.FromFile("img\\1.png"); 
       pictureBox2.Image = Image.FromFile("img\\2.png"); 
       pictureBox3.Image = Image.FromFile("img\\3.png"); 
       pictureBox4.Image = Image.FromFile("img\\4.png"); 

      } 
      int[] arr = new int[4]; 

      void Mix() 
      { 

       Random rnd = new Random(); 

       int n = 4; 

       // Fill array 1-4 
       for (int i = 0; i < n; i++) 
       { 
        arr[i] = i + 1; 
       } 
       // Rnd array 

       for (int j = 0; j < n; j++) 
       { 
        int one = rnd.Next(n); 
        int another = rnd.Next(n); 
        int temp = arr[one]; 
        arr[one] = arr[another]; 
        arr[another] = temp; 

       } 

       for (int i = 0; i < 4; i++) 
       { 
        Console.WriteLine(arr[i].ToString()); 
       } 

       // PictureBox1 
       if (arr[0]==0) 
       { 
        pictureBox1.Left = 0; 
        pictureBox1.Top = 0; 
       } 

       if (arr[0] == 1) 
       { 
        pictureBox1.Left = 150; 
        pictureBox1.Top = 0; 
       } 

       if (arr[0] == 2) 
       { 
        pictureBox1.Left = 0; 
        pictureBox1.Top = 150; 
       } 

       if (arr[0] == 3) 
       { 
        pictureBox1.Left = 150; 
        pictureBox1.Top = 150; 
       } 

       // PictureBox2 
       if (arr[1] == 0) 
       { 
        pictureBox2.Left = 0; 
        pictureBox2.Top = 0; 
       } 

       if (arr[1] == 1) 
       { 
        pictureBox2.Left = 150; 
        pictureBox2.Top = 0; 
       } 

       if (arr[1] == 2) 
       { 
        pictureBox2.Left = 0; 
        pictureBox2.Top = 150; 
       } 

       if (arr[1] == 3) 
       { 
        pictureBox2.Left = 150; 
        pictureBox2.Top = 150; 
       } 

       // PictureBox3 
       if (arr[2] == 0) 
       { 
        pictureBox3.Left = 0; 
        pictureBox3.Top = 0; 
       } 

       if (arr[2] == 1) 
       { 
        pictureBox3.Left = 150; 
        pictureBox3.Top = 0; 
       } 

       if (arr[2] == 2) 
       { 
        pictureBox3.Left = 0; 
        pictureBox3.Top = 150; 
       } 

       if (arr[2] == 3) 
       { 
        pictureBox3.Left = 150; 
        pictureBox3.Top = 150; 
       } 

       // PictureBox4 
       if (arr[3] == 0) 
       { 
        pictureBox4.Left = 0; 
        pictureBox4.Top = 0; 
       } 

       if (arr[3] == 1) 
       { 
        pictureBox4.Left = 150; 
        pictureBox4.Top = 0; 
       } 

       if (arr[3] == 2) 
       { 
        pictureBox4.Left = 0; 
        pictureBox4.Top = 150; 
       } 

       if (arr[3] == 3) 
       { 
        pictureBox4.Left = 150; 
        pictureBox4.Top = 150; 
       } 
      } 

      void CheckWin() 
      { 
       if (pictureBox1.Left==0 && pictureBox1.Top == 0 && 
        pictureBox2.Left == 0 && pictureBox2.Top == 0 && 
        pictureBox3.Left == 0 && pictureBox3.Top == 0 && 
        pictureBox4.Left == 0 && pictureBox4.Top == 0) 
       { 
        MessageBox.Show("You have won!"); 
       } 
      } 

      private void pictureBox1_Click(object sender, EventArgs e) 
      { 
       pictureBoxKöztes.Image = pictureBox1.Image; 
       pictureBox1.Image = pictureBox2.Image; 
       pictureBox2.Image = pictureBoxKöztes.Image; 
       CheckWin(); 

      } 

      private void pictureBox2_Click(object sender, EventArgs e) 
      { 
       pictureBoxKöztes.Image = pictureBox2.Image; 
       pictureBox2.Image = pictureBox1.Image; 
       pictureBox1.Image = pictureBoxKöztes.Image; 
       CheckWin(); 
      } 

      private void pictureBox3_Click_1(object sender, EventArgs e) 
      { 
       pictureBoxKöztes.Image = pictureBox3.Image; 
       pictureBox3.Image = pictureBox4.Image; 
       pictureBox4.Image = pictureBoxKöztes.Image; 
       CheckWin(); 
      } 

      private void pictureBox4_Click(object sender, EventArgs e) 
      { 
       pictureBoxKöztes.Image = pictureBox4.Image; 
       pictureBox4.Image = pictureBox3.Image; 
       pictureBox3.Image = pictureBoxKöztes.Image; 
       CheckWin(); 
      } 

      private void buttonFirstCol_Click(object sender, EventArgs e) 
      { 
       pictureBoxKöztes.Image = pictureBox1.Image; 
       pictureBox1.Image = pictureBox3.Image; 
       pictureBox3.Image = pictureBoxKöztes.Image; 
       CheckWin(); 
      } 

      private void buttonSecondCol_Click(object sender, EventArgs e) 
      { 
       pictureBoxKöztes.Image = pictureBox2.Image; 
       pictureBox2.Image = pictureBox4.Image; 
       pictureBox4.Image = pictureBoxKöztes.Image; 
       CheckWin(); 
      } 

     } 
    } 


    [1]: https://i.stack.imgur.com/G9dMy.png 
+4

你如何告诉我们什么是错,而不是强迫我们猜测。也建议首先使用调试器来弄清楚为什么它没有按照你想要的做 – UnholySheep

回答

1

的CheckWin()方法是问题所在。 所有你正在做的是检查图片框的左边和顶部== 0. 但是没有代码移动它们,所以它永远不会等于真。