Skip to content

Commit

Permalink
Updated, cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
colding10 committed Jan 13, 2024
1 parent 12d055a commit a12164d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 67 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@
"vector": "cpp",
"algorithm": "cpp",
"list": "cpp",
"span": "cpp"
"span": "cpp",
"hash_map": "cpp",
"*.tcc": "cpp",
"compare": "cpp",
"concepts": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"utility": "cpp"
},
"C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools"
}
52 changes: 50 additions & 2 deletions src/kingfish/ai/searcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,59 @@ Searcher::search(std::vector<Position> hist, int depth) {
}
}

void Searcher::searchTimed(std::vector<Position> &hist, int ms_time) {
std::string move_str = "";
this->stop_search = false;
auto start_time = Clock::now();

for (int depth = 1; depth < 1000; depth++) {
if (stop_search) {
break;
}

auto result_moves_gen = search(hist, depth);
for (; result_moves_gen.next();) {
if (stop_search) {
break;
}

int gamma, score;
Move move;

auto result = result_moves_gen.value();
std::tie(gamma, score, move) = result;

int i = move.i, j = move.j;
if (hist.size() % 2 == 0) {
i = 119 - i, j = 119 - j;
}
move_str = render(i) + render(j) + (char)tolower(move.prom);
int time =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - start_time)
.count();
std::cout << "info depth " << depth << " score cp " << score
<< " nodes " << this->nodes_searched << " nps "
<< (this->nodes_searched * 1000) / std::max(time, 1)
<< " hashfull " << this->tp_score.getPermillFull()
<< " time " << time << " pv " << move_str << std::endl;

if (move_str.length() &&
deltaMs(std::chrono::high_resolution_clock::now(), start_time) >
ms_time) {
stop_search = true;
break;
}
}
}
std::cout << "bestmove " << (move_str.length() ? move_str : "(none)")
<< std::endl;
}
void Searcher::searchInfinite(std::vector<Position> &hist) {
std::string move_str;
this->stop_search = false;
auto start_time = Clock::now();

for (int depth = 1; !stop_search; depth++) {
for (auto result_moves_gen = search(hist, depth);
result_moves_gen.next();) {
Expand Down Expand Up @@ -200,6 +248,6 @@ void Searcher::searchInfinite(std::vector<Position> &hist) {
<< std::endl;
}

void Searcher::stopInfiniteSearch() {
void Searcher::stopSearch() {
this->stop_search = true;
}
9 changes: 5 additions & 4 deletions src/kingfish/ai/searcher.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef KINGFISH_SEARCHER_H
#define KINGFISH_SEARCHER_H

#include <atomic>
#include <functional>
#include <iostream>
#include <map>
Expand All @@ -9,9 +10,9 @@
#include <utility>
#include <vector>

#include "../position.h"
#include "../consts.h"
#include "../move.h"
#include "../position.h"
#include "../utils/generator.h"
#include "../utils/hashtable.h"

Expand All @@ -31,11 +32,11 @@ class Searcher {
Generator<std::tuple<int, int, Move>> search(std::vector<Position> hist,
int depth);

void searchTimed(std::vector<Position> &hist, int ms_time);
void searchInfinite(std::vector<Position> &hist);
void stopInfiniteSearch();
void stopSearch();

private:
bool stop_search = false;
std::atomic<bool> stop_search;
};

#endif // !KINGFISH_SEARCHER_H
3 changes: 1 addition & 2 deletions src/kingfish/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <iostream>

#include "types.h"
#include "bitboard.h"

// bit manipulation macros
#define get_bit(bitboard, index) (bitboard & (1ULL << index))
Expand Down Expand Up @@ -42,4 +41,4 @@ Bitboard queenAttacks(Square square, Bitboard block);
void initLeaperAttacks();
} // namespace BBS

#endif
#endif
2 changes: 1 addition & 1 deletion src/kingfish/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ inline i8 bitScanR(ui64 n) {
}
} // namespace bits

#endif //
#endif //
3 changes: 2 additions & 1 deletion src/kingfish/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "consts.h"
#include "move.h"
#include "zobrist.h"
#include "bitboard.h"

void Position::printBBoards() const {
for (Color c : {CL_WHITE, CL_BLACK}) {
Expand Down Expand Up @@ -298,4 +299,4 @@ bool Position::isCheckmate() const {

PositionHash Position::hash() const {
return zobristHash(*this, true);
}
}
2 changes: 1 addition & 1 deletion src/kingfish/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,4 @@ inline constexpr Square getSquare(BoardFile file, BoardRank rank) {
static_cast<int>(file));
}

#endif
#endif
69 changes: 15 additions & 54 deletions src/kingfish/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ int uciMainLoop() {
hist = {Position(INITIAL, 0, {true, true}, {true, true}, 0, 0)};

if (args.size() > 2) {
for (int ply = 0; ply < (int)args.size() - 3; ply++) {
std::string move = args[ply + 3];
for (int ply = 3; ply < (int)args.size(); ply++) {
std::string move = args[ply];

int i = parse(move.substr(0, 2));
int j = parse(move.substr(2, 2));
Expand All @@ -115,7 +115,7 @@ int uciMainLoop() {
std::transform(
prom.begin(), prom.end(), prom.begin(), ::toupper);

if (ply % 2 == 1) {
if (ply % 2 == 0) {
i = 119 - i;
j = 119 - j;
}
Expand All @@ -130,67 +130,28 @@ int uciMainLoop() {

int ms_time = getSearchTime(args, hist);

auto start_time = Clock::now();

Move move;

if (!infinite) {
int gamma, score = 0;
bool flag = false;
std::string move_str = "";

for (int depth = 1; depth < 1000; depth++) {
if (flag) {
break;
}
std::thread infinite_thread(
std::mem_fn(&Searcher::searchTimed),
&searcher,
std::ref(hist),
ms_time);

auto result_moves_gen = searcher.search(hist, depth);
for (; result_moves_gen.next();) {
auto result = result_moves_gen.value();
std::tie(gamma, score, move) = result;

int i = move.i, j = move.j;
if (hist.size() % 2 == 0) {
i = 119 - i, j = 119 - j;
}
move_str =
render(i) + render(j) + (char)tolower(move.prom);
int time =
std::chrono::duration_cast<
std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() -
start_time)
.count();
std::cout
<< "info depth " << depth << " score cp " << score
<< " nodes " << searcher.nodes_searched << " nps "
<< (searcher.nodes_searched * 1000) / std::max(time, 1)
<< " hashfull "
<< searcher.tp_score.getPermillFull() << " time "
<< time << " pv " << move_str << std::endl;

if (move_str.length() &&
deltaMs(std::chrono::high_resolution_clock::now(),
start_time) > ms_time) {
flag = true;
break;
}
}
while (std::getline(std::cin, line) && line != "stop" && !searcher.stop_search) {
// Keep reading input until the "stop" command is received. Or searcher stops the search
}
std::cout << "bestmove "
<< (move_str.length() ? move_str : "(none)")
<< std::endl;

searcher.stopSearch();
infinite_thread.join();
} else {
std::thread infinite_thread(
std::mem_fn(&Searcher::searchInfinite),
&searcher,
std::ref(hist));

while (std::getline(std::cin, line) && line != "stop") {
// Keep reading input until the "stop" command is received.
while (std::getline(std::cin, line) && line != "stop" && !searcher.stop_search) {
// Keep reading input until the "stop" command is received.Or searcher stops the search
}
searcher.stopInfiniteSearch();
searcher.stopSearch();
infinite_thread.join();
}
} else if (args[0] == "debug") {
Expand Down
2 changes: 1 addition & 1 deletion src/kingfish/zobrist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ Move parseMove(std::uint16_t* move_bytes) {
}

return Move(starting_square, ending_square, prom);
}
}

0 comments on commit a12164d

Please sign in to comment.