Skip to content

Commit

Permalink
fixed bug where mate in x was causing a crash in review
Browse files Browse the repository at this point in the history
  • Loading branch information
camtisocial committed Feb 7, 2025
1 parent 9e64249 commit e140c0b
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 26 deletions.
71 changes: 51 additions & 20 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void printBoardWhite(std::vector<std::vector<std::shared_ptr<ChessPiece>>> board
int openingLen = opening.length();
int evalLen = currentCentipawnEval.length();
int openingSpaces = terminalWidth - 18 - openingLen;
int evalSpaces = terminalWidth - 29 - evalLen;
int evalSpaces = terminalWidth - 33;

std::cout << std::endl;
if (to_play) {
Expand All @@ -245,7 +245,6 @@ void printBoardWhite(std::vector<std::vector<std::shared_ptr<ChessPiece>>> board
std::cout << whitePieces << " White " << "\x1B[37m" << "to play" << "\033[0m" << std::string(openingSpaces, ' ') << opening << std::endl;
}


if (evalSetting) {
//to account for the fact that I am only getting whole turn numbers from fen readings
int turnIncrement{};
Expand All @@ -257,19 +256,37 @@ void printBoardWhite(std::vector<std::vector<std::shared_ptr<ChessPiece>>> board
turnIncrement = ((turn-1)*2)+1;
}

//account for eval being mate
if (evalHistory[turnIncrement].find("mate") != std::string::npos) {

if (to_play) {
if(evalHistory[turnIncrement] == "mate 0") {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << "##" << std::endl;
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << evalHistory[turnIncrement] << std::endl;
}
} else {
if(evalHistory[turnIncrement] == "mate 0") {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << "##" << std::endl;
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << evalHistory[turnIncrement] << std::endl;
}
}

} else {
int evalAsInt = std::stoi(evalHistory[turnIncrement]);
float formattedEval = evalAsInt/100.0;

if (evalAsInt > 0) {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: +" << whitePieces << formattedEval << std::endl;
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: +" << whitePieces << formattedEval << std::endl;
} else if (evalAsInt < 0) {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << formattedEval << std::endl;
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << formattedEval << std::endl;
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << formattedEval << std::endl;
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << formattedEval << std::endl;
}
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::endl;
}
}} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::endl;
}

