The Pedigree Project  0.1
Bitboard.h
1 /*
2  *
3  * Copyright (c) 2008-2014, Pedigree Developers
4  *
5  * Please see the CONTRIB file in the root of the source tree for a full
6  * list of contributors.
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #ifndef BITBOARD_H
22 #define BITBOARD_H
23 
24 #include <stdint.h>
25 #include "Square.h"
26 
27 class Bitboard
28 {
29 public:
33  Bitboard();
34 
39  Bitboard(unsigned char squares[64]);
40  Bitboard(Square sq)
41  {
42  member.i = 0;
43  set(sq.row, sq.col);
44  }
45 
46  ~Bitboard() {}
47 
51  unsigned char getRank(int n)
52  {
53  return member.c[n];
54  }
55 
59  void setRank(int n, unsigned char c)
60  {
61  member.c[n] = c;
62  }
63 
67  bool test(int rank, int file)
68  {
69  return (bool) (member.c[rank] & (0x80 >> file));
70  }
71 
75  bool clear(int rank, int file)
76  {
77  member.c[rank] &= ~(0x80 >> file);
78  }
79 
83  bool set(int rank, int file)
84  {
85  member.c[rank] |= 0x80 >> file;
86  }
87 
91  void clear()
92  {
93  member.i = 0;
94  }
95 
99  bool empty()
100  {
101  return member.i == 0;
102  }
103 
107  void flip();
108 
112  void rotate180();
113 
114  unsigned char getDiagonalRank45(int rank, int file, int *newFile);
115  unsigned char getDiagonalRank315(int rank, int file, int *newFile);
116  void setDiagonalRank45(int rank, int file, unsigned char newRank);
117  void setDiagonalRank315(int rank, int file, unsigned char newRank);
118 
119  void shift(int colOffset, int rowOffset);
120 
121  Square getAndClearFirstSetBit();
122 
126  void print();
127 
132  Bitboard operator&(Bitboard b2);
133  Bitboard operator xor(Bitboard b2);
134  Bitboard operator~();
135  operator bool();
136 
137 private:
138  union
139  {
140  uint64_t i;
141  unsigned char c[8];
142  } member;
143 };
144 
145 #endif
unsigned char getRank(int n)
Definition: Bitboard.h:51
void clear()
Definition: Bitboard.h:91
void print()
Definition: Bitboard.cc:40
void rotate180()
Definition: Bitboard.cc:77
void setRank(int n, unsigned char c)
Definition: Bitboard.h:59
Bitboard operator|(Bitboard b2)
Definition: Bitboard.cc:209
void flip()
Definition: Bitboard.cc:56
bool test(int rank, int file)
Definition: Bitboard.h:67
bool empty()
Definition: Bitboard.h:99
Bitboard()
Definition: Bitboard.cc:25
Definition: Square.h:24
bool clear(int rank, int file)
Definition: Bitboard.h:75