chess 0.2.0 → 0.3.2

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.
Files changed (70) hide show
  1. checksums.yaml +5 -5
  2. data/.github/FUNDING.yml +12 -0
  3. data/.github/workflows/ruby.yml +58 -0
  4. data/.gitignore +2 -2
  5. data/.rubocop.yml +51 -0
  6. data/Gemfile +1 -1
  7. data/Gemfile.lock +70 -0
  8. data/README.md +26 -22
  9. data/Rakefile +5 -4
  10. data/chess.gemspec +15 -11
  11. data/docs/Chess.html +157 -0
  12. data/docs/Chess/BadNotationError.html +237 -0
  13. data/docs/Chess/Board.html +1759 -0
  14. data/docs/Chess/CGame.html +2296 -0
  15. data/docs/Chess/Game.html +1277 -0
  16. data/docs/Chess/Gnuchess.html +366 -0
  17. data/docs/Chess/IllegalMoveError.html +137 -0
  18. data/docs/Chess/InvalidFenFormatError.html +237 -0
  19. data/docs/Chess/InvalidPgnFormatError.html +217 -0
  20. data/docs/Chess/Pgn.html +1477 -0
  21. data/docs/Chess/UTF8Notation.html +270 -0
  22. data/docs/_index.html +217 -0
  23. data/docs/class_list.html +51 -0
  24. data/docs/css/common.css +1 -0
  25. data/docs/css/full_list.css +58 -0
  26. data/docs/css/style.css +497 -0
  27. data/docs/file.README.html +116 -0
  28. data/docs/file_list.html +56 -0
  29. data/docs/frames.html +17 -0
  30. data/docs/index.html +116 -0
  31. data/docs/js/app.js +314 -0
  32. data/docs/js/full_list.js +216 -0
  33. data/docs/js/jquery.js +4 -0
  34. data/docs/method_list.html +531 -0
  35. data/docs/top-level-namespace.html +110 -0
  36. data/ext/bitboard.c +1 -1
  37. data/ext/bitboard.h +1 -1
  38. data/ext/board.c +1 -1
  39. data/ext/board.h +1 -1
  40. data/ext/chess.c +2 -2
  41. data/ext/chess.h +1 -1
  42. data/ext/common.c +1 -1
  43. data/ext/common.h +8 -4
  44. data/ext/extconf.rb +7 -6
  45. data/ext/game.c +2 -2
  46. data/ext/game.h +1 -1
  47. data/ext/special.c +10 -3
  48. data/ext/special.h +3 -2
  49. data/lib/chess/exceptions.rb +2 -5
  50. data/lib/chess/game.rb +55 -77
  51. data/lib/chess/gnuchess.rb +24 -27
  52. data/lib/chess/pgn.rb +32 -26
  53. data/lib/chess/utf8_notation.rb +3 -3
  54. data/lib/chess/version.rb +1 -1
  55. data/test/test_big_pgn_collection.rb +3 -4
  56. data/test/test_checkmate.rb +4 -6
  57. data/test/test_errors.rb +22 -0
  58. data/test/test_fifty_rule_move.rb +2 -4
  59. data/test/test_game.rb +82 -0
  60. data/test/test_helper.rb +16 -3
  61. data/test/test_illegal_moves.rb +1 -3
  62. data/test/test_insufficient_material.rb +6 -7
  63. data/test/test_load_fen.rb +11 -1
  64. data/test/test_move_generator.rb +13 -14
  65. data/test/test_particular_situations.rb +0 -2
  66. data/test/test_pgn.rb +82 -1
  67. data/test/test_pgn_collection.rb +2 -2
  68. data/test/test_stalemate.rb +1 -3
  69. data/test/test_threefold_repetition.rb +1 -3
  70. metadata +145 -15
