这段代码有什么问题?
我写了一些代码来移动国际象棋游戏中的国王;你能告诉我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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你知道这行代码的含义吗?
Do you know the implications of this line of code?
不,您没有正确定义返回类型,因为该代码中的任何位置都没有任何返回值。
让我们看看代码会做什么...
第一个问题是这一行:
FromFile
方法将创建一个新对象,并且该对象永远不会与存储在图片中的对象相同框,条件始终为 false,并且永远不会调用方法f
。在
picarray
方法中,您使用变量pic
,但该变量与f
方法中使用的变量不同,由于该变量是在该方法中本地声明的,因此在
f
方法中,您声明了一个您操作的图片框数组,但随后您只需退出该方法而不对该数组执行任何操作,因此该数组就这样消失了,结果在任何地方都不会可见。由于数组是新创建的,它仅包含空引用,因此将它们从数组中的一项复制到另一项不会完成任何操作。您还将项目从两个位置复制到同一位置,因此第二个副本将覆盖第一个副本。
由于变量
i
和j
设置为零,[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:
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 methodf
is never called.In the method
picarray
you are using the variablepic
, but that won't be the same variable as the one used in thef
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
andj
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 theImage
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.
很难说你想用这个片段在整个代码中做什么,但它看起来不正确,不。
我看到的问题是,在
f()
中,您创建了PictureBox pic
,设置了它的一些属性,但随后不对其执行任何操作。在f()
结束时,该方法返回并且pic
被销毁。如果此代码实际上可以通过在
picarray()
方法中使用pix[]
进行编译,我会说您在游戏板的某个地方有一个类级别变量。在这种情况下,您不需要这一行: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 thePictureBox pic
, set some of its properties but then dont do anything with it. At the end off()
the method returns andpic
is destroyed.If this code actually compiles then from your use of
pix[]
in thepicarray()
method I would say you have a class level variable somewhere that is your game board. In that case you dont need the line:in
f()
because its just creating a new empty board that exists only withinf()
rather than updating your real board.我认为删除这一行
,您再次用新图片初始化图片数组
Remove this line
i think , you are again initilizing of pic array with new picture