-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitboard.cpp
135 lines (111 loc) · 2.19 KB
/
bitboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "bitboard.h"
#include "board.h"
#include <iostream>
#include <intrin.h>
using std::cout;
using std::cin;
U64 RANK_MASK_TABLE[64] = {};
U64 RANK_NOT_MASK_TABLE[64] = {};
U64 FILE_MASK_TABLE[64] = {};
U64 FILE_NOT_MASK_TABLE[64] = {};
void InitRankMaskTable()
{
U64 rank_mask = 0b11111111ull;
for (int rank = 0; rank < 8; ++rank)
{
for (int file = 0; file < 8; ++file)
{
int square = rank * 8 + file;
RANK_MASK_TABLE[square] = rank_mask;
RANK_NOT_MASK_TABLE[square] = ~rank_mask;
}
rank_mask <<= 8;
}
}
void InitFileMaskTable()
{
U64 file_mask = (1ull | 1ull << 8 | 1ull << 16 | 1ull << 24 | 1ull << 32 | 1ull << 40 | 1ull << 48 | 1ull << 56);
for (int file = 0; file < 8; ++file)
{
for (int rank = 0; rank < 8; ++rank)
{
int square = rank * 8 + file;
FILE_MASK_TABLE[square] = file_mask;
FILE_NOT_MASK_TABLE[square] = ~file_mask;
}
file_mask <<= 1;
}
}
int GetRank(int square)
{
return square >> 3;
}
int GetFile(int square)
{
return square & (8-1);
}
int ToRank(char c)
{
return 8 - (c - '0');
}
int ToFile(char c)
{
return tolower(c) - 'a';
}
int Bitboard::GetBit(int square) const
{
return this->bitboard >> square & 1;
}
void Bitboard::SetBit(int square)
{
this->bitboard |= 1ull << square;
}
void Bitboard::ClearBit(int square)
{
this->bitboard &= ~(1ull << square);
}
void Bitboard::PopBit()
{
this->bitboard &= this->bitboard - 1;
}
void Bitboard::FlipBit(int square)
{
this->bitboard ^= 1ull << square;
}
int Bitboard::CountBit() const
{
return static_cast<int>(__popcnt64(this->bitboard));
}
int Bitboard::GetLeastSigBit() const
{
if (this->bitboard == 0) return -1;
return static_cast<int>(__popcnt64((this->bitboard ^ (this->bitboard-1))) - 1);
}
void Bitboard::PrintBitboard() const
{
for (int rank = 0; rank < 8; ++rank)
{
cout << 8 - rank << " ";
for (int file = 0; file < 8; ++file)
{
int square = rank * 8 + file;
cout << this->GetBit(square)<<" ";
}
cout << '\n';
}
cout << " a b c d e f g h\n";
}
Bitboard::operator U64() const
{
return this->bitboard;
}
Bitboard::operator U64&()
{
return this->bitboard;
}
Bitboard::Bitboard() : bitboard(0)
{
}
Bitboard::Bitboard(U64 new_bitboard) : bitboard(new_bitboard)
{
}