Direction.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Direction.h This file contains the declaration of class Direction.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef DIRECTION_H
25 #define DIRECTION_H
26 
27 #ifndef USE_PCH
28 #endif
29 
30 #include "BitBoard.h"
31 #include "Type.h"
32 #include "Flags.h"
33 
34 namespace cwchess {
35 
36 uint8_t const df_rook_mover = rook_bits;
37 uint8_t const df_bishop_mover = bishop_bits;
38 uint8_t const df_pinned_horizontally = 0 << 3;
39 uint8_t const df_pinned_vertically = 1 << 3;
40 uint8_t const df_pinned_slashy = 2 << 3;
41 uint8_t const df_pinned_backslashy = 3 << 3;
42 uint8_t const df_pinned_mask = 3 << 3;
43 
44 struct Direction {
45  int8_t const shift;
46  int8_t const offset;
47  int8_t const index;
48  uint8_t const flags;
49 
50  FlagsData mover_flags(void) const { FlagsData result; result.M_bits = flags & type_mask; return result; }
51  FlagsData pinning_flags(void) const { FlagsData result; result.M_bits = flags >> 3; return result; }
52  bool is_horizontal(void) const { return shift == 1; }
53  bool matches(Type const& type) const { return (type() & flags) == (type_mask & flags); }
54 
55  //! @brief Return a BitBoard with all squares in this direction.
56  BitBoard from(Index const& index) const { return direction_table[((static_cast<int>(index())) << 3) + this->index]; }
57 
58  friend Index operator+(Index const& index, Direction const& direction) { Index result(index); result += direction.offset; return result; }
59  friend Index operator+(Direction const& direction, Index const& index) { Index result(index); result += direction.offset; return result; }
60  friend Index operator-(Index const& index, Direction const& direction) { Index result(index); result -= direction.offset; return result; }
61  friend Index operator-(Direction const& direction, Index const& index) { Index result(index); result -= direction.offset; return result; }
62 
63  private:
64  static BitBoardData const direction_table[64 * 8];
65 };
66 
67 Direction const south_west = { 9, -9, 0, df_bishop_mover | df_pinned_slashy };
68 Direction const south = { 8, -8, 1, df_rook_mover | df_pinned_vertically };
69 Direction const south_east = { 7, -7, 2, df_bishop_mover | df_pinned_backslashy };
70 Direction const west = { 1, -1, 3, df_rook_mover | df_pinned_horizontally };
71 Direction const east = { 1, 1, 4, df_rook_mover | df_pinned_horizontally };
72 Direction const north_west = { 7, 7, 5, df_bishop_mover | df_pinned_backslashy };
73 Direction const north = { 8, 8, 6, df_rook_mover | df_pinned_vertically };
74 Direction const north_east = { 9, 9, 7, df_bishop_mover | df_pinned_slashy };
75 
76 extern BitBoardData const from_to_table[64 * 64];
77 
78 inline BitBoard squares_from_to(Index const& from, Index const& to)
79 {
80  return from_to_table[(from() << 6) | to()];
81 }
82 
83 extern uint8_t const direction_index_table[256];
84 
85 inline Direction const& direction_from_to(Index const& from, Index const& to)
86 {
87  uint8_t from_bits(from());
88  uint8_t to_bits(to());
89  uint8_t from_col_bits = from_bits & 0x07;
90  from_bits <<= 1;
91  uint8_t from_row_bits = from_bits & 0x70;
92  uint8_t to_col_bits = to_bits & 0x07;
93  to_bits <<= 1;
94  uint8_t to_row_bits = to_bits & 0x70;
95  uint8_t row_diff = to_row_bits - from_row_bits;
96  uint8_t col_diff = (to_col_bits - from_col_bits) & 0x0f;
97  static Direction const directions[9] = { south_west, south, south_east, west, east, north_west, north, north_east, {0,0,0,0} };
98  return directions[direction_index_table[row_diff | col_diff]];
99 }
100 
101 } // namespace cwchess
102 
103 #endif // DIRECTION_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
This file contains the declaration of class Type.
The POD base type of class Flags.
Definition: Flags.h:37
The POD base type of class BitBoard.
Definition: BitBoard.h:87
uint8_t const bishop_bits
The underlaying integral value of type& #39;bishop&#39;.
Definition: Type.h:57
uint8_t M_bits
0TKNQ000, T=can move two squares, K=can take king side, N=is not blocked, Q=can take queen side...
Definition: Flags.h:38
A chess piece type.
Definition: Type.h:87
A one-boolean-per-square chessboard.
Definition: BitBoard.h:266
The index of a chess square.
Definition: Index.h:211
This file contains the declaration of class BitBoard.
BitBoard from(Index const& index) const
Return a BitBoard with all squares in this direction.
Definition: Direction.h:56
uint8_t const type_mask
A mask for the bits used for the type of a piece.
Definition: Type.h:60
This file contains the declaration of class Flags.
uint8_t const rook_bits
The underlaying integral value of type& #39;rook&#39;.
Definition: Type.h:58

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.