chess_validator 0.2.10 → 0.2.15

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/board_logic.rb +9 -11
  3. data/lib/move_logic.rb +7 -6
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b367a02178081794d8ac5adc34cada855ae9aa3615459fd2c6aa81698f0cc8e7
4
- data.tar.gz: 3f49bc22122a0318a991fb6340b0b6f22b0d30d919f07396b0a02de00affc219
3
+ metadata.gz: 755cc8b1ad78feff8ad130c8a3e5c11803263d8ccf41db3ec1c67fa7a08427aa
4
+ data.tar.gz: b926a815e705bda86df14da9269533229c76c36d2bc0301a5c18200d5cf1e9f4
5
5
  SHA512:
6
- metadata.gz: b6074c987ce55676e9f41e05c501438459757c6fe8ac24dec622f50d9b7ca7e80c637b495393839df2a9bee05efca5601e41088e2b035dfe824b8cd05ff26272
7
- data.tar.gz: 3a06c0c82eb0d9317b0b439cb37d47975aaecfe484d65d28d3d0733f71b89b07b7dcae6895f599632921293ed0642ccae113a6517279cfad1f7dee5064d429bd
6
+ metadata.gz: 9bc14f5b3a08834a3a80da545fcd9612a5624ff6f869918dc6d1a4798320708034a0120d2c6b9509f9746f532dbfdfeccf4f0c8ae07d28e05bf3084345aff996
7
+ data.tar.gz: ab77aa9915b79663a765596d3f31bb792363feb393e49b618a1c35c5ab46cdac5591cd60a8c781f2bb4449f9e82d2de799eafafe3bcb7456cfcf5fb5b76b5b44
@@ -20,7 +20,7 @@ module ChessValidator
20
20
  def self.to_fen_notation(board, previous_fen, piece, move)
21
21
  notation = handle_position(board)
22
22
  notation += find_turn(previous_fen.active)
23
- notation += handle_castle(previous_fen.castling, piece)
23
+ notation += handle_castle(previous_fen.castling, piece, board)
24
24
  notation += handle_en_passant(piece, move)
25
25
  notation += handle_half_move_clock(board.size, previous_fen, piece)
26
26
  notation += piece.color == 'b' ? previous_fen.fullmove.next : previous_fen.fullmove
@@ -35,17 +35,15 @@ module ChessValidator
35
35
  end
36
36
  end
37
37
 
38
- def self.handle_castle(castling, piece)
38
+ def self.handle_castle(castling, piece, board)
39
39
  return castling if castling == '-'
40
- if ['K', 'Q', 'k', 'q'].include?(piece.piece_type)
41
- castling.size == 1 ? '-' : castling.delete(piece.piece_type)
42
- elsif piece.piece_type.downcase == 'r'
43
- castle_side = piece.position[0] == 'a' ? 'q' : 'k'
44
- castle_side = castle_side.capitalize if piece.color == 'w'
45
- castling.size == 1 ? '-' : castling.delete(castle_side)
46
- else
47
- castling
48
- end
40
+ castling.delete!('K') if board[64].nil? || board[64].piece_type != 'R'
41
+ castling.delete!('K') if board[61].nil? || board[61].piece_type != 'K'
42
+ castling.delete!('Q') if board[57].nil? || board[57].piece_type != 'R'
43
+ castling.delete!('k') if board[8].nil? || board[8].piece_type != 'r'
44
+ castling.delete!('k') if board[5].nil? || board[5].piece_type != 'k'
45
+ castling.delete!('q') if board[1].nil? || board[1].piece_type != 'r'
46
+ castling.size == 1 ? '-' : castling
49
47
  end
50
48
 
51
49
  def self.handle_en_passant(piece, move)
@@ -21,7 +21,7 @@ module ChessValidator
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
26
  else
27
27
  piece.move_potential << move
@@ -29,12 +29,12 @@ module ChessValidator
29
29
  end
30
30
  end
31
31
 
32
- def find_target(board, piece, move)
32
+ def find_target(board, piece, move, en_passant)
33
33
  if piece.piece_type.downcase == 'p' && piece.position[0] == move[0]
34
34
  nil
35
35
  elsif board[INDEX_KEY[move]]
36
36
  board[INDEX_KEY[move]]
37
- elsif piece.piece_type.downcase == 'p'
37
+ elsif piece.piece_type.downcase == 'p' && move == en_passant
38
38
  en_passant_position = piece.color == 'w' ? move[0] + '5' : move[0] + '4'
39
39
  board[INDEX_KEY[en_passant_position]]
40
40
  end
@@ -130,6 +130,7 @@ module ChessValidator
130
130
  def king_will_be_safe?(piece, board, move)
131
131
  new_board = with_next_move(piece, board, move)
132
132
  king, occupied_spaces = find_king_and_spaces(new_board, piece.color)
133
+ return false if king.nil?
133
134
  king_is_safe?(king.color, new_board, king.position, occupied_spaces)
134
135
  end
135
136
 
@@ -161,7 +162,7 @@ module ChessValidator
161
162
  board.values.none? { |piece| [between, move].include?(piece.position) } &&
162
163
  empty_b_square
163
164
  else
164
- valid_destination?(king, board, move) && king_is_safe?(king.color, board, move, occupied_spaces)
165
+ valid_destination?(king, board, move) && king_will_be_safe?(king, board, move)
165
166
  end
166
167
  end
167
168
 
@@ -210,7 +211,7 @@ module ChessValidator
210
211
  def valid_destination?(piece, board, move)
211
212
  target_piece = find_piece(board, move)
212
213
  if target_piece
213
- target_piece.color != piece.color
214
+ target_piece.color != piece.color && target_piece.piece_type.downcase != 'k'
214
215
  else
215
216
  true
216
217
  end
@@ -221,7 +222,7 @@ module ChessValidator
221
222
  end
222
223
 
223
224
  def empty_square?(board, move)
224
- board.values.detect { |piece| piece.position == move }.nil?
225
+ board.values.none? { |piece| piece.position == move }
225
226
  end
226
227
 
227
228
  def valid_move_path?(piece, move, occupied_spaces)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chess_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Ellison