@@ -0,0 +1,110 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Chess
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" />
16
+
17
+ <script type="text/javascript">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ </div>
80
+
81
+ <h2>Defined Under Namespace</h2>
82
+ <p class="children">
83
+
84
+
85
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Chess.html" title="Chess (module)">Chess</a></span>
86
+
87
+
88
+
89
+
90
+ </p>
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+ </div>
101
+
102
+ <div id="footer">
103
+ Generated on Thu Jan 28 17:31:40 2021 by
104
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
+ 0.9.26 (ruby-2.6.1).
106
+ </div>
107
+
108
+ </div>
109
+ </body>
110
+ </html>
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -723,7 +723,7 @@ Init_chess ()
723
723
  rb_define_method (board_klass, "stalemate?", board_stalemate, 0);
724
724
  rb_define_method (board_klass, "insufficient_material?", board_insufficient_material, 0);
725
725
  rb_define_method (board_klass, "only_kings?", board_only_kings, 0);
726
- rb_define_method (board_klass, "fifty_rule_move?", board_fifty_move_rule, 0);
726
+ rb_define_method (board_klass, "fifty_move_rule?", board_fifty_move_rule, 0);
727
727
  rb_define_method (board_klass, "active_color", board_active_color, 0);
728
728
  rb_define_method (board_klass, "halfmove_clock", board_halfmove_clock, 0);
729
729
  rb_define_method (board_klass, "fullmove_number", board_fullmove_number, 0);
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -13,9 +13,15 @@
13
13
  #include <string.h>
14
14
  #include <ctype.h>
15
15
 
16
- // Macros
16
+ // Boolean definition
17
+
18
+ #ifndef RUBY_BACKWARD2_BOOL_H
17
19
  #define FALSE 0
18
20
  #define TRUE 1
21
+ typedef unsigned short bool;
22
+ #endif
23
+
24
+ // Macros
19
25
 
20
26
  #define A1 0
21
27
  #define B1 1
@@ -95,8 +101,6 @@
95
101
  #define DRAW 2
96
102
  #define IN_PROGRESS 3
97
103
 
98
- typedef unsigned short bool;
99
-
100
104
  char square_to_file (int square);
101
105
  char square_to_rank (int square);
102
106
  int coord_to_square (const char *coord);
@@ -1,4 +1,4 @@
1
- ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
1
+ ENV['RC_ARCHS'] = '' if RUBY_PLATFORM.include?('darwin')
2
2
 
3
3
  require 'mkmf'
4
4
 
@@ -10,8 +10,8 @@ INCLUDE_DIRS = [
10
10
  # Then search /usr/local for people that installed from source
11
11
  '/usr/local/include',
12
12
  # Finally fall back to /usr
13
- '/usr/include',
14
- ]
13
+ '/usr/include'
14
+ ].freeze
15
15
 
16
16
  LIB_DIRS = [
17
17
  # Check the ruby install locations
@@ -21,9 +21,10 @@ LIB_DIRS = [
21
21
  # Then search /usr/local for people that installed from source
22
22
  '/usr/local/lib',
23
23
  # Finally fall back to /usr
24
- '/usr/lib',
25
- ]
24
+ '/usr/lib'
25
+ ].freeze
26
26
 
27
- #dir_config('chess', INCLUDE_DIRS, LIB_DIRS)
27
+ # dir_config('chess', INCLUDE_DIRS, LIB_DIRS)
28
+ $CFLAGS << ' -std=c99'
28
29
 
29
30
  create_makefile('chess/chess')
data/ext/game.c CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -98,7 +98,7 @@ apply_move (Game *g, int from, int to, char promote_in)
98
98
  }
99
99
  }
100
100
  // Ok move is legal, update the game
101
- update_castling (new_board, from);
101
+ update_castling (new_board, from, to);
102
102
  update_en_passant (new_board, from, to);
103
103
  if (new_board->active_color)
104
104
  new_board->fullmove_number++;
data/ext/game.h CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -9,9 +9,9 @@
9
9
 
10
10
  // Update the board castling bits (FEN style).
11
11
  void
12
- update_castling (Board *board, int from)
12
+ update_castling_by_square (Board *board, int square)
13
13
  {
14
- switch (from)
14
+ switch (square)
15
15
  {
16
16
  case A1:
17
17
  board->castling ^= board->castling & 0x0100;
@@ -34,6 +34,13 @@ update_castling (Board *board, int from)
34
34
  }
35
35
  }
36
36
 
