chess_validator 0.1.5 → 0.1.6

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: d391ec459430072a8e9df514b4b68f1363fc16705971d5dc262ea601ebc64d9e
4
+ data.tar.gz: d5f8beef914ae16c35185441f80ec0f0f3459875480a8a2d1518e08f7f72fe8f
5
5
  SHA512:
6
- metadata.gz: ed34ebd9d472f73f9e06119592a8766fea05e0570e5c48dce52d4d9440a8bb26dff7afb90f885ebcdee2cfe197c41d8857f186085f0c300bb16e251859710550
7
- data.tar.gz: 366776136d638c090c71cad003617e4a10c3c74463ca6bcd1d6c281ddcca033e656a1f17f9acde324173d1ee242ca6c8eafec29c90d16d2345540b49281faaab
6
+ metadata.gz: 0572624b876491ca99ddd3a8999993c1a023c1496f4435375c7e1f087342839175e6455dce3a8128916fcd323125640ec11c8c46a72f27449353aaeddd91e066
7
+ data.tar.gz: 57b46adca5631c768e9386c0467b8b6318e4888ec3e44567b7b0240f13f84883419ecdbd8ac2840b10e477fa24776f33f7625b524c801d382a7fc59940cf8438
@@ -1,11 +1,9 @@
1
- require 'piece'
2
-
3
1
  module ChessValidator
4
2
  class BoardLogic
5
3
  def self.build_board(fen)
6
4
  board = {}
7
5
  square_index = 1
8
- fen.to_s.split(' ').first.chars.each do |char|
6
+ fen.board_string.chars.each do |char|
9
7
  if empty_square?(char)
10
8
  square_index += char.to_i
11
9
  elsif char != '/'
@@ -1,2 +1,3 @@
1
1
  require 'move_logic'
2
+ require 'game_logic'
2
3
  require 'pry'
@@ -0,0 +1,37 @@
1
+ module ChessValidator
2
+ class GameLogic
3
+ class << self
4
+ def find_game_result(fen_notation)
5
+ fen = PGN::FEN.new(fen_notation)
6
+ board = BoardLogic.build_board(fen)
7
+ no_moves = MoveLogic.find_next_moves(fen).empty?
8
+ checkmate_result = checkmate_value(fen, board, no_moves)
9
+
10
+ if checkmate_result
11
+ checkmate_result
12
+ elsif draw?(fen, board, no_moves)
13
+ '1/2-1/2'
14
+ end
15
+ end
16
+
17
+ def checkmate_value(fen, board, no_moves)
18
+ king, occupied_spaces = MoveLogic.find_king_and_spaces(board, fen.active)
19
+ in_check = !MoveLogic.king_is_safe?(fen.active, board, king.position, occupied_spaces)
20
+
21
+ if no_moves && in_check
22
+ fen.active == 'w' ? '0-1' : '1-0'
23
+ end
24
+ end
25
+
26
+ def draw?(fen, board, no_moves)
27
+ [fen.halfmove == '50', no_moves, insufficient_material?(board)].any?
28
+ end
29
+
30
+ def insufficient_material?(board)
31
+ if board.size < 4
32
+ board.values.none? { |piece| ['q', 'r', 'p'].include?(piece.piece_type.downcase) }
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,11 +1,8 @@
1
- require 'board_logic'
2
- require 'pgn'
3
-
4
1
  module ChessValidator
5
2
  class MoveLogic
6
3
  class << self
7
- def find_next_moves(fen_notatioin)
8
- fen = PGN::FEN.new(fen_notatioin)
4
+ def find_next_moves(fen_notation)
5
+ fen = PGN::FEN.new(fen_notation)
9
6
  next_moves(fen)
10
7
  end
11
8
 
@@ -89,14 +86,14 @@ module ChessValidator
89
86
  board
90
87
  end
91
88
 
92
- def make_random_move(fen_notatioin, pieces_with_moves)
89
+ def make_random_move(fen_notation, pieces_with_moves)
93
90
  piece_to_move = pieces_with_moves.sample
94
91
  move = piece_to_move.valid_moves.sample
95
- make_move(piece_to_move, move, fen_notatioin)
92
+ make_move(piece_to_move, move, fen_notation)
96
93
  end
97
94
 
98
- def make_move(piece, move, fen_notatioin)
99
- fen = PGN::FEN.new(fen_notatioin)
95
+ def make_move(piece, move, fen_notation)
96
+ fen = PGN::FEN.new(fen_notation)
100
97
  board = BoardLogic.build_board(fen)
101
98
  new_board = with_next_move(piece, board, move)
102
99
 
@@ -113,15 +110,20 @@ module ChessValidator
113
110
 
114
111
  def king_will_be_safe?(piece, board, move)
115
112
  new_board = with_next_move(piece, board, move)
113
+ king, occupied_spaces = find_king_and_spaces(new_board, piece.color)
114
+
115
+ return false if king.nil?
116
+ king_is_safe?(king.color, new_board, king.position, occupied_spaces)
117
+ end
118
+
119
+ def find_king_and_spaces(board, color)
116
120
  occupied_spaces = []
117
121
  king = nil
118
- new_board.values.each do |p|
119
- king = p if p.piece_type.downcase == 'k' && p.color == piece.color
122
+ board.values.each do |p|
123
+ king = p if p.piece_type.downcase == 'k' && p.color == color
120
124
  occupied_spaces << p.position
121
125
  end
122
-
123
- return false if king.nil?
124
- king_is_safe?(king.color, new_board, king.position, occupied_spaces)
126
+ [king, occupied_spaces]
125
127
  end
126
128
 
127
129
  def handle_king(king, board, move, fen, 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.1.5
4
+ version: 0.1.6
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