chess_validator 0.1.4 → 0.1.9
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 +4 -4
- data/lib/board_logic.rb +70 -1
- data/lib/chess_validator.rb +2 -0
- data/lib/game_logic.rb +41 -0
- data/lib/move_logic.rb +28 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 012e2a3f6be44c4d117d14d0403be0a039d3f003716e899885470ef1fc4459fa
|
4
|
+
data.tar.gz: c11fd3a325de4e9d743fe26a15ec5cf0f91a09bf280e99027966d9ba09fc3d09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 570cedffbcfe74a5427954fa454b889feaf4a962147e99125cbc3d6f4904415f0077180c05af56035e4f67cab0191d3ce69b8263de15e034938cee7de0f9da7a
|
7
|
+
data.tar.gz: d45df24d49ff075283b84565cecd31eab976e792fea274718498f3db1b5e62257d08f336d217ee179740a85fb2c8d0d8802d10b34d8cb89b46ee686a811d81c1
|
data/lib/board_logic.rb
CHANGED
@@ -5,7 +5,7 @@ module ChessValidator
|
|
5
5
|
def self.build_board(fen)
|
6
6
|
board = {}
|
7
7
|
square_index = 1
|
8
|
-
fen.
|
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
|
data/lib/chess_validator.rb
CHANGED
data/lib/game_logic.rb
ADDED
@@ -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
|
data/lib/move_logic.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'board_logic'
|
2
2
|
require 'pgn'
|
3
|
-
|
3
|
+
|
4
4
|
module ChessValidator
|
5
5
|
class MoveLogic
|
6
6
|
class << self
|
7
|
-
def find_next_moves(
|
8
|
-
fen = PGN::FEN.new(
|
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
|
-
|
105
|
-
king = p if p.piece_type.downcase == 'k' && p.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
|
-
|
129
|
+
[king, occupied_spaces]
|
109
130
|
end
|
110
131
|
|
111
132
|
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.
|
4
|
+
version: 0.1.9
|
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.
|
44
|
+
rubygems_version: 3.1.2
|
44
45
|
signing_key:
|
45
46
|
specification_version: 4
|
46
47
|
summary: A chess move validator
|