summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGökçen Eraslan <gokcen.eraslan@gmail.com>2016-01-06 19:50:21 +0100
committerGökçen Eraslan <gokcen.eraslan@gmail.com>2016-01-06 19:50:21 +0100
commitdcb66bd8ebe8a4f8ee6605b00af09e8a48ace0ff (patch)
tree5e5a7aafb12bf65246265b9f6e82f817842898ca
parentdf31cc20c63c9447ba057d599d48cc67c4555ca7 (diff)
Add a SWIG interface file, a Bash script to generate Go, Python and Lua bindings and add Python test file.
-rw-r--r--bindings/python/test.py63
-rw-r--r--bindings/quackle.i101
-rw-r--r--bindings/swig.sh22
3 files changed, 186 insertions, 0 deletions
diff --git a/bindings/python/test.py b/bindings/python/test.py
new file mode 100644
index 0000000..a5e2373
--- /dev/null
+++ b/bindings/python/test.py
@@ -0,0 +1,63 @@
+# coding: utf-8
+
+import quackle
+
+# Set up the data manager
+dm = quackle.DataManager()
+dm.setComputerPlayers(quackle.ComputerPlayerCollection.fullCollection())
+dm.setBackupLexicon('twl06')
+dm.setAppDataDirectory('../../data')
+
+# Set up the alphabet
+abc = quackle.AlphabetParameters.findAlphabetFile('english')
+fa = quackle.FlexibleAlphabetParameters()
+assert fa.load(quackle.Util.stdStringToQString(abc))
+dm.setAlphabetParameters(fa)
+
+# Set up the board
+board = quackle.BoardParameters()
+dm.setBoardParameters(board)
+
+# Find the lexicon
+dawg = quackle.LexiconParameters.findDictionaryFile('twl06.dawg')
+gaddag = quackle.LexiconParameters.findDictionaryFile('twl06.gaddag')
+dm.lexiconParameters().loadDawg(dawg)
+dm.lexiconParameters().loadGaddag(gaddag)
+
+dm.strategyParameters().initialize('twl06')
+
+# Create a computer player
+player1, found = dm.computerPlayers().playerForName('Speedy Player')
+assert found
+player1 = player1.computerPlayer()
+print player1.name()
+
+# Create the Game file (.gcg) reader
+gamereader = quackle.GCGIO()
+game = gamereader.read(quackle.Util.stdStringToQString('../../test/positions/short_game_with_bad_moves.gcg'),
+ quackle.Logania.MaintainBoardPreparation)
+
+# Get the current position
+position = game.currentPosition()
+
+player1.setPosition(position)
+
+racks = quackle.ProbableRackList()
+unseenbag = position.unseenBag()
+if unseenbag.size() <= dm.parameters().rackSize() + 3:
+ enum = quackle.Enumerator(unseenbag)
+ enum.enumerate(racks)
+ for rack in racks:
+ print rack
+
+movesToShow = 10
+
+print "Board state: \n%s" % position.board().toString()
+print "Move made: %s" % position.moveMade().toString()
+print "Current player: %s" % position.currentPlayer().storeInformationToString()
+print "Turn number: %i" % position.turnNumber()
+
+movelist = player1.moves(10)
+
+# Show 10 moves suggested by computer player
+for move in movelist: print move.toString()
diff --git a/bindings/quackle.i b/bindings/quackle.i
new file mode 100644
index 0000000..8ded915
--- /dev/null
+++ b/bindings/quackle.i
@@ -0,0 +1,101 @@
+%module quackle
+%{
+#include "fixedstring.h"
+#include "uv.h"
+#include "alphabetparameters.h"
+#include "move.h"
+#include "rack.h"
+#include "bag.h"
+#include "board.h"
+#include "boardparameters.h"
+#include "evaluator.h"
+#include "catchall.h"
+#include "player.h"
+#include "game.h"
+#include "gameparameters.h"
+#include "sim.h"
+#include "computerplayer.h"
+#include "computerplayercollection.h"
+#include "datamanager.h"
+#include "endgame.h"
+#include "endgameplayer.h"
+#include "enumerator.h"
+#include "bogowinplayer.h"
+#include "clock.h"
+#include "generator.h"
+#include "gaddag.h"
+#include "lexiconparameters.h"
+#include "preendgame.h"
+#include "reporter.h"
+#include "resolvent.h"
+#include "strategyparameters.h"
+
+#include <QString>
+#include "quackleio/flexiblealphabet.h"
+#include "quackleio/util.h"
+#include "quackleio/logania.h"
+#include "quackleio/gcgio.h"
+%}
+
+%include "std_string.i"
+%include "std_vector.i"
+
+/*Needed to generate proper iterable types */
+%template(MoveVector) std::vector<Quackle::Move>;
+%template(PlayerVector) std::vector<Quackle::Player>;
+%template(ProbableRackList) std::vector<Quackle::ProbableRack>;
+%template(PositionList) std::vector<Quackle::GamePosition>;
+
+%include "fixedstring.h"
+%include "uv.h"
+%include "alphabetparameters.h"
+%include "move.h"
+%include "rack.h"
+%include "bag.h"
+%include "board.h"
+%include "boardparameters.h"
+%include "evaluator.h"
+%include "catchall.h"
+%include "player.h"
+
+/* handle output arguments of PlayerList methods using cool SWIG typemaps */
+/* what we do here is just to tell SWIG that last bool& argument is an output argument */
+%include "typemaps.i"
+
+using namespace std;
+namespace Quackle
+{
+ class PlayerList : public vector<Player>
+ {
+ public:
+ PlayerList();
+
+ const Player &playerForId(int id, bool &OUTPUT) const;
+ const Player &playerForName(const UVString &name, bool &OUTPUT) const;
+ };
+}
+
+%include "game.h"
+%include "gameparameters.h"
+%include "sim.h"
+%include "computerplayer.h"
+%include "computerplayercollection.h"
+%include "datamanager.h"
+%include "endgame.h"
+%include "endgameplayer.h"
+%include "enumerator.h"
+%include "bogowinplayer.h"
+%include "clock.h"
+%include "generator.h"
+%include "gaddag.h"
+%include "lexiconparameters.h"
+%include "preendgame.h"
+%include "reporter.h"
+%include "resolvent.h"
+%include "strategyparameters.h"
+
+%include <QString>
+%include "quackleio/flexiblealphabet.h"
+%include "quackleio/util.h"
+%include "quackleio/logania.h"
+%include "quackleio/gcgio.h"
diff --git a/bindings/swig.sh b/bindings/swig.sh
new file mode 100644
index 0000000..62ec220
--- /dev/null
+++ b/bindings/swig.sh
@@ -0,0 +1,22 @@
+set -eu
+
+#################### Generate Python bindings
+ln -sf ../../lib/release/libquackle.so.0 python/libquackle.so.0
+ln -sf ../../quackleio/lib/release/libquackleio.so.0 python/libquackleio.so
+swig -c++ -o python/pyquackle.cxx -I../ `pkg-config QtCore --cflags` -python quackle.i
+g++ -c -std=c++11 -fPIC python/pyquackle.cxx `pkg-config QtCore --cflags` `pkg-config python2 --cflags` -I../ -o python/pyquackle.o
+g++ -std=c++11 -shared ../lib/release/libquackle.so ../quackleio/lib/release/libquackleio.so python/pyquackle.o -o python/_quackle.so
+
+#################### Generate Go bindings
+mkdir go
+ln -sf ../../lib/release/libquackle.so.0 go/libquackle.so.0
+ln -sf ../../quackleio/lib/release/libquackleio.so.0 go/libquackleio.so
+swig -c++ -o go/quackle_wrap.cxx -I../ `pkg-config QtCore --cflags` -go -cgo -intgosize 64 quackle.i
+
+#################### Generate Lua bindings
+mkdir lua
+ln -sf ../../lib/release/libquackle.so.0 lua/libquackle.so.0
+ln -sf ../../quackleio/lib/release/libquackleio.so.0 lua/libquackleio.so
+swig -c++ -o lua/quackle_wrap.cxx -I../ `pkg-config QtCore --cflags` -lua quackle.i
+g++ -std=c++11 -fPIC `pkg-config lua52 --cflags` `pkg-config QtCore --cflags` -I../ -c lua/quackle_wrap.cxx -o lua/quackle_wrap.o
+g++ -std=c++11 -shared `pkg-config lua52 --libs` ../lib/release/libquackle.so ../quackleio/lib/release/libquackleio.so lua/quackle_wrap.o -o lua/quackle.so