summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-07-02 21:38:18 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-07-02 21:38:29 +0200
commit3c18dd46b52b1e45341df1f9423c1654462fbb09 (patch)
treeab196d5eb6a0a50ae87d7a16a74912f7041d45a7
parent4a2389a8a97874393d48237ac638bdb51ebbe768 (diff)
Make AI choice configurable
-rw-r--r--Makefile6
-rwxr-xr-xcompile_ai.sh16
-rw-r--r--main.cpp12
3 files changed, 32 insertions, 2 deletions
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;