Skip to content

Commit 539fb67

Browse files
committed
Automatic version & ctest
Fix bug in state::get_move_info() that treats walls as movable pieces Fix inconsistency of promote color in state::state()
1 parent 91bfd43 commit 539fb67

15 files changed

Lines changed: 371 additions & 72 deletions

File tree

CMakeLists.txt

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
cmake_minimum_required(VERSION 3.20)
2-
project(5dchess_engine VERSION 0.2.0 LANGUAGES CXX)
2+
project(5dchess_engine VERSION 0.0.0 LANGUAGES CXX)
3+
4+
# Extract version from git tags with fallback to CMake project version
5+
find_package(Git QUIET)
6+
if(GIT_FOUND)
7+
execute_process(
8+
COMMAND ${GIT_EXECUTABLE} describe --tags --always
9+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
10+
OUTPUT_VARIABLE GIT_VERSION
11+
OUTPUT_STRIP_TRAILING_WHITESPACE
12+
ERROR_QUIET
13+
)
14+
15+
# Only use git version if we're in a git repo and got a valid tag
16+
if(GIT_VERSION AND NOT GIT_VERSION STREQUAL "")
17+
# Remove 'v' prefix if present
18+
string(REGEX REPLACE "^v" "" GIT_VERSION "${GIT_VERSION}")
19+
set(PROJECT_VERSION "${GIT_VERSION}")
20+
message(STATUS "Version from git tag: ${PROJECT_VERSION}")
21+
else()
22+
message(STATUS "Not in a git repo or no tags found, using default version: ${PROJECT_VERSION}")
23+
endif()
24+
else()
25+
message(STATUS "Git not found, using default version: ${PROJECT_VERSION}")
26+
endif()
327

428
# Make all targets PIC by default (needed for Python .so on github actions)
529
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@@ -43,6 +67,11 @@ option(TEST "Build test executable" ON)
4367
option(PYMODULE "Build Python module" OFF)
4468
option(EMMODULE "Build Emscripten/WebAssembly module" OFF)
4569

