diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Makefile | 38 | ||||
-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.cpp | 19 | ||||
-rw-r--r-- | ai_rand.h | 11 | ||||
-rw-r--r-- | main.cpp | 16 |
7 files changed, 79 insertions, 10 deletions
@@ -1,2 +1,3 @@ -ai +aimc +airand .objs/ @@ -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 @@ -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/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); +} @@ -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; } |