stockfish_chess_engine 0.7.1
stockfish_chess_engine: ^0.7.1 copied to clipboard
Use Stockfish chess engine directly in your Flutter project.
stockfish_chess_engine #
Use Stockfish chess engine in your Flutter project.
This project is based on sources for Stockfish 17.
Usage #
final stockfish = new Stockfish()
// Create a subscribtion on stdout : subscription that you'll have to cancel before disposing Stockfish.
final stockfishSubscription = stockfish.stdout.listen((message) {
print(message);
});
// Create a subscribtion on stderr : subscription that you'll have to cancel before disposing Stockfish.
final stockfishErrorsSubscription = stockfish.stderr.listen((message) {
print(message);
});
// Get Stockfish ready
stockfish.stdin = 'isready'
// Send you commands to Stockfish stdin
stockfish.stdin = 'position startpos' // set up start position
stockfish.stdin = 'position fen rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2' // set up custom position
stockfish.stdin = 'go movetime 1500' // search move for at most 1500ms
// Don't remember to dispose Stockfish when you're done.
stockfishErrorsSubscription.cancel();
stockfishSubscription.cancel();
stockfish.dispose();
You can see an example usage in example folder.
Important notes #
-
You must check the position validity before sending it to stdin, otherwise program will crash on illegal position ! For that, you can use the chess package.
-
As the library creates two isolates, you must dispose Stockfish before perfoming an hot reload / hot restart, and then creating a new Stockfish instance.
-
If testing on IPhone simulator, consider disabling Impeller if the program fails to show UI :
flutter run --no-enable-impeller
For stockfish chess engine developpers #
- Adjust the path of "llvm-path" in file ffigen.yaml (linux users)
- Run
flutter pub get
. - Uncomment line
#define _ffigen
on top of src/stockfish.h (for the ffi generation to pass). - Run command
dart run ffigen --config ffigen.yaml
. More on https://pub.dev/packages/ffigen for the prerequesites per OS. - Comment line
#define _ffigen
in src/stockfish.h (otherwise Stockfish engine compilation will pass but be incorrect).
Changing Stockfish source files #
If you need to upgrade Stockfish source files, create a folder Stockfish inside src folder, copy the src folder from the stockfish sources into the new Stockfish folder (and also replace the readme file for Stockfish).
Also you need to make some more adaptive works :
Adapting streams
- replace all calls to
cout << #SomeContent# << endl
byfakeout << #SomeContent# << fakeendl
(without the std:: prefix if any) (And ajust also calls tocout.rdbuf()
byfakeout.rdbuf()
) But do not replace calls to sync_cout add include to ../../fixes/fixes.h in all related files (and adjust the include path accordingly). Do the same for calls tocout.#method#
. Don't forget to replace calls toendl
(with or without std:: prefix) : once more justendl
notsync_endl
- proceed accordingly for
cin
: replace byfakein
- and the same for
cerr
: replace byfakeerr
- in misc.h replace
#define sync_cout std::cout << IO_LOCK
#define sync_endl std::endl << IO_UNLOCK
with
#define sync_cout fakeout << IO_LOCK
#define sync_endl fakeendl << IO_UNLOCK
and include ../../fixes/fixes.h (if not already done)
Adding main.h source file
Add the file src/Stockfish/src/main.h with the following content :
#ifndef __MAIN_H__
#define __MAIN_H__
int main(int argc, char* argv[]);
#endif // __MAIN_H__
and replace main.cpp so that it includes this new file.
Copying code for ios and mac
Then, copy src/Stockfish folder to
- folder ios/Classes
- folder macos/Classes
Adapting code for Windows
- In file src/Stockfish/misc.cpp we have to remove call to
_get_pgmptr
as this time Stockfish is not a standalone program. So, in functionstd::string CommandLine::get_binary_directory
remove the matching section :
#ifdef _WIN32
pathSeparator = "\\";
#ifdef _MSC_VER
// Under windows argv[0] may not have the extension. Also _get_pgmptr() had
// issues in some Windows 10 versions, so check returned values carefully.
char* pgmptr = nullptr;
if (!_get_pgmptr(&pgmptr) && pgmptr != nullptr && *pgmptr)
argv0 = pgmptr;
#endif
#else
pathSeparator = "/";
#endif
and replace with the following :
#ifdef _WIN32
pathSeparator = "\\";
std::string basePath = "build\\windows\\x64\\runner\\";
#ifdef NDEBUG
argv0 = basePath + "Release";
#else
argv0 = basePath + "Debug";
#endif
#else
pathSeparator = "/";
#endif
- In file src/Stockfish/uci.cpp, a lambda function can make the compilation failing, as the MSVC compiler is strictier than GCC. So in function
std::string UCIEngine::format_score
, in the lambda usingTB_CP
, we just need to declare it inside the lambda :
const auto format =
overload{[](Score::Mate mate) -> std::string {
auto m = (mate.plies > 0 ? (mate.plies + 1) : mate.plies) / 2;
return std::string("mate ") + std::to_string(m);
},
[](Score::Tablebase tb) -> std::string {
constexpr int TB_CP = 20000;
return std::string("cp ")
+ std::to_string((tb.win ? TB_CP - tb.plies : -TB_CP - tb.plies));
},
[](Score::InternalUnits units) -> std::string {
return std::string("cp ") + std::to_string(units.value);
}};
Adapting the NNUE names
- Copy the big and small nnue names from src/Stockfish/src/evaluate.h
- Replace their names in file src/CMakeLists.txt
- Also replace their names in file ios/stockfish_chess_engine.podspec and macos/stockfish_chess_engine.podspec
Changing the downloaded NNUE file #
- Go to Stockfish NNUE files page and select a reference from the list.
- Modify CMakeLists.txt, by replacing lines starting by
set (NNUE_NAME )
by setting your reference name, without any quote. - Modify the reference name in
evaluate.h
in the line containing#define EvalFileDefaultName
, by setting your nnue file name, with the quotes of course. - Don't forget to clean project before building again (
flutter clean
thenflutter pub get
).
Credits #
- Using source code from Stockfish.
- Using source code from Flutter Stockfish.
- Using source code from Flutter Stockfish Plugin