#include<stdio.h>
#include<math.h>
/*8*8的棋盘,用8未表示,前四位表示列,后四位表示行*/
unsigned char binary_row = 0x07; /*求与后只有行*/
unsigned char binary_col = 0x70; /*求与后只有列*/
bool isSameRow(unsigned char a, unsigned char b)
{
a = a & binary_row;
b = b & binary_row;
if( a == b)
return true;
return false;
}
bool isSameCol(unsigned char a, unsigned char b)
{
a = a & binary_col;
b = b & binary_col;
if(a == b)
return true;
return false;
}
bool IsDiagonal(unsigned char a, unsigned char b)
{
/*右移a b求列的差 行的差可直接求与*/
if(abs( (a>>4) - (b>>4) ) == abs( (a&binary_row) - (b&binary_row) ))
return true;
return false;
}
void test()
{
unsigned char apos = 0x40;
unsigned char bpos = 0x11;
unsigned char cpos = 0x22;
unsigned char dpos = 0x14;
unsigned char epos = 0x54;
if(isSameCol(bpos, dpos))
printf("b and d in the same col\n");
if(isSameRow(dpos, epos))
printf("d and e in the same row\n");
if(IsDiagonal(apos, cpos))
printf("a and c on the diagonal\n");
}
int main()
{
test();
return 0;
}
8*8的棋盘的二进制表示法
最新推荐文章于 2022-12-09 00:37:02 发布