Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef DIRECTION_H
00025 #define DIRECTION_H
00026
00027 #ifndef USE_PCH
00028 #endif
00029
00030 #include "BitBoard.h"
00031 #include "Type.h"
00032 #include "Flags.h"
00033
00034 namespace cwchess {
00035
00036 uint8_t const df_rook_mover = rook_bits;
00037 uint8_t const df_bishop_mover = bishop_bits;
00038 uint8_t const df_pinned_horizontally = 0 << 3;
00039 uint8_t const df_pinned_vertically = 1 << 3;
00040 uint8_t const df_pinned_slashy = 2 << 3;
00041 uint8_t const df_pinned_backslashy = 3 << 3;
00042 uint8_t const df_pinned_mask = 3 << 3;
00043
00044 struct Direction {
00045 int8_t const shift;
00046 int8_t const offset;
00047 int8_t const index;
00048 uint8_t const flags;
00049
00050 FlagsData mover_flags(void) const { FlagsData result; result.M_bits = flags & type_mask; return result; }
00051 FlagsData pinning_flags(void) const { FlagsData result; result.M_bits = flags >> 3; return result; }
00052 bool is_horizontal(void) const { return shift == 1; }
00053 bool matches(Type const& type) const { return (type() & flags) == (type_mask & flags); }
00054
00055
00056 BitBoard from(Index const& index) const { return direction_table[((static_cast<int>(index())) << 3) + this->index]; }
00057
00058 friend Index operator+(Index const& index, Direction const& direction) { Index result(index); result += direction.offset; return result; }
00059 friend Index operator+(Direction const& direction, Index const& index) { Index result(index); result += direction.offset; return result; }
00060 friend Index operator-(Index const& index, Direction const& direction) { Index result(index); result -= direction.offset; return result; }
00061 friend Index operator-(Direction const& direction, Index const& index) { Index result(index); result -= direction.offset; return result; }
00062
00063 private:
00064 static BitBoardData const direction_table[64 * 8];
00065 };
00066
00067 Direction const south_west = { 9, -9, 0, df_bishop_mover | df_pinned_slashy };
00068 Direction const south = { 8, -8, 1, df_rook_mover | df_pinned_vertically };
00069 Direction const south_east = { 7, -7, 2, df_bishop_mover | df_pinned_backslashy };
00070 Direction const west = { 1, -1, 3, df_rook_mover | df_pinned_horizontally };
00071 Direction const east = { 1, 1, 4, df_rook_mover | df_pinned_horizontally };
00072 Direction const north_west = { 7, 7, 5, df_bishop_mover | df_pinned_backslashy };
00073 Direction const north = { 8, 8, 6, df_rook_mover | df_pinned_vertically };
00074 Direction const north_east = { 9, 9, 7, df_bishop_mover | df_pinned_slashy };
00075
00076 extern BitBoardData const from_to_table[64 * 64];
00077
00078 inline BitBoard squares_from_to(Index const& from, Index const& to)
00079 {
00080 return from_to_table[(from() << 6) | to()];
00081 }
00082
00083 extern uint8_t const direction_index_table[256];
00084
00085 inline Direction const& direction_from_to(Index const& from, Index const& to)
00086 {
00087 uint8_t from_bits(from());
00088 uint8_t to_bits(to());
00089 uint8_t from_col_bits = from_bits & 0x07;
00090 from_bits <<= 1;
00091 uint8_t from_row_bits = from_bits & 0x70;
00092 uint8_t to_col_bits = to_bits & 0x07;
00093 to_bits <<= 1;
00094 uint8_t to_row_bits = to_bits & 0x70;
00095 uint8_t row_diff = to_row_bits - from_row_bits;
00096 uint8_t col_diff = (to_col_bits - from_col_bits) & 0x0f;
00097 static Direction const directions[9] = { south_west, south, south_east, west, east, north_west, north, north_east, {0,0,0,0} };
00098 return directions[direction_index_table[row_diff | col_diff]];
00099 }
00100
00101 }
00102
00103 #endif // DIRECTION_H