37
+ // Update the board castling bits checking if squares from and to are involved.
38
+ void
39
+ update_castling (Board *board, int from, int to) {
40
+ update_castling_by_square (board, from);
41
+ update_castling_by_square (board, to);
42
+ }
43
+
37
44
  // Returns the castling type for the move from-to. If not a castling move
38
45
  // returns 0.
39
46
  int
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * chess - a fast library to play chess in Ruby
3
3
  *
4
- * Copyright (c) 2011-2017, Enrico Pilotto <epilotto@gmx.com>
4
+ * Copyright (c) 2011-2018, Enrico Pilotto <epilotto@gmx.com>
5
5
  * This code is under LICENSE LGPLv3
6
6
  */
7
7
 
@@ -11,7 +11,8 @@
11
11
  #include "common.h"
12
12
  #include "board.h"
13
13
 
14
- void update_castling (Board *board, int from);
14
+ void update_castling_by_square (Board *board, int square);
15
+ void update_castling (Board *board, int from, int to);
15
16
  int castling_type (Board *board, int from, int to);
16
17
  char* castling (Board *board, int castling_type, Board *new_board);
17
18
  void update_en_passant (Board *board, int from, int to);
@@ -1,5 +1,4 @@
1
1
  module Chess
2
-
3
2
  # This exception will be raised when an invalid short algebraic chess notation
4
3
  # string is passed to the {Game#move} function.
5
4
  class BadNotationError < StandardError
@@ -11,9 +10,8 @@ module Chess
11
10
 
12
11
  # This exception will be raised when a malformed PGN file is loaded.
13
12
  class InvalidPgnFormatError < StandardError
14
- # @param [String] filename The PGN filename
15
- def initialize(filename)
16
- super("Invalid PGN file '#{filename}'")
13
+ def initialize
14
+ super('Invalid PGN format')
17
15
  end
18
16
  end
19
17
 
@@ -24,5 +22,4 @@ module Chess
24
22
  super("Invalid FEN string '#{fen_string}'")
25
23
  end
26
24
  end
27
-
28
25
  end
@@ -1,14 +1,13 @@
1
1
  require File.join(File.dirname(__FILE__), '../../ext/chess')
2
2
 
3
3
  module Chess
4
-
5
4
  # This class rappresents a chess game.
6
5
  class Game < CGame
7
-
8
6
  # @param [Array<String>] moves If an array of moves is provided, the moves will be performed.
9
7
  # @raise [IllegalMoveError]
10
8
  # @raise [BadNotationError]
11
9
  def initialize(moves = [])
10
+ super()
12
11
  moves.each { |m| move(m) }
13
12
  end
14
13
 
@@ -22,7 +21,7 @@ module Chess
22
21
  pgn = Chess::Pgn.new(file)
23
22
  game = Chess::Game.new
24
23
  pgn.moves.each { |m| game.move(m) }
25
- if !game.over?
24
+ unless game.over?
26
25
  case pgn.result
27
26
  when '1-0'
28
27
  game.resign(:black)
@@ -41,19 +40,17 @@ module Chess
41
40
  # @raise [InvalidFenFormatError]
42
41
  # @note This game do not have history before the FEN placement.
43
42
  def self.load_fen(fen)
44
- if fen =~ /^((?:[PRNBQKprnbqk1-8]{1,8}\/){7}[RNBQKPrnbqkp1-8]{1,8})\s(w|b)\s(K?Q?k?q?|-)\s([a-h][1-8]|-)\s(\d+)\s(\d+)$/
45
- game = Chess::Game.new
46
- game.set_fen!(fen)
47
- return game
48
- else
49
- raise InvalidFenFormatError.new(fen)
50
- end
43
+ raise InvalidFenFormatError.new(fen) unless /^((?:[PRNBQKprnbqk1-8]{1,8}\/){7}[RNBQKPrnbqkp1-8]{1,8})\s(w|b)\s(K?Q?k?q?|-)\s([a-h][1-8]|-)\s(\d+)\s(\d+)$/.match?(fen)
44
+
45
+ game = Chess::Game.new
46
+ game.set_fen!(fen)
47
+ return game
51
48
  end
52
49
 
