chess_rb 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chess_rb/piece.rb +4 -0
- data/lib/chess_rb/position.rb +74 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8387c3c7b4b44a16284cf21d3598c9e6f1377fc9
|
4
|
+
data.tar.gz: 9e9d8036aeb5e539135a2ea3f2fa7f6781dd5734
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e22e5ccee3e43694dcaafe311c3c76a84389b4235af5c5ea76857829cc1fad4d47c536abdcde4b0ebf112db7a7a6f5b0d426334b0a1deab9ac581443377a6e7
|
7
|
+
data.tar.gz: b55e649dcf6243982b3493ce22ae99950b0770fe91fa628b98572c2d2d0c94866e13f7505beb491819730c450167b96c4f8393b2604a77ff68cde4bfe5022e25
|
data/lib/chess_rb/piece.rb
CHANGED
data/lib/chess_rb/position.rb
CHANGED
@@ -153,14 +153,14 @@ class ChessRB::Position
|
|
153
153
|
move([8 - move.from_rank, move.from_file - 1],
|
154
154
|
[8 - move.to_rank, move.to_file - 1])
|
155
155
|
|
156
|
-
|
156
|
+
update_fen(move, true)
|
157
157
|
end
|
158
158
|
|
159
159
|
def undo_move(move, piece)
|
160
160
|
undo([8 - move.from_rank, move.from_file - 1],
|
161
161
|
[8 - move.to_rank, move.to_file - 1], piece.code)
|
162
162
|
|
163
|
-
|
163
|
+
update_fen(move, false)
|
164
164
|
end
|
165
165
|
|
166
166
|
def to_s(dark_background = true)
|
@@ -204,6 +204,33 @@ class ChessRB::Position
|
|
204
204
|
return board
|
205
205
|
end
|
206
206
|
|
207
|
+
def board_to_fen
|
208
|
+
fen = ""
|
209
|
+
|
210
|
+
@board.each_with_index do |r, i|
|
211
|
+
fen += '/' if i != 0
|
212
|
+
count = 0
|
213
|
+
r.each_with_index do |c|
|
214
|
+
p = ChessRB::Piece.new(c)
|
215
|
+
if p.empty?
|
216
|
+
count += 1
|
217
|
+
else
|
218
|
+
fen += count.to_s if count != 0
|
219
|
+
count = 0
|
220
|
+
if p.color == 'W'
|
221
|
+
fen += p.type.upcase
|
222
|
+
else
|
223
|
+
fen += p.type.downcase
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
fen += count.to_s if count != 0
|
228
|
+
end
|
229
|
+
|
230
|
+
puts fen.to_s
|
231
|
+
return fen
|
232
|
+
end
|
233
|
+
|
207
234
|
def move(from, to)
|
208
235
|
@board[to[0]][to[1]] = @board[from[0]][from[1]]
|
209
236
|
@board[from[0]][from[1]] = 0
|
@@ -213,4 +240,49 @@ class ChessRB::Position
|
|
213
240
|
@board[from[0]][from[1]] = @board[to[0]][to[1]]
|
214
241
|
@board[to[0]][to[1]] = code
|
215
242
|
end
|
243
|
+
|
244
|
+
def update_fen(move, forward)
|
245
|
+
if forward
|
246
|
+
sym = nil
|
247
|
+
t = @fen_components[1]
|
248
|
+
if move.king_castle?
|
249
|
+
sym = t == 'w' ? 'K' : 'k'
|
250
|
+
if @fen_components[2].include? sym
|
251
|
+
@fen_components[2].sub(sym, '')
|
252
|
+
end
|
253
|
+
elsif move.queen_castle?
|
254
|
+
sym = t == 'w' ? 'Q' : 'q'
|
255
|
+
@fen_components[2]
|
256
|
+
end
|
257
|
+
if sym && @fen_components[2].include?(sym)
|
258
|
+
@fen_components[2].sub(sym, '')
|
259
|
+
@fen_components[2] = '-' if @fen_components[2].empty?
|
260
|
+
end
|
261
|
+
|
262
|
+
if (pawn_move = piece_on(move.to).type == 'P') &&
|
263
|
+
(move.from_rank - move.to_rank).abs == 2
|
264
|
+
|
265
|
+
@fen_components[3] = move.san[0]
|
266
|
+
@fen_components[3] += (t == 'w') ? '3' : '6'
|
267
|
+
else
|
268
|
+
@fen_components[3] = '-'
|
269
|
+
end
|
270
|
+
|
271
|
+
if pawn_move || move.capture?
|
272
|
+
@fen_components[4] = 0
|
273
|
+
else
|
274
|
+
@fen_components[4] = (@fen_components[4].to_i + 1).to_s
|
275
|
+
end
|
276
|
+
|
277
|
+
if t == 'b'
|
278
|
+
@fen_components[5] = (@fen_components[5].to_i + 1).to_s
|
279
|
+
end
|
280
|
+
else
|
281
|
+
# do things for undo_move
|
282
|
+
end
|
283
|
+
@fen_components[0] = board_to_fen()
|
284
|
+
@fen_components[1] = @fen_components[1] == 'w' ? 'b' : 'w'
|
285
|
+
|
286
|
+
@fen = @fen_components.join(' ')
|
287
|
+
end
|
216
288
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chess_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sabar Dasgupta
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Chess functions written in Ruby
|
14
14
|
email: sabar.dasgupta@gmail.com
|