Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
archishou committed Feb 2, 2023
1 parent d70c923 commit eee67ff
Show file tree
Hide file tree
Showing 11 changed files with 1,877 additions and 31,603 deletions.
10 changes: 5 additions & 5 deletions ChessEngine/ChessEngine_lib/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include "string"

const std::string UCI_DEBUG_STRING = "info string ";
static const std::string UCI_DEBUG_STRING = "info string ";

const std::string INITIAL_BOARD_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
const std::string KIWIPETE_FEN = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1";
const std::string TALKCHESS_FEN = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8";
const std::string PERFT_RESULTS_POS4_FEN = "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1";
static const std::string INITIAL_BOARD_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
static const std::string KIWIPETE_FEN = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1";
static const std::string TALKCHESS_FEN = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8";
static const std::string PERFT_RESULTS_POS4_FEN = "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1";
static const std::string& ASSERTION_ERR_SEARCH_DEPTH = "Test not completed, did not search deep enough";
31,460 changes: 0 additions & 31,460 deletions ChessEngine/ChessEngine_lib/engine_uci_inputs_diagnostics.txt

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions ChessEngine/ChessEngine_lib/move_search/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "move_generation/position.h"
#include "alphabeta.h"
#include "constants.h"

typedef Move Line[MAX_DEPTH];
const int LINE_SIZE = MAX_DEPTH;
Expand Down Expand Up @@ -58,7 +59,7 @@ std::ostream& operator<<(std::ostream& os, const BestMoveSearchResults& results)
return os;
}

