pgn 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/TODO.md +9 -1
- data/lib/pgn/board.rb +19 -21
- data/lib/pgn/fen.rb +2 -0
- data/lib/pgn/move_calculator.rb +10 -19
- data/lib/pgn/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67674551215924868ef976e50328c035e718d064
|
4
|
+
data.tar.gz: 83baeece613b7cee8ccbdbdf648383bd03c94f55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dc3ad633890315dd863a7bf0892232761ebfb267f9769c4bec561bfe80d5125e0edabc631fd912e926d7039395c7920cbb9a2bf7c966adb24201fec6c7ab467
|
7
|
+
data.tar.gz: a85bad51cefc2a9d90cd19ac78db6e96ab97c7646c14c74c3daef10dc55c871c17ab516bea12b6a7e77b41d2a9d9879cf3ab852763e2df374292577bdc0b3f53
|
data/TODO.md
CHANGED
data/lib/pgn/board.rb
CHANGED
@@ -13,14 +13,14 @@ module PGN
|
|
13
13
|
# The starting, internal representation of a chess board
|
14
14
|
#
|
15
15
|
START = [
|
16
|
-
["R", "P",
|
17
|
-
["N", "P",
|
18
|
-
["B", "P",
|
19
|
-
["Q", "P",
|
20
|
-
["K", "P",
|
21
|
-
["B", "P",
|
22
|
-
["N", "P",
|
23
|
-
["R", "P",
|
16
|
+
["R", "P", nil, nil, nil, nil, "p", "r"],
|
17
|
+
["N", "P", nil, nil, nil, nil, "p", "n"],
|
18
|
+
["B", "P", nil, nil, nil, nil, "p", "b"],
|
19
|
+
["Q", "P", nil, nil, nil, nil, "p", "q"],
|
20
|
+
["K", "P", nil, nil, nil, nil, "p", "k"],
|
21
|
+
["B", "P", nil, nil, nil, nil, "p", "b"],
|
22
|
+
["N", "P", nil, nil, nil, nil, "p", "n"],
|
23
|
+
["R", "P", nil, nil, nil, nil, "p", "r"],
|
24
24
|
]
|
25
25
|
|
26
26
|
FILE_TO_INDEX = {
|
@@ -62,7 +62,7 @@ module PGN
|
|
62
62
|
'B' => "\u{2657}",
|
63
63
|
'N' => "\u{2658}",
|
64
64
|
'P' => "\u{2659}",
|
65
|
-
|
65
|
+
nil => '_',
|
66
66
|
}
|
67
67
|
|
68
68
|
attr_accessor :squares
|
@@ -77,14 +77,14 @@ module PGN
|
|
77
77
|
# @example
|
78
78
|
# PGN::Board.new(
|
79
79
|
# [
|
80
|
-
# ["R", "P",
|
81
|
-
# ["N", "P",
|
82
|
-
# ["B", "P",
|
83
|
-
# ["Q", "P",
|
84
|
-
# ["K", "P",
|
85
|
-
# ["B", "P",
|
86
|
-
# ["N", "P",
|
87
|
-
# ["R", "P",
|
80
|
+
# ["R", "P", nil, nil, nil, nil, "p", "r"],
|
81
|
+
# ["N", "P", nil, nil, nil, nil, "p", "n"],
|
82
|
+
# ["B", "P", nil, nil, nil, nil, "p", "b"],
|
83
|
+
# ["Q", "P", nil, nil, nil, nil, "p", "q"],
|
84
|
+
# ["K", "P", nil, nil, nil, nil, "p", "k"],
|
85
|
+
# ["B", "P", nil, nil, nil, nil, "p", "b"],
|
86
|
+
# ["N", "P", nil, nil, nil, nil, "p", "n"],
|
87
|
+
# ["R", "P", nil, nil, nil, nil, "p", "r"],
|
88
88
|
# ]
|
89
89
|
# )
|
90
90
|
#
|
@@ -106,14 +106,12 @@ module PGN
|
|
106
106
|
# board.at("e4") #=> "P"
|
107
107
|
#
|
108
108
|
def at(*args)
|
109
|
-
|
109
|
+
case args.length
|
110
110
|
when 1
|
111
111
|
self.at(*coordinates_for(args.first))
|
112
112
|
when 2
|
113
113
|
self.squares[args[0]][args[1]]
|
114
114
|
end
|
115
|
-
|
116
|
-
str == "_" ? nil : str
|
117
115
|
end
|
118
116
|
|
119
117
|
# @param changes [Hash<String, <String, nil>>] changes to make to the board
|
@@ -136,7 +134,7 @@ module PGN
|
|
136
134
|
#
|
137
135
|
def update(square, piece)
|
138
136
|
coords = coordinates_for(square)
|
139
|
-
self.squares[coords[0]][coords[1]] = piece
|
137
|
+
self.squares[coords[0]][coords[1]] = piece
|
140
138
|
self
|
141
139
|
end
|
142
140
|
|
data/lib/pgn/fen.rb
CHANGED
@@ -87,6 +87,7 @@ module PGN
|
|
87
87
|
squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i }
|
88
88
|
.split("/")
|
89
89
|
.map {|row| row.split('') }
|
90
|
+
.map {|row| row.map {|e| e == "_" ? nil : e } }
|
90
91
|
.reverse
|
91
92
|
.transpose
|
92
93
|
self.board = PGN::Board.new(squares)
|
@@ -101,6 +102,7 @@ module PGN
|
|
101
102
|
.squares
|
102
103
|
.transpose
|
103
104
|
.reverse
|
105
|
+
.map {|row| row.map {|e| e.nil? ? "_" : e } }
|
104
106
|
.map {|row| row.join }
|
105
107
|
.join("/")
|
106
108
|
.gsub(/_+/) {|match| match.length }
|
data/lib/pgn/move_calculator.rb
CHANGED
@@ -90,13 +90,12 @@ module PGN
|
|
90
90
|
def initialize(board, move)
|
91
91
|
self.board = board
|
92
92
|
self.move = move
|
93
|
+
self.origin = compute_origin
|
93
94
|
end
|
94
95
|
|
95
96
|
# @return [PGN::Board] the board after the move is made
|
96
97
|
#
|
97
98
|
def result_board
|
98
|
-
compute_origin
|
99
|
-
|
100
99
|
new_board = self.board.dup
|
101
100
|
new_board.change!(changes)
|
102
101
|
|
@@ -106,8 +105,6 @@ module PGN
|
|
106
105
|
# @return [Array<String>] which castling moves are no longer available
|
107
106
|
#
|
108
107
|
def castling_restrictions
|
109
|
-
compute_origin
|
110
|
-
|
111
108
|
restrict = case self.move.piece
|
112
109
|
when "K" then "KQ"
|
113
110
|
when "k" then "kq"
|
@@ -140,8 +137,6 @@ module PGN
|
|
140
137
|
# @return [String, nil] the en passant square if applicable
|
141
138
|
#
|
142
139
|
def en_passant_square
|
143
|
-
compute_origin
|
144
|
-
|
145
140
|
return nil if move.castle
|
146
141
|
|
147
142
|
if self.move.pawn? && (self.origin[1].to_i - self.move.destination[1].to_i).abs == 2
|
@@ -154,8 +149,6 @@ module PGN
|
|
154
149
|
private
|
155
150
|
|
156
151
|
def changes
|
157
|
-
compute_origin
|
158
|
-
|
159
152
|
changes = {}
|
160
153
|
changes.merge!(CASTLING[self.move.castle]) if self.move.castle
|
161
154
|
changes.merge!(
|
@@ -178,19 +171,17 @@ module PGN
|
|
178
171
|
def compute_origin
|
179
172
|
return nil if move.castle
|
180
173
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
end
|
187
|
-
|
188
|
-
if possibilities.length > 1
|
189
|
-
possibilities = disambiguate(possibilities)
|
190
|
-
end
|
174
|
+
possibilities = case move.piece
|
175
|
+
when /[brq]/i then direction_origins
|
176
|
+
when /[kn]/i then move_origins
|
177
|
+
when /p/i then pawn_origins
|
178
|
+
end
|
191
179
|
|
192
|
-
|
180
|
+
if possibilities.length > 1
|
181
|
+
possibilities = disambiguate(possibilities)
|
193
182
|
end
|
183
|
+
|
184
|
+
self.board.position_for(possibilities.first)
|
194
185
|
end
|
195
186
|
|
196
187
|
# From the destination square, move in each direction stopping if we
|
data/lib/pgn/version.rb
CHANGED