chess_validator 0.1.4 → 0.1.5

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: 5b8816b4191a4c8cbaf59dad22084cb96e39a1b6f8a492503f3ad32c5302060f
4
- data.tar.gz: 78fbff9e7f03c7c051e9d2c471228e46b2993a0412ce7db3664e4041bfed3be8
3
+ metadata.gz: b894e53ae4302055b3a8078c4bbfba1265960621d25932d9d02c66b181874068
4
+ data.tar.gz: 8e8b040cab68bf0bd3742a89c63ce03738968cb754d6f1877e95edb5bdfe97ba
5
5
  SHA512:
6
- metadata.gz: 66e0e177d1d49cd5b48c418ee4fee08125c4eb309e6d69ae75fd28d22a8a9b58cb6cdf11ed5bf18ac486f1c2d8953ce09341bcb8eb7b9b099ad99f5aeb783148
7
- data.tar.gz: d780ab271eea4b858d20569c7f955f76252510f73592ca31b2baad3c92f6db206f84fa0269429416046a7063f5d521e6ca31eb8b7bdbfcd54588f06ba49db150
6
+ metadata.gz: ed34ebd9d472f73f9e06119592a8766fea05e0570e5c48dce52d4d9440a8bb26dff7afb90f885ebcdee2cfe197c41d8857f186085f0c300bb16e251859710550
7
+ data.tar.gz: 366776136d638c090c71cad003617e4a10c3c74463ca6bcd1d6c281ddcca033e656a1f17f9acde324173d1ee242ca6c8eafec29c90d16d2345540b49281faaab
@@ -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,2 @@
1
1
  require 'move_logic'
2
+ require 'pry'
@@ -1,6 +1,6 @@
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
@@ -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_notatioin, 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_notatioin)
96
+ end
97
+
98
+ def make_move(piece, move, fen_notatioin)
99
+ fen = PGN::FEN.new(fen_notatioin)
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
@@ -105,6 +119,8 @@ module ChessValidator
105
119
  king = p if p.piece_type.downcase == 'k' && p.color == piece.color
106
120
  occupied_spaces << p.position
107
121
  end
122
+
123
+ return false if king.nil?
108
124
  king_is_safe?(king.color, new_board, king.position, occupied_spaces)
109
125
  end
110
126
 
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
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Ellison
@@ -40,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  requirements: []
43
- rubygems_version: 3.1.3
43
+ rubygems_version: 3.1.2
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: A chess move validator