aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-07-30 09:18:53 +0200
committerAdam <adammegarules1@gmail.com>2026-07-30 09:18:53 +0200
commita6a5cb9b092e2162c8d9ec3a63be49c1bfbbe785 (patch)
treec82db1857176035d024092f1e8a2af943f0c27e2
parent009c245757fc1ebbaa0212f89a4147d21d8b47e5 (diff)
fixng
-rw-r--r--src/board/board.cpp75
-rw-r--r--src/uci.cpp23
2 files changed, 53 insertions, 45 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp
index a76ec42..22db36e 100644
--- a/src/board/board.cpp
+++ b/src/board/board.cpp
@@ -2,6 +2,7 @@
#include "../moves.hpp"
#include "zobrist/zobrist.hpp"
#include <cassert>
+#include <cstdlib>
Piece createPiece(PieceType type, bool color) {
Piece piece;
@@ -69,11 +70,11 @@ UndoMove MakeMove(Move move, Game *g) {
if (piece.type == PAWN && g->canEnpassant && move.To == g->enPassant) {
Position capturedPawn = move.To;
capturedPawn.rank += piece.color ? 1 : -1;
- g->pieces[PositionToIndex(capturedPawn)] = {false, NONEPIECE};
undo.wasEnPassantCapture = true;
undo.enPassantCapturedSquare = capturedPawn;
undo.enPassantCapturedPiece = g->pieces[PositionToIndex(capturedPawn)];
+ g->pieces[PositionToIndex(capturedPawn)] = {false, NONEPIECE};
}
g->canEnpassant = false;
@@ -97,33 +98,38 @@ UndoMove MakeMove(Move move, Game *g) {
}
// caslte
- if (piece.type == KING && move.From.rank == (piece.color ? 0 : 7) &&
- (move.From.file + 2 == move.To.file ||
- move.From.file - 2 == move.To.file)) {
+ if (piece.type == KING && move.From.rank == (piece.color ? 7 : 0) &&
+ std::abs(move.To.file - move.From.file) == 2) {
undo.wasCastle = true;
- undo.CastledSide = (move.To.file > move.From.file);
- if (move.From.file + 2 == move.To.file) {
- Position rook_pos = move.To;
- rook_pos.file += 1;
- Piece rook = g->pieces[PositionToIndex(rook_pos)];
- g->pieces[PositionToIndex(rook_pos)] = {false, NONEPIECE};
- rook_pos = move.From;
- rook_pos.file -= 1;
- g->pieces[PositionToIndex(rook_pos)] = rook;
- }
- if (move.From.file - 2 == move.To.file) {
- Position rook_pos = move.To;
- rook_pos.file -= 1;
- Piece rook = g->pieces[PositionToIndex(rook_pos)];
- g->pieces[PositionToIndex(rook_pos)] = {false, NONEPIECE};
- rook_pos = move.From;
- rook_pos.file += 1;
- g->pieces[PositionToIndex(rook_pos)] = rook;
+ undo.CastledSide = move.To.file > move.From.file;
+
+ if (undo.CastledSide) {
+ Position rookFrom = move.From;
+ rookFrom.file = 7;
+
+ Position rookTo = move.From;
+ rookTo.file = 5;
+
+ g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
+ g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE};
+ } else {
+ // Queenside: a -> d
+ Position rookFrom = move.From;
+ rookFrom.file = 0;
+
+ Position rookTo = move.From;
+ rookTo.file = 3;
+
+ g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
+ g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE};
}
- };
+ }
- // removing caslte rights
+ /*
+ * remove castling right if king has moved
+ * but only for color of the king
+ */
if (piece.type == KING) {
if (piece.color) {
g->whiteCastleKing = false;
@@ -219,27 +225,28 @@ void UnMakeMove(UndoMove undo, Game *g) {
if (undo.wasCastle) {
if (undo.CastledSide) {
- // rook f-file -> h-file
- Position rookFrom = undo.to;
- rookFrom.file -= 1; // f1/f8
+ // f -> h
+ Position rookFrom = undo.from;
+ rookFrom.file = 5;
- Position rookTo = undo.to;
- rookTo.file += 1; // h1/h8
+ Position rookTo = undo.from;
+ rookTo.file = 7;
g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE};
} else {
- // rook d-file -> a-file
- Position rookFrom = undo.to;
- rookFrom.file += 1; // d1/d8
+ // d -> a
+ Position rookFrom = undo.from;
+ rookFrom.file = 3;
- Position rookTo = undo.to;
- rookTo.file -= 2; // a1/a8
+ Position rookTo = undo.from;
+ rookTo.file = 0;
g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)];
g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE};
}
}
+
g->ThreeFoldMap[undo.zobristKey] -= 1;
if (g->ThreeFoldMap[undo.zobristKey] <= 0) {
g->ThreeFoldMap.erase(undo.zobristKey);
diff --git a/src/uci.cpp b/src/uci.cpp
index 70bf648..e425cde 100644
--- a/src/uci.cpp
+++ b/src/uci.cpp
@@ -3,6 +3,7 @@
#include "board/fen.hpp"
#include "bot.hpp"
#include "moves.hpp"
+#include "zobrist/zobrist.hpp"
#include <algorithm>
#include <cassert>
@@ -10,7 +11,6 @@
#include <cstdint>
#include <cstdio>
#include <iostream>
-#include <print>
#include <sstream>
#include <string>
@@ -98,6 +98,8 @@ Game initBoard(
setBoardFen(startingFEN, &b);
+ uint64_t key = GenerateZobristKey(&b);
+ b.ThreeFoldMap[key] = 1;
return b;
};
void Uci() {
@@ -121,18 +123,17 @@ void Uci() {
cout.flush();
}
if (cmd == "board") {
- for (int i = 0; i < 64; i++) {
- Piece piece = game.pieces[i];
- if (piece.type == NONEPIECE) {
- cout << " --- ";
- continue;
- }
- printf(" %d%d", piece.type, piece.color);
- if (i % 8 == 0) {
- println();
+ for (int rank = 0; rank < 8; rank++) {
+ std::printf("%d |", 8 - rank);
+
+ for (int file = 0; file < 8; file++) {
+ Piece piece = game.pieces[rank * 8 + file];
+
+ std::printf(" %d%d", piece.type, piece.color);
}
+
+ std::printf(" | %d\n", 8 - rank);
}
- cout.flush();
}
if (cmd == "load") {
LoadTable("cache/positions.txt", &transpositions);