Class: RGnuchess::Gnuchess
- Inherits:
-
Object
- Object
- RGnuchess::Gnuchess
- Defined in:
- lib/rgnuchess.rb
Overview
Gnuchess represents an interface to the program ‘gnuchess’
Instance Method Summary collapse
-
#board ⇒ Object
retrieve the current Board.
-
#initialize ⇒ Gnuchess
constructor
A new instance of Gnuchess.
-
#load(file) ⇒ Object
load a file that gnuchess can load (e.g. a .epd file).
-
#make_move(move) ⇒ Object
make a move.
-
#moves ⇒ Object
get an array of all valid moves.
- #test ⇒ Object
-
#undo ⇒ Object
undo a move.
Constructor Details
Instance Method Details
#board ⇒ Object
retrieve the current Board
162 163 164 165 166 167 |
# File 'lib/rgnuchess.rb', line 162 def board command("show board") @gc.gets_until(/ /) board_lines = @gc.gets_until(/^$/,1)[0...-1] board = Board.parse board_lines end |
#load(file) ⇒ Object
load a file that gnuchess can load (e.g. a .epd file)
156 157 158 159 160 |
# File 'lib/rgnuchess.rb', line 156 def load(file) command "load #{file}" count=0 @gc.gets_until(/^$/, 3) end |
#make_move(move) ⇒ Object
make a move
191 192 193 194 195 196 197 198 199 |
# File 'lib/rgnuchess.rb', line 191 def make_move(move) command "#{move}" #eat until prompt count=0 if @gc.gets=~/^Illegal move/ raise InvalidMoveException, "illegal move" end @gc.gets_until(/^$/) end |
#moves ⇒ Object
get an array of all valid moves
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rgnuchess.rb', line 169 def moves command "show moves" lines = @gc.gets_until(/No\. of moves/) #parse moves mvs=lines[0...-1].join("\n").scan(/[a-zA-Z][^\s]*/) mvs.map! { |str| Move.parse( str ) } #this should never happen, but we check anyway count=lines.last.scan(/\d+/)[0].to_i fail "wrong number of moves!" unless mvs.size==count #filter invalid moves mvs = mvs.select do |mv| begin make_move mv undo true rescue InvalidMoveException false end end return mvs end |
#test ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/rgnuchess.rb', line 206 def test load "board.epd" mv = moves.select{|mv| mv.checkmate?}.first make_move mv undo puts "test complete" end |
#undo ⇒ Object
undo a move
201 202 203 204 205 |
# File 'lib/rgnuchess.rb', line 201 def undo command "undo" count=0 @gc.gets_until(/^$/,2) end |