Skip to content

Commit

Permalink
V2! (#13)
Browse files Browse the repository at this point in the history
* Potentially SEE? Also mate scores

* Fixing (sorta)

* do the things that were todos, still not working

* Work in progress Datagen

* Update readme

* Uncomment the actual engine

* Fix readme formatting

* Movestogo TM
  • Loading branch information
Vast342 authored Nov 5, 2023
1 parent 7d8da44 commit f32fbdc
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 104 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ CMakeCache.txt
build_command.txt
command.txt
lichess-big3-resolved.txt
datagencommand.txt
.vscode/settings.json
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
"streambuf": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"sstream": "cpp"
"sstream": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"cstdarg": "cpp",
"ctime": "cpp",
"ratio": "cpp",
"format": "cpp",
"fstream": "cpp",
"iomanip": "cpp"
}
}
20 changes: 13 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ src/movegen.cpp
src/psqt.cpp
src/search.cpp
src/tests.cpp
src/tt.cpp
src/uci.cpp)
src/tt.cpp)

# 3 seperate builds, one using BMI2's pext, one using typical magic bitboards, and one that's native. The native doesn't detect what your cpu uses though.
add_executable(Clarity_Pext ${Common_SRC_Files} src/bmi2.cpp)
add_executable(Clarity_Magic ${Common_SRC_Files} src/magic.cpp)
add_executable(Clarity_Native ${Common_SRC_Files} src/bmi2.cpp)
add_executable(Clarity_Pext ${Common_SRC_Files} src/bmi2.cpp src/uci.cpp)
add_executable(Clarity_Magic ${Common_SRC_Files} src/magic.cpp src/uci.cpp)
add_executable(Clarity_Native ${Common_SRC_Files} src/bmi2.cpp src/uci.cpp)

# building with all warnings for all of these so that
# data generator build
add_executable(Clarity_Datagen ${Common_SRC_Files} src/bmi2.cpp src/datagen.cpp)

# building with all warnings for all of these so that I can try to pick up on issues
target_compile_options(Clarity_Pext PRIVATE -Wall -Wextra -Wpedantic -march=haswell)
target_compile_options(Clarity_Magic PRIVATE -Wall -Wextra -Wpedantic -march=x86-64)
target_compile_options(Clarity_Native PRIVATE -Wall -Wextra -Wpedantic -march=native)

target_compile_options(Clarity_Datagen PRIVATE -Wall -Wextra -Wpedantic -march=native)

# making the entire binary need only itself to run, for ease of use
target_link_libraries(Clarity_Pext -static)
target_link_libraries(Clarity_Magic -static)
target_link_libraries(Clarity_Native -static)
target_link_libraries(Clarity_Native -static)

target_link_libraries(Clarity_Datagen -static)
89 changes: 27 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,21 @@
# Clarity

The C++ rewrite is complete! Clarity is now faster and better, using new techniques, and I have learned a new language!

For those who didn't see the engine in its depressing, confusing, error-filled C# state, this engine was originally written in C# before I decided to switch to C++. That decision was one of the greatest I think I've ever had, as the engine is now faster, easier to read, less error-filled(hopefully), and better overall.

My current Estimate of elo (calculated by playing large sums of games against engines with known CCRL ratings) is 2250.

### Special Thanks (in no particular order):

