The Pedigree Project  0.1
Bitboard.cc
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 #include "Bitboard.h"
22 #include <stdio.h>
23 #include <string.h>
24 
26 {
27  member.i = 0;
28 }
29 
30 Bitboard::Bitboard(unsigned char squares[64])
31 {
32  member.i = 0;
33  for (int i = 0; i < 64; i++)
34  {
35  if (squares[i] != 0)
36  set(i/8, i%8);
37  }
38 }
39 
41 {
42  for (int i = 7; i >= 0; i--)
43  {
44  for (int j = 0; j < 8; j++)
45  {
46  if (test(i,j))
47  putchar ('x');
48  else
49  putchar ('.');
50  }
51  printf(" %d\n", i+1);
52  }
53  printf("ABCDEFGH\n");
54 }
55 
57 {
58  // Rows become columns, columns become rows.
59  char c[8];
60  memset(c, 0, 8);
61 
62  for(int i = 0; i < 8 ; i++)
63  {
64  c[0] |= (member.c[i] & 0x80) >> i;
65  c[1] |= (i-1 < 0) ? (member.c[i] & 0x40) << 1-i : (member.c[i] & 0x40) >> i-1;
66  c[2] |= (i-2 < 0) ? (member.c[i] & 0x20) << 2-i : (member.c[i] & 0x20) >> i-2;
67  c[3] |= (i-3 < 0) ? (member.c[i] & 0x10) << 3-i : (member.c[i] & 0x10) >> i-3;
68  c[4] |= (i-4 < 0) ? (member.c[i] & 0x08) << 4-i : (member.c[i] & 0x08) >> i-4;
69  c[5] |= (i-5 < 0) ? (member.c[i] & 0x04) << 5-i : (member.c[i] & 0x04) >> i-5;
70  c[6] |= (i-6 < 0) ? (member.c[i] & 0x02) << 6-i : (member.c[i] & 0x02) >> i-6;
71  c[7] |= (i-7 < 0) ? (member.c[i] & 0x01) << 7-i : (member.c[i] & 0x01) >> i-7;
72  }
73 
74  memcpy(member.c, c, 8);
75 }
76 
78 {
79  char c[8];
80  memset(c, 0, 8);
81 
82  // Flip all rows and flip all bits in them.
83  for (int i = 0; i < 8; i++)
84  {
85  if (member.c[7-i] & 0x80) c[i] |= 0x01;
86  if (member.c[7-i] & 0x40) c[i] |= 0x02;
87  if (member.c[7-i] & 0x20) c[i] |= 0x04;
88  if (member.c[7-i] & 0x10) c[i] |= 0x08;
89  if (member.c[7-i] & 0x08) c[i] |= 0x10;
90  if (member.c[7-i] & 0x04) c[i] |= 0x20;
91  if (member.c[7-i] & 0x02) c[i] |= 0x40;
92  if (member.c[7-i] & 0x01) c[i] |= 0x80;
93  }
94 
95  memcpy(member.c, c, 8);
96 }
97 
98 unsigned char Bitboard::getDiagonalRank45(int rank, int file, int *newFile)
99 {
100  int f = file;
101  int r = rank;
102  int nf = 0;
103  while (f-1 >= 0 && r-1 >= 0)
104  {
105  f --;
106  r --;
107  nf ++;
108  }
109 
110  unsigned char toRet = 0;
111  int n = 0;
112  while (f < 8 && r < 8)
113  {
114  if (test(r,f))
115  toRet |= 0x80 >> n;
116  n++;
117  f++;
118  r++;
119  }
120  *newFile = nf;
121  return toRet;
122 }
123 
124 unsigned char Bitboard::getDiagonalRank315(int rank, int file, int *newFile)
125 {
126  int f = file;
127  int r = rank;
128  int nf = 0;
129  while (f-1 >= 0 && r+1 < 8)
130  {
131  f --;
132  r ++;
133  nf ++;
134  }
135 
136  unsigned char toRet = 0;
137  int n = 0;
138  while (f < 8 && r >= 0)
139  {
140  if (test(r,f))
141  toRet |= 0x80 >> n;
142  n++;
143  f++;
144  r--;
145  }
146  *newFile = nf;
147  return toRet;
148 }
149 
150 void Bitboard::setDiagonalRank45(int rank, int file, unsigned char newRank)
151 {
152  int f = file;
153  int r = rank;
154  while (f-1 >= 0 && r-1 >= 0)
155  {
156  f --;
157  r --;
158  }
159 
160  int n = 0;
161  while (f < 8 && r < 8)
162  {
163  if ( newRank & (0x80 >> n) )
164  set (r, f);
165  n++;
166  f++;
167  r++;
168  }
169 }
170 
171 void Bitboard::setDiagonalRank315(int rank, int file, unsigned char newRank)
172 {
173  int f = file;
174  int r = rank;
175  while (f-1 >= 0 && r+1 < 8)
176  {
177  f --;
178  r ++;
179  }
180 
181  int n = 0;
182  while (f < 8 && r >= 0)
183  {
184  if ( newRank & (0x80 >> n) )
185  set (r, f);
186  n++;
187  f++;
188  r--;
189  }
190 }
191 
192 void Bitboard::shift(int colOffset, int rowOffset)
193 {
194  char c[8];
195  memset(c, 0, 8);
196 
197  // Shift rows first.
198  for (int i = 0; i < 8; i++)
199  {
200  if ((i+rowOffset) >= 0 && (i+rowOffset) < 8)
201  {
202  // While we're here, shift columns.
203  c[i+rowOffset] = (colOffset < 0) ? member.c[i] << -colOffset : member.c[i] >> colOffset;
204  }
205  }
206  memcpy(member.c, c, 8);
207 }
208 
210 {
211  Bitboard b3;
212  b3.member.i = member.i | b2.member.i;
213  return b3;
214 }
215 
216 Bitboard Bitboard::operator&(Bitboard b2)
217 {
218  Bitboard b3;
219  b3.member.i = member.i & b2.member.i;
220  return b3;
221 }
222 
223 Bitboard Bitboard::operator xor(Bitboard b2)
224 {
225  Bitboard b3;
226  b3.member.i = member.i xor b2.member.i;
227  return b3;
228 }
229 
230 Bitboard Bitboard::operator~()
231 {
232  Bitboard b3;
233  b3.member.i = ~member.i;
234  return b3;
235 }
236 
237 Bitboard::operator bool()
238 {
239  return member.i != 0;
240 }
241 
242 Square Bitboard::getAndClearFirstSetBit()
243 {
244  for(int i = 0; i < 8; i++)
245  {
246  if (member.c[i] == 0)
247  continue;
248 
249  if (member.c[i] & 0x80)
250  {
251  member.c[i] &= ~0x80;
252  return Square(0, i);
253  }
254  if (member.c[i] & 0x40)
255  {
256  member.c[i] &= ~0x40;
257  return Square(1, i);
258  }
259  if (member.c[i] & 0x20)
260  {
261  member.c[i] &= ~0x20;
262  return Square(2, i);
263  }
264  if (member.c[i] & 0x10)
265  {
266  member.c[i] &= ~0x10;
267  return Square(3, i);
268  }
269  if (member.c[i] & 0x8)
270  {
271  member.c[i] &= ~0x8;
272  return Square(4, i);
273  }
274  if (member.c[i] & 0x4)
275  {
276  member.c[i] &= ~0x4;
277  return Square(5, i);
278  }
279  if (member.c[i] & 0x2)
280  {
281  member.c[i] &= ~0x2;
282  return Square(6, i);
283  }
284  if (member.c[i] & 0x1)
285  {
286  member.c[i] &= ~0x1;
287  return Square(7, i);
288  }
289  }
290  return Square(-1, -1);
291 }
void print()
Definition: Bitboard.cc:40
void rotate180()
Definition: Bitboard.cc:77
Bitboard operator|(Bitboard b2)
Definition: Bitboard.cc:209
void flip()
Definition: Bitboard.cc:56
bool test(int rank, int file)
Definition: Bitboard.h:67
Bitboard()
Definition: Bitboard.cc:25
Definition: Square.h:24