chess_validator 0.2.6 → 0.2.11
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 +23 -14
- 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: 7246b6d061d29511f381b3b152c8b9558a041976aed4b65a2f70b00790b39bfc
|
4
|
+
data.tar.gz: 8f5f544a578d432a5303185fc95777a4a6c218fd1159021af2a08378e6b19b0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5886da44a02c98e30b802d79072c3d26f5ac81146d58b2a6f0d2cb4f39c032bbd958f7c13288f4b0749efe42574e758f7293c2a4dda7772c809206ce0769315d
|
7
|
+
data.tar.gz: 52b7d23f1c6ca1d96e68095032f5bde8c431d5da688b25626329969ff2dbd155cb6cd5264e4844c2253610aa6698172d9ef4b1fb0aa3106c94d663249843ee43
|
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,20 +17,24 @@ 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
|
-
target = find_target(board, piece, move)
|
24
|
+
target = find_target(board, piece, move, fen.en_passant)
|
25
25
|
piece.targets << target if target
|
26
|
+
else
|
27
|
+
piece.move_potential << move
|
26
28
|
end
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
|
-
def find_target(board, piece, move)
|
31
|
-
if
|
32
|
+
def find_target(board, piece, move, en_passant)
|
33
|
+
if piece.piece_type.downcase == 'p' && piece.position[0] == move[0]
|
34
|
+
nil
|
35
|
+
elsif board[INDEX_KEY[move]]
|
32
36
|
board[INDEX_KEY[move]]
|
33
|
-
elsif piece.piece_type.downcase == 'p' &&
|
37
|
+
elsif piece.piece_type.downcase == 'p' && move == en_passant
|
34
38
|
en_passant_position = piece.color == 'w' ? move[0] + '5' : move[0] + '4'
|
35
39
|
board[INDEX_KEY[en_passant_position]]
|
36
40
|
end
|
@@ -187,17 +191,22 @@ module ChessValidator
|
|
187
191
|
end
|
188
192
|
|
189
193
|
def advance_pawn?(pawn, board, move)
|
190
|
-
if (pawn
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
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
|
196
199
|
end
|
197
|
-
|
200
|
+
else
|
201
|
+
false
|
198
202
|
end
|
199
203
|
end
|
200
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
|
+
|
201
210
|
def valid_destination?(piece, board, move)
|
202
211
|
target_piece = find_piece(board, move)
|
203
212
|
if target_piece
|
@@ -212,7 +221,7 @@ module ChessValidator
|
|
212
221
|
end
|
213
222
|
|
214
223
|
def empty_square?(board, move)
|
215
|
-
board.values.
|
224
|
+
board.values.none? { |piece| piece.position == move }
|
216
225
|
end
|
217
226
|
|
218
227
|
def valid_move_path?(piece, move, occupied_spaces)
|
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.11
|
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
|