chess_validator 0.1.7 → 0.2.2

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: 6d13b525f8a4b8ad4edca48708ea50c53b3e754fe3c3f0115fcc13c0a6e12c6f
4
- data.tar.gz: 773e8ee7bf7c8c5481b1d8457e2a273b70f3cf9d6ae9ab1f55fe67fe6c16b1dd
3
+ metadata.gz: 0e4f35f7365cba31d8882b494232dd0357745db3b3390d9da373225e9e378ee1
4
+ data.tar.gz: bab4ecf238d41abc2290e2f1a7bac33d727d14605140e57414cef484225d581f
5
5
  SHA512:
6
- metadata.gz: d64ee9edd77f419cf3233c075816f4403cec25a25f29c19da18a4265751f1d2e2807de72f40e6a5b430feb7b75c7c6b6568d73e5a0fd264c4c5b75a936d3356b
7
- data.tar.gz: 682bc4d527bed82cff43f3f46f9c3b0c5a6be767604411633bf0eaccbc32b1d364e293df96b6a2358d92451ae5dfc44b72a60b2070f1066b8a6abb2540db5b69
6
+ metadata.gz: cde6774a3a7e10f367805944038f8ddafb61c1aa4054067d20de055171b062ed270081deee99a50e14f771a8fd9a4f71bee23eaa19faa0256ba374151e6605f2
7
+ data.tar.gz: fdec3d74fb38c1bd2a4ecbce08c2d1f7c11e705856f58b858868f0628dfab8d1917e4bb972afb4fb07d0c85ac755cae0f7d04685a427a3dcc8322ad5f15e37b2
@@ -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 @@
1
- require 'move_logic'
2
- require 'game_logic'
3
- require 'pry'
1
+ require 'engine'
@@ -0,0 +1,29 @@
1
+ require 'move_logic'
2
+ require 'game_logic'
3
+
4
+ module ChessValidator
5
+ class Engine
6
+ class << self
7
+ def find_next_moves(fen_notation)
8
+ fen = PGN::FEN.new(fen_notation)
9
+ MoveLogic.next_moves(fen)
10
+ end
11
+
12
+ def find_next_moves_from_moves(moves)
13
+ fen = PGN::Game.new(moves).positions.last.to_fen
14
+ MoveLogic.next_moves(fen)
15
+ end
16
+
17
+ def make_random_move(fen_notation, pieces_with_moves)
18
+ piece_to_move = pieces_with_moves.sample
19
+ move = piece_to_move.valid_moves.sample
20
+ MoveLogic.make_move(piece_to_move, move, fen_notation)
21
+ end
22
+
23
+ def get_pieces(fen_notation)
24
+ fen = PGN::Game.new(moves).positions.last.to_fen
25
+ BoardLogic.build_board(fen).values
26
+ end
27
+ end
28
+ end
29
+ end
@@ -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,16 +1,9 @@
1
+ require 'board_logic'
2
+ require 'pgn'
3
+
1
4
  module ChessValidator
2
5
  class MoveLogic
3
6
  class << self
4
- def find_next_moves(fen_notation)
5
- fen = PGN::FEN.new(fen_notation)
6
- next_moves(fen)
7
- end
8
-
9
- def find_next_moves_from_moves(moves)
10
- fen = PGN::Game.new(moves).positions.last.to_fen
11
- next_moves(fen)
12
- end
13
-
14
7
  def next_moves(fen)
15
8
  board = BoardLogic.build_board(fen)
16
9
  pieces = []
@@ -48,7 +41,8 @@ module ChessValidator
48
41
  def with_next_move(piece, board, move)
49
42
  index = INDEX_KEY[move]
50
43
  new_board = board.clone
51
- new_piece = Piece.new(piece.piece_type, index)
44
+ piece_type = resolve_piece_type(piece.piece_type, move)
45
+ new_piece = Piece.new(piece_type, index)
52
46
  new_board.delete(piece.square_index)
53
47
  new_board[index] = new_piece
54
48
  new_board = handle_castle(new_board, move) if castled?(piece, move)
@@ -56,6 +50,14 @@ module ChessValidator
56
50
  new_board
57
51
  end
58
52
 
53
+ def resolve_piece_type(piece_type, move)
54
+ if piece_type.downcase == 'p' && ['1', '8'].include?(move[1])
55
+ move[1] == '8' ? 'Q' : 'q'
56
+ else
57
+ piece_type
58
+ end
59
+ end
60
+
59
61
  def handle_en_passant(board, pawn_color, move)
60
62
  if pawn_color == 'w'
61
63
  index = INDEX_KEY[move[0] + '5']
@@ -111,8 +113,6 @@ module ChessValidator
111
113
  def king_will_be_safe?(piece, board, move)
112
114
  new_board = with_next_move(piece, board, move)
113
115
  king, occupied_spaces = find_king_and_spaces(new_board, piece.color)
114
-
115
- return false if king.nil?
116
116
  king_is_safe?(king.color, new_board, king.position, occupied_spaces)
117
117
  end
118
118
 
@@ -151,9 +151,13 @@ module ChessValidator
151
151
  def king_is_safe?(king_color, board, king_position, occupied_spaces)
152
152
  board.values.none? do |piece|
153
153
  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
154
+ moves_for_piece(piece).any? do |move|
155
+ if piece.piece_type.downcase == 'p'
156
+ king_position == move && piece.position[0] != king_position[0]
157
+ else
158
+ king_position == move && valid_move_path?(piece, king_position, occupied_spaces)
159
+ end
160
+ end
157
161
  end
158
162
  end
159
163
 
@@ -161,11 +165,12 @@ module ChessValidator
161
165
  position = piece.position
162
166
 
163
167
  if position[0] == move[0]
164
- advance_pawn?(piece, board, move)
168
+ valid = advance_pawn?(piece, board, move)
165
169
  else
166
170
  target_piece = find_piece(board, move)
167
- (target_piece && target_piece.color != piece.color) || move == fen.en_passant
171
+ valid = (target_piece && target_piece.color != piece.color) || move == fen.en_passant
168
172
  end
173
+ valid && king_will_be_safe?(piece, board, move)
169
174
  end
170
175
 
171
176
  def advance_pawn?(pawn, board, move)
@@ -1,4 +1,4 @@
1
- require_relative './constants/square_key'
1
+ require_relative './constants/move_key'
2
2
 
3
3
  module ChessValidator
4
4
  class Piece
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.7
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Ellison
@@ -18,7 +18,8 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/board_logic.rb
20
20
  - lib/chess_validator.rb
21
- - lib/constants/square_key.rb
21
+ - lib/constants/move_key.rb
22
+ - lib/engine.rb
22
23
  - lib/game_logic.rb
23
24
  - lib/move_logic.rb
24
25
  - lib/piece.rb