aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/uci.cpp99
-rw-r--r--src/uci.hpp4
2 files changed, 58 insertions, 45 deletions
diff --git a/src/uci.cpp b/src/uci.cpp
index 1854019..b1fc1b7 100644
--- a/src/uci.cpp
+++ b/src/uci.cpp
@@ -8,8 +8,10 @@
#include <fstream>
#include <iomanip>
#include <iostream>
+#include <print>
#include <sstream>
#include <string>
+#include <type_traits>
#include <unordered_map>
#include "board/board.hpp"
@@ -20,6 +22,9 @@
#include "uci.hpp"
#include "zobrist.hpp"
+static const std::string starting_fen =
+ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
+
std::ofstream uciLog("cache/uci_log.txt", std::ios::app);
void LogUci(const std::string &message) {
@@ -37,7 +42,7 @@ void LogUci(const std::string &message) {
}
using namespace std;
-Move UciToMove(const string uci) {
+Move UciToMove(const string &uci) {
Position from;
Position to;
PieceType promotion = NONEPIECE;
@@ -73,11 +78,11 @@ Move UciToMove(const string uci) {
}
}
- return {from, to, promotion};
+ return {.From = from, .To = to, .promotion = promotion};
}
Game initBoard(
- string startingFEN,
+ const std::string &startingFEN,
std::unordered_map<uint64_t, TranspositionsEntry> *transPositions) {
Game b{};
b.enPassant = IndexToPosition(0); // default value
@@ -89,8 +94,8 @@ Game initBoard(
b.Transpositions = transPositions;
Piece EmptyPiece = {
- false,
- NONEPIECE,
+ .color = false,
+ .type = NONEPIECE,
};
for (int i = 0; i < 64; i++) {
b.pieces[i] = EmptyPiece;
@@ -109,7 +114,7 @@ static void PrintBitboard(uint64_t bb, const std::string &name) {
std::printf("%d | ", 8 - rank);
for (int file = 0; file < 8; file++) {
- int sq = rank * 8 + file;
+ int sq = (rank * 8) + file;
std::cout << (((bb >> sq) & 1ULL) ? "1 " : ". ");
}
@@ -135,7 +140,7 @@ static void DebugBitboards(const Game *g) {
std::cout << "Piece Bitboards\n";
std::cout << "========================\n";
- for (int color = 0; color < 2; ++color) {
+ for (bool color : {false, true}) {
std::cout << (color ? "White\n" : "Black\n");
for (int type = PAWN; type <= KING; ++type) {
@@ -143,17 +148,34 @@ static void DebugBitboards(const Game *g) {
}
}
}
+static void printPromotionLetter(PieceType p) {
+ if (p == NONEPIECE) {
+ return;
+ }
+ switch (p) {
+ case QUEEN:
+ cout << "q";
+ break;
+ case ROOK:
+ cout << "r";
+ break;
+ case KNIGHT:
+ cout << "n";
+ break;
+ case BISHOP:
+ cout << "b";
+ break;
+ default:
+ assert(false && "Unexpected promotion type");
+ }
+}
void Uci() {
unordered_map<uint64_t, TranspositionsEntry> transpositions = {};
- Game game =
- initBoard("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
- &transpositions);
- string line;
+ Game game = initBoard(starting_fen, &transpositions);
+ string cmd;
- while (getline(cin, line)) {
- string cmd = line;
- LogUci("GUI -> ENGINE: " + line);
- transform(cmd.begin(), cmd.end(), cmd.begin(), ::tolower);
+ while (getline(cin, cmd)) {
+ LogUci("GUI -> ENGINE: " + cmd);
if (cmd == "quit") {
break;
@@ -163,9 +185,13 @@ void Uci() {
std::printf("%d |", 8 - rank);
for (int file = 0; file < 8; file++) {
- Piece piece = game.pieces[rank * 8 + file];
+ Piece piece = game.pieces[(rank * 8) + file];
- std::printf(" %d%d", piece.type, piece.color);
+ if (piece.type == NONEPIECE) {
+ print(" 00");
+ continue;
+ };
+ std::printf(" %d%c", piece.type, piece.color ? 'W' : 'B');
}
std::printf(" | %d\n", 8 - rank);
@@ -186,7 +212,7 @@ void Uci() {
cout << "readyok\n";
cout.flush();
} else if (cmd.starts_with("go")) {
- stringstream ss(line);
+ stringstream ss(cmd);
string token;
int depth = -1;
@@ -220,36 +246,23 @@ void Uci() {
exit(1);
}
move_options options = {
- .wtime = wtime, .btime = btime, .wncr = wncr, .bncr = bncr};
+ .wtime = wtime,
+ .btime = btime,
+ .wncr = wncr,
+ .bncr = bncr,
+ };
Move best = GetBestMove(&game, depth, options);
cout << "bestmove ";
cout << static_cast<char>(best.From.file + 'a') << 1 + best.From.rank
<< static_cast<char>(best.To.file + 'a') << 1 + best.To.rank;
- if (best.promotion != NONEPIECE) {
- switch (best.promotion) {
- case QUEEN:
- cout << "q";
- break;
- case ROOK:
- cout << "r";
- break;
- case KNIGHT:
- cout << "n";
- break;
- case BISHOP:
- cout << "b";
- break;
- default:
- assert(false && "Unexpected promotion type");
- }
- }
+ printPromotionLetter(best.promotion);
cout << "\n";
cout.flush();
} else if (cmd.starts_with("position")) {
- stringstream ss(line);
+ stringstream ss(cmd);
string token;
ss >> token; // position
@@ -257,9 +270,7 @@ void Uci() {
ss >> token;
if (token == "startpos") {
- game = initBoard(
- "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
- &transpositions);
+ game = initBoard(starting_fen, &transpositions);
} else if (token == "fen") {
string fen;
string part;
@@ -267,8 +278,9 @@ void Uci() {
// FEN has spaces, need 6 parts
for (int i = 0; i < 6; i++) {
ss >> part;
- if (i)
+ if (i != 0) {
fen += " ";
+ }
fen += part;
}
@@ -277,8 +289,9 @@ void Uci() {
// check for moves
while (ss >> token) {
- if (token == "moves")
+ if (token == "moves") {
break;
+ }
}
while (ss >> token) {
diff --git a/src/uci.hpp b/src/uci.hpp
index 372f007..8160442 100644
--- a/src/uci.hpp
+++ b/src/uci.hpp
@@ -5,11 +5,11 @@
#include <string>
#include <unordered_map>
-Move UciToMove(const std::string uci);
+Move UciToMove(const std::string &uci);
void LogUci(const std::string &message);
void Uci();
Game initBoard(
- std::string startingFEN,
+ const std::string &startingFEN,
std::unordered_map<uint64_t, TranspositionsEntry> *transPositions);
void SaveTranspositionTable(
const std::unordered_map<uint64_t, TranspositionsEntry> &table);