Added undo support for the GNU Shogi engine
[kaya.git] / lib / plugins / engines / gnushogi.rb
blob49f242239d367b1ae6a3dc7f06ca62f970a92b67
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'plugins/plugin'
9 require 'interaction/match'
10 require_bundle 'engines', 'engine'
12 class GNUShogiEngine < Engine
13   include Plugin
14   
15   plugin :name => 'GNUShogi Engine Protocol',
16          :protocol => 'GNUShogi',
17          :interface => :engine
19   def on_move(data)
20     text = @serializer.serialize(data[:move], data[:old_state])
21     send_command text
22     unless @playing
23       send_command "go"
24       @playing = true
25     end
26   end
27   
28   def on_engine_start
29     send_command "new"
30     send_command "force"
31     if @color == :black
32       send_command "go"
33       @playing = true
34     end
35   end
36   
37   def extra_command(text)
38     if text =~ /^[0-9]*\. (\.\.\.) (\S+)/
39       move = @serializer.deserialize($2, @match.state)
40       if move
41         @match.move(self, move)
42       end
43     end
44   end
46   def on_close(data)
47     send_command "quit"
48     @engine.kill
49   end
50   
51   def allow_undo?(player, manager)
52     # gnushogi waits when you tell it to undo
53     send_command "undo"
54     send_command "undo"
55     manager.undo(self, 2)
56   end
57 end