Difference between revisions of "Ranks"

From Chessprogramming wiki
Jump to: navigation, search
m
Line 41: Line 41:
 
rankDistance = abs (rank2 - rank1);
 
rankDistance = abs (rank2 - rank1);
 
</pre>
 
</pre>
 +
=Two Squares on a Rank=
 +
Two [[Squares]] are on the same Rank, if their Rank distance is zero.
 +
<pre>
 +
bool squaresOnSameRank(int sq1, int sq2) {
 +
return ((sq2 >> 3) - (sq1 >> 3)) == 0;
 +
}
 +
</pre>
 +
The [https://en.wikipedia.org/wiki/Symmetric_difference Symmetric difference] aka xor is sufficent for the test of the rank bits as well <ref>[http://talkchess.com/forum3/viewtopic.php?f=7&t=74821 An undetected bug for 10 years] by [[Oliver Brausch]], [[CCC]], August 18, 2020</ref>
 +
<pre>
 +
bool squaresOnSameRank(int sq1, int sq2) {
 +
return ((sq1 ^ sq2) & 56) == 0;
 +
}
 +
</pre>
 +
 
=See also=  
 
=See also=  
 
* [[Files]]
 
* [[Files]]
Line 48: Line 62:
 
* [[Intersection Squares]]
 
* [[Intersection Squares]]
  
 +
=References=
 +
<references/>
 
'''[[Chess|Up one Level]]'''
 
'''[[Chess|Up one Level]]'''

Revision as of 13:57, 20 August 2020

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

Up one Level