void update_best_move_results(BestMoveSearchResults& search_results, AlphaBetaData& ab_results, int sub_depth) {
void update_best_move_results(BestMoveSearchResults& search_results, AlphaBetaData& ab_results, int sub_depth, bool debug) {
search_results.value = ab_results.value;
search_results.best_move = ab_results.best_move;
search_results.depth_searched = sub_depth;
Expand All @@ -67,9 +68,14 @@ void update_best_move_results(BestMoveSearchResults& search_results, AlphaBetaDa
search_results.seldepth = ab_results.seldepth;
search_results.tt_key_collisions = ab_results.tt_key_collisions;
search_results.nodes_in_transposition_table = ab_results.nodes_in_transposition_table;
search_results.time_searched = get_elapsed_time(Milliseconds);
for (int i = 0; i < ab_results.pv.length[0]; i++) {
search_results.pv[i] = ab_results.pv.table[0][i];
}
if (debug) {
std::cout << "info depth " << sub_depth << " score cp " << search_results.value << " time " <<
search_results.time_searched << " nodes " << search_results.nodes_searched << " pv " << search_results.pv << std::endl;
}
}

template<Color color>
Expand All @@ -79,7 +85,7 @@ BestMoveSearchResults iterative_deepening(Position& board, int time_limit, short
reset_clock();
TranspositionTable t_table = TranspositionTable();
if (debug) {
std::cout << "Time to initialize transposition table: " << get_elapsed_time(Milliseconds) << std::endl;
std::cout << UCI_DEBUG_STRING << "Time to initialize transposition table: " << get_elapsed_time(Milliseconds) << std::endl;
}
reset_clock();

Expand All @@ -89,7 +95,7 @@ BestMoveSearchResults iterative_deepening(Position& board, int time_limit, short
}
struct AlphaBetaData ab_results = alpha_beta_root<color>(board, sub_depth, time_limit, t_table);
if (ab_results.search_completed) {
update_best_move_results(search_results, ab_results, sub_depth);
update_best_move_results(search_results, ab_results, sub_depth, debug);
}
}

Expand Down
107 changes: 18 additions & 89 deletions ChessEngine/ChessEngine_lib/uci_interpreter/uci_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,14 @@
#include "move_search/search.h"
#include "parse_uci_move.h"
#include "helpers.h"
#include "constants.h"
#include "sstream"

using namespace std;

struct ReadUCIParameters {
std::string diagnostic_file_path;
std::string uci_file_input_path;
std::string uci_file_output_path;
bool read_from_file = false;
bool output_diagnostic_file = false;
bool debug_info;
};

struct UCIStreams {
std::ifstream input_file;
ofstream diagnostics_file;
ofstream output_file;
};

enum OutputLocations {
UCIOutputFile,
DiagnosticFile,
StdOut,
};

bool using_diagnostic_file(ReadUCIParameters& parameters) {
return !parameters.diagnostic_file_path.empty() && parameters.output_diagnostic_file;
}

bool using_file_io(ReadUCIParameters& parameters) {
return (!parameters.uci_file_input_path.empty() && !parameters.uci_file_output_path.empty()
&& parameters.read_from_file);
}

void initialize_uci_streams(UCIStreams& streams, ReadUCIParameters& parameters) {
if (using_diagnostic_file(parameters)) {
streams.diagnostics_file.open(parameters.diagnostic_file_path);
}
if (using_file_io(parameters)) {
streams.input_file = std::ifstream(parameters.uci_file_input_path);
streams.output_file.open(parameters.uci_file_output_path);
}
}

void close_streams(UCIStreams& streams, ReadUCIParameters& parameters) {
if (using_diagnostic_file(parameters)) {
streams.diagnostics_file.close();
streams.diagnostics_file.open(parameters.diagnostic_file_path);
}
if (using_file_io(parameters)) {
streams.input_file.close();
streams.output_file.close();
}
}

bool get_input(std::string &input_line, UCIStreams &streams, ReadUCIParameters &parameters) {
if (using_file_io(parameters)) {
if (std::getline(streams.input_file, input_line)) return true;
} else {
if (std::getline(cin, input_line)) return true;
}
return false;
}

void send_output(const std::string& message, UCIStreams& streams, ReadUCIParameters& parameters, bool send_to_std) {
if (send_to_std) std::cout << message << std::endl;
if (using_diagnostic_file(parameters)) {
streams.diagnostics_file << message << std::endl;
}
if (using_file_io(parameters)) {
streams.output_file << message << std::endl;
}
}


void send_output(const std::string& message, UCIStreams& streams, ReadUCIParameters& parameters) {
send_output(message, streams, parameters, true);
}

void initialize_uci(Position& p) {
initialise_all_databases();
zobrist::initialise_zobrist_keys();
Expand Down Expand Up @@ -111,7 +40,7 @@ void uci_position(Position& board, const string& input_line) {
}
}

void uci_go(Position& board, const string& input_line, UCIStreams& streams, ReadUCIParameters& parameters) {
void uci_go(Position& board, const string& input_line, ReadUCIParameters& uci_parameters) {
auto t_start = std::chrono::high_resolution_clock::now();

BestMoveSearchResults results;
Expand All @@ -120,41 +49,41 @@ void uci_go(Position& board, const string& input_line, UCIStreams& streams, Read
const BestMoveSearchParameters params = BestMoveSearchParameters {
.depth = MAX_DEPTH,
.time_limit = move_time,
.debug_info = parameters.output_diagnostic_file
.debug_info = uci_parameters.debug_info
};
results = best_move(board, params);
std::cout << "bestmove " << results.best_move << std::endl;
}

void read_uci(ReadUCIParameters& parameters) {
void read_uci() {
Position board;
UCIStreams streams;

initialize_uci_streams(streams, parameters);
ReadUCIParameters parameters = {};
initialize_uci(board);

string input_line; //to read the command given by the GUI
string input_line;

cout.setf(ios::unitbuf);// Make sure that the outputs are sent straight away to the GUI
cout.setf(ios::unitbuf);

while (get_input(input_line, streams, parameters)) {
send_output(input_line, streams, parameters, false);
while (std::getline(cin, input_line)) {
if (input_line == "uci") {
send_output("id name Midnight", streams, parameters);
send_output("id author Archishmaan Peyyety", streams, parameters);
send_output("uciok", streams, parameters);
std::cout << "id name Midnight" << std::endl;
std::cout << "id author Archishmaan Peyyety" << std::endl;
std::cout << "uciok" << std::endl;
} else if (input_line == "quit") {
send_output("Bye Bye", streams, parameters);
std::cout << "Bye Bye" << std::endl;
break;
} else if (input_line == "isready") {
send_output("readyok", streams, parameters);
std::cout << "readyok" << std::endl;
} else if (input_line == "ucinewgame") {}
if (input_line.substr(0, 8) == "position") {
uci_position(board, input_line);
} else if (input_line == "stop") {
} else if (input_line == "debug on") {
parameters.debug_info = true;
} else if (input_line == "debug off") {
parameters.debug_info = false;
} else if (input_line.substr(0, 2 ) == "go") {
uci_go(board, input_line, streams, parameters);
uci_go(board, input_line, parameters);
}
}
close_streams(streams, parameters);
}
Loading

0 comments on commit eee67ff

Please sign in to comment.