Skip to content

Commit 69b860d

Browse files
optimized the isSquareAttacked function
1 parent 7dea305 commit 69b860d

File tree

5 files changed

+11
-15
lines changed

5 files changed

+11
-15
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_subdirectory(src)
2626
target_link_libraries(ChessEngine src)
2727

2828
if (NOT WASM)
29-
add_subdirectory(tests)
29+
# add_subdirectory(tests)
3030
endif ()
3131

3232
if (WASM)

src/CLI.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Interface {
1111
void CLI::start() {
1212
board = ChessBoard();
1313
Instruction currentInstruction;
14+
currentInstruction.command = invalid;
1415
while (currentInstruction.command != quit) {
1516
std::string input;
1617
std::getline(std::cin, input);

src/Move.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ namespace Moves {
8888
{-21, -19, -12, -8, 8, 12, 19, 21},
8989
{-11, -9, 9, 11, 0, 0, 0, 0},
9090
{-10, -1, 1, 10, 0, 0, 0, 0},
91-
{-11, -10, -9, -1, 1, 9, 10, 11},
92-
{-11, -10, -9, -1, 1, 9, 10, 11}
91+
{-11, -10, -9, -1, 11, 10, 9, 1},
92+
{-11, -10, -9, -1, 11, 10, 9, 1}
9393
};
9494
}
9595

src/MoveGenerator.cpp

+7-11
Original file line numberDiff line numberDiff line change
@@ -174,27 +174,27 @@ std::vector<Move> MoveGenerator::tacticalMoves(const ChessBoard&board) {
174174
}
175175

176176

177-
bool MoveGenerator::isSquareAttacked(const ChessBoard&board, int_fast8_t square, Color color) {
177+
bool MoveGenerator::isSquareAttacked(const ChessBoard&board, const int_fast8_t square, const Color color) {
178178
const int_fast8_t sign = (color == WHITE) ? -1 : 1;
179179

180180
//PAWNS
181181
for (int i = 1; i < 3; ++i) {
182182
const int_fast8_t n = MAILBOX[MAILBOX64[square] + (sign * OFFSET[PAWN][i])];
183183
if (n != -1) {
184-
if (board.squares[n].color != color && board.squares[n].type == PAWN) return true;
184+
if (board.squares[n].type == PAWN && board.squares[n].color != color) return true;
185185
}
186186
}
187187
//KNIGHTS
188188
for (int i = 0; i < OFFSETS[KNIGHT]; ++i) {
189189
const int_fast8_t n = MAILBOX[MAILBOX64[square] + OFFSET[KNIGHT][i]];
190190
if (n != -1) {
191-
if (board.squares[n].color != color && board.squares[n].type == KNIGHT) return true;
191+
if (board.squares[n].type == KNIGHT && board.squares[n].color != color) return true;
192192
}
193193
}
194194
//REST
195195
for (int i = 0; i < OFFSETS[QUEEN]; ++i) {
196196
int_fast8_t n = square;
197-
int_fast8_t offset = OFFSET[QUEEN][i];
197+
const int_fast8_t offset = OFFSET[QUEEN][i];
198198
bool sliding = false;
199199
while (true) {
200200
n = MAILBOX[MAILBOX64[n] + offset];
@@ -203,9 +203,9 @@ bool MoveGenerator::isSquareAttacked(const ChessBoard&board, int_fast8_t square,
203203
if (target.type != EMPTY) {
204204
if (target.color != color && (SLIDE[target.type] || !sliding) && target.type != PAWN &&
205205
target.type != KNIGHT) {
206-
const int_fast8_t* offsetPtr = std::find(OFFSET[target.type], OFFSET[target.type] + OFFSETS[target.type],
207-
offset);
208-
if (offsetPtr != OFFSET[target.type] + OFFSETS[target.type]) return true;
206+
if (target.type == QUEEN || target.type == KING) return true;
207+
if (i % 2 == 0) {if (target.type == BISHOP) return true;}
208+
else {if (target.type == ROOK) return true;}
209209
}
210210
break;
211211
}
@@ -232,10 +232,6 @@ unsigned long long MoveGenerator::perft(int depth, ChessBoard&board) {
232232
board.makeMove(move);
233233
if (!inCheck(board, invertColor(board.sideToMove))) {
234234
const unsigned long long childNodes = perft(depth - 1, board);
235-
// if(depth == 6){
236-
// printf("Move: %s-%s %d\n", Util::positionToString(move.start).c_str(), Util::positionToString(move.end).c_str(), move.flag);
237-
// printf("Nodes: %llu\n", childNodes);
238-
// }
239235
nodes += childNodes;
240236
}
241237
board.unMakeMove();

src/Search.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ int Search::alphaBeta(const int depth, int alpha, int beta, const int ply) {
148148
bool shouldFullSearch = true;
149149
if (i > 2 && depth > 3 && !move.tactical()) {
150150
score = -alphaBeta(depth - 2, -alpha - 1, -alpha, ply + 1);
151-
152151
shouldFullSearch = score > alpha;
153152
}
154153

0 commit comments

Comments
 (0)