这段代码有什么问题?

发布于 2024-10-06 04:22:47 字数 1242 浏览 4 评论 0原文

我写了一些代码来移动国际象棋游戏中的国王;你能告诉我king不动的这段代码问题出在哪里吗?谢谢。

编辑2:

public partial class Form1 : Form
{
    PictureBox[,] pic = new PictureBox[8, 8];

private void pictureBox34_Click(object sender, EventArgs e)
    {

if (pictureBox34.Image == chess9.Properties.Resources.siyahsah2)

{

f();
}
}

public void picarray()
{
        pic[0, 0] = pictureBox54;
        pic[0, 1] = pictureBox64;
        pic[0, 2] = pictureBox48;
        pic[0, 3] = pictureBox42;
        pic[0, 4] = pictureBox34;
        pic[0, 5] = pictureBox26;
        pic[0, 6] = pictureBox18;
        pic[0, 7] = pictureBox8;
        pic[1, 0] = pictureBox1;
        pic[1, 1] = pictureBox2;
        pic[1, 2] = pictureBox3;
        pic[1, 3] = pictureBox4;
              .
              .///thats so long(64 arrays)
              .
 }

public void f()
{



        int x = 3;
        int y = 3;

       for (int i = 1; i < x; i++)
       {
            for (int j = 1; j < y; j++)
            {
                pic[i, j] = new PictureBox();

                pic[i, j] = pic[i + 1, j + 1];

                pic[i, j] = new PictureBox();
                pic[i, j].Image = Image.FromFile("pic/siyahsah2.jpg");

          }

}
}

I wrote some code to move the king in chess game; can you tell me where is the problem of this code that king doesn't move? Thanks.

EDITED2:

public partial class Form1 : Form
{
    PictureBox[,] pic = new PictureBox[8, 8];

private void pictureBox34_Click(object sender, EventArgs e)
    {

if (pictureBox34.Image == chess9.Properties.Resources.siyahsah2)

{

f();
}
}

public void picarray()
{
        pic[0, 0] = pictureBox54;
        pic[0, 1] = pictureBox64;
        pic[0, 2] = pictureBox48;
        pic[0, 3] = pictureBox42;
        pic[0, 4] = pictureBox34;
        pic[0, 5] = pictureBox26;
        pic[0, 6] = pictureBox18;
        pic[0, 7] = pictureBox8;
        pic[1, 0] = pictureBox1;
        pic[1, 1] = pictureBox2;
        pic[1, 2] = pictureBox3;
        pic[1, 3] = pictureBox4;
              .
              .///thats so long(64 arrays)
              .
 }

public void f()
{



        int x = 3;
        int y = 3;

       for (int i = 1; i < x; i++)
       {
            for (int j = 1; j < y; j++)
            {
                pic[i, j] = new PictureBox();

                pic[i, j] = pic[i + 1, j + 1];

                pic[i, j] = new PictureBox();
                pic[i, j].Image = Image.FromFile("pic/siyahsah2.jpg");

          }

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最美的太阳 2024-10-13 04:22:47
if (pictureBox34.Image == Image.FromFile("pic/siyahsah2.jpg"))

你知道这行代码的含义吗?

  • 每次执行此语句时,都会使用存储在磁盘上的图像创建一个新实例。
  • 比较永远不会计算为 true,因为比较是基于引用的相等性,这对于新创建的对象显然不可能为 true
if (pictureBox34.Image == Image.FromFile("pic/siyahsah2.jpg"))

Do you know the implications of this line of code?

  • Every time this statement is executed, a new instance is created, using the image stored on disk
  • The comparison never evaluates to true, since the comparison is based on the equality of references, which obviously cannot be true for a newly created object
可遇━不可求 2024-10-13 04:22:47

不,您没有正确定义返回类型,因为该代码中的任何位置都没有任何返回值。

让我们看看代码会做什么...

第一个问题是这一行:

if (pictureBox34.Image == Image.FromFile("pic/siyahsah2.jpg"))

FromFile 方法将创建一个新对象,并且该对象永远不会与存储在图片中的对象相同框,条件始终为 false,并且永远不会调用方法 f

picarray 方法中,您使用变量 pic,但该变量与 f 方法中使用的变量不同,由于该变量是在该方法中本地声明的,因此

f 方法中,您声明了一个您操作的图片框数组,但随后您只需退出该方法而不对该数组执行任何操作,因此该数组就这样消失了,结果在任何地方都不会可见。

由于数组是新创建的,它仅包含空引用,因此将它们从数组中的一项复制到另一项不会完成任何操作。您还将项目从两个位置复制到同一位置,因此第二个副本将覆盖第一个副本。

由于变量 ij 设置为零,[i - 1, j - 1] 将尝试访问外部的项目数组,这会给你一个例外。

您试图在数组中一项的 Image 属性中存储某些内容,但由于数组中的所有项均为空,因此没有可以设置 Image 的图片框 的属性。

很难说出你想要做什么,但这些信息至少应该帮助你理解代码不做什么。

No, you didn't define the return types correctly, as you don't have any return values at all anywhere in that code.

Let's see what the code might do...

The first problem is this line:

if (pictureBox34.Image == Image.FromFile("pic/siyahsah2.jpg"))

The FromFile method will create a new object, and as that object will never be the same object as the one stored in the picture box, the condition is always false and the method f is never called.

In the method picarray you are using the variable pic, but that won't be the same variable as the one used in the f method, as that variable is declared locally in that method,

In the f method you are declaring an array of picture boxes which you manipulate, but then you just exit the method without doing anything with the array, so the array just goes away and the result will never be visible anywhere.

As the array is newly created, it contains only null referecens, so copying them from one item to another in the array doesn't accomplish anything. You are also copying items from two positions into the same position, so the second copy will overwrite the first.

As the variables i and j are set to zero, [i - 1, j - 1] will try to access an item that is outside the array, which would give you an exception.

You are trying to store something in the Image property of one of the items in the array, but as all items in the array are null, there is no picture box that you can set the Image property of.

It's hard to tell what you are trying to do, but this information should at least help you to understand what the code doesn't do.

撞了怀 2024-10-13 04:22:47

很难说你想用这个片段在整个代码中做什么,但它看起来不正确,不。

我看到的问题是,在 f() 中,您创建了 PictureBox pic,设置了它的一些属性,但随后不对其执行任何操作。在f() 结束时,该方法返回并且pic 被销毁。

如果此代码实际上可以通过在 picarray() 方法中使用 pix[] 进行编译,我会说您在游戏板的某个地方有一个类级别变量。在这种情况下,您不需要这一行:

PictureBox[,] pic = new PictureBox[8, 8];

in f() 因为它只是创建一个仅存在于 f() 中的新空板,而不是更新您的真实板。

Its hard to tell what you are trying to do in the overall code with this snippet but it doesnt look right, no.

The problem I see is that in f() you create the PictureBox pic, set some of its properties but then dont do anything with it. At the end of f() the method returns and pic is destroyed.

If this code actually compiles then from your use of pix[] in the picarray() method I would say you have a class level variable somewhere that is your game board. In that case you dont need the line:

PictureBox[,] pic = new PictureBox[8, 8];

in f() because its just creating a new empty board that exists only within f() rather than updating your real board.

中二柚 2024-10-13 04:22:47

我认为删除这一行

PictureBox[,] pic = new PictureBox[8, 8]; from the f() function

,您再次用新图片初始化图片数组

Remove this line

PictureBox[,] pic = new PictureBox[8, 8]; from the f() function

i think , you are again initilizing of pic array with new picture

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文