Class: ChessGame
- Inherits:
-
Object
- Object
- ChessGame
- Defined in:
- lib/bangkok/chessgame.rb
Overview
A ChessGame can read a chess play file and write a MIDI file created from the moves in the file.
Instance Method Summary collapse
-
#initialize(listener = GameListener.new) ⇒ ChessGame
constructor
A new instance of ChessGame.
-
#play(io) ⇒ Object
Writes a MIDI file.
-
#read(io) ⇒ Object
Read the chess game.
-
#read_moves(io) ⇒ Object
Read the chess game and turn it into Moves.
Constructor Details
#initialize(listener = GameListener.new) ⇒ ChessGame
Returns a new instance of ChessGame.
11 12 13 |
# File 'lib/bangkok/chessgame.rb', line 11 def initialize(listener = GameListener.new) @listener = listener end |
Instance Method Details
#play(io) ⇒ Object
Writes a MIDI file.
43 44 45 46 47 48 |
# File 'lib/bangkok/chessgame.rb', line 43 def play(io) @listener.start_game(io) board = Board.new(@listener) @moves.each { | move | board.apply(move) } @listener.end_game end |
#read(io) ⇒ Object
Read the chess game. Set player names and return a string containing the chess moves (“1. f4 Nf6 2. Nf3 c5…”).
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bangkok/chessgame.rb', line 27 def read(io) game_text = '' io.each { | line | line.chomp! case line when /\[(.*)\]/ # New games starting (if multi-game file) when /^\s*$/ else game_text << ' ' game_text << line end } game_text end |
#read_moves(io) ⇒ Object
Read the chess game and turn it into Moves.
16 17 18 19 20 21 22 23 |
# File 'lib/bangkok/chessgame.rb', line 16 def read_moves(io) game_text = read(io) @moves = [] game_text.scan(/\d+\.\s+(\S+)\s+(\S+)/).each { | white, black | @moves << Move.new(:white, white) @moves << Move.new(:black, black) unless black == '1-0' || black == '0-1' } end |