[Toanth](https://github.com/toanth): General help and explaining things I didn't understand before

[Ciekce](https://github.com/Ciekce): Preventing the cardinal sins of C++ since day 1

[RedBedHed](https://github.com/RedBedHed): Lookup tables for move generation

[JW](https://github.com/jw1912): More random C++ things

[A_randomnoob](https://github.com/mcthouacbb): Helping with a lot of random engine bits

[zzzzz](https://github.com/zzzzz151/): Ideas, planning, and a lot that I probably forgot

### Todo lists:

General Things:
1. Web version of the engine
2. or unleash it on lichess

Board Representation:
1. General performancey things

### Divided by release:

#### Clarity V2.0.0 Plans:
1. Killer move table shenanigans
2. CMH+FMH (also known as continuation history (conthist))
3. SEE for move ordering, pruning, etc
4. try again with LMP
5. tune some search things
6. Maybe 2500 at this point

#### Clarity V3.0.0 Plans:
1. nnue

#### Clarity V4.0.0 Plans:
1. actual futility pruning (not reverse)
2. Razoring
3. Delta pruning in qsearch
4. Syzgyzy tablebase support (oh no not the 3000 uncommented lines)
5. history pruning
6. mate distance pruning
7. capture history heuristic?
8. singular extensions (huh)
9. multicut(huh)

<img src="assets/Clarity%20Logo.png" width="150" height="150">

> Relatively strong UCI chess engine
## Feature List:

CLI:
1. UCI Implementation
1. Functional (but not complete) UCI implementation
2. printstate: shows the state of the board.
3. perftsuite <suite>: performs a suite of perft tests, currently only supports the suite ethereal.
4. perft <depth>: performs a perft test from the current position and outputs the result.
5. splitperft <depth>: performs a perft test from the current position and outputs the result seperated by which move is the first one done.
6: getfen: outputs a string of Forsyth-Edwards Notation (FEN) that encodes the current position.
7: incheck: outputs if the current position is in check or not.
8: evaluate: outputs the evaluation of the current position.
9: bench <depth>: performs the bench test, a fixed depth search on a series of 50 positions.
6. getfen: outputs a string of Forsyth-Edwards Notation (FEN) that encodes the current position.
7. incheck: outputs if the current position is in check or not.
8. evaluate: outputs the evaluation of the current position.
9. bench <depth>: performs the bench test, a fixed depth search on a series of 50 positions.

Board Representation:
1. Copymake moves
Expand All @@ -84,13 +33,29 @@ Search:
5. Null Move Pruning
6. Late Move Reductions (log function, generated on startup)
7. Fail-soft
8. Currently nonfunctional LMP

Evaluation:
1. PeSTO PSQTs and piece weights (soon to be tuned)
2. Non-functional passed pawn detection
1. Tuned PSQTs (soon to be NNUE)

Move ordering:
1. TT best move
2. MVV-LVA
3. History Heuristic
3. Killer Move Heuristic
4. History Heuristic


### Special Thanks (in no particular order):

[Toanth](https://github.com/toanth): General help and explaining things I didn't understand before

[Ciekce](https://github.com/Ciekce): Preventing the cardinal sins of C++ since day 1

[RedBedHed](https://github.com/RedBedHed): Lookup tables for move generation

[JW](https://github.com/jw1912): More random C++ things

[A_randomnoob](https://github.com/mcthouacbb): Helping with a lot of random engine bits

[zzzzz](https://github.com/zzzzz151/): Ideas, planning, and a lot that I probably forgot

**Logo generated by AI**
Binary file added assets/Clarity Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,4 +892,21 @@ bool Board::isLegalMove(const Move& move) {
}
}
return false;
}

uint64_t Board::getAttackers(int square) const {
uint64_t occupied = getOccupiedBitboard();
return (getPawnAttacks(square, 0) & getColoredPieceBitboard(1, Pawn))
| (getPawnAttacks(square, 1) & getColoredPieceBitboard(0, Pawn))
| (getKnightAttacks(square) & pieceBitboards[Knight])
| (getRookAttacks(square, occupied) & (pieceBitboards[Rook] | pieceBitboards[Queen]))
| (getBishopAttacks(square, occupied) & (pieceBitboards[Bishop] | pieceBitboards[Queen]))
| (getKingAttacks(square) & pieceBitboards[King]);
}

uint64_t Board::getColoredBitboard(int color) const {
return coloredBitboards[color];
}
uint64_t Board::getPieceBitboard(int piece) const {
return pieceBitboards[piece];
}
109 changes: 109 additions & 0 deletions src/datagen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "globals.h"
#include "datagen.h"
#include "search.h"


int main() {
std::cout << "Generate data? Y/N\n";
std::string response = "";
std::cin >> response;
if(response == "y" || response == "Y") {
generateData();
}
return 0;
}

// manages the threads
void generateData() {
for(int i = 0; i < 10; i++) {
threadFunction();
std::cout << "finished game " << i << '\n';
}
}

// run on each thread
void threadFunction() {
std::vector<std::string> fenVector;
double result = runGame(fenVector);
if(result == 2) std::cout << "Error! invalid game result\n";
dumpToArray(result, fenVector);
}

// idk what to use here lol
constexpr uint8_t threadCount = 5;

// manages the games
double runGame(std::vector<std::string>& fenVector) {
int score = 0;
Board board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
for(int i = 0; i <= 100; i++) {
if(i < 8) {
// make a random move
std::random_device rd;
std::mt19937_64 gen(rd());

// get moves
std::array<Move, 256> moves;
const int totalMoves = board.getMoves(moves);

// distribution
std::uniform_int_distribution distribution{0, totalMoves - 1};

// legality check
int numTestedMoves = 0;
while(true) {
const int index = distribution(gen);
if(board.makeMove(moves[index])) {
break;
}
numTestedMoves++;
// position has no legal moves, checkmate? stalemate? restart.
if(numTestedMoves > 100) {
board = Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
i = 0;
}
}
// move has been made now, cool
std::cout << board.getFenString() << std::endl;
} else {
// get move from engine normally
std::cout << "sending board with position " << board.getFenString() << std::endl;
std::pair<Move, int> move = dataGenSearch(board, 8);
score = move.second;
std::cout << "score is now " << score << std::endl;
if(board.makeMove(move.first)) {
if(abs(move.second) < abs(mateScore + 256)) {
// non-mate, add fen string to vector
fenVector.push_back(board.getFenString() + " " + std::to_string(move.second));
} else {
// checkmate found, no more use for this
break;
}
} else {
std::cout << "Engine made an illegal move\n";
}
std::cout << board.getFenString() << std::endl;
}
}

// return 1 if white won, 0 if black won, and 0.5 if draw, this will be useful later
if(score > 1) {
return 1;
} else if(score < -1) {
return 0;
} else {
return 0.5;
}
// error
return 2;
}

void dumpToArray(double result, std::vector<std::string>& fenVector) {
for(std::string fen : fenVector) {
std::cout << "adding " << fen << " " << result << " to file\n";
// add to file

// add [result]\n

}
}
7 changes: 7 additions & 0 deletions src/datagen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

void generateData();
void threadFunction();
void dumpToArray(double result, std::vector<std::string>& fenVector);
int checkForMates(Board& board);
double runGame(std::vector<std::string>& fenVector);
3 changes: 3 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ struct Board {
bool isRepeatedPosition();
int detectPassedPawns();
bool isLegalMove(const Move& move);
uint64_t getAttackers(int square) const;
uint64_t getColoredBitboard(int color) const;
uint64_t getPieceBitboard(int piece) const;
private:
std::array<uint64_t, 2> coloredBitboards;
std::array<uint64_t, 6> pieceBitboards;
Expand Down
Loading

0 comments on commit f32fbdc

Please sign in to comment.