From 3c18dd46b52b1e45341df1f9423c1654462fbb09 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 2 Jul 2018 21:38:18 +0200 Subject: Make AI choice configurable --- Makefile | 6 +++++- compile_ai.sh | 16 ++++++++++++++++ main.cpp | 12 +++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100755 compile_ai.sh diff --git a/Makefile b/Makefile index 2be8c3c..7536e18 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,10 @@ CXXFLAGS = -Wall -Wextra -O3 -std=c++17 -fwrapv -flto TARGET = main +ifneq ($(value AI),) + CXXFLAGS += "-DAI_CHOICE=$(value AI)" +endif + .PHONY: all clean all: $(TARGET) @@ -12,7 +16,7 @@ clean: main: $(patsubst %.cpp,%.o,$(wildcard *.cpp)) - $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS) + $(CXX) $(CXXFLAGS) $^ -o $@ %.o: %.cpp $(wildcard *.h) $(CXX) $(CXXFLAGS) -c -o $@ $< diff --git a/compile_ai.sh b/compile_ai.sh new file mode 100755 index 0000000..a24cd3f --- /dev/null +++ b/compile_ai.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -eo pipefail + +cd "$(dirname "$0")" + +if [[ -f main ]]; then + have_main=1 + mv main .compile_ai_main_bak +fi + +make AI="$1" -W main.cpp +mv main "$1" + +if [[ $have_main -eq 1 ]]; then + mv .compile_ai_main_bak main +fi diff --git a/main.cpp b/main.cpp index 4fb66a9..9dfdfe9 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,14 @@ using namespace std; +#ifndef AI_CHOICE +#define AI_CHOICE MC +#endif + +#define STR_(x) #x +#define STR(x) STR_(x) +#define AI_CHOICE_STR STR(AI_CHOICE) + static void skipLine(istream &stream) { while (stream.get() != '\n'); @@ -15,7 +23,7 @@ static void skipLine(istream &stream) { static Move findMove(const Board &bd, int player) { clock_t start = clock(); - Move mv = AI::MC::findMove(bd, player); + Move mv = AI:: AI_CHOICE ::findMove(bd, player); clock_t diff = clock() - start; cerr << "Time taken: " << (double)diff / CLOCKS_PER_SEC << " seconds" << endl; return mv; @@ -26,6 +34,8 @@ int main() { gettimeofday(&tv, nullptr); srand(tv.tv_sec * 1000000UL + tv.tv_usec); + cerr << "Using AI: " << AI_CHOICE_STR << endl; + Board bd = Board::makeInitial(); // cerr << bd << endl; -- cgit v1.2.3