2017-10-09 85 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. 但是沒有代碼移動它們,所以它永遠不會等於真。