diff options
author | Martin DeMello <mdemello@google.com> | 2019-02-06 20:11:58 -0800 |
---|---|---|
committer | John Fultz <jfultz@wolfram.com> | 2019-02-07 16:20:28 -0600 |
commit | d387d2e719f736d28db797d47ffc4fca29a202fa (patch) | |
tree | 100d4b6c794f855acfbcb878b8163cd59b3a8d75 /bindings | |
parent | 3ce63c2e3f4056f6fe8f4191de41290a87f42730 (diff) |
Add separate python2 and python3 targets, and a flag for Qt version.
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/Makefile | 63 | ||||
-rw-r--r-- | bindings/python2/__init__.py | 0 | ||||
-rw-r--r-- | bindings/python2/test1_position.py (renamed from bindings/python/test1_position.py) | 0 | ||||
-rw-r--r-- | bindings/python2/test2_selfplay.py (renamed from bindings/python/test2_selfplay.py) | 5 | ||||
-rw-r--r-- | bindings/python3/__init__.py | 0 | ||||
-rw-r--r-- | bindings/python3/test1_position.py | 78 | ||||
-rw-r--r-- | bindings/python3/test2_selfplay.py | 85 |
7 files changed, 218 insertions, 13 deletions
diff --git a/bindings/Makefile b/bindings/Makefile index 9cdb6a3..1726d81 100644 --- a/bindings/Makefile +++ b/bindings/Makefile @@ -1,15 +1,29 @@ CC=g++ -QTFLAGS := $(shell pkg-config QtCore --cflags) -QTLIBS := $(shell pkg-config QtCore --libs) -PYTHONFLAGS := $(shell pkg-config python2 --cflags) +#--------------------------------- +# Package versions for pkg-config + +# QT=QtCore # Qt4 +QT=Qt5Core # Qt5 +LUA=lua + +#--------------------------------- +# Flags + +QTFLAGS := $(shell pkg-config $(QT) --cflags) +QTLIBS := $(shell pkg-config $(QT) --libs) +PYTHON2FLAGS := $(shell pkg-config python2 --cflags) +PYTHON3FLAGS := $(shell pkg-config python3 --cflags) PHPFLAGS := $(shell php-config --includes) PHPLIBS := $(shell php-config --libs) -LUAFLAGS := $(shell pkg-config lua5.1 --cflags) +LUAFLAGS := $(shell pkg-config $(LUA) --cflags) INCLUDES=-I.. QUACKLELIBS=../lib/release/libquackle.a ../quackleio/lib/release/libquackleio.a +#--------------------------------- +# PHP + php/quackle_wrap.cxx: @test -d php || mkdir php swig -c++ -o $@ $(INCLUDES) $(QTFLAGS) -php quackle.i @@ -20,20 +34,43 @@ php/quackle_wrap.o: php/quackle_wrap.cxx php: php/quackle_wrap.o $(CC) -std=c++11 -shared -Wl,--whole-archive $(QUACKLELIBS) -Wl,--no-whole-archive $(QTLIBS) $< -o php/quackle.so -python/quackle_wrap.cxx: - @test -d python || mkdir python +#--------------------------------- +# Python2 + +python2/quackle_wrap.cxx: + @test -d python2 || mkdir python2 swig -c++ -o $@ $(INCLUDES) $(QTFLAGS) -python quackle.i -python/quackle_wrap.o: python/quackle_wrap.cxx - $(CC) -std=c++11 -fPIC $(QTFLAGS) $(PYTHONFLAGS) $(INCLUDES) -c $< -o $@ +python2/quackle_wrap.o: python2/quackle_wrap.cxx + $(CC) -std=c++11 -fPIC $(QTFLAGS) $(PYTHON2FLAGS) $(INCLUDES) -c $< -o $@ + +python2: python2/quackle_wrap.o + $(CC) -std=c++11 -shared -Wl,--whole-archive $(QUACKLELIBS) -Wl,--no-whole-archive $(QTLIBS) $< -o python2/_quackle.so + +#--------------------------------- +# Python3 -python: python/quackle_wrap.o - $(CC) -std=c++11 -shared -Wl,--whole-archive $(QUACKLELIBS) -Wl,--no-whole-archive $(QTLIBS) $< -o python/_quackle.so +python3/quackle_wrap.cxx: + @test -d python3 || mkdir python3 + swig -c++ -o $@ $(INCLUDES) $(QTFLAGS) -python -py3 quackle.i + +python3/quackle_wrap.o: python3/quackle_wrap.cxx + $(CC) -std=c++11 -fPIC $(QTFLAGS) $(PYTHON3FLAGS) $(INCLUDES) -c $< -o $@ + +python3: python3/quackle_wrap.o + $(CC) -std=c++11 -shared -Wl,--whole-archive $(QUACKLELIBS) -Wl,--no-whole-archive $(QTLIBS) $< -o python3/_quackle.so + +#--------------------------------- +# Go go: ln -sf ../quackle.i go/quackle.swigcxx go build ./go/... + +#--------------------------------- +# Lua + lua/quackle_wrap.cxx: @test -d lua || mkdir lua swig -c++ -o $@ $(INCLUDES) $(QTFLAGS) -lua quackle.i @@ -44,10 +81,14 @@ lua/quackle_wrap.o: lua/quackle_wrap.cxx lua: lua/quackle_wrap.o $(CC) -std=c++11 -shared $(LUAFLAGS) -Wl,--whole-archive $(QUACKLELIBS) -Wl,--no-whole-archive $(QTLIBS) $< -o lua/quackle.so +#--------------------------------- + .PHONY: clean go clean: - -rm -rf python/quackle.py + -rm -rf python2/quackle.py + -rm -rf python3/quackle.py + -rm -rf python3/__pycache__ -rm -rf php/*php* -rm -rf */*_wrap.cxx -rm -rf */*.o diff --git a/bindings/python2/__init__.py b/bindings/python2/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/bindings/python2/__init__.py diff --git a/bindings/python/test1_position.py b/bindings/python2/test1_position.py index e04081a..e04081a 100644 --- a/bindings/python/test1_position.py +++ b/bindings/python2/test1_position.py diff --git a/bindings/python/test2_selfplay.py b/bindings/python2/test2_selfplay.py index bb6e889..79a5ac7 100644 --- a/bindings/python/test2_selfplay.py +++ b/bindings/python2/test2_selfplay.py @@ -75,9 +75,10 @@ for i in range(50): break player = game.currentPosition().currentPlayer() - move = game.haveComputerPlay() - #print "Player: " + player.name() + print "Player: " + player.name() print "Rack : " + player.rack().toString() + + move = game.haveComputerPlay() print 'Move: ' + move.toString() print 'Board: \n' + game.currentPosition().board().toString() diff --git a/bindings/python3/__init__.py b/bindings/python3/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/bindings/python3/__init__.py diff --git a/bindings/python3/test1_position.py b/bindings/python3/test1_position.py new file mode 100644 index 0000000..ecc094c --- /dev/null +++ b/bindings/python3/test1_position.py @@ -0,0 +1,78 @@ +# coding: utf-8 + +import quackle + +def startUp(lexicon='twl06', + alphabet='english', + datadir='../../data'): + + # Set up the data manager + dm = quackle.DataManager() + dm.setComputerPlayers(quackle.ComputerPlayerCollection.fullCollection()) + dm.setBackupLexicon(lexicon) + dm.setAppDataDirectory(datadir) + + # Set up the alphabet + abc = quackle.AlphabetParameters.findAlphabetFile(alphabet) + abc2 = quackle.Util.stdStringToQString(abc) #convert to qstring + fa = quackle.FlexibleAlphabetParameters() + + assert fa.load(abc2) + dm.setAlphabetParameters(fa) + + # Set up the board + board = quackle.BoardParameters() + dm.setBoardParameters(board) + + # Find the lexicon + dawg = quackle.LexiconParameters.findDictionaryFile(lexicon + '.dawg') + gaddag = quackle.LexiconParameters.findDictionaryFile(lexicon + '.gaddag') + dm.lexiconParameters().loadDawg(dawg) + dm.lexiconParameters().loadGaddag(gaddag) + + dm.strategyParameters().initialize(lexicon) + return dm + + +def getComputerPlayer(dm, name='Speedy Player'): + player, found = dm.computerPlayers().playerForName(name) + assert found + player = player.computerPlayer() + return player + + +dm = startUp() + +# Create a computer player +player1 = getComputerPlayer(dm) +print(player1.name()) + +# Create the Game file (.gcg) reader +gamereader = quackle.GCGIO() +gamePath = quackle.Util.stdStringToQString('../../test/positions/short_game_with_bad_moves.gcg') +game = gamereader.read(gamePath, 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/python3/test2_selfplay.py b/bindings/python3/test2_selfplay.py new file mode 100644 index 0000000..5cd3141 --- /dev/null +++ b/bindings/python3/test2_selfplay.py @@ -0,0 +1,85 @@ +# coding: utf-8 + +import time + +import quackle + +def startUp(lexicon='twl06', + alphabet='english', + datadir='../../data'): + + # Set up the data manager + dm = quackle.DataManager() + dm.setComputerPlayers(quackle.ComputerPlayerCollection.fullCollection()) + dm.setBackupLexicon(lexicon) + dm.setAppDataDirectory(datadir) + + # Set up the alphabet + abc = quackle.AlphabetParameters.findAlphabetFile(alphabet) + abc2 = quackle.Util.stdStringToQString(abc) #convert to qstring + fa = quackle.FlexibleAlphabetParameters() + + assert fa.load(abc2) + dm.setAlphabetParameters(fa) + + # Set up the board + board = quackle.BoardParameters() + dm.setBoardParameters(board) + + # Find the lexicon + dawg = quackle.LexiconParameters.findDictionaryFile(lexicon + '.dawg') + gaddag = quackle.LexiconParameters.findDictionaryFile(lexicon + '.gaddag') + dm.lexiconParameters().loadDawg(dawg) + dm.lexiconParameters().loadGaddag(gaddag) + + dm.strategyParameters().initialize(lexicon) + return dm + + +def getComputerPlayer(dm, name='Speedy Player'): + player, found = dm.computerPlayers().playerForName(name) + assert found + player = player.computerPlayer() + return player + + +dm = startUp() + +p1 = getComputerPlayer(dm) +p2 = getComputerPlayer(dm) + +# Create computer players +player1 = quackle.Player('Compy1', quackle.Player.ComputerPlayerType, 0) +player1.setComputerPlayer(p1) +print(player1.name()) + +player2 = quackle.Player('Compy2', quackle.Player.ComputerPlayerType, 1) +player2.setComputerPlayer(p2) +print(player2.name()) + +dm.seedRandomNumbers(42) + +game = quackle.Game() +players = quackle.PlayerList() + +players.append(player1) +players.append(player2) + +game.setPlayers(players) +game.associateKnownComputerPlayers() +game.addPosition() + +for i in range(50): + if game.currentPosition().gameOver(): + print("GAME OVER") + break + + player = game.currentPosition().currentPlayer() + print("Player: " + player.name()) + print("Rack : " + player.rack().toString()) + + move = game.haveComputerPlay() + print('Move: ' + move.toString()) + print('Board: \n' + game.currentPosition().board().toString()) + + time.sleep(1) |