Class: ChessRB::Notation

Inherits:
Object
  • Object
show all
Defined in:
lib/chess_rb/notation.rb

Class Method Summary collapse

Class Method Details

.algebraic_to_square(move) ⇒ Object

TODO



51
52
53
# File 'lib/chess_rb/notation.rb', line 51

def self.algebraic_to_square(move)
  return move.square
end

.square_to_algebraic(move) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chess_rb/notation.rb', line 2

def self.square_to_algebraic(move)
  board = move.board
  piece = board.piece_on(move.from)
  san = ""

  raise ArgumentError.new "Invalid move and/or position" if !move.valid? ||
    !board.valid?


  if move.queen_castle?
    return "O-O-O"
  elsif move.king_castle?
    return "O-O"
  else
    if piece.type == 'P'
      if move.capture?
        san = move.from[0]
      end
    else
      san += piece.type.upcase

      # test ambiguities
      # file
      # rank
      # both
    end
  end

  if move.capture?
    san += "x"
  end

  san += move.to

  san += "=#{move.promotion}" if move.promotion

  if !board.squares_with(['WK']).empty? && !board.squares_with(['BK']).empty?
    undo_info = board.piece_on(move.to)
    board.make_move(move)
    if (board.check?)
      san += board.mate? ? "#" : "+"
    end
    board.undo_move(move, undo_info)
  end

  return san
end