我的图像被切成4块(2x2)。我以随机顺序绘制它们,如果小图片的顺序正确,则写出“你赢了”。 如果单击图像,则应与其旁边的另一个图像交换。 如果单击左键,则首先使用第三个更改位置(右键2-4)。代码有什么问题?
[![在此处输入图像说明] [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
答案 0 :(得分:1)
CheckWin()方法就是问题所在。 您所做的只是检查图片框的左侧和顶部== 0。 但是没有任何代码可以移动它们,所以它不可能是真的。