waseem1345 0 Light Poster

hello, this is the part of the code I'm having a problem with

if(a1==r1a || a2==r1b)
               {//REPLACING

                       for(p=0;p<8;p++)
                       {
                           for(r=0;r<8;r++)
                           {

                               if( i[p][r]==1)
                               {

                                   i[p][r]=0;
                               }
                               if(p==a1 && r==a2)
                               {
                                   i[p][r]=1;
                                   r1a=p;
                                   r1b=r;
                               }
                           }
                       }

                }

I have only 2 units in the board of my chess game, intiger i is a 2D array (which is the board), there are unit 1 and unit 2 on it, unit 2 is placed in i[r2a][r2b].
and unit 1 previous position is in i[r1a][r2b]
this code change the position of rook 1 from place to another if there are no units in the new place, and if the new place is in a straight line. But I don't know how to make it not pass other units.. Can you show me a small code to let it not pass the unit 2 which is positioned in i[r2a][r2b]?

Thanks in advance :)

P.S: if the position = 0 then it's empty. if it's =1 then it contains the rook, if it's = other number then it contains another unit.
[a1][a2] is the new position which the user entered.

Dani AI

Generated

A simple, reliable approach is to walk every square between the rook's current cell and the target and refuse the move if any intermediate square is non‑zero. The helper below uses the same names from 's post (i for the board, r1a,r1b for the rook's current row/col and a1,a2 for the requested row/col). It returns 1 when the path is clear (destination not checked), 0 otherwise.

int rook_path_clear(const int board[8][8], int from_r, int from_c, int to_r, int to_c)
{
    if (from_r == to_r && from_c == to_c) return 0; /* no move */
    if (to_r < 0 || to_r > 7 || to_c < 0 || to_c > 7) return 0; /* bounds */

    /* must move in same row or same column */
    if (to_r != from_r && to_c != from_c) return 0;

    int dr = (to_r > from_r) ? 1 : (to_r < from_r) ? -1 : 0;
    int dc = (to_c > from_c) ? 1 : (to_c < from_c) ? -1 : 0;

    int r = from_r + dr;
    int c = from_c + dc;
    while (r != to_r || c != to_c) {
        if (board[r][c] != 0) return 0; /* blocked by something */
        r += dr;
        c += dc;
    }
    return 1; /* path clear (destination still needs its own check) */
}

Use it like this before updating the board:

if (rook_path_clear(i, r1a, r1b, a1, a2) && i[a1][a2] == 0) {
    i[r1a][r1b] = 0;
    i[a1][a2] = 1;
    r1a = a1; r1b = a2;
}

Notes: start the loop from the first square after the rook and stop before the destination so you detect blocking pieces only on the path. If you later want captures, replace i[a1][a2] == 0 with a test that allows enemy pieces but still prevents jumping. Also double‑check your row/column ordering (some code uses [row][col], others [y][x]) and be careful with off‑by‑one errors when computing dr/dc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.