chess_validator 0.2.20 → 0.2.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/board_logic.rb +5 -1
- data/lib/engine.rb +4 -0
- data/lib/move_logic.rb +91 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94df1700b50a798d45895b3d8b235fc6cd8abbe15736f36bbc97e1fe31da88e1
|
4
|
+
data.tar.gz: 6e0eab463eee127e30e55effeef21466cf71e9ac08be5c8f2f6b258112e723cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '098971e3e410d7f62ec4bf11fb62ae9a3397322add2a7a40a508c24149e19680460d7e5a659add721e17acb84473308c79fc55d5be3d7fba4b3f9c8bf159ce13'
|
7
|
+
data.tar.gz: 7e0b9b6b18cbddbbc7fc25d2860a2830ea21c52d06254a2b3be28513ab391c8e7f10b0591b1a3c3471763d2c95e6d53940049e74e34d44a9c6870d445cb20ef1
|
data/lib/board_logic.rb
CHANGED
@@ -3,9 +3,13 @@ require 'piece'
|
|
3
3
|
module ChessValidator
|
4
4
|
class BoardLogic
|
5
5
|
def self.build_board(fen)
|
6
|
+
build_board_from_string(fen.board_string)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.build_board_from_string(board_string)
|
6
10
|
board = {}
|
7
11
|
square_index = 1
|
8
|
-
|
12
|
+
board_string.chars.each do |char|
|
9
13
|
if empty_square?(char)
|
10
14
|
square_index += char.to_i
|
11
15
|
elsif char != '/'
|
data/lib/engine.rb
CHANGED
data/lib/move_logic.rb
CHANGED
@@ -5,18 +5,56 @@ module ChessValidator
|
|
5
5
|
class MoveLogic
|
6
6
|
class << self
|
7
7
|
def next_moves(fen)
|
8
|
-
|
8
|
+
fen_notation = fen.to_s
|
9
|
+
return next_moves_for[fen_notation] if next_moves_for[fen_notation]
|
10
|
+
|
11
|
+
|
12
|
+
board = get_board(fen.to_s)
|
9
13
|
pieces = []
|
14
|
+
|
10
15
|
board.values.each do |piece|
|
11
16
|
if piece.color == fen.active
|
12
17
|
load_move_data(board, piece, fen)
|
13
18
|
pieces << piece if piece.valid_moves.size > 0
|
14
19
|
end
|
15
20
|
end
|
21
|
+
next_moves_for[fen_notation] = pieces
|
16
22
|
|
17
23
|
pieces
|
18
24
|
end
|
19
25
|
|
26
|
+
def boards
|
27
|
+
@boards ||= {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_board(fen_string)
|
31
|
+
return boards[fen_string] if boards[fen_string]
|
32
|
+
|
33
|
+
board = BoardLogic.build_board_from_string(fen_string.split.first)
|
34
|
+
boards[fen_string] = board
|
35
|
+
board
|
36
|
+
end
|
37
|
+
|
38
|
+
def next_moves_for
|
39
|
+
@next_moves_for ||= {}
|
40
|
+
end
|
41
|
+
|
42
|
+
def occupied_square_sets
|
43
|
+
@occupied_square_sets ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_occupied_spaces(fen_string)
|
47
|
+
return occupied_square_sets[fen_string] if occupied_square_sets[fen_string]
|
48
|
+
board = get_board(fen_string)
|
49
|
+
|
50
|
+
occupied_spaces = []
|
51
|
+
board.values.each { |p| occupied_spaces << p.position }
|
52
|
+
|
53
|
+
occupied_square_sets[fen_string] = occupied_spaces
|
54
|
+
occupied_spaces
|
55
|
+
end
|
56
|
+
|
57
|
+
|
20
58
|
def load_move_data(board, piece, fen)
|
21
59
|
moves_for_piece(piece).each do |move|
|
22
60
|
if valid_move?(piece, board, move, fen)
|
@@ -41,14 +79,13 @@ module ChessValidator
|
|
41
79
|
end
|
42
80
|
|
43
81
|
def valid_move?(piece, board, move, fen)
|
44
|
-
occupied_spaces = []
|
45
|
-
board.values.each { |p| occupied_spaces << p.position }
|
46
82
|
case piece.piece_type.downcase
|
47
83
|
when 'k'
|
48
|
-
handle_king(piece, board, move, fen,
|
84
|
+
handle_king(piece, board, move, fen) && valid_destination?(piece, board, move)
|
49
85
|
when 'p'
|
50
86
|
handle_pawn(piece, board, move, fen)
|
51
87
|
else
|
88
|
+
occupied_spaces = get_occupied_spaces(fen.to_s)
|
52
89
|
valid_move_path?(piece, move, occupied_spaces) &&
|
53
90
|
valid_destination?(piece, board, move) &&
|
54
91
|
king_will_be_safe?(piece, board, move)
|
@@ -112,10 +149,9 @@ module ChessValidator
|
|
112
149
|
end
|
113
150
|
|
114
151
|
def make_move(piece, move, fen_notation)
|
115
|
-
|
116
|
-
board = BoardLogic.build_board(fen)
|
152
|
+
board = get_board(fen_notation)
|
117
153
|
new_board = with_next_move(piece, board, move)
|
118
|
-
|
154
|
+
fen = PGN::FEN.new(fen_notation)
|
119
155
|
BoardLogic.to_fen_notation(new_board, fen, piece, move, board[INDEX_KEY[move]])
|
120
156
|
end
|
121
157
|
|
@@ -135,16 +171,16 @@ module ChessValidator
|
|
135
171
|
end
|
136
172
|
|
137
173
|
def find_king_and_spaces(board, color)
|
138
|
-
occupied_spaces = []
|
139
174
|
king = nil
|
140
|
-
board.values.
|
175
|
+
occupied_spaces = board.values.map do |p|
|
141
176
|
king = p if p.piece_type.downcase == 'k' && p.color == color
|
142
|
-
|
177
|
+
p.position
|
143
178
|
end
|
179
|
+
|
144
180
|
[king, occupied_spaces]
|
145
181
|
end
|
146
182
|
|
147
|
-
def handle_king(king, board, move, fen
|
183
|
+
def handle_king(king, board, move, fen)
|
148
184
|
if (king.position[0].ord - move[0].ord).abs == 2
|
149
185
|
empty_b_square = true
|
150
186
|
if move[0] == 'c'
|
@@ -155,6 +191,8 @@ module ChessValidator
|
|
155
191
|
castle_code = 'k'
|
156
192
|
between = 'f' + move[1]
|
157
193
|
end
|
194
|
+
occupied_spaces = get_occupied_spaces(fen.to_s)
|
195
|
+
|
158
196
|
(fen.castling.include?(castle_code) && king.color == 'b' || fen.castling.include?(castle_code.upcase) && king.color == 'w') &&
|
159
197
|
king_is_safe?(king.color, board, king.position, occupied_spaces) &&
|
160
198
|
king_is_safe?(king.color, board, between, occupied_spaces) &&
|
@@ -162,7 +200,7 @@ module ChessValidator
|
|
162
200
|
board.values.none? { |piece| [between, move].include?(piece.position) } &&
|
163
201
|
empty_b_square
|
164
202
|
else
|
165
|
-
|
203
|
+
king_will_be_safe?(king, board, move)
|
166
204
|
end
|
167
205
|
end
|
168
206
|
|
@@ -271,6 +309,47 @@ module ChessValidator
|
|
271
309
|
!(move_path & occupied_spaces).empty?
|
272
310
|
end
|
273
311
|
|
312
|
+
def can_defend?(piece, board, position, fen)
|
313
|
+
case piece.piece_type.downcase
|
314
|
+
when 'k'
|
315
|
+
handle_king(piece, board, position, fen)
|
316
|
+
when 'p'
|
317
|
+
if piece.position[0] != position[0]
|
318
|
+
target_piece = find_piece(board, position)
|
319
|
+
valid = (target_piece && target_piece.color != piece.color) || position == fen.en_passant
|
320
|
+
valid && king_will_be_safe?(piece, board, position)
|
321
|
+
end
|
322
|
+
else
|
323
|
+
occupied_spaces = get_occupied_spaces(fen.to_s)
|
324
|
+
valid_move_path?(piece, position, occupied_spaces) && king_will_be_safe?(piece, board, position)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
def defended_pieces_by_square(fen_notation)
|
329
|
+
board = get_board(fen_notation)
|
330
|
+
|
331
|
+
fen = PGN::FEN.new(fen_notation)
|
332
|
+
space_index = {}
|
333
|
+
|
334
|
+
pieces = board.values.map do |piece|
|
335
|
+
space_index[piece.position] = piece.piece_type + piece.color
|
336
|
+
piece
|
337
|
+
end
|
338
|
+
|
339
|
+
defended = Hash.new([])
|
340
|
+
|
341
|
+
pieces.each do |piece|
|
342
|
+
moves_for_piece(piece).each do |move|
|
343
|
+
if space_index[move] && space_index[move][-1] == piece.color && can_defend?(piece, board, move, fen)
|
344
|
+
defended[move] += [piece]
|
345
|
+
puts 'hi'
|
346
|
+
end
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
defended
|
351
|
+
end
|
352
|
+
|
274
353
|
def moves_for_piece(piece)
|
275
354
|
case piece.piece_type.downcase
|
276
355
|
when 'r'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chess_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.21
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charles Ellison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Documentation: https://github.com/chadellison/chess_validator'
|
14
14
|
email: chad.ellison0123@gmail.com
|