chess_validator 0.1.5 → 0.2.0

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: b894e53ae4302055b3a8078c4bbfba1265960621d25932d9d02c66b181874068
4
- data.tar.gz: 8e8b040cab68bf0bd3742a89c63ce03738968cb754d6f1877e95edb5bdfe97ba
3
+ metadata.gz: 82d293dc8df9520f07a2f6d92b6d9e896f96b244e77088c400ec2896afe099ef
4
+ data.tar.gz: 52af33ff9401c878693c751483bedcc05f0e5713e39af53ac5c82d99c7ac09bd
5
5
  SHA512:
6
- metadata.gz: ed34ebd9d472f73f9e06119592a8766fea05e0570e5c48dce52d4d9440a8bb26dff7afb90f885ebcdee2cfe197c41d8857f186085f0c300bb16e251859710550
7
- data.tar.gz: 366776136d638c090c71cad003617e4a10c3c74463ca6bcd1d6c281ddcca033e656a1f17f9acde324173d1ee242ca6c8eafec29c90d16d2345540b49281faaab
6
+ metadata.gz: fb8d4a4129e5bb7d639dd2851bb4d1c9ce4c44879f2541eb1baf5f3c0ec272cc9c368d6bc7af1a9718eb303263c3405e189bcfeae4bc38b6761877f5f85af7e1
7
+ data.tar.gz: 0abbe121cba65bebfae2f3ed8caa4115e4daee8c35c82af697e215db9fc144ff182490690ed843a8bc1943a232d211cba4b01a9e0448ddca67e83ee28d1acf2a
@@ -5,7 +5,7 @@ module ChessValidator
5
5
  def self.build_board(fen)
6
6
  board = {}
7
7
  square_index = 1
8
- fen.to_s.split(' ').first.chars.each do |char|
8
+ fen.board_string.chars.each do |char|
9
9
  if empty_square?(char)
10
10
  square_index += char.to_i
11
11
  elsif char != '/'
@@ -1,2 +1,2 @@
1
1
  require 'move_logic'
2
- require 'pry'
2
+ require 'game_logic'
@@ -0,0 +1,41 @@
1
+ require 'board_logic'
2
+ require 'move_logic'
3
+ require 'pgn'
4
+
5
+ module ChessValidator
6
+ class GameLogic
7
+ class << self
8
+ def find_game_result(fen_notation)
9
+ fen = PGN::FEN.new(fen_notation)
10
+ board = BoardLogic.build_board(fen)
11
+ no_moves = MoveLogic.next_moves(fen).empty?
12
+ checkmate_result = checkmate_value(fen, board, no_moves)
13
+
14
+ if checkmate_result
15
+ checkmate_result
16
+ elsif draw?(fen, board, no_moves)
17
+ '1/2-1/2'
18
+ end
19
+ end
20
+
21
+ def checkmate_value(fen, board, no_moves)
22
+ king, occupied_spaces = MoveLogic.find_king_and_spaces(board, fen.active)
23
+ in_check = !MoveLogic.king_is_safe?(fen.active, board, king.position, occupied_spaces)
24
+
25
+ if no_moves && in_check
26
+ fen.active == 'w' ? '0-1' : '1-0'
27
+ end
28
+ end
29
+
30
+ def draw?(fen, board, no_moves)
31
+ [fen.halfmove == '50', no_moves, insufficient_material?(board)].any?
32
+ end
33
+
34
+ def insufficient_material?(board)
35
+ if board.size < 4
36
+ board.values.none? { |piece| ['q', 'r', 'p'].include?(piece.piece_type.downcase) }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -4,8 +4,8 @@ require 'pgn'
4
4
  module ChessValidator
5
5
  class MoveLogic
6
6
  class << self
7
- def find_next_moves(fen_notatioin)
8
- fen = PGN::FEN.new(fen_notatioin)
7
+ def find_next_moves(fen_notation)
8
+ fen = PGN::FEN.new(fen_notation)
9
9
  next_moves(fen)
10
10
  end
11
11
 
@@ -89,14 +89,14 @@ module ChessValidator
89
89
  board
90
90
  end
91
91
 
92
- def make_random_move(fen_notatioin, pieces_with_moves)
92
+ def make_random_move(fen_notation, pieces_with_moves)
93
93
  piece_to_move = pieces_with_moves.sample
94
94
  move = piece_to_move.valid_moves.sample
95
- make_move(piece_to_move, move, fen_notatioin)
95
+ make_move(piece_to_move, move, fen_notation)
96
96
  end
97
97
 
98
- def make_move(piece, move, fen_notatioin)
99
- fen = PGN::FEN.new(fen_notatioin)
98
+ def make_move(piece, move, fen_notation)
99
+ fen = PGN::FEN.new(fen_notation)
100
100
  board = BoardLogic.build_board(fen)
101
101
  new_board = with_next_move(piece, board, move)
102
102
 
@@ -113,15 +113,18 @@ module ChessValidator
113
113
 
114
114
  def king_will_be_safe?(piece, board, move)
115
115
  new_board = with_next_move(piece, board, move)
116
+ king, occupied_spaces = find_king_and_spaces(new_board, piece.color)
117
+ king_is_safe?(king.color, new_board, king.position, occupied_spaces)
118
+ end
119
+
120
+ def find_king_and_spaces(board, color)
116
121
  occupied_spaces = []
117
122
  king = nil
118
- new_board.values.each do |p|
119
- king = p if p.piece_type.downcase == 'k' && p.color == piece.color
123
+ board.values.each do |p|
124
+ king = p if p.piece_type.downcase == 'k' && p.color == color
120
125
  occupied_spaces << p.position
121
126
  end
122
-
123
- return false if king.nil?
124
- king_is_safe?(king.color, new_board, king.position, occupied_spaces)
127
+ [king, occupied_spaces]
125
128
  end
126
129
 
127
130
  def handle_king(king, board, move, fen, occupied_spaces)
@@ -159,11 +162,12 @@ module ChessValidator
159
162
  position = piece.position
160
163
 
161
164
  if position[0] == move[0]
162
- advance_pawn?(piece, board, move)
165
+ valid = advance_pawn?(piece, board, move)
163
166
  else
164
167
  target_piece = find_piece(board, move)
165
- (target_piece && target_piece.color != piece.color) || move == fen.en_passant
168
+ valid = (target_piece && target_piece.color != piece.color) || move == fen.en_passant
166
169
  end
170
+ valid && king_will_be_safe?(piece, board, move)
167
171
  end
168
172
 
169
173
  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.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Ellison
@@ -19,6 +19,7 @@ files:
19
19
  - lib/board_logic.rb
20
20
  - lib/chess_validator.rb
21
21
  - lib/constants/square_key.rb
22
+ - lib/game_logic.rb
22
23
  - lib/move_logic.rb
23
24
  - lib/piece.rb
24
25
  homepage: https://rubygems.org/search?utf8=%E2%9C%93&query=chess_validator