chess_validator 0.2.5 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/move_logic.rb +29 -10
- data/lib/piece.rb +4 -1
- 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: b367a02178081794d8ac5adc34cada855ae9aa3615459fd2c6aa81698f0cc8e7
|
4
|
+
data.tar.gz: 3f49bc22122a0318a991fb6340b0b6f22b0d30d919f07396b0a02de00affc219
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6074c987ce55676e9f41e05c501438459757c6fe8ac24dec622f50d9b7ca7e80c637b495393839df2a9bee05efca5601e41088e2b035dfe824b8cd05ff26272
|
7
|
+
data.tar.gz: 3a06c0c82eb0d9317b0b439cb37d47975aaecfe484d65d28d3d0733f71b89b07b7dcae6895f599632921293ed0642ccae113a6517279cfad1f7dee5064d429bd
|
data/lib/move_logic.rb
CHANGED
@@ -9,7 +9,7 @@ module ChessValidator
|
|
9
9
|
pieces = []
|
10
10
|
board.values.each do |piece|
|
11
11
|
if piece.color == fen.active
|
12
|
-
|
12
|
+
load_move_data(board, piece, fen)
|
13
13
|
pieces << piece if piece.valid_moves.size > 0
|
14
14
|
end
|
15
15
|
end
|
@@ -17,15 +17,29 @@ module ChessValidator
|
|
17
17
|
pieces
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def load_move_data(board, piece, fen)
|
21
21
|
moves_for_piece(piece).each do |move|
|
22
22
|
if valid_move?(piece, board, move, fen)
|
23
23
|
piece.valid_moves << move
|
24
|
-
|
24
|
+
target = find_target(board, piece, move)
|
25
|
+
piece.targets << target if target
|
26
|
+
else
|
27
|
+
piece.move_potential << move
|
25
28
|
end
|
26
29
|
end
|
27
30
|
end
|
28
31
|
|
32
|
+
def find_target(board, piece, move)
|
33
|
+
if piece.piece_type.downcase == 'p' && piece.position[0] == move[0]
|
34
|
+
nil
|
35
|
+
elsif board[INDEX_KEY[move]]
|
36
|
+
board[INDEX_KEY[move]]
|
37
|
+
elsif piece.piece_type.downcase == 'p'
|
38
|
+
en_passant_position = piece.color == 'w' ? move[0] + '5' : move[0] + '4'
|
39
|
+
board[INDEX_KEY[en_passant_position]]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
29
43
|
def valid_move?(piece, board, move, fen)
|
30
44
|
occupied_spaces = []
|
31
45
|
board.values.each { |p| occupied_spaces << p.position }
|
@@ -177,17 +191,22 @@ module ChessValidator
|
|
177
191
|
end
|
178
192
|
|
179
193
|
def advance_pawn?(pawn, board, move)
|
180
|
-
if (pawn
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
occupied_spaces << piece.position
|
194
|
+
if empty_square?(board, forward_by(pawn, 1))
|
195
|
+
if (pawn.position[1].to_i - move[1].to_i).abs == 2
|
196
|
+
empty_square?(board, forward_by(pawn, 2))
|
197
|
+
else
|
198
|
+
true
|
186
199
|
end
|
187
|
-
|
200
|
+
else
|
201
|
+
false
|
188
202
|
end
|
189
203
|
end
|
190
204
|
|
205
|
+
def forward_by(piece, count)
|
206
|
+
position = piece.position
|
207
|
+
piece.color == 'w' ? position[0] + (position[1].to_i + count).to_s : position[0] + (position[1].to_i - count).to_s
|
208
|
+
end
|
209
|
+
|
191
210
|
def valid_destination?(piece, board, move)
|
192
211
|
target_piece = find_piece(board, move)
|
193
212
|
if target_piece
|
data/lib/piece.rb
CHANGED
@@ -2,7 +2,8 @@ require_relative './constants/move_key'
|
|
2
2
|
|
3
3
|
module ChessValidator
|
4
4
|
class Piece
|
5
|
-
attr_accessor :position, :piece_type, :color, :square_index, :valid_moves,
|
5
|
+
attr_accessor :position, :piece_type, :color, :square_index, :valid_moves,
|
6
|
+
:targets, :move_potential, :defenders
|
6
7
|
|
7
8
|
def initialize(char, square_index)
|
8
9
|
@piece_type = char
|
@@ -11,6 +12,8 @@ module ChessValidator
|
|
11
12
|
@position = get_position(square_index)
|
12
13
|
@valid_moves = []
|
13
14
|
@targets = []
|
15
|
+
@move_potential = []
|
16
|
+
@defenders = []
|
14
17
|
end
|
15
18
|
|
16
19
|
def get_position(square_index)
|
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.10
|
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-
|
11
|
+
date: 2020-11-16 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
|