std::cout << std::endl;
std::cout << std::endl;
Expand Down Expand Up @@ -361,7 +378,7 @@ void printBoardBlack(std::vector<std::vector<std::shared_ptr<ChessPiece>>> board

int openingLen = opening.length();
int openingSpaces = terminalWidth - 18 - openingLen;
int evalSpaces = terminalWidth - 29 - currentCentipawnEval.length();
int evalSpaces = terminalWidth - 33;

std::cout << std::endl;
if (to_play) {
Expand All @@ -370,9 +387,8 @@ void printBoardBlack(std::vector<std::vector<std::shared_ptr<ChessPiece>>> board
std::cout << whitePieces << " White " << "\x1B[37m" << "to play" << "\033[0m" << std::string(openingSpaces, ' ') << opening << std::endl;
}


if (evalSetting) {

//to account for the fact that I am only getting whole turn numbers from fen readings
int turnIncrement{};
if (turn == 1 && to_play == 0) {
turnIncrement = 0;
Expand All @@ -382,22 +398,37 @@ void printBoardBlack(std::vector<std::vector<std::shared_ptr<ChessPiece>>> board
turnIncrement = ((turn-1)*2)+1;
}

//account for eval being mate
if (evalHistory[turnIncrement].find("mate") != std::string::npos) {

if (to_play) {
if(evalHistory[turnIncrement] == "mate 0") {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << "##" << std::endl;
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << evalHistory[turnIncrement] << std::endl;
}
} else {
if(evalHistory[turnIncrement] == "mate 0") {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << "##" << std::endl;
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << evalHistory[turnIncrement] << std::endl;
}
}

} else {
int evalAsInt = std::stoi(evalHistory[turnIncrement]);
float formattedEval = evalAsInt/100.0;

if (evalAsInt > 0) {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: +" << whitePieces << formattedEval << std::endl;
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: +" << whitePieces << formattedEval << std::endl;
} else if (evalAsInt < 0) {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << formattedEval << std::endl;
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << blackPieces << formattedEval << std::endl;
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << formattedEval << std::endl;
std::cout << " Turn: " << static_cast<int>(turn) << std::string(evalSpaces, ' ') << " Evaluation: " << whitePieces << formattedEval << std::endl;
}
} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::endl;
}



}} else {
std::cout << " Turn: " << static_cast<int>(turn) << std::endl;
}

std::cout << std::endl;
std::cout << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "main.h"

//BUG: review crashes on checkmate i think
//BUG: stockfish eval numbers seem random
//config
Config config = parseConfig("/usr/share/terminalChess/settings.ini");
int localPort = config.local_port;
Expand Down
23 changes: 17 additions & 6 deletions src/stockFishInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stockFishInterface.h"


namespace bp = boost::process;

void getStockFishEval(std::string fen, std::string stockfish_path,
Expand All @@ -12,15 +11,15 @@ void getStockFishEval(std::string fen, std::string stockfish_path,
std::string evaluatedPosition;
std::string logFile = "./stockfish.txt";

//stockfish_input << "uci\n";
stockfish_input << "isready\n";
stockfish_input << "position fen " << fen << "\n";
stockfish_input << "d\n";
stockfish_input << "go depth " << std::to_string(depth) <<"\n";
stockfish_input << "go depth " << std::to_string(depth) << "\n";
stockfish_input.flush();

std::string line;
while (std::getline(stockfish_output, line)) {

// Check for centipawn score
if (line.find("score cp") != std::string::npos) {
std::istringstream iss(line);
std::string token;
Expand All @@ -31,8 +30,21 @@ void getStockFishEval(std::string fen, std::string stockfish_path,
}
}
}
// Check for mate score
else if (line.find("score mate") != std::string::npos) {
std::istringstream iss(line);
std::string token;
while (iss >> token) {
if (token == "mate") {
iss >> evaluatedPosition;
evaluatedPosition = "mate " + evaluatedPosition; // Store as "mate X"
break;
}
}
}

if (line.find("bestmove") != std::string::npos) {
break;
break;
}
}

Expand All @@ -41,6 +53,5 @@ void getStockFishEval(std::string fen, std::string stockfish_path,

currentCentipawnEval = evaluatedPosition;
evalHistory.push_back(evaluatedPosition);

}

1 change: 1 addition & 0 deletions src/stockFishInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>
#include <mutex>
#include <atomic>
#include <regex>

void getStockFishEval(std::string fen, std::string stockfish_path,
int depth, std::vector<std::string>& evalHistory, std::string& currentCentipawnEval);
Expand Down
77 changes: 77 additions & 0 deletions src/stockfish.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
readyok
info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
info depth 1 seldepth 1 multipv 1 score cp 38 nodes 20 nps 10000 tbhits 0 time 2 pv d2d4
info depth 2 seldepth 2 multipv 1 score cp 82 nodes 51 nps 25500 tbhits 0 time 2 pv e2e4 a7a6
info depth 3 seldepth 3 multipv 1 score cp 55 nodes 154 nps 77000 tbhits 0 time 2 pv e2e4 c7c6 d2d4
info depth 4 seldepth 4 multipv 1 score cp 22 nodes 807 nps 201750 tbhits 0 time 4 pv g1f3 d7d5 d2d4 g8f6
info depth 5 seldepth 5 multipv 1 score cp 54 nodes 1061 nps 265250 tbhits 0 time 4 pv e2e4 c7c5 g1f3
info depth 6 seldepth 6 multipv 1 score cp 54 nodes 1761 nps 293500 tbhits 0 time 6 pv e2e4 c7c5 g1f3 d7d5 e4d5 d8d5
info depth 7 seldepth 8 multipv 1 score cp 50 nodes 5459 nps 389928 tbhits 0 time 14 pv e2e4 e7e5 g1f3 b8c6 b1c3 g8f6 d2d4
info depth 8 seldepth 8 multipv 1 score cp 50 nodes 6998 nps 411647 tbhits 0 time 17 pv e2e4 e7e5 g1f3 b8c6 d2d4 e5d4 f3d4 g8f6 b1c3
info depth 9 seldepth 11 multipv 1 score cp 54 nodes 12053 nps 415620 tbhits 0 time 29 pv e2e4 e7e5 g1f3 b8c6 d2d4 e5d4 f3d4 g8f6 d4c6
info depth 10 seldepth 13 multipv 1 score cp 35 nodes 28785 nps 417173 tbhits 0 time 69 pv e2e4 e7e6 d2d4 d7d5 e4d5 e6d5 g1f3 f8d6 f1d3 g8f6 e1g1 e8g8
info depth 11 seldepth 14 multipv 1 score cp 50 nodes 34551 nps 426555 tbhits 0 time 81 pv e2e4 e7e5 g1f3 b8c6 d2d4 e5d4 f3d4 g8f6 b1c3 f8b4
info depth 12 seldepth 18 multipv 1 score cp 37 nodes 103152 nps 390727 tbhits 0 time 264 pv e2e4 c7c5 g1f3 d7d6 f1b5 b8d7 e1g1 a7a6 b5d7 c8d7 f1e1 e7e6 c2c3 g8f6 d2d4
info depth 13 seldepth 19 multipv 1 score cp 33 nodes 166195 nps 401437 tbhits 0 time 414 pv e2e4 c7c5 g1f3 d7d6 f1b5 b8d7 c2c4 g8f6 d2d3 g7g6 b1c3 f8g7
info depth 14 seldepth 18 multipv 1 score cp 38 nodes 226798 nps 393064 tbhits 0 time 577 pv e2e4 c7c5 g1f3 d7d6 f1b5 b8d7 c2c4 e7e6 b1c3 g8f6 e1g1 f8e7 d2d4 c5d4 f3d4 a7a6 b5a4 e8g8
bestmove e2e4 ponder c7c5
Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
readyok
info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
info depth 1 seldepth 1 multipv 1 score cp 125 nodes 20 nps 20000 tbhits 0 time 1 pv e7e5
info depth 2 seldepth 2 multipv 1 score cp 172 nodes 44 nps 44000 tbhits 0 time 1 pv e7e5 a2a3
info depth 3 seldepth 3 multipv 1 score cp 169 nodes 79 nps 39500 tbhits 0 time 2 pv d7d5 c2c3 e7e5
info depth 4 seldepth 4 multipv 1 score cp 61 nodes 852 nps 213000 tbhits 0 time 4 pv b8c6 d2d4 e7e5 d4e5
info depth 5 seldepth 5 multipv 1 score cp 58 nodes 1401 nps 233500 tbhits 0 time 6 pv e7e5 d2d4 e5d4 d1d4 d7d5 e2e4
info depth 6 seldepth 6 multipv 1 score cp 109 nodes 1617 nps 231000 tbhits 0 time 7 pv e7e5 e2e4 f8c5 g1e2
info depth 7 seldepth 7 multipv 1 score cp 113 nodes 3662 nps 281692 tbhits 0 time 13 pv e7e5 b1c3 d7d5 d2d4 e5d4 d1d4 g8f6
info depth 8 seldepth 8 multipv 1 score cp 82 nodes 6714 nps 353368 tbhits 0 time 19 pv e7e5 e2e4 f8c5 b1c3 b8c6 g1e2 a7a5 c3a4
info depth 9 seldepth 13 multipv 1 score cp 73 nodes 21023 nps 396660 tbhits 0 time 53 pv e7e5 e2e3 b8c6 b1c3 d7d5 d2d4 g8f6 d4e5 c6e5 e3e4 f8c5 c3d5 f6d5 d1d5
info depth 10 seldepth 16 multipv 1 score cp 105 nodes 31545 nps 399303 tbhits 0 time 79 pv e7e5 b1c3 b8c6 e2e4 g8f6 f1b5 c6d4
info depth 11 seldepth 12 multipv 1 score cp 113 nodes 45934 nps 410125 tbhits 0 time 112 pv e7e5 e2e3 b8c6 c2c4 g8f6 b1c3 d7d5 c4d5 f6d5 f1b5 d8h4 g2g3
info depth 12 seldepth 17 multipv 1 score cp 82 nodes 112483 nps 400295 tbhits 0 time 281 pv e7e5 b1c3 b8c6 e2e4 g8f6 f1c4 c6d4 g1e2 c7c6 c4b3 d7d5 d2d3 d4b3 a2b3
info depth 13 seldepth 19 multipv 1 score cp 87 nodes 209001 nps 394341 tbhits 0 time 530 pv e7e5 e2e4 b8c6 f1c4 f8c5 d2d3 a7a6 b1c3 g8e7 g1e2 e8g8
info depth 14 seldepth 16 multipv 1 score cp 93 nodes 267684 nps 391923 tbhits 0 time 683 pv e7e5 e2e4 b8c6 b1c3 f8c5 f3f4 d7d6 g1f3 c8g4 f1e2 f7f5 f4e5 d6e5 d2d3 g8f6
bestmove e7e5 ponder e2e4
Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
readyok
info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
info depth 1 seldepth 1 multipv 1 score cp -75 nodes 46 nps 23000 tbhits 0 time 2 pv d2d4
info depth 2 seldepth 2 multipv 1 score cp -69 nodes 67 nps 33500 tbhits 0 time 2 pv d2d4 e5d4
info depth 3 seldepth 3 multipv 1 score cp -69 nodes 219 nps 73000 tbhits 0 time 3 pv d2d4 e5d4 d1d4
info depth 4 seldepth 4 multipv 1 score cp -82 nodes 536 nps 107200 tbhits 0 time 5 pv b1c3 d7d5 d2d4 e5d4
info depth 5 seldepth 5 multipv 1 score cp -56 nodes 799 nps 133166 tbhits 0 time 6 pv b1c3 g8f6 e2e4
info depth 6 seldepth 6 multipv 1 score cp -104 nodes 2869 nps 220692 tbhits 0 time 13 pv e2e4 d7d5 e4d5 g8f6 d2d4 e5d4
info depth 7 seldepth 7 multipv 1 score cp -90 nodes 3699 nps 231187 tbhits 0 time 16 pv b1c3 d7d5 d2d4 e5d4 d1d4 g8f6 e2e4
info depth 8 seldepth 9 multipv 1 score cp -98 nodes 8445 nps 248382 tbhits 0 time 34 pv e2e4 f8c5 b1c3 b8c6 f1c4 c5g1 h1g1 d8h4 e1f1
info depth 9 seldepth 12 multipv 1 score cp -69 nodes 16772 nps 284271 tbhits 0 time 59 pv e2e3 g8f6 d2d4 e5d4 e3d4 d7d5 g1e2
info depth 10 seldepth 15 multipv 1 score cp -84 nodes 68821 nps 364132 tbhits 0 time 189 pv e2e3 b8c6 b1c3 d7d5 d2d4 g8f6 f1b5 f8d6 g1e2 e8g8
info depth 11 seldepth 14 multipv 1 score cp -84 nodes 84215 nps 377645 tbhits 0 time 223 pv e2e3 b8c6 b1c3 g8f6 f1b5 d7d5 d2d4 f8d6 g1e2 e8g8 e1g1 f8e8
info depth 12 seldepth 15 multipv 1 score cp -81 nodes 140708 nps 396360 tbhits 0 time 355 pv b1c3 b8c6 e2e3 g8f6 d2d4 d7d5 d4e5 c6e5 e3e4 f8b4 d1d4
info depth 13 seldepth 20 multipv 1 score cp -84 nodes 268191 nps 394979 tbhits 0 time 679 pv b1c3 g8f6 e2e4 d7d5 d2d4 e5d4 d1d4 b8c6 d4f2 d5d4 c3d1 f8c5 f1d3 e8g8
info depth 14 seldepth 19 multipv 1 score cp -75 nodes 317415 nps 399767 tbhits 0 time 794 pv b1c3 g8f6 e2e4 d7d5 d2d4 e5d4 d1d4 b8c6 f1b5 c8d7 b5c6 d7c6 c1g5 d5e4 d4d8 a8d8
bestmove b1c3 ponder g8f6
Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
readyok
info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
info depth 1 seldepth 1 multipv 1 score mate 1 nodes 31 nps 15500 tbhits 0 time 2 pv d8h4
info depth 2 seldepth 2 multipv 1 score mate 1 nodes 61 nps 30500 tbhits 0 time 2 pv d8h4
info depth 3 seldepth 2 multipv 1 score mate 1 nodes 91 nps 45500 tbhits 0 time 2 pv d8h4
info depth 4 seldepth 2 multipv 1 score mate 1 nodes 121 nps 60500 tbhits 0 time 2 pv d8h4
info depth 5 seldepth 2 multipv 1 score mate 1 nodes 151 nps 75500 tbhits 0 time 2 pv d8h4
info depth 6 seldepth 2 multipv 1 score mate 1 nodes 181 nps 90500 tbhits 0 time 2 pv d8h4
info depth 7 seldepth 2 multipv 1 score mate 1 nodes 211 nps 70333 tbhits 0 time 3 pv d8h4
info depth 8 seldepth 2 multipv 1 score mate 1 nodes 241 nps 80333 tbhits 0 time 3 pv d8h4
info depth 9 seldepth 2 multipv 1 score mate 1 nodes 271 nps 90333 tbhits 0 time 3 pv d8h4
info depth 10 seldepth 2 multipv 1 score mate 1 nodes 301 nps 100333 tbhits 0 time 3 pv d8h4
info depth 11 seldepth 2 multipv 1 score mate 1 nodes 331 nps 110333 tbhits 0 time 3 pv d8h4
info depth 12 seldepth 2 multipv 1 score mate 1 nodes 361 nps 120333 tbhits 0 time 3 pv d8h4
info depth 13 seldepth 2 multipv 1 score mate 1 nodes 391 nps 130333 tbhits 0 time 3 pv d8h4
info depth 14 seldepth 2 multipv 1 score mate 1 nodes 421 nps 140333 tbhits 0 time 3 pv d8h4
bestmove d8h4
Stockfish 14.1 by the Stockfish developers (see AUTHORS file)
readyok
info string NNUE evaluation using nn-13406b1dcbe0.nnue enabled
info depth 0 score mate 0
bestmove (none)

0 comments on commit e140c0b

Please sign in to comment.