chess_validator 0.1.6 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d391ec459430072a8e9df514b4b68f1363fc16705971d5dc262ea601ebc64d9e
4
- data.tar.gz: d5f8beef914ae16c35185441f80ec0f0f3459875480a8a2d1518e08f7f72fe8f
3
+ metadata.gz: 1af34872a259842b2169becd1ee7570ceac96f7fc626e4c4306e7544fca7e25a
4
+ data.tar.gz: 821da801fa31123bd8ec1a6d151bb14364da5ad60d833b11b129ef205ec51411
5
5
  SHA512:
6
- metadata.gz: 0572624b876491ca99ddd3a8999993c1a023c1496f4435375c7e1f087342839175e6455dce3a8128916fcd323125640ec11c8c46a72f27449353aaeddd91e066
7
- data.tar.gz: 57b46adca5631c768e9386c0467b8b6318e4888ec3e44567b7b0240f13f84883419ecdbd8ac2840b10e477fa24776f33f7625b524c801d382a7fc59940cf8438
6
+ metadata.gz: dae4d729c77310493648f7c3ec78ee954b3f04055b94a9a7410a74a3028d1b86918a6161e148bdaa27eaf75249a3a80e9ee9e2c54ed69cfcae8250740a62ba54
7
+ data.tar.gz: d0dcfb4661d6c5f8b231207ad0cd96c7634ed84898c3d76331c10f64aad54f345d4ef10538e3f27fb428eadf686958f7ccd69ea48fbb18bd1ceb0d40a4bf2b05
@@ -1,3 +1,5 @@
1
+ require 'piece'
2
+
1
3
  module ChessValidator
2
4
  class BoardLogic
3
5
  def self.build_board(fen)
@@ -1,3 +1,2 @@
1
1
  require 'move_logic'
2
2
  require 'game_logic'
3
- require 'pry'
@@ -1,10 +1,14 @@
1
+ require 'board_logic'
2
+ require 'move_logic'
3
+ require 'pgn'
4
+
1
5
  module ChessValidator
2
6
  class GameLogic
3
7
  class << self
4
8
  def find_game_result(fen_notation)
5
9
  fen = PGN::FEN.new(fen_notation)
6
10
  board = BoardLogic.build_board(fen)
7
- no_moves = MoveLogic.find_next_moves(fen).empty?
11
+ no_moves = MoveLogic.next_moves(fen).empty?
8
12
  checkmate_result = checkmate_value(fen, board, no_moves)
9
13
 
10
14
  if checkmate_result
@@ -1,3 +1,6 @@
1
+ require 'board_logic'
2
+ require 'pgn'
3
+
1
4
  module ChessValidator
2
5
  class MoveLogic
3
6
  class << self
@@ -111,8 +114,6 @@ module ChessValidator
111
114
  def king_will_be_safe?(piece, board, move)
112
115
  new_board = with_next_move(piece, board, move)
113
116
  king, occupied_spaces = find_king_and_spaces(new_board, piece.color)
114
-
115
- return false if king.nil?
116
117
  king_is_safe?(king.color, new_board, king.position, occupied_spaces)
117
118
  end
118
119
 
@@ -151,9 +152,13 @@ module ChessValidator
151
152
  def king_is_safe?(king_color, board, king_position, occupied_spaces)
152
153
  board.values.none? do |piece|
153
154
  piece.color != king_color &&
154
- moves_for_piece(piece).select do |move|
155
- king_position == move && valid_move_path?(piece, king_position, occupied_spaces)
156
- end.size > 0
155
+ moves_for_piece(piece).any? do |move|
156
+ if piece.piece_type.downcase == 'p'
157
+ king_position == move && piece.position[0] != king_position[0]
158
+ else
159
+ king_position == move && valid_move_path?(piece, king_position, occupied_spaces)
160
+ end
161
+ end
157
162
  end
158
163
  end
159
164
 
@@ -161,11 +166,12 @@ module ChessValidator
161
166
  position = piece.position
162
167
 
163
168
  if position[0] == move[0]
164
- advance_pawn?(piece, board, move)
169
+ valid = advance_pawn?(piece, board, move)
165
170
  else
166
171
  target_piece = find_piece(board, move)
167
- (target_piece && target_piece.color != piece.color) || move == fen.en_passant
172
+ valid = (target_piece && target_piece.color != piece.color) || move == fen.en_passant
168
173
  end
174
+ valid && king_will_be_safe?(piece, board, move)
169
175
  end
170
176
 
171
177
  def advance_pawn?(pawn, board, move)
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.1.6
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Ellison