70+
# Enable testing if TEST option is ON
71+
if(TEST)
72+
enable_testing()
73+
endif()
74+
4675
# Find all source files in src/ and src/core/ directories
4776
file(GLOB_RECURSE ENGINE_SOURCES
4877
"${CMAKE_CURRENT_SOURCE_DIR}/src/core/*.cpp"
@@ -60,6 +89,11 @@ file(GLOB_RECURSE ENGINE_HEADERS
6089
# Create the core library target (without Python bindings)
6190
add_library(5dchess_engine_core OBJECT ${ENGINE_SOURCES})
6291

92+
# Add version as compile definition for all targets using this library
93+
target_compile_definitions(5dchess_engine_core PUBLIC
94+
PROJECT_VERSION_STRING="${PROJECT_VERSION}"
95+
)
96+
6397
# Ensure header files show up in IDEs
6498
target_sources(5dchess_engine_core PRIVATE ${ENGINE_HEADERS})
6599

@@ -79,17 +113,31 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
79113
message(STATUS ">>> Compiling test.cpp...")
80114
add_executable(test_exec "test.cpp")
81115
target_link_libraries(test_exec PRIVATE 5dchess_engine_core)
116+
# Note: test_exec is not registered as a ctest (it hangs)
82117

83118
add_subdirectory(test) # Add test directory
84119

85120
add_executable(cli "cli.cpp")
86121
target_link_libraries(cli PRIVATE 5dchess_engine_core)
122+
target_compile_definitions(cli PRIVATE
123+
PROJECT_VERSION_STRING="${PROJECT_VERSION}"
124+
)
87125

88126
add_custom_command(TARGET cli POST_BUILD
89127
COMMAND ${CMAKE_COMMAND} -E copy_directory
90128
${CMAKE_SOURCE_DIR}/test/pgn
91129
$<TARGET_FILE_DIR:cli>/pgn
92130
)
131+
132+
# Register perftest for each .5dpgn file
133+
file(GLOB PGN_FILES "${CMAKE_SOURCE_DIR}/test/pgn/*.5dpgn")
134+
foreach(PGN_FILE ${PGN_FILES})
135+
get_filename_component(PGN_NAME ${PGN_FILE} NAME_WE)
136+
add_test(
137+
NAME perftest_${PGN_NAME}
138+
COMMAND bash -c "cat ${CMAKE_SOURCE_DIR}/test/pgn/${PGN_NAME}.5dpgn | $<TARGET_FILE:cli> perftest"
139+
)
140+
endforeach()
93141
endif()
94142

95143
if(PYMODULE)
@@ -100,6 +148,9 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
100148
# Create the Python module
101149
pybind11_add_module(engine pymodule.cpp)
102150
target_link_libraries(engine PRIVATE 5dchess_engine_core)
151+
target_compile_definitions(engine PRIVATE
152+
PROJECT_VERSION_STRING="${PROJECT_VERSION}"
153+
)
103154

104155
# Fix Visual Studio output directories on Windows
105156
if(WIN32)
@@ -123,6 +174,9 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
123174
# Create the WebAssembly module
124175
add_executable(engine_wasm emmodule.cpp)
125176
target_link_libraries(engine_wasm PRIVATE 5dchess_engine_core)
177+
target_compile_definitions(engine_wasm PRIVATE
178+
PROJECT_VERSION_STRING="${PROJECT_VERSION}"
179+
)
126180

127181
# Enable WebAssembly native exception handling
128182
target_compile_options(engine_wasm PRIVATE -fwasm-exceptions)

README.md

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
5dchess_engine
22
==================
33

4-
<bold style="color:#ff6347;">**IMPORTANT NOTE**</bold> This module rely on two separate submodules. It is impossible to build the python library without them. Make sure use
5-
```sh
6-
git clone --recurse-submodules <link-to-this-repo>
7-
```
8-
to download both this repository and the necessary submodules.
94

10-
If interaction with the [graphics interface](https://github.com/SuZero-5DChess/5dchess_client) is preferred, please install `flask` and `flask_socketio` via `pip`.
5+
5dchess_engine is an engine for analysing 5D chess game. It can be used as a C++ library or a python library or javascript library. There are two ways to present a game: via cli or webpage.
6+
7+
### Try it online!
8+
9+
Visit <https://ftxi.github.io/5dchess_engine/>.
1110

1211
### Features
1312

14-
5dchess_engine is an engine for analysing 5D chess game. It can be used as a C++ library or a python library. There are two ways to present a game: via cli (no dependencies) or webpage (requires python, read "IMPORTANT NOTE" above).
1513

1614
This program supports reading arbitary 5d chess variant specified by 5dfen. For moves, it supports long algebraic notation (which looks like `(0T13)b6b5` for physical moves and `(-1T19)e8(0T18)f8` for superphysical moves) or simplified 5dpgn notation specified in [docs/pgn-bnf.txt](docs/pgn-bnf.txt).
1715

@@ -27,59 +25,94 @@ From my testing, hc has a better worse case performance than naive, especially w
2725

2826
This program supports tree shaped traversal.
2927

30-
### Usage (MacOS, etc.)
31-
32-
33-
```sh
34-
mkdir build
35-
cd build
36-
cmake ..
37-
make
38-
```
28+
### Usage
3929

40-
After that, *go to base directory* and run `host.py`. If the server starts smoothly, the chessboard can be found at <http://127.0.0.1:5000>.
30+
The CMake program and a modern C++ complier (C++ 20 or newer) is required. On MacOS, Xcode is enough. On windows, I suggest Visual Studio Community version 2022.
4131

42-
### Usage (Windows)
32+
There are a number of ways to use the program:
33+
1. Use the static webpage hosted on github pages. See *Try it online* above.
34+
2. Use command-line interface. No dependencies other than cmake and a c++ compiler. See *Build Test*.
35+
3. Build python module and host a graphics interface server via python. Requires a python runtime with `flask` and `flask_socketio` installed. See *Build Python Module*.
36+
4. Build javascript module and host the static webpage same as the online version. See *Build WASM*.
4337

44-
The CMake program and a modern C++ complier (C++ 20 or newer) is required. I suggest Visual Studio Community version 2022.
38+
#### Build Tests
4539

46-
```cmd
40+
```sh
4741
mkdir build
4842
cd build
49-
cmake ..
43+
cmake .. -DTEST=on -DCMAKE_BUILD_TYPE=Release
5044
cmake --build .
5145
```
46+
The performance of this code depends significantly on compiler optimizations. Without optimization, the plain (unoptimized) version may run x6 ~ x7 times slower compared to the same code compiled with `-O3` optimization.
47+
The flag `-DCMAKE_BUILD_TYPE=Release` above is used to enable optimizations.
5248

53-
The last step is same as above.
5449

55-
### Debugging/Command Line Interface
50+
The command line tool will be built as `build/cli`. To use it, type `cli <option>`, press enter, and then input the game in 5dpgn (press control+D to complete). Current features of the command line tool including:
51+
- `print`: print the final state of the game
52+
- `count [fast|naive] [<max>]`: display number of avialible moves capped by <max>
53+
- `all [fast|naive] [<max>]`: display all legal moves capped by `<max>`
54+
- `checkmate [fast|naive]`: determine whether the final state is checkmate/stalemate
55+
- `diff`: compare the output of two algorithms.
56+
- `perftest [fast|naive]`: on each intermediate state, print 1 if it is checkmate/stalemate, 0 otherwise
57+
5658
It is possible to run the c++ part of the code without interacting with python or web interface at all. It also makes sense to use a modern programming IDE:
5759
```sh
58-
mkdir build_xcode
59-
cd build_xcode
60+
mkdir build-xcode
61+
cd build-xcode
6062
cmake .. -DTEST=on -GXcode
6163
```
6264
On Windows, the last line should be:
6365
```cmd
6466
cmake .. -DTEST=on -G"Visual Studio 17 2022"
6567
```
6668

67-
The performance of this code depends significantly on compiler optimizations. Without optimization, the plain (unoptimized) version may run x6 ~ x7 times slower compared to the same code compiled with `-O3` optimization.
69+
### Build Python Module
6870

69-
To enable optimizations, configure the build using:
71+
<bold style="color:#ff6347;">**IMPORTANT NOTE**</bold> This module rely on two separate submodules. It is impossible to build the python library without them. Make sure use
72+
```sh
73+
git clone --recurse-submodules <link-to-this-repo>
74+
```
75+
to download both this repository and the necessary submodules.
76+
77+
If interaction with the [graphics interface](https://github.com/SuZero-5DChess/5dchess_client) is preferred, please install `flask` and `flask_socketio` via `pip`.
7078

79+
```sh
80+
mkdir build
81+
cd build
82+
cmake .. -DPYMODULE=on -DCMAKE_BUILD_TYPE=Release
83+
cmake --build .
7184
```
72-
cmake .. -DCMAKE_BUILD_TYPE=Release -DTEST=on
85+
86+
To use it, go to the base directory of this project and run `host.py`. Then, visit `http://127.0.0.1:5000` with your favourite browser.
87+
88+
### Build WASM
89+
90+
Requires [emscripten](https://emscripten.org).
91+
92+
```sh
93+
mkdir build-wasm
94+
cd build-wasm
95+
emcmake cmake .. -DEMMODULE=on -DCMAKE_BUILD_TYPE=Release
96+
cmake --build .
7397
```
7498

75-
The command line tool will be built as `build/cli`. To use it, type `cli <option>`, press enter, and then input the game in 5dpgn (press control+D to complete). Current features of the command line tool including:
76-
- `print`: print the final state of the game
77-
- `count [fast|naive] [<max>]`: display number of avialible moves capped by <max>
78-
- `all [fast|naive] [<max>]`: display all legal moves capped by `<max>`
79-
- `checkmate [fast|naive]`: determine whether the final state is checkmate/stalemate
80-
- `diff`: compare the output of two algorithms
99+
The static website is generated in the `/build-wasm/ui/`.
100+
Note that simply double-clicking index.html will likely fail to initialize the JavaScript components due to [CORS (Cross-Origin Resource Sharing)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) restrictions enforced by modern browsers when using the `file://` protocol.
81101

102+
To run the application correctly, you must serve the directory via a local web server. Use one of the following methods from within the `build-wasm/` folder:
82103

104+
If you have python installed:
105+
```sh
106+
python -m http.server 8080 --directory ui/
107+
```
108+
If the emsdk is already sourced in your environment:
109+
```sh
110+
emrun ui/
111+
```
112+
If you prefer [darkhttpd](https://github.com/emikulic/darkhttpd):
113+
```sh
114+
darkhttpd ui/
115+
```
83116

84117
### Documentation
85118

@@ -95,4 +128,6 @@ For more detail, please read [this page](docs/index.md).
95128
- [ ] L/T numbers
96129
- [ ] documentation
97130
- [x] variants loading
98-
131+
- [x] ctest
132+
- [ ] Reduce resource usage when displaying pgn
133+
- [ ] Check arrows in the new ui

0 commit comments

Comments
 (0)