Module: Chess::Gnuchess

Defined in:
lib/chess/gnuchess.rb

Overview

Note:

Gnuchess binary have to be installed.

Use Gnuchess to I.A. (Only a draft).

To use this module, extend a game object with Gnuchess.

Examples:

g = Chess::Game.new
g.extend Chess::Gnuchess
g.gnuchess_move!
puts g

Instance Method Summary collapse

Instance Method Details

#gnuchess_moveString

Returns the next move calculated by Gnuchess.

Returns:

  • (String)

    Returns the short algebraic chess notation of the move.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chess/gnuchess.rb', line 16

def gnuchess_move
  pipe = IO.popen('gnuchess -x', 'r+')
  begin
    pipe.puts('depth 1')
    pipe.puts('manual')
    self.coord_moves.each do |m|
      pipe.puts(m)
    end
    pipe.puts('go')
    while (line = pipe.gets)
      raise IllegalMoveError if line.include?('Invalid move')

      match = line.match(/My move is : ([a-h][1-8][a-h][1-8][rkbq]?)/)
      return match[1] if match
    end
  ensure
    pipe.puts('quit')
    pipe.close
  end
  return moves
end

#gnuchess_move!String

Note:

This add a new Board in the Chess::Game.

Make a move using Gnuchess engine.

Returns:

  • (String)

    Returns the short algebraic chess notation of the move.



41
42
43
44
# File 'lib/chess/gnuchess.rb', line 41

def gnuchess_move!
  next_move = self.gnuchess_move
  self.move(next_move) if next_move
end