chess_validator 0.1.3 → 0.1.8

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: 76c92b66d08e4f224ed65e0464616d2607bfb816c3549d094da30ac9329925fb
4
- data.tar.gz: 2353327aa052a4b674fa6c9132fb73e71acad51610d89267e317de60ff7f5775
3
+ metadata.gz: 332c370c589e98a08b37e03c361317322600511f918a8667a8830e176ed7bac8
4
+ data.tar.gz: f66908986c5b9e1ae09943a23cc8eea48a642b69ce51da3f6c2d5c091a0c0848
5
5
  SHA512:
6
- metadata.gz: 818c1c112a6a59e40ff4ace6f0736c91f50359a4055e72d1f0053835817a6660b17cb09c9b7af757367ca40122f21f11d1b75296c3bbc4b76505044fea70480c
7
- data.tar.gz: b0bcfe8bc32af31dd89643124d4f0bf33c9121ae5b485c57d1ecca6fb07d1bedb06d776c756f365dc691476c3f24e368a6b70bc7adc006587c5d9efc152fcb1c
6
+ metadata.gz: b225a87c0c37fcaf75d169fb4125b18cdc0adc04708efee206733e3f3a28b788d385bd79d43540bc81f7c49bcf5d9dc0e575f7760c173daf175c4836a896e9b6
7
+ data.tar.gz: f78be11ce467f85861584da97ae493d89a0c230f133fe629ee0b90140bd70d50d70709493c894c8517cd3882a2af561dfcdf86743e3b5c21b8ec8841400bc3af
@@ -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 != '/'
@@ -17,6 +17,75 @@ module ChessValidator
17
17
  board
18
18
  end
19
19
 
20
+ def self.to_fen_notation(board, previous_fen, piece, move)
21
+ notation = handle_position(board)
22
+ notation += find_turn(previous_fen.active)
23
+ notation += handle_castle(previous_fen.castling, piece)
24
+ notation += handle_en_passant(piece, move)
25
+ notation += handle_half_move_clock(board.size, previous_fen, piece)
26
+ notation += piece.color == 'b' ? previous_fen.fullmove.next : previous_fen.fullmove
27
+ notation
28
+ end
29
+
30
+ def self.handle_half_move_clock(board_size, previous_fen, piece)
31
+ if piece.piece_type == 'pawn' || build_board(previous_fen).size > board_size
32
+ '0 '
33
+ else
34
+ previous_fen.halfmove.next + ' '
35
+ end
36
+ end
37
+
38
+ def self.handle_castle(castling, piece)
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
49
+ end
50
+
51
+ def self.handle_en_passant(piece, move)
52
+ en_passant = ' - '
53
+ if (piece.piece_type.downcase == 'p' && (piece.position[1].to_i - move[1].to_i).abs > 1)
54
+ column = piece.color == 'w' ? '3' : '6'
55
+ ' ' + piece.position[0] + column + ' '
56
+ else
57
+ ' - '
58
+ end
59
+ end
60
+
61
+ def self.handle_position(board)
62
+ notation = ''
63
+ square_gap = 0
64
+ 64.times do |n|
65
+ if n > 0 && n % 8 == 0
66
+ notation += square_gap.to_s if square_gap > 0
67
+ notation += '/'
68
+ square_gap = 0
69
+ end
70
+
71
+ piece = board[n + 1]
72
+ if piece
73
+ notation += square_gap.to_s if square_gap > 0
74
+ notation += piece.piece_type
75
+ square_gap = 0
76
+ elsif n < 63
77
+ square_gap += 1
78
+ else
79
+ notation += (square_gap + 1).to_s
80
+ end
81
+ end
82
+ notation
83
+ end
84
+
85
+ def self.find_turn(current_turn)
86
+ current_turn == 'w' ? ' b ' : ' w '
87
+ end
88
+
20
89
  def self.empty_square?(char)
21
90
  ('1'..'8').include?(char)
22
91
  end
@@ -1 +1,3 @@
1
1
  require 'move_logic'
2
+ require 'game_logic'
3
+ require 'pry'
@@ -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.find_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
@@ -1,11 +1,11 @@
1
1
  require 'board_logic'
2
2
  require 'pgn'
3
- require 'pry'
3
+
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
 
@@ -20,7 +20,7 @@ module ChessValidator
20
20
  board.values.each do |piece|
21
21
  if piece.color == fen.active
22
22
  load_valid_moves(board, piece, fen)
23
- pieces << piece
23
+ pieces << piece if piece.valid_moves.size > 0
24
24
  end
25
25
  end
26
26
 
@@ -89,6 +89,20 @@ module ChessValidator
89
89
  board
90
90
  end
91
91
 
92
+ def make_random_move(fen_notation, pieces_with_moves)
93
+ piece_to_move = pieces_with_moves.sample
94
+ move = piece_to_move.valid_moves.sample
95
+ make_move(piece_to_move, move, fen_notation)
96
+ end
97
+
98
+ def make_move(piece, move, fen_notation)
99
+ fen = PGN::FEN.new(fen_notation)
100
+ board = BoardLogic.build_board(fen)
101
+ new_board = with_next_move(piece, board, move)
102
+
103
+ BoardLogic.to_fen_notation(new_board, fen, piece, move)
104
+ end
105
+
92
106
  def castled?(piece, move)
93
107
  piece.piece_type.downcase == 'k' && (piece.position[0].ord - move[0].ord).abs == 2
94
108
  end
@@ -99,13 +113,20 @@ module ChessValidator
99
113
 
100
114
  def king_will_be_safe?(piece, board, move)
101
115
  new_board = with_next_move(piece, board, move)
116
+ king, occupied_spaces = find_king_and_spaces(new_board, piece.color)
117
+
118
+ return false if king.nil?
119
+ king_is_safe?(king.color, new_board, king.position, occupied_spaces)
120
+ end
121
+
122
+ def find_king_and_spaces(board, color)
102
123
  occupied_spaces = []
103
124
  king = nil
104
- new_board.values.each do |p|
105
- king = p if p.piece_type.downcase == 'k' && p.color == piece.color
125
+ board.values.each do |p|
126
+ king = p if p.piece_type.downcase == 'k' && p.color == color
106
127
  occupied_spaces << p.position
107
128
  end
108
- king_is_safe?(king.color, new_board, king.position, occupied_spaces)
129
+ [king, occupied_spaces]
109
130
  end
110
131
 
111
132
  def handle_king(king, board, move, fen, occupied_spaces)
@@ -290,6 +311,7 @@ module ChessValidator
290
311
  column.chr + (row + 1).to_s,
291
312
  column.chr + (row - 1).to_s
292
313
  ]
314
+ remove_out_of_bounds(moves)
293
315
  end
294
316
 
295
317
  def moves_for_king(position)
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.3
4
+ version: 0.1.8
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
@@ -40,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
41
  - !ruby/object:Gem::Version
41
42
  version: '0'
42
43
  requirements: []
43
- rubygems_version: 3.1.3
44
+ rubygems_version: 3.1.2
44
45
  signing_key:
45
46
  specification_version: 4
46
47
  summary: A chess move validator