diff options
| author | Charlie Blake | 2017-10-13 19:05:13 +0100 |
|---|---|---|
| committer | GitHub | 2017-10-13 19:05:13 +0100 |
| commit | c1f4a1a4368bd45747f864910ae5136dccb857a6 (patch) | |
| tree | b3bdce7b7cd6c46f6928368f2c91f9d994fc48e7 | |
| parent | Merge pull request #8 from thecharlesblake/docs (diff) | |
| parent | Update build instructions with new install (diff) | |
| download | metasol-c1f4a1a4368bd45747f864910ae5136dccb857a6.tar.gz metasol-c1f4a1a4368bd45747f864910ae5136dccb857a6.zip | |
Merge pull request #10 from thecharlesblake/project-setup-cpp
Project setup cpp
| -rw-r--r-- | .gitignore | 54 | ||||
| -rw-r--r-- | .idea/.name | 1 | ||||
| -rw-r--r-- | .idea/Solvitaire.iml | 2 | ||||
| -rw-r--r-- | .idea/misc.xml | 4 | ||||
| -rw-r--r-- | .idea/modules.xml | 8 | ||||
| -rw-r--r-- | .idea/vcs.xml | 6 | ||||
| -rw-r--r-- | .travis.yml | 31 | ||||
| -rw-r--r-- | CMakeLists.txt | 131 | ||||
| -rw-r--r-- | CMakeLists.txt.in | 15 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | build-instructions.md | 54 | ||||
| -rwxr-xr-x | clean-build.sh | 13 | ||||
| -rw-r--r-- | docs/project-tools.md | 27 | ||||
| -rw-r--r-- | src/main/Card.cpp | 25 | ||||
| -rw-r--r-- | src/main/Card.h | 19 | ||||
| -rw-r--r-- | src/main/main.cpp | 121 | ||||
| -rw-r--r-- | src/test/main_test.cpp | 19 |
17 files changed, 531 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dfd52ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +######### +# CLion # +######### + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ +cmake-build-release/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..9d853fa --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +solvitaire
\ No newline at end of file diff --git a/.idea/Solvitaire.iml b/.idea/Solvitaire.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/Solvitaire.iml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> +</project>
\ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..98d02a1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/../Solvitaire/.idea/Solvitaire.iml" filepath="$PROJECT_DIR$/../Solvitaire/.idea/Solvitaire.iml" /> + </modules> + </component> +</project> diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project>
\ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0a05d99 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,31 @@ +language: c++ + +install: +# Upgrade GCC +- sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y +- sudo apt-get update -qq +- sudo apt-get install -qq g++-6 +- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6 + +# Boost +- echo "Installing Boost Libraries" +- sudo wget -O boost_1_63_0.tar.gz http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.gz/download > /dev/null 2>&1 +- sudo tar xzvf boost_1_63_0.tar.gz > /dev/null 2>&1 +- cd boost_1_63_0/ +- sudo ./bootstrap.sh --with-libraries=log > /dev/null 2>&1 +- sudo ./b2 link=shared threading=multi toolset=gcc optimization=space > /dev/null 2>&1 +- sudo ./b2 install > /dev/null 2>&1 +- cd .. + +script: +# Generate build +- cmake -G "CodeBlocks - Unix Makefiles" -D CMAKE_CXX_COMPILER=g++-6 -DCMAKE_BUILD_TYPE=DEBUG --target solvitaire -Bcmake-build-debug -H. +- cmake -G "CodeBlocks - Unix Makefiles" -D CMAKE_CXX_COMPILER=g++-6 -DCMAKE_BUILD_TYPE=RELEASE --target solvitaire -Bcmake-build-release -H. + +# Build +- cmake --build cmake-build-debug +- cmake --build cmake-build-release + +# Run unit tests +- ./cmake-build-debug/bin/unit_tests +- ./cmake-build-release/bin/unit_tests diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ee413e5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,131 @@ +# File based on https://stackoverflow.com/questions/21163188/most-simple-but-complete-cmake-example + +cmake_minimum_required(VERSION 3.0) +project(solvitaire) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +############################################################################### +## source files ############################################################### +############################################################################### + +set(sources src/main/main.cpp src/main/Card.h src/main/Card.cpp) +set(sources_test src/test/main_test.cpp) + +############################################################################### +## Boost setup ################################################################ +############################################################################### + +set(Boost_USE_STATIC_LIBS OFF) +set(Boost_USE_MULTITHREADED ON) +set(Boost_USE_STATIC_RUNTIME OFF) +find_package(Boost 1.63.0 REQUIRED COMPONENTS log) + +if(Boost_FOUND) + include_directories(${Boost_INCLUDE_DIRS}) +else() + message(WARNING "Must install Boost libraries") +endif() + +############################################################################### +## GTest setup ################################################################ +############################################################################### + +# Download and unpack googletest at configure time +configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) +execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "CMake step for googletest failed: ${result}") +endif() +execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE result + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) +if(result) + message(FATAL_ERROR "Build step for googletest failed: ${result}") +endif() + +# Add googletest directly to our build. This defines +# the gtest and gtest_main targets. +add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src + ${CMAKE_BINARY_DIR}/googletest-build) + +# The gtest/gtest_main targets carry header search path +# dependencies automatically when using CMake 2.8.11 or +# later. Otherwise we have to add them here ourselves. +if (CMAKE_VERSION VERSION_LESS 2.8.11) + include_directories("${gtest_SOURCE_DIR}/include") +endif() + +############################################################################### +## compile options ############################################################ +############################################################################### + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -DBOOST_LOG_DYN_LINK -pthread -pedantic -Wall -Wextra \ +-Wcast-align -Wcast-qual -Wformat=2 -Winit-self \ +-Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept \ +-Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow \ +-Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Werror") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -g") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -s -DNDEBUG -Wl,-O1") + +############################################################################### +## target definitions ######################################################### +############################################################################### + +# Default to building the 'Debug' configuration +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Debug CACHE STRING "The build configuration" FORCE) +endif() + +# add the data to the target, so it becomes visible in some IDE +add_executable(solvitaire ${sources}) + +# this lets me include files relative to the root src dir with a <> pair +target_include_directories(solvitaire PUBLIC src/main) + +target_link_libraries(solvitaire LINK_PUBLIC ${Boost_LIBRARIES}) + +# this copies all resource files in the build directory +# we need this, because we want to work with paths relative to the executable +file(COPY ${data} DESTINATION resources) + +############################################################################### +## testing #################################################################### +############################################################################### + +add_executable(unit_tests ${sources_test}) + +# this allows us to use our executable as a link library +# therefore we can inherit all compiler options and library dependencies +set_target_properties(solvitaire PROPERTIES ENABLE_EXPORTS on) + +target_link_libraries(unit_tests gtest_main) + +ENABLE_TESTING() +ADD_TEST(NAME unit_tests + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin + COMMAND unit_tests) + +############################################################################### +## packaging ################################################################## +############################################################################### + +# all install commands get the same destination. this allows us to use paths +# relative to the executable. +install(TARGETS solvitaire DESTINATION solvitaire) +# this is basically a repeat of the file copy instruction that copies the +# resources in the build directory, but here we tell cmake that we want it +# in the package +install(DIRECTORY resources DESTINATION solvitaire) + +set(CPACK_OUTPUT_FILE_PREFIX installer) +set(CPACK_PACKAGE_VERSION "1.0.0") + +# we don't want to split our program up into several things +set(CPACK_MONOLITHIC_INSTALL 1) + +# This must be last +include(CPack)
\ No newline at end of file diff --git a/CMakeLists.txt.in b/CMakeLists.txt.in new file mode 100644 index 0000000..4c67ef5 --- /dev/null +++ b/CMakeLists.txt.in @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 2.8.2) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG master + SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) @@ -1,4 +1,4 @@ -# General Solitaire Solver +# Solvitaire A solver for a variety of single-player, perfect information solitaire card games. diff --git a/build-instructions.md b/build-instructions.md new file mode 100644 index 0000000..1a8980d --- /dev/null +++ b/build-instructions.md @@ -0,0 +1,54 @@ +# How to build / run the project + +The project uses the CMake build system generator. there are several stages for +building the project, running the program, and creating an installer. + +### Installing dependencies + +#### Upgrade GCC (if you don't have version 6) +`sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y` + +`sudo apt-get update -qq` + +`sudo apt-get install -qq g++-6` + +`sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6` + +#### Boost + +Note: this doesn't have to (and probably shouldn't) be run from within the project, unlike the other commands listed here. + +`sudo wget -O boost_1_63_0.tar.gz http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.gz/download` + +`sudo tar xzvf boost_1_63_0.tar.gz` + +`cd boost_1_63_0/` + +`sudo ./bootstrap.sh --with-libraries=log` + +`sudo ./b2 link=shared threading=multi toolset=gcc optimization=space` + +`sudo ./b2 install` + +`cd ..` + +### Generate Build System + +`cmake [-G "CodeBlocks - Unix Makefiles"] -D CMAKE_CXX_COMPILER=g++-6 -DCMAKE_BUILD_TYPE=[DEBUG|RELEASE] -Bcmake-build-[debug|release] -H.` + +### Build project + +`cmake --build cmake-build-[debug|release]` + +### Run executable + +`./cmake-build-[debug|release]/bin/solvitaire` + +### Create project installer + +`cpack --config cmake-build-[debug|release]/CPackConfig.cmake` + +## A Note on CLion + +If CLion complains that a dependency is missing, you may need to run the conan +install command, and then in Clion run 'File->Reload CMake Project diff --git a/clean-build.sh b/clean-build.sh new file mode 100755 index 0000000..5b2fd26 --- /dev/null +++ b/clean-build.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +echo "Cleaning..." +rm -rf cmake-build-debug; mkdir cmake-build-debug; +rm -rf cmake-build-release; mkdir cmake-build-release; + +echo "Generating build..." +cmake -G "CodeBlocks - Unix Makefiles" -D CMAKE_CXX_COMPILER=g++-6 -DCMAKE_BUILD_TYPE=DEBUG --target solvitaire -Bcmake-build-debug -H. +cmake -G "CodeBlocks - Unix Makefiles" -D CMAKE_CXX_COMPILER=g++-6 -DCMAKE_BUILD_TYPE=RELEASE --target solvitaire -Bcmake-build-release -H. + +echo "Building..." +cmake --build cmake-build-debug +cmake --build cmake-build-release diff --git a/docs/project-tools.md b/docs/project-tools.md new file mode 100644 index 0000000..4bbfed8 --- /dev/null +++ b/docs/project-tools.md @@ -0,0 +1,27 @@ +# Project Tools + +**Language:** C++ + +**IDE:** CLion + +**Build Tool:** CMake + +**Dependency Management:** Conan.io + +**Unit Testing:** GTest + +**Logging:** Boost logging library + +**Documentation:** Doxygen + +**Profiling:** Gperftools + +**Debugging:** GDB + +**Continuous Integration:** TravisCI + +**Version Control:** Git + +**Agile Tracking:** Trello / Kanban + +**Documentation:** Doxygen diff --git a/src/main/Card.cpp b/src/main/Card.cpp new file mode 100644 index 0000000..ead2fd5 --- /dev/null +++ b/src/main/Card.cpp @@ -0,0 +1,25 @@ +// +// Created by thecharlesblake on 10/11/17. +// + +#include "Card.h" +#include <string> + +Card::Card(const int r, const std::string s): rank(r), suit(s) {} + +std::ostream & operator<<(std::ostream & Str, Card const & c) { + std::string r; + if (c.rank == 1) { + r = "A"; + } else if (c.rank <= 10) { + r = std::to_string(c.rank); + } else if (c.rank == 11) { + r = "J"; + } else if (c.rank == 12) { + r = "Q"; + } else if (c.rank == 13) { + r = "K"; + } + + return Str << r << c.suit; +}
\ No newline at end of file diff --git a/src/main/Card.h b/src/main/Card.h new file mode 100644 index 0000000..651df71 --- /dev/null +++ b/src/main/Card.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/11/17. +// + +#ifndef SOLITAIRE_PIP_CARD_H +#define SOLITAIRE_PIP_CARD_H + +#include <string> + +struct Card { + Card(const int, const std::string); + const int rank; + const std::string suit; +}; + +std::ostream & operator<<(std::ostream &, Card const &); + + +#endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/main.cpp b/src/main/main.cpp new file mode 100644 index 0000000..1b75252 --- /dev/null +++ b/src/main/main.cpp @@ -0,0 +1,121 @@ +#include <string> +#include <iostream> +#include <vector> +#include <map> +#include <algorithm> +#include <random> +#include "Card.h" +#include <boost/log/trivial.hpp> + +#if defined NDEBUG + #define LOG_DEBUG(msg) +#else + #define LOG_DEBUG(msg) BOOST_LOG_TRIVIAL(debug) << msg +#endif +#define LOG_INFO(msg) BOOST_LOG_TRIVIAL(info) << msg +#define LOG_WARNING(msg) BOOST_LOG_TRIVIAL(warning) << msg +#define LOG_ERROR(msg) BOOST_LOG_ERROR(error) << msg + +using namespace std; + +bool search(vector<vector<Card*>* >, Card* hole); + +int main() +{ + const int topRank = 4; + + vector<vector<Card*>* > tableaux; + + vector<Card*> shuffleCards; + for (int r = 1; r <= topRank; r++) { + for (int s = 0; s < 4; s++) { + string suit; + if (s == 0) { + suit = "D"; + } else if (s == 1) { + suit = "S"; + } else if (s == 2) { + suit = "H"; + } else { + suit = "C"; + } + + Card* c = new Card(r, suit); + shuffleCards.push_back(c); + } + } + + auto rng = std::default_random_engine(2); // 3 has solution + std::shuffle(std::begin(shuffleCards), std::end(shuffleCards), rng); + + for (int i = 0; i < topRank * 2; i++) { + vector<Card*>* tab = new vector<Card*>(); + for (int j = 0; j < 2; j++) { + int idx = i + (topRank * 2 * j); + if (shuffleCards[idx]->suit == "S" && shuffleCards[idx]->rank == 1) { + continue; + } else { + tab->push_back(shuffleCards[idx]); + } + } + tableaux.push_back(tab); + } + + Card c(2, string("H")); + LOG_DEBUG(c); + + cout << (search(tableaux, new Card(1, "S")) ? "Solution Found" : "No Solution"); + + return 0; +} + +bool search(vector<vector<Card*>* > tableaux, Card* hole) { + // If the tableaux are all empty, return true + + // Seaches through each tableau. If the top card is one away from + // the hole card, puts it in the hole and recurses. + // If that fails then put the card back and repeat for the next foundation + // If it succeeds, return true + + // If we get to the end of the function, return false + +#ifndef NDEBUG + string stateRep; + for (unsigned int i = 0; i < 2; i++) { + for (auto tableau : tableaux) { + if (tableau->size() > i) { + cout << *(*tableau)[i]; + } + cout << "\t"; + } + cout << "\n"; + } + cout << "Hole: " << *hole << "\n"; +#endif + + bool allTableauxEmpty = true; + for (unsigned int i = 0; i < tableaux.size(); i++) { + auto tableau = tableaux[i]; + + if (tableau->empty()) { + continue; + } else { + allTableauxEmpty = false; + } + + Card* topCard = tableau->back(); + if (topCard->rank == hole->rank + 1 || topCard->rank == hole->rank - 1) { + tableau->pop_back(); + + bool success = search(tableaux, topCard); + + if (success) { + return true; + } else { + tableau->push_back(topCard); + } + } + } + + return allTableauxEmpty; +}
\ No newline at end of file diff --git a/src/test/main_test.cpp b/src/test/main_test.cpp new file mode 100644 index 0000000..3f2081f --- /dev/null +++ b/src/test/main_test.cpp @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +const int actualValTrue = 4; +const int actualValFalse = 8; +const int expectVal = 4; + +namespace { + TEST(StrCompare, CStrEqual) { + EXPECT_EQ(expectVal, actualValTrue); + } + + TEST(StrCompare, CStrNotEqual) { + EXPECT_NE(expectVal, actualValFalse); + } +} |