53
50
  # Make a move.
54
51
  # @note This add a new {Board} in the {Game}.
55
- # @param [String] m Represents the short algebraic chess notation string of
56
- # the move. `m` can be also _from_square_ plus _to_square_ _('e2e4', ...,
52
+ # @param [String] notation Represents the short algebraic chess notation string of
53
+ # the move. `notation` can be also _from_square_ plus _to_square_ _('e2e4', ...,
57
54
  # 'b1c3')_ (coordinate chess notation).
58
55
  # @return [String] Returns a string that represents the short algebraic
59
56
  # chess notation of the move.
@@ -61,24 +58,20 @@ module Chess
61
58
  # @raise {IllegalMoveError} if the move is illegal.
62
59
  # @raise {BadNotationError} if the short algebraic chess notation is
63
60
  # malformed.
64
- def move(m)
65
- begin
66
- expand = expand_move(m)
67
- if expand[:from]
68
- move2(expand[:from], expand[:to], expand[:promotion])
69
- else
70
- super(expand[:name], expand[:dis], expand[:to], expand[:promotion])
71
- end
72
- rescue IllegalMoveError
73
- if ENV['DEBUG']
74
- raise IllegalMoveError.new("Illegal move '#{m}'\nStatus: #{self.status}\nPlayer turn #{self.active_player}\n#{self.to_s}")
75
- else
76
- raise IllegalMoveError.new("Illegal move '#{m}'")
77
- end
61
+ def move(notation)
62
+ expand = expand_move(notation)
63
+ if expand[:from]
64
+ move2(expand[:from], expand[:to], expand[:promotion])
65
+ else
66
+ super(expand[:name], expand[:dis], expand[:to], expand[:promotion])
78
67
  end
68
+ rescue IllegalMoveError
69
+ raise IllegalMoveError.new("Illegal move '#{notation}'\nStatus: #{self.status}\nPlayer turn #{self.active_player}\n#{self}") if ENV['DEBUG']
70
+
71
+ raise IllegalMoveError.new("Illegal move '#{notation}'")
79
72
  end
80
- alias :move= :move
81
- alias :<< :move
73
+ alias move= move
74
+ alias << move
82
75
 
83
76
  # Make the array of moves.
84
77
  # @param [Array<String>] moves The array of moves to performe.
@@ -110,38 +103,28 @@ module Chess
110
103
  # * `black_won_resign`: black player has won for resign.
111
104
  # * `stalemate`: draw for stalemate.
112
105
  # * `insufficient_material`: draw for insufficient material to checkmate.
113
- # * `fifty_rule_move`: draw for fifty rule move.
114
- # * `threefold_repetition`: draw for threefold_repetition.
115
- # * `unknown`: something went wrong.
116
- # @return [String]
106
+ # * `fifty_move_rule`: draw for fifty-move rule.
107
+ # * `threefold_repetition`: draw for threefold repetition.
108
+ # @return [Symbol]
117
109
  def status
118
110
  case self.result
119
111
  when '*'
120
112
  return :in_progress
121
113
  when '1-0'
122
- if self.board.checkmate?
123
- return :white_won
124
- else
125
- return :white_won_resign
126
- end
114
+ return :white_won if self.board.checkmate?
115
+
116
+ return :white_won_resign
127
117
  when '0-1'
128
- if self.board.checkmate?
129
- return :black_won
130
- else
131
- return :black_won_resign
132
- end
118
+ return :black_won if self.board.checkmate?
119
+
120
+ return :black_won_resign
133
121
  when '1/2-1/2'
134
- if self.board.stalemate?
135
- return :stalemate
136
- elsif self.board.insufficient_material?
137
- return :insufficient_material
138
- elsif self.board.fifty_rule_move?
139
- return :fifty_rule_move
140
- elsif self.threefold_repetition?
141
- return :threefold_repetition
142
- end
122
+ return :stalemate if self.board.stalemate?
123
+ return :insufficient_material if self.board.insufficient_material?
124
+ return :fifty_move_rule if self.board.fifty_move_rule?
125
+
126
+ return :threefold_repetition if self.threefold_repetition?
143
127
  end
