Skip to content

Commit b9653ef

Browse files
authored
Upload code
1 parent 6c01de8 commit b9653ef

20 files changed

+2989
-0
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# CMakeList.txt : CMake project for ChessEngine, include source and define
2+
# project specific logic here.
3+
#
4+
cmake_minimum_required (VERSION 3.8)
5+
6+
# Add source to this project's executable.
7+
set(CMAKE_CXX_STANDARD 20)
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
set(CMAKE_CXX_EXTENSIONS OFF)
10+
add_compile_options("/std:c++latest")
11+
add_executable(chessengine main.cpp board.cpp "test.hpp" "attack.hpp" "attack.cpp" "move.hpp" "__move.txt" "validate.hpp" "movegen.hpp" "movegen.cpp" "perft.hpp" "pvtable.hpp" "search.hpp" "search.cpp" "evaluate.hpp" "uci.hpp")
12+
add_compile_definitions(USE_ASM)
13+
14+
# TODO: Add tests and install targets if needed.

attack.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "attack.hpp"
2+
#include "constants.hpp"
3+
#include "validate.hpp"
4+
5+
int attack::isSquareAttacked(int square, constants::Color player, board::BoardState& state) {
6+
7+
assert(validate::is120OnBoard(square));
8+
assert(validate::isSideValid(asInt(player)));
9+
assert(state.checkBoard());
10+
11+
if (player == constants::Color::WHITE) {
12+
if (state.pieces[square - 11] == asInt(constants::Piece::wP) || state.pieces[square - 9] == asInt(constants::Piece::wP))
13+
return true;
14+
}
15+
else {
16+
if (state.pieces[square + 11] == asInt(constants::Piece::bP) || state.pieces[square + 9] == asInt(constants::Piece::bP))
17+
return true;
18+
}
19+
20+
for (int move : knight_dir) {
21+
int piece = state.pieces[square + move];
22+
if (piece == asInt(constants::Square::OFFBOARD))
23+
continue;
24+
25+
if (constants::IS_KNIGHT[piece] && (constants::PIECE_COLOR[piece] == player))
26+
return state.pieces[square + move];
27+
}
28+
29+
for (int move : rook_dir) {
30+
int _square = square + move;
31+
int piece = state.pieces[_square];
32+
33+
while (piece != asInt(constants::Square::OFFBOARD)) {
34+
if (piece != asInt(constants::Piece::EMPTY)) {
35+
if (constants::IS_ROOK_QUEEN[piece] && constants::PIECE_COLOR[piece] == player)
36+
return true;
37+
break;
38+
}
39+
40+
_square += move;
41+
piece = state.pieces[_square];
42+
}
43+
}
44+
45+
for (int move : bishop_dir) {
46+
int _square = square + move;
47+
int piece = state.pieces[_square];
48+
49+
while (piece != asInt(constants::Square::OFFBOARD)) {
50+
if (piece != asInt(constants::Piece::EMPTY)) {
51+
if (constants::IS_BISHOP_QUEEN[piece] && constants::PIECE_COLOR[piece] == player)
52+
return true;
53+
break;
54+
}
55+
56+
_square += move;
57+
piece = state.pieces[_square];
58+
}
59+
}
60+
61+
62+
for (int move : king_dir) {
63+
int piece = state.pieces[square + move];
64+
65+
if (piece == asInt(constants::Square::OFFBOARD))
66+
continue;
67+
68+
if (constants::IS_KING[piece] && (constants::PIECE_COLOR[piece] == player))
69+
return state.pieces[square + move];
70+
}
71+
72+
return false;
73+
}

attack.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <array>
4+
5+
#include "board.hpp"
6+
7+
namespace attack {
8+
9+
const std::array<int, 8> knight_dir = { -8, -19, -21, -12, 8, 19, 21, 12 };
10+
const std::array<int, 4> rook_dir = { -1, -10, 1, 10 };
11+
const std::array<int, 4> bishop_dir = { -9, -11, 11, 9 };
12+
const std::array<int, 8> king_dir = { -1, -10, 1, 10, -9, -11, 11, 9};
13+
14+
int isSquareAttacked(int square, constants::Color player, board::BoardState& state);
15+
16+
}

