1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
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.
11 def initialize(rep, validator_factory,
12 move_factory, piece_factory, notation)
14 @validator = validator_factory
16 @piece = piece_factory
20 def serialize(move, ref)
23 dst = @notation.point_to_coord(move.dst)
24 result = if move.dropped
25 @piece.symbol(move.dropped.type) + '*' + dst
27 @notation.point_to_coord(move.src) + dst
29 result += '+' if move.promote?
32 compact move, ref, lambda{|t| @piece.symbol(t) }
34 compact move, ref, lambda{|t| "{#{t.to_s}}" }
38 def deserialize(s, ref)
39 notation = @notation.read(s)
40 read_notation ref, notation if notation
43 def compact(move, ref, sym)
46 result = sym[move.dropped.type] + '*' +
47 @notation.point_to_coord(move.dst)
49 piece = ref.board[move.src]
51 capture_square = ref.capture_square(move)
52 captured = ref.board[capture_square]
54 result = sym[piece.type]
55 notation = minimal_notation ref,
59 :promote => move.promote?
61 result += @notation.point_to_coord(notation[:src]) if notation[:src]
62 result += (captured ? 'x' : '-')
63 result += @notation.point_to_coord(notation[:dst])
70 validate = @validator.new(ref)
71 alt = @move.new(move.src, move.dst, :promote => true)
80 def read_notation(ref, san)
82 validate = @validator.new(ref)
85 mv = @move.new(san[:src], san[:dst], :promote => san[:promote])
88 mv = @move.drop(@piece.new(ref.turn, san[:type]), san[:dst])
91 ref.board.each_square do |p|
92 mv = @move.new(p, san[:dst], :promote => san[:promote])
95 piece.type == san[:type] and
96 piece.color == ref.turn
111 def minimal_notation(ref, notation)
112 @notation.each_alternative(notation) do |alternative|
113 return alternative if read_notation(ref, alternative)