diff options
author | Martin DeMello <mdemello@google.com> | 2019-02-07 00:52:02 -0800 |
---|---|---|
committer | John Fultz <jfultz@wolfram.com> | 2019-02-07 16:20:28 -0600 |
commit | 9e93419a236f34cfb016461a7e37a1280177e05a (patch) | |
tree | 2196679c562381de0ae9ccc44835733d01c0dbf9 /bindings | |
parent | d387d2e719f736d28db797d47ffc4fca29a202fa (diff) |
Add ruby bindings
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/Makefile | 16 | ||||
-rw-r--r-- | bindings/ruby/test1_position.rb | 42 | ||||
-rw-r--r-- | bindings/ruby/test2_selfplay.rb | 47 | ||||
-rw-r--r-- | bindings/ruby/test_base.rb | 35 | ||||
-rw-r--r-- | bindings/ruby_quackle.i | 3 |
5 files changed, 143 insertions, 0 deletions
diff --git a/bindings/Makefile b/bindings/Makefile index 1726d81..70415c2 100644 --- a/bindings/Makefile +++ b/bindings/Makefile @@ -6,6 +6,7 @@ CC=g++ # QT=QtCore # Qt4 QT=Qt5Core # Qt5 LUA=lua +RUBY=ruby-2.6 #--------------------------------- # Flags @@ -17,6 +18,7 @@ PYTHON3FLAGS := $(shell pkg-config python3 --cflags) PHPFLAGS := $(shell php-config --includes) PHPLIBS := $(shell php-config --libs) LUAFLAGS := $(shell pkg-config $(LUA) --cflags) +RUBYFLAGS := $(shell pkg-config $(RUBY) --cflags) INCLUDES=-I.. QUACKLELIBS=../lib/release/libquackle.a ../quackleio/lib/release/libquackleio.a @@ -82,6 +84,19 @@ lua: lua/quackle_wrap.o $(CC) -std=c++11 -shared $(LUAFLAGS) -Wl,--whole-archive $(QUACKLELIBS) -Wl,--no-whole-archive $(QTLIBS) $< -o lua/quackle.so #--------------------------------- +# Ruby + +ruby/quackle_wrap.cxx: + @test -d ruby || mkdir ruby + swig -c++ -o $@ $(INCLUDES) $(QTFLAGS) -ruby ruby_quackle.i + +ruby/quackle_wrap.o: ruby/quackle_wrap.cxx + $(CC) -std=c++11 -fPIC $(QTFLAGS) $(RUBYFLAGS) $(INCLUDES) -c $< -o $@ + +ruby: ruby/quackle_wrap.o + $(CC) -std=c++11 -shared -Wl,--whole-archive $(QUACKLELIBS) -Wl,--no-whole-archive $(QTLIBS) $< -o ruby/quackle.so + +#--------------------------------- .PHONY: clean go @@ -90,6 +105,7 @@ clean: -rm -rf python3/quackle.py -rm -rf python3/__pycache__ -rm -rf php/*php* + -rm -rf ruby/quackle.rb -rm -rf */*_wrap.cxx -rm -rf */*.o -rm -rf */*.so diff --git a/bindings/ruby/test1_position.rb b/bindings/ruby/test1_position.rb new file mode 100644 index 0000000..b76ca56 --- /dev/null +++ b/bindings/ruby/test1_position.rb @@ -0,0 +1,42 @@ +require_relative 'quackle' +require_relative 'test_base' + +dm = initialize_dm + +# Create a computer player +player1 = get_computer_player(dm) +puts player1.name + +# Create the Game file (.gcg) reader +gamereader = Quackle::GCGIO.new +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.new +unseenbag = position.unseenBag +if unseenbag.size <= dm.parameters.rackSize + 3 + enum = Quackle::Enumerator.new(unseenbag) + enum.enumerate(racks) + racks.each do |rack| + puts rack + end +end + +movesToShow = 10 + +puts "Board state: \n%s" % position.board.toString +puts "Move made: %s" % position.moveMade.toString +puts "Current player: %s" % position.currentPlayer.storeInformationToString +puts "Turn number: %i" % position.turnNumber + +movelist = player1.moves(10) + +# Show 10 moves suggested by computer player +movelist.each do |move| + puts move.toString +end diff --git a/bindings/ruby/test2_selfplay.rb b/bindings/ruby/test2_selfplay.rb new file mode 100644 index 0000000..3cc101b --- /dev/null +++ b/bindings/ruby/test2_selfplay.rb @@ -0,0 +1,47 @@ +require_relative 'quackle' +require_relative 'test_base' + + +dm = initialize_dm + +p1 = get_computer_player(dm) +p2 = get_computer_player(dm) + +# Create computer players +player1 = Quackle::Player.new('Compy1', Quackle::Player::ComputerPlayerType, 0) +player1.setComputerPlayer(p1) +puts player1.name + +player2 = Quackle::Player.new('Compy2', Quackle::Player::ComputerPlayerType, 1) +player2.setComputerPlayer(p2) +puts player2.name + +dm.seedRandomNumbers(42) + +game = Quackle::Game.new +players = Quackle::PlayerList.new + +players << player1 +players << player2 + +game.setPlayers(players) +game.associateKnownComputerPlayers +game.addPosition + + +50.times do + if game.currentPosition.gameOver + puts 'GAME OVER' + break + end + + player = game.currentPosition.currentPlayer + puts 'Player: ' + player.name + puts 'Rack : ' + player.rack.toString + + move = game.haveComputerPlay + puts 'Move: ' + move.toString + puts "Board: \n" + game.currentPosition.board.toString + + sleep(1) +end diff --git a/bindings/ruby/test_base.rb b/bindings/ruby/test_base.rb new file mode 100644 index 0000000..6fa7f05 --- /dev/null +++ b/bindings/ruby/test_base.rb @@ -0,0 +1,35 @@ +def initialize_dm(lexicon: 'twl06', alphabet: 'english', datadir: '../../data') + # Set up the data manager + dm = Quackle::DataManager.new + 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.new + + fail unless fa.load(abc2) + dm.setAlphabetParameters(fa) + + # Set up the board + board = Quackle::BoardParameters.new + 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.init(lexicon) + return dm +end + +def get_computer_player(dm, name: 'Speedy Player') + player, found = dm.computerPlayers.playerForName(name) + fail unless found + player.computerPlayer +end + diff --git a/bindings/ruby_quackle.i b/bindings/ruby_quackle.i new file mode 100644 index 0000000..abdd3a3 --- /dev/null +++ b/bindings/ruby_quackle.i @@ -0,0 +1,3 @@ +%rename(init) *::initialize; + +%include quackle.i |