summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2019-02-15 11:32:44 +0100
committerTom Smeding <tom.smeding@gmail.com>2019-02-15 11:32:44 +0100
commit3711c92f43eedbd698a9c477e3248fb87892fbd9 (patch)
tree7132c9f0396304acfbab612469d6386a9a2ab2a2
parent99f3ad9bc30815213af5c666150daa6080c509db (diff)
Multiple AI's (+RAND)
-rw-r--r--.gitignore3
-rw-r--r--Makefile38
-rw-r--r--ai_mc.cpp (renamed from mc.cpp)2
-rw-r--r--ai_mc.h (renamed from mc.h)0
-rw-r--r--ai_rand.cpp19
-rw-r--r--ai_rand.h11
-rw-r--r--main.cpp16
7 files changed, 79 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 4f457ba..3e5e27a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
-ai
+aimc
+airand
.objs/
diff --git a/Makefile b/Makefile
index c8bca05..c1a3ee7 100644
--- a/Makefile
+++ b/Makefile
@@ -2,20 +2,36 @@ CXX := g++
CXXFLAGS := -Wall -Wextra -std=c++11 -g -O2
LDFLAGS :=
-TARGET := ai
OBJDIR := .objs
-CXX_SOURCES := $(wildcard *.cpp)
-OBJ_FILES := $(patsubst %.cpp,$(OBJDIR)/%.o,$(CXX_SOURCES))
-DEP_FILES := $(patsubst %.cpp,$(OBJDIR)/%.d,$(CXX_SOURCES))
+ifneq ($(value AI),)
+ AI_UPPER := $(shell echo '$(value AI)' | tr a-z A-Z)
+ AI_LOWER := $(shell echo '$(value AI)' | tr A-Z a-z)
+else
+ AI_UPPER := MC
+ AI_LOWER := mc
+endif
+CXXFLAGS += -DAI=$(AI_UPPER)
-.PHONY: all clean
+MAIN_OBJ := $(OBJDIR)/main_$(AI_LOWER).o
+CXX_SOURCES := $(filter-out main.cpp ai_%.cpp,$(wildcard *.cpp)) ai_$(AI_LOWER).cpp
+OBJ_FILES := $(patsubst %.cpp,$(OBJDIR)/%.o,$(CXX_SOURCES)) $(MAIN_OBJ)
+DEP_FILES := $(patsubst %.cpp,$(OBJDIR)/%.d,$(CXX_SOURCES)) $(MAIN_OBJ:.o=.d)
+
+TARGET := ai$(AI_LOWER)
+
+AI_ALL_LOWER := $(patsubst ai_%.cpp,%,$(wildcard ai_*.cpp))
+
+TARGET_ALL := $(patsubst %,ai%,$(AI_ALL_LOWER))
+
+
+.PHONY: all clean debug
all: $(TARGET)
clean:
- rm -f $(TARGET)
+ rm -f $(TARGET_ALL)
rm -rf $(OBJDIR)
$(TARGET): $(OBJ_FILES) termio/libtermio.a
@@ -32,6 +48,16 @@ $(OBJDIR)/%.d: %.cpp termio/libtermio.a
@echo DEP $<
@$(CXX) -MT $(OBJDIR)/$*.o -MM $(CXXFLAGS) $< >$@
+$(OBJDIR)/main_$(AI_LOWER).o: main.cpp termio/libtermio.a
+ @mkdir -p $(dir $@)
+ @echo CXX "$< -o $(notdir $@)"
+ @$(CXX) $(CXXFLAGS) -c -o $@ $<
+
+$(OBJDIR)/main_$(AI_LOWER).d: main.cpp termio/libtermio.a
+ @mkdir -p $(dir $@)
+ @echo DEP "$< -o $(notdir $@)"
+ @$(CXX) -MT $(OBJDIR)/$*.o -MM $(CXXFLAGS) $< >$@
+
termio/.git:
git submodule update --init
diff --git a/mc.cpp b/ai_mc.cpp
index 5e64102..6175d96 100644
--- a/mc.cpp
+++ b/ai_mc.cpp
@@ -1,7 +1,7 @@
#include <iostream>
#include <cassert>
#include <climits>
-#include "mc.h"
+#include "ai_mc.h"
#include "util.h"
using namespace std;
diff --git a/mc.h b/ai_mc.h
index d4221d9..d4221d9 100644
--- a/mc.h
+++ b/ai_mc.h
diff --git a/ai_rand.cpp b/ai_rand.cpp
new file mode 100644
index 0000000..7479d01
--- /dev/null
+++ b/ai_rand.cpp
@@ -0,0 +1,19 @@
+#include <iostream>
+#include <cassert>
+#include <climits>
+#include "ai_rand.h"
+#include "util.h"
+
+using namespace std;
+
+
+int RAND::calcMove(Board &bd, uint8_t myclr) {
+ (void)myclr;
+ assert(bd.bag.totalLeft() > 0);
+
+ int moves[BSZ * BSZ], nmoves = 0;
+ bd.forEachMove([&moves, &nmoves](int idx) { moves[nmoves++] = idx; });
+
+ assert(nmoves > 0);
+ return moves[random() % nmoves];
+}
diff --git a/ai_rand.h b/ai_rand.h
new file mode 100644
index 0000000..9185ccd
--- /dev/null
+++ b/ai_rand.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "board.h"
+
+using namespace std;
+
+
+namespace RAND {
+ // bd will be unchanged upon return
+ int calcMove(Board &bd, uint8_t myclr);
+}
diff --git a/main.cpp b/main.cpp
index c53f5ae..61cd573 100644
--- a/main.cpp
+++ b/main.cpp
@@ -2,18 +2,28 @@
#include <cstdlib>
#include <sys/time.h>
#include "board.h"
-#include "mc.h"
+#include "ai_mc.h"
+#include "ai_rand.h"
#include "ui.h"
#include "util.h"
using namespace std;
+#define STR_(x) #x
+#define STR(x) STR_(x)
+
+#ifndef AI
+#define AI MC
+#endif
+
int main() {
struct timeval tv;
gettimeofday(&tv, nullptr);
srandom(tv.tv_sec * 1000000U + tv.tv_usec);
+ cerr << "Using AI " << STR(AI) << endl;
+
Board bd = Board::makeEmpty();
cerr << "Initial stone at " << Idx(BSZ * BMID + BMID) << endl;
bd.put(BSZ * BMID + BMID, bd.bag.drawRandom());
@@ -30,7 +40,7 @@ int main() {
cout << "YOUR TURN." << endl;
idx = UI::getMove(bd);
} else {
- idx = MC::calcMove(bd, onturn);
+ idx = AI::calcMove(bd, onturn);
}
uint8_t clr = bd.bag.drawRandom();
@@ -45,4 +55,6 @@ int main() {
onturn = NEXTTURN(onturn);
}
+
+ cout << "TIE" << endl;
}