📄 square.cpp
字号:
// square.cpp
// includes
#include "square.h"
// "constants"
const int square_from_64[64] =
{
(0x44), (0x45), (0x46), (0x47), (0x48), (0x49), (0x4A), (0x4B), (0x54), (0x55), (0x56), (0x57), (0x58), (0x59),
(0x5A), (0x5B), (0x64), (0x65), (0x66), (0x67), (0x68), (0x69), (0x6A), (0x6B), (0x74), (0x75), (0x76), (0x77),
(0x78), (0x79), (0x7A), (0x7B), (0x84), (0x85), (0x86), (0x87), (0x88), (0x89), (0x8A), (0x8B), (0x94), (0x95),
(0x96), (0x97), (0x98), (0x99), (0x9A), (0x9B), (0xA4), (0xA5), (0xA6), (0xA7), (0xA8), (0xA9), (0xAA), (0xAB),
(0xB4), (0xB5), (0xB6), (0xB7), (0xB8), (0xB9), (0xBA), (0xBB),
};
const int rank_mask[2] =
{
0, 0xF
};
const int promote_rank[2] =
{
0xB0, 0x40
};
// variables
int square_to_64[256];
bool square_is_promote[256];
// functions
// square_init()
void square_init()
{
int sq;
// square_to_64[]
for ( sq = 0; sq < 256; sq++ )
square_to_64[sq] = -1;
for ( sq = 0; sq < 64; sq++ )
{
square_to_64[square_from_64[sq]] = sq;
}
// square_is_promote[]
for ( sq = 0; sq < 256; sq++ )
{
square_is_promote[sq] = SQUARE_IS_OK(sq) && (SQUARE_RANK(sq) == (0x4) || SQUARE_RANK(sq) == (0xB));
}
}
// file_from_char()
int file_from_char(int c)
{
return (0x4) + (c - 'a');
}
// rank_from_char()
int rank_from_char(int c)
{
return (0x4) + (c - '1');
}
// file_to_char()
int file_to_char(int file)
{
return 'a' + (file - (0x4));
}
// rank_to_char()
int rank_to_char(int rank)
{
return '1' + (rank - (0x4));
}
// square_to_string()
bool square_to_string(int square, char string [], int size)
{
if(size < 3)
return false;
string[0] = file_to_char(SQUARE_FILE(square));
string[1] = rank_to_char(SQUARE_RANK(square));
string[2] = '\0';
return true;
}
// square_from_string()
int square_from_string(const char string [])
{
int file, rank;
if(string[0] < 'a' || string[0] > 'h')
return 0;
if(string[1] < '1' || string[1] > '8')
return 0;
if(string[2] != '\0')
return 0;
file = file_from_char(string[0]);
rank = rank_from_char(string[1]);
return SQUARE_MAKE(file, rank);
}
// end of square.cpp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -