Ranks

From Chessprogramming wiki
Jump to: navigation, search

Home * Chess * Ranks

Ranks are the eight horizontal lines or rows of a Chessboard, labeled from '1' to '8'. Each rank contains eight horizontally arranged Squares of alternating white and black, or light and dark Color.

Back Ranks

The First Rank is White's back rank, while the Eights Rank is Black's back rank.

 abcdefgh 
8
7
6
5
4
3
2
1
    
    
    
    
    
    
    
    
        
        
        
        
        
        
        
        
••••••••
        
        
        
        
        
        
••••••••
8
7
6
5
4
3
2
1
 abcdefgh 

Square Mapping Notes

Whether the square difference of neighbored squares on a rank or file is either 1 or 8, depends on the square mapping. We further rely on little-endian rank-file mapping, which keeps consecutive squares of a rank as neighbored elements in Memory (or register).

Square Difference

Within a 0..63 square index range and the mentioned square mapping (a1 = 0), the difference of two neighbored squares on a Rank is one.

Enumeration

As mentioned, on a Chessboard the eight ranks are labeled from '1' to '8'. To make them zero based indices as appropriate for C-like programming languages, we enumerate ranks from 0 to 7. Little-endian rank-mapping (as used here) assigns the first Rank to index zero. Three bits are required to enumerate from 0 to 7.

A little-endian rank-mapping enumeration in C++ might look like this:

enum enumRank {
  er1stRank = 0,
  er2ndRank = 1,
  er3rdRank = 2,
  er4thRank = 3,
  er5thRank = 4,
  er6thRank = 5,
  er7thRank = 6,
  er8thRank = 7,
};

Rank from Square

Rank-File mapping of squares keeps the rank index as the three upper bits of a six bit Square index.

rank = square >> 3; // div 8

Rank-Distance

The rank-distance is the absolute difference between two ranks (their 0-7 indices). The maximum rank-distance is 7.

rankDistance = abs (rank1 - rank2);
rankDistance = abs (rank2 - rank1);

Two Squares on a Rank

Two Squares are on the same Rank, if their Rank distance is zero.

bool squaresOnSameRank(int sq1, int sq2) {
 return ((sq2 >> 3) - (sq1 >> 3)) == 0;
}

The Symmetric difference aka xor is sufficent for the test of the rank bits as well [1]

bool squaresOnSameRank(int sq1, int sq2) {
 return ((sq1 ^ sq2) & 56) == 0;
}

See also

References