0

I am trying to create a chess game for windows application.And I created a chess table and I put pictures of chess pieces on the chess board.But I didnt moves pieces. I have written the following code:

//the creation of the chess board

this.ClientSize = new System.Drawing.Size(600, 600);

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                satranctahtasi[i, j] = new Kareler(i,j);
                this.Controls.Add(satranctahtasi[i, j]);            
            }
        }
        for (int j = 0; j < 8; j++)
        {
            int i = 1;
            satranctahtasi[i, j].Image = TasResimleri.er_beyaz;

        }
        for (int j = 0; j < 8; j++)
        {
            int i = 6;
            satranctahtasi[i, j].Image = TasResimleri.er_siyah;

        }
        satranctahtasi[0, 1].Image = TasResimleri.at_beyaz;
        satranctahtasi[0, 6].Image = TasResimleri.at_beyaz;

        satranctahtasi[7, 1].Image = TasResimleri.at_siyah;
        satranctahtasi[7, 6].Image = TasResimleri.at_siyah;

        satranctahtasi[0, 0].Image = TasResimleri.kale_beyaz;
        satranctahtasi[0, 7].Image = TasResimleri.kale_beyaz;

        satranctahtasi[7, 0].Image = TasResimleri.kale_siyah;
        satranctahtasi[7, 7].Image = TasResimleri.kale_siyah;

        satranctahtasi[0, 2].Image = TasResimleri.fil_beyaz;
        satranctahtasi[0, 5].Image = TasResimleri.fil_beyaz;

        satranctahtasi[7, 2].Image = TasResimleri.fil_beyaz;
        satranctahtasi[7, 5].Image = TasResimleri.fil_beyaz;

        satranctahtasi[0, 3].Image = TasResimleri.sah_beyaz;
        satranctahtasi[7, 3].Image = TasResimleri.sah_siyah;

        satranctahtasi[0, 4].Image = TasResimleri.vezir_beyaz;
        satranctahtasi[7, 4].Image = TasResimleri.vezir_siyah;

how to move pic chess pieces? Please Help Me

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
enz
  • 11
  • 2

2 Answers2

2

First of all try to create Objects each of which draw himself to the board,

Create class Board, Figure and etc...after that it will be easy to make movments with real objects like this for example, also you can use Drag And Drop

Community
  • 1
  • 1
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
1

If you want to be able to move in a 2D array, you'll have to understand this:

   /* increase point by one (moving) */
   ++X; //right
   --X; //left
   --Y; //up 
   ++Y; //down


 y ^
   |
   |       ++/down
   |  --/left  ++/right
   |       --/up
---|----------------->
   |0                x

Here the one coordination point stays the same while the other increases/decreases, in this case by one.

Initially, when you draw a 2D array you begin with 0(y), 0(x) and then increase the values in a loop. In this case you have a 7x7 chess board (actually 8x8 - you begin with 0x0).

Assign a position to your piece by changing it's coordination point in the array.

   satranctahtasi[y, ++x].Image = TasResimleri.[whatever_piece]; //right
   satranctahtasi[y, --x].Image = TasResimleri.[whatever_piece]; //left
   satranctahtasi[--y, x].Image = TasResimleri.[whatever_piece]; //up
   satranctahtasi[++y, x].Image = TasResimleri.[whatever_piece]; //down

For example, if you want your pawn moved by one place from position 0,3 to position 1,3 (down) you increase the value of variable i and leave the value of variable j the same.

  satranctahtasi[++i, j].Image = TasResimleri.er_beyaz; //down

Hope this helps!

Secko
  • 7,664
  • 5
  • 31
  • 37