Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(CONNECT4)
SET(CONNECT4_VERSION_MAJOR 0)
SET(CONNECT4_VERSION_MINOR 9)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -W -Wall -O3 -DNDEBUG")

ADD_LIBRARY(c4solver_dependencies STATIC
Solver.cpp
Solver.hpp
Position.hpp
TranspositionTable.hpp
OpeningBook.hpp
MoveSorter.hpp)

ADD_EXECUTABLE(c4solver
main.cpp)

ADD_EXECUTABLE(c4generator
generator.cpp)

TARGET_LINK_LIBRARIES(c4solver c4solver_dependencies)
TARGET_LINK_LIBRARIES(c4generator c4solver_dependencies)

INSTALL(TARGETS c4solver_dependencies DESTINATION lib)
INSTALL(TARGETS c4generator DESTINATION bin)
INSTALL(TARGETS c4solver DESTINATION bin)
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
This C++ source code is published under AGPL v3 license.

Read the associated [step by step tutorial to build a perfect Connect 4 AI](http://blog.gamesolver.org) for explanations.

## Quick build
```
mkdir build && cd build
cmake ../
```
11 changes: 6 additions & 5 deletions generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ std::unordered_set<uint64_t> visited;
* Explore and print all possible position under a given depth.
* symetric positions are printed only once.
*/
void explore(const Position &P, char* pos_str, const int depth) {
void explore(const Position &P, char* const pos_str, const int depth) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a const-annotation to the pointer here to ensure memory safety and optimization

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter is actually not const.
The code does not even compile with your suggestion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, the integer values are variable, but the pointer is const, no? I compiled on my machine and all of this worked;.

Copy link
Owner

@PascalPons PascalPons May 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, sorry I misplaced the const when copying the code.

uint64_t key = P.key3();
if(visited.count(key)) return; // already explored position
visited.insert(key); // flag new position as visited

int nb_moves = P.nbMoves();
const int nb_moves = P.nbMoves();
if(nb_moves <= depth)
std::cout << pos_str << std::endl;
if(nb_moves >= depth) return; // do not explore at further depth

for(int i = 0; i < Position::WIDTH; i++) // explore all possible moves
for(int i = 0; i < Position::WIDTH; ++i) // explore all possible moves
if(P.canPlay(i) && !P.isWinningMove(i)) {
Position P2(P);
P2.playCol(i);
Expand Down Expand Up @@ -71,8 +71,9 @@ void generate_opening_book() {
*/
int main(int argc, char** argv) {
if(argc > 1) {
int depth = atoi(argv[1]);
char pos_str[depth + 1] = {0};
const int depth = atoi(argv[1]);
char pos_str[depth + 1];
memset(pos_str, 0, (depth + 1) * sizeof(int));
explore(Position(), pos_str, depth);
} else generate_opening_book();
}