Skip to content

Commit ff55a1b

Browse files
authored
Merge pull request #37 from justinhj/operation-modernize
Add CMake configuration and instructions
2 parents 7ef6511 + 73d84c0 commit ff55a1b

File tree

14 files changed

+143
-40
lines changed

14 files changed

+143
-40
lines changed
File renamed without changes.

.github/workflows/c-cpp.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ jobs:
1313

1414
steps:
1515
- uses: actions/checkout@v3
16-
- name: make test
17-
working-directory: ./cpp
18-
run: make test
16+
- name: Configure CMake
17+
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
18+
- name: Build
19+
run: cmake --build build
20+
- name: Test
21+
working-directory: ./build
22+
run: ./tests

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ cpp/8puzzle
33
cpp/findpath
44
cpp/minpathbucharest
55
cpp/tests
6+
build/
7+
build-release
File renamed without changes.

CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(astar-algorithm-cpp)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
set(CMAKE_CXX_STANDARD_REQUIRED True)
6+
7+
# Header-only library interface (optional but good practice)
8+
add_library(stlastar INTERFACE)
9+
target_include_directories(stlastar INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
10+
11+
# Executables
12+
add_executable(8puzzle 8puzzle.cpp)
13+
target_link_libraries(8puzzle stlastar)
14+
15+
add_executable(findpath findpath.cpp)
16+
target_link_libraries(findpath stlastar)
17+
18+
add_executable(minpathbucharest min_path_to_Bucharest.cpp)
19+
target_link_libraries(minpathbucharest stlastar)
20+
21+
# Tests
22+
enable_testing()
23+
add_executable(tests tests.cpp)
24+
target_link_libraries(tests stlastar)
25+
add_test(NAME tests COMMAND tests)

GEMINI.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# A* Algorithm C++ Implementation
2+
3+
## Project Overview
4+
5+
This project provides a clean, efficient, and header-only C++ implementation of the A* (A-Star) pathfinding algorithm. It is designed for high-performance applications like video games and includes an optional fixed-size memory allocator (`fsa.h`) to minimize memory fragmentation and allocation overhead.
6+
7+
The core logic resides in `stlastar.h`, which uses C++ templates to work with any user-defined state class that satisfies the required interface.
8+
9+
## Building and Running
10+
11+
### Prerequisites
12+
* C++ compiler supporting C++11 (e.g., `g++`, `clang++`).
13+
* `make` utility.
14+
15+
### Build Commands
16+
The project uses a `makefile` to manage builds.
17+
18+
* **Build All:**
19+
```bash
20+
make
21+
```
22+
This compiles the library examples and tests, producing the following executables:
23+
* `8puzzle`: Solves the 8-puzzle sliding tile game.
24+
* `findpath`: Finds a path on a simple grid map.
25+
* `minpathbucharest`: Solves the "classic" AI problem of finding the shortest path to Bucharest.
26+
* `tests`: Runs the unit tests.
27+
28+
* **Run Tests:**
29+
```bash
30+
make test
31+
```
32+
33+
* **Clean Build:**
34+
```bash
35+
make clean
36+
```
37+
38+
### Running Examples
39+
* **8-Puzzle:**
40+
```bash
41+
./8puzzle [board_string]
42+
# Example: ./8puzzle 013824765
43+
```
44+
* **Pathfinder:**
45+
```bash
46+
./findpath
47+
```
48+
49+
## Development Conventions
50+
51+
### Core Architecture
52+
* **`stlastar.h`**: The main header file containing the `AStarSearch` class template. This is a header-only library; simply include this file in your project.
53+
* **`fsa.h`**: A fixed-size block memory allocator used internally by `AStarSearch` to optimize node allocation. Can be toggled via `USE_FSA_MEMORY` in `stlastar.h`.
54+
55+
### User State Interface
56+
To use the A* search, you must define a class (e.g., `MapSearchNode`) that represents a state in your search space. This class acts as a template argument to `AStarSearch` and **must** implement the following methods:
57+
58+
```cpp
59+
class YourStateClass {
60+
public:
61+
// Heuristic estimate of distance to goal (e.g., Manhattan distance, Euclidean distance)
62+
float GoalDistanceEstimate(YourStateClass &nodeGoal);
63+
64+
// Returns true if this node is the goal
65+
bool IsGoal(YourStateClass &nodeGoal);
66+
67+
// Generates successors and adds them to the search
68+
// Implementation should call astarsearch->AddSuccessor(NewNode) for each valid move
69+
bool GetSuccessors(AStarSearch<YourStateClass> *astarsearch, YourStateClass *parent_node);
70+
71+
// Cost of moving from this node to the successor
72+
float GetCost(YourStateClass &successor);
73+
74+
// Returns true if this state is identical to rhs
75+
bool IsSameState(YourStateClass &rhs);
76+
77+
// Returns a hash of the state (required for std::unordered_set)
78+
size_t Hash();
79+
};
80+
```
81+
82+
### Usage Pattern
83+
1. Instantiate `AStarSearch<YourStateClass> astarsearch;`.
84+
2. Create start and goal states.
85+
3. Call `astarsearch.SetStartAndGoalStates(start, goal)`.
86+
4. Loop `astarsearch.SearchStep()` until the state is `SEARCH_STATE_SUCCEEDED` or `SEARCH_STATE_FAILED`.
87+
5. Retrieve the path using `astarsearch.GetSolutionStart()`, `GetSolutionNext()`, etc.
88+
6. Call `astarsearch.FreeSolutionNodes()` and `astarsearch.EnsureMemoryFreed()` to clean up.
89+
90+
### Testing
91+
* Tests are located in `tests.cpp`.
92+
* Ensure all tests pass with `make test` before submitting changes.

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,25 @@ If you wish to be added to the list of known products/educational projects using
5454
* Lighthouses AI contest https://github.com/marcan/lighthouses_aicontest
5555
* Self-Driving Car Engineer Nanodegree Program https://github.com/vanAken/CarND-Path-Planning-Project
5656

57-
### Compilation
57+
### Building
5858

59-
Enter the cpp folder and run make
59+
Generally you can just include the stlastar.h and, optionally, the fsa.h header files and use it directly. The build instructions below are purely for the test suite and example executables.
6060

61-
#### Introduction
61+
#### Using CMake
6262

63-
This implementation is intended to be simple to read yet fairly
64-
efficient. To build it you can compile, with any recent C++ compiler,
65-
the following files :
63+
Some examples:
6664

67-
For 8-puzzle solver
65+
** Use make and make a debug build **
66+
```bash
67+
cmake -S . -B builddebug -DCMAKE_BUILD_TYPE=debug
68+
```
6869

69-
* 8puzzle.cpp
70-
* stlastar.h
71-
* optionally fsa.h
70+
** Use Ninja and make a Release build **
71+
```bash
72+
cmake -S . -B ninjabuildrelease -DCMAKE_BUILD_TYPE=release
73+
```
74+
75+
In both cases you can execute the build using `make -C [build folder]` or `ninja -C [build folder]`.
7276

7377
#### Command line
7478

@@ -99,4 +103,4 @@ it if you hit an out of memory assert during the search.
99103

100104
Compatibility notes:
101105

102-
This version of the code requires any standards compliant C++ using std C++11
106+
This version of the code requires any standards compliant C++ using std C++11. To build it requires a minimum cmake version you can find in the `CMakeLists.txt` file.

cpp/makefile

Lines changed: 0 additions & 25 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)