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");
}
}
}
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.