144
- return :unknown
145
128
  end
146
129
 
147
130
  # Returns `true` if the game is over.
@@ -150,7 +133,7 @@ module Chess
150
133
  end
151
134
 
152
135
  # Returns the PGN rappresenting the game.
153
- # @return [String]
136
+ # @return [Chess::Pgn]
154
137
  def pgn
155
138
  pgn = Chess::Pgn.new
156
139
  pgn.moves = self.moves
@@ -160,41 +143,36 @@ module Chess
160
143
 
161
144
  private
162
145
 
163
- # Expand the short algebraic chess notation string `m` in a hash like this:
146
+ # Expand the short algebraic chess notation string `notation` in a hash like this:
164
147
  #
165
148
  # Ngxe2 ==> { name: 'N', dis: 'g', from: nil, to: 'e2', promotion: nil }
166
- def expand_move(m)
167
- if match = m.match(Chess::MOVE_REGEXP)
149
+ def expand_move(notation)
150
+ if (match = notation.match(MOVE_REGEXP))
168
151
  expand = {
169
152
  name: match[1] || 'P', # Piece name [RNBQK]
170
153
  dis: match[2], # Disambiguating move
171
154
  to: match[3], # Move to
172
- promotion: match[4], # Promote with
155
+ promotion: match[4] # Promote with
173
156
  }
174
157
  expand[:from] = match[2] if match[2] && match[2].size == 2
175
158
  return expand
176
- elsif m =~ SHORT_CASTLING_REGEXP
177
- if self.board.active_color # black king short castling
178
- return { name: 'K', dis: nil, from: 'e8', to: 'g8', promotion: nil }
179
- else # white king short castling
180
- return { name: 'K', dis: nil, from: 'e1', to: 'g1', promotion: nil }
181
- end
182
- elsif m =~ LONG_CASTLING_REGEXP
183
- if self.board.active_color # black king long castling
184
- return { name: 'K', dis: nil, from: 'e8', to: 'c8', promotion: nil }
185
- else # white king long castling
186
- return { name: 'K', dis: nil, from: 'e1', to: 'c1', promotion: nil }
187
- end
159
+ elsif SHORT_CASTLING_REGEXP.match?(notation)
160
+ return { name: 'K', dis: nil, from: 'e8', to: 'g8', promotion: nil } if self.board.active_color # black king short castling
161
+
162
+ return { name: 'K', dis: nil, from: 'e1', to: 'g1', promotion: nil } # white king short castling
163
+ elsif LONG_CASTLING_REGEXP.match?(notation)
164
+ return { name: 'K', dis: nil, from: 'e8', to: 'c8', promotion: nil } if self.board.active_color # black king long castling
165
+
166
+ return { name: 'K', dis: nil, from: 'e1', to: 'c1', promotion: nil } # white king long castling
188
167
  end
189
- raise BadNotationError.new(m)
168
+ raise BadNotationError.new(notation)
190
169
  end
191
-
192
170
  end
193
171
 
194
- private
195
-
196
- MOVE_REGEXP = /^([RNBQK])?([a-h]|[1-8]|[a-h][1-8])?(?:x)?([a-h][1-8])(?:=?([RrNnBbQq]))?(?:ep)?(?:\+|\#)?$/
197
- SHORT_CASTLING_REGEXP = /^([0O])-([0O])([\+#])?$/
198
- LONG_CASTLING_REGEXP = /^([0O])-([0O])-([0O])([\+#])?$/
199
-
172
+ MOVE_REGEXP = /^([RNBQK])?([a-h]|[1-8]|[a-h][1-8])?(?:x)?([a-h][1-8])(?:=?([RrNnBbQq]))?(?:ep)?(?:\+|\#)?$/.freeze
173
+ SHORT_CASTLING_REGEXP = /^([0O])-([0O])([+#])?$/.freeze
174
+ LONG_CASTLING_REGEXP = /^([0O])-([0O])-([0O])([+#])?$/.freeze
175
+ private_constant :MOVE_REGEXP
176
+ private_constant :SHORT_CASTLING_REGEXP
177
+ private_constant :LONG_CASTLING_REGEXP
200
178
  end