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/python2 | |
parent | 3ce63c2e3f4056f6fe8f4191de41290a87f42730 (diff) |
Add separate python2 and python3 targets, and a flag for Qt version.
Diffstat (limited to 'bindings/python2')
-rw-r--r-- | bindings/python2/__init__.py | 0 | ||||
-rw-r--r-- | bindings/python2/test1_position.py | 78 | ||||
-rw-r--r-- | bindings/python2/test2_selfplay.py | 85 |
3 files changed, 163 insertions, 0 deletions
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/python2/test1_position.py b/bindings/python2/test1_position.py new file mode 100644 index 0000000..e04081a --- /dev/null +++ b/bindings/python2/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/python2/test2_selfplay.py b/bindings/python2/test2_selfplay.py new file mode 100644 index 0000000..79a5ac7 --- /dev/null +++ b/bindings/python2/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) |