Class: RubyShogi::Xboard
- Inherits:
-
Object
- Object
- RubyShogi::Xboard
- Defined in:
- lib/ruby_shogi/xboard.rb
Instance Method Summary collapse
- #cmd_go ⇒ Object
- #cmd_level(level) ⇒ Object
- #cmd_move(move) ⇒ Object
- #cmd_new ⇒ Object
- #cmd_variant(variant) ⇒ Object
- #go ⇒ Object
-
#initialize(random_mode = false) ⇒ Xboard
constructor
A new instance of Xboard.
- #play ⇒ Object
- #print_xboard ⇒ Object
- #update_movestogo ⇒ Object
Constructor Details
#initialize(random_mode = false) ⇒ Xboard
Returns a new instance of Xboard.
10 11 12 13 14 15 16 17 |
# File 'lib/ruby_shogi/xboard.rb', line 10 def initialize(random_mode = false) @random_mode = random_mode @engine = RubyShogi::Engine.new(random_mode: random_mode) @movestogo_orig = 40 @forcemode = false Signal.trap("SIGPIPE", "SYSTEM_DEFAULT") trap("INT", "IGNORE") # no interruptions end |
Instance Method Details
#cmd_go ⇒ Object
54 55 56 57 58 59 |
# File 'lib/ruby_shogi/xboard.rb', line 54 def cmd_go if @canmakemove puts "move #{play}" @canmakemove = false end end |
#cmd_level(level) ⇒ Object
49 50 51 52 |
# File 'lib/ruby_shogi/xboard.rb', line 49 def cmd_level(level) @engine.movestogo = level.to_i @movestogo_orig = @engine.movestogo end |
#cmd_move(move) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ruby_shogi/xboard.rb', line 61 def cmd_move(move) #@engine.board.print_board update_movestogo # update counter if @engine.make_move?(move) @canmakemove = true if @canmakemove && ! @engine.gameover puts "move #{play}" @canmakemove = false end end end |
#cmd_new ⇒ Object
44 45 46 47 |
# File 'lib/ruby_shogi/xboard.rb', line 44 def cmd_new @engine = RubyShogi::Engine.new(random_mode: @random_mode) @canmakemove = true end |
#cmd_variant(variant) ⇒ Object
40 41 42 |
# File 'lib/ruby_shogi/xboard.rb', line 40 def cmd_variant(variant) @variant = variant end |
#go ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruby_shogi/xboard.rb', line 73 def go puts "#{RubyShogi::NAME} #{RubyShogi::VERSION} by #{RubyShogi::AUTHOR}" @movestogo_orig = 40 @canmakemove = true $stdin.each do |cmd| cmd.strip! case cmd when "xboard" then when "hard" then when "easy" then when "random" then when "nopost" then when "post" then when "white" then when "black" then # ignore when "remove" then @engine.history_remove when "undo" then @engine.history_undo when "?" then @engine.move_now = true when /^computer/ then when /^st/ then when /^otim/ then when /^accepted/ then when /^result/ then # ignore when /^protover/ then print_xboard when /^ping\s+(.*)/ then puts "pong #{$1}" when /^variant\s+(.*)/ then cmd_variant($1) when "new" then cmd_new when "list" then @engine.move_list when /^level\s+(.+)\s+.*/ then cmd_level($1) when /^time\s+(.+)/ then @engine.time = 0.01 * $1.to_i when /^setboard\s+(.+)/ then @engine.board.fen($1) when "quit" then return when "p" then @engine.board.print_board when "force" then @forcemode = true when "go" then cmd_go else # assume move cmd_move(cmd) end end end |
#play ⇒ Object
28 29 30 |
# File 'lib/ruby_shogi/xboard.rb', line 28 def play @engine.think end |
#print_xboard ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/ruby_shogi/xboard.rb', line 19 def print_xboard rv = @random_mode ? " random" : "" puts "feature myname=\"#{RubyShogi::NAME} #{RubyShogi::VERSION}#{rv}\"" puts "feature variants=\"shogi\"" puts "feature setboard=1" puts "feature ping=1" puts "feature done=1" end |
#update_movestogo ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/ruby_shogi/xboard.rb', line 32 def update_movestogo if @engine.movestogo == 1 @engine.movestogo = @movestogo_orig elsif @engine.movestogo > 0 @engine.movestogo -= 1 end end |