From 1a9cc0b5a57bbf6649776a3ece71c6007c5ef55f Mon Sep 17 00:00:00 2001 From: Matthew Daiter Date: Tue, 30 Apr 2019 14:11:15 -0500 Subject: [PATCH] [mdaiter]: add CMake file for easier build --- CMakeLists.txt | 27 +++++++++++++++++++++++++++ README.md | 6 ++++++ generator.cpp | 11 ++++++----- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c55776d --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index d7f5d28..690dcbd 100644 --- a/README.md +++ b/README.md @@ -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 ../ +``` diff --git a/generator.cpp b/generator.cpp index bd4f0b8..747a1ba 100644 --- a/generator.cpp +++ b/generator.cpp @@ -13,17 +13,17 @@ std::unordered_set 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) { 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); @@ -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(); }