Thread: How to make a rook rule in chess to not pass other units on the line?

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    4

    How to make a rook rule in chess to not pass other units on the line?

    hello, this is the part of the code I'm having a problem with:
    Code:
    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] or tell me how to do it?

    Note: 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.

    Thanks in advance

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Think about it logically....

    traverse through the straight "line(column or row)" of the 2d array while there is no other piece in your way, if you hit an enemy piece, replace your rook with that cell. if you hit your own piece throw some exception.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No rule to make target 'all'. Stop.
    By kirba in forum C++ Programming
    Replies: 0
    Last Post: 12-08-2012, 10:59 AM
  2. Replies: 2
    Last Post: 04-27-2011, 04:14 PM
  3. a strange make rule
    By George2 in forum Tech Board
    Replies: 11
    Last Post: 08-28-2006, 08:18 PM
  4. [Build Error] No rule to make target.......
    By Obbin in forum C Programming
    Replies: 4
    Last Post: 07-30-2006, 01:06 PM
  5. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM

Tags for this Thread