From 027385d0ff9f1223cb98410a29937e38d82de397 Mon Sep 17 00:00:00 2001 From: Gökçen Eraslan Date: Thu, 7 Jan 2016 18:32:14 +0100 Subject: bindings: Add another python test file to demonstrate selfplay. --- bindings/python/test1_position.py | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 bindings/python/test1_position.py (limited to 'bindings/python/test1_position.py') diff --git a/bindings/python/test1_position.py b/bindings/python/test1_position.py new file mode 100644 index 0000000..3a95808 --- /dev/null +++ b/bindings/python/test1_position.py @@ -0,0 +1,80 @@ +# 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() + fa.thisown = False + + assert fa.load(abc2) + dm.setAlphabetParameters(fa) + + # Set up the board + board = quackle.BoardParameters() + board.thisown = False + 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() -- cgit v1.2.3 From 49ff349b67c7fed35782bf6407242fe58ef654c7 Mon Sep 17 00:00:00 2001 From: Gökçen Eraslan Date: Sun, 24 Jan 2016 13:18:24 +0100 Subject: bindings: Fix DataManager ownership problems using DISOWN. --- bindings/python/test1_position.py | 2 -- bindings/python/test2_selfplay.py | 2 -- bindings/quackle.i | 25 ++++++++++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) (limited to 'bindings/python/test1_position.py') diff --git a/bindings/python/test1_position.py b/bindings/python/test1_position.py index 3a95808..e04081a 100644 --- a/bindings/python/test1_position.py +++ b/bindings/python/test1_position.py @@ -16,14 +16,12 @@ def startUp(lexicon='twl06', abc = quackle.AlphabetParameters.findAlphabetFile(alphabet) abc2 = quackle.Util.stdStringToQString(abc) #convert to qstring fa = quackle.FlexibleAlphabetParameters() - fa.thisown = False assert fa.load(abc2) dm.setAlphabetParameters(fa) # Set up the board board = quackle.BoardParameters() - board.thisown = False dm.setBoardParameters(board) # Find the lexicon diff --git a/bindings/python/test2_selfplay.py b/bindings/python/test2_selfplay.py index 1c41874..bb6e889 100644 --- a/bindings/python/test2_selfplay.py +++ b/bindings/python/test2_selfplay.py @@ -18,14 +18,12 @@ def startUp(lexicon='twl06', abc = quackle.AlphabetParameters.findAlphabetFile(alphabet) abc2 = quackle.Util.stdStringToQString(abc) #convert to qstring fa = quackle.FlexibleAlphabetParameters() - fa.thisown = False assert fa.load(abc2) dm.setAlphabetParameters(fa) # Set up the board board = quackle.BoardParameters() - board.thisown = False dm.setBoardParameters(board) # Find the lexicon diff --git a/bindings/quackle.i b/bindings/quackle.i index cc8e390..e45eb88 100644 --- a/bindings/quackle.i +++ b/bindings/quackle.i @@ -37,21 +37,23 @@ #include "quackleio/gcgio.h" %} + %include "std_string.i" %include "std_vector.i" +%include "typemaps.i" + +%include "fixedstring.h" +%include "uv.h" +%include "alphabetparameters.h" /*Needed to generate proper iterable types */ %template(MoveVector) std::vector; %template(PlayerVector) std::vector; %template(ProbableRackList) std::vector; %template(PositionList) std::vector; - -%include "fixedstring.h" -%include "uv.h" -%include "alphabetparameters.h" - %template(LetterParameterVector) std::vector; %template(LetterStringVector) std::vector; + %include "move.h" %include "rack.h" %include "bag.h" @@ -61,10 +63,6 @@ %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 { @@ -83,6 +81,15 @@ namespace Quackle %include "sim.h" %include "computerplayer.h" %include "computerplayercollection.h" + +%apply SWIGTYPE *DISOWN {Quackle::AlphabetParameters *alphabetParameters}; +%apply SWIGTYPE *DISOWN {Quackle::BoardParameters *boardParameters}; +%apply SWIGTYPE *DISOWN {Quackle::StrategyParameters *lexiconParameters}; +%apply SWIGTYPE *DISOWN {Quackle::LexiconParameters *lexiconParameters}; +%apply SWIGTYPE *DISOWN {Quackle::Evaluator *evaluator}; +%apply SWIGTYPE *DISOWN {Quackle::GameParameters *parameters}; +%apply SWIGTYPE *DISOWN {const Quackle::PlayerList &playerList}; + %include "datamanager.h" %include "endgame.h" %include "endgameplayer.h" -- cgit v1.2.3