bitboard.hpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <vector>
5+
#include <cassert>
6+
#include <cinttypes>
7+
#include <bitset>
8+
#include <string>
9+
10+
#include "constants.hpp"
11+
#include "util.hpp"
12+
13+
namespace bitboard
14+
{
15+
typedef std::uint64_t Bitboard;
16+
17+
constexpr bool hasBitAt(const Bitboard board, int position)
18+
{
19+
assert(position >= 0 && position < constants::SQUARES_AMOUNT);
20+
return (board >> position) & 1;
21+
}
22+
23+
constexpr Bitboard setBitAt(Bitboard board, int position)
24+
{
25+
assert(position >= 0 && position < constants::SQUARES_AMOUNT);
26+
return board | (static_cast<uint64_t>(1ULL) << position);
27+
}
28+
29+
constexpr Bitboard clearBitAt(Bitboard board, int position)
30+
{
31+
assert(position >= 0 && position < constants::SQUARES_AMOUNT);
32+
return board & ~(static_cast<uint64_t>(1ULL) << position);
33+
}
34+
35+
constexpr Bitboard mask(Bitboard board, Bitboard mask)
36+
{
37+
return board & mask;
38+
}
39+
40+
constexpr Bitboard swapMask(Bitboard board, Bitboard mask)
41+
{
42+
return board ^ mask;
43+
}
44+
45+
inline std::string toString(Bitboard board)
46+
{
47+
std::string s;
48+
49+
for (int rank = asInt(constants::Rank::_8); rank >= asInt(constants::Rank::_1); --rank)
50+
{
51+
for (int file = asInt(constants::File::A); file <= asInt(constants::File::H); file++)
52+
{
53+
int square = util::_120xyToSquare(file, rank);
54+
int _64_square = util::_120To64(square);
55+
56+
if (hasBitAt(board, _64_square))
57+
{
58+
s += "1 ";
59+
}
60+
else
61+
{
62+
s += "0 ";
63+
}
64+
}
65+
66+
s += "\n";
67+
}
68+
return s;
69+
}
70+
71+
#ifdef USE_ASM
72+
# ifndef _MSC_VER
73+
inline int bitscanForward(uint64_t x)
74+
{
75+
uint64_t r;
76+
77+
asm(
78+
"bsf %[x],%[r];"
79+
: [ r ] "=a"(r)
80+
: [ x ] "r"(x)
81+
);
82+
83+
return r;
84+
}
85+
86+
inline int bitscanReverse(uint64_t x)--
87+
{
88+
uint64_t r;
89+
90+
asm(
91+
"bsr %[x],%[r];"
92+
: [ r ] "=a"(r)
93+
: [ x ] "r"(x));
94+
95+
return r;
96+
}
97+
# else
98+
99+
inline int bitscanForward(uint64_t x) {
100+
unsigned long result = -1;
101+
_BitScanForward64(&result, x);
102+
return result;
103+
}
104+
105+
inline int bitscanReverse(uint64_t x) {
106+
unsigned long result = -1;
107+
_BitScanReverse64(&result, x);
108+
return result;
109+
}
110+
# endif
111+
#else
112+
#pragma message("USE_ASM not defined: Using slow alternative implementation of bitscan.")
113+
114+
inline int bitscanForward(uint64_t x)
115+
{
116+
int result = 0;
117+
while ((x & 1) == 0)
118+
{
119+
x >>= 1;
120+
++result;
121+
}
122+
123+
return result;
124+
}
125+
126+
inline int bitscanReverse(uint64_t x)
127+
{
128+
int result = 0;
129+
while ((x & (0ULL | 1ULL << 63)) == 0)
130+
{
131+
x <<= 1;
132+
++result;
133+
}
134+
135+
return result;
136+
}
137+
#endif
138+
139+
inline int countBits(Bitboard board)
140+
{
141+
int bit_count = 0;
142+
Bitboard current_board = board;
143+
144+
while (current_board)
145+
{
146+
current_board = current_board >> (1 + bitscanForward(current_board));
147+
bit_count++;
148+
}
149+
150+
return bit_count;
151+
}
152+
153+
} // namespace bitboard

0 commit comments

Comments
 (0)