pgn 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b151e9dfbad65addcdbcc73985f280a1b7b42a55
4
- data.tar.gz: 1aecd46315e114eec514ff627c442dd559bf5237
3
+ metadata.gz: 67674551215924868ef976e50328c035e718d064
4
+ data.tar.gz: 83baeece613b7cee8ccbdbdf648383bd03c94f55
5
5
  SHA512:
6
- metadata.gz: 4bb8e0a0e1ca54fb4fb4644e96a68ef3162a16c3dfc6e602993fe6e195dd1fcbe9b31e42239a61f608ca15698ff126a51ea5f0f202433e83aa52e2b5cd92e8d5
7
- data.tar.gz: 6f070630bebabc25cd9261819cd2beb0d3d7a0309dcba56244f6712fcd339fab0db154c1ae785b3d33ca1e3f1888576d0b4143d7ddd8d1fe52f7941dd585ee84
6
+ metadata.gz: 2dc3ad633890315dd863a7bf0892232761ebfb267f9769c4bec561bfe80d5125e0edabc631fd912e926d7039395c7920cbb9a2bf7c966adb24201fec6c7ab467
7
+ data.tar.gz: a85bad51cefc2a9d90cd19ac78db6e96ab97c7646c14c74c3daef10dc55c871c17ab516bea12b6a7e77b41d2a9d9879cf3ab852763e2df374292577bdc0b3f53
data/TODO.md CHANGED
@@ -1,4 +1,12 @@
1
1
  # TODOs
2
2
 
3
- - `Game#to_pgn`
3
+ ## Parsing
4
+
5
+ - Accept a more flexible input format
6
+ - Support recursive variations
7
+ - Support numeric annotation glyphs
8
+
9
+ ## Misc
10
+
11
+ - Support converting a game to pgn format
4
12
  - Speed up parsing
data/lib/pgn/board.rb CHANGED
@@ -13,14 +13,14 @@ module PGN
13
13
  # The starting, internal representation of a chess board
14
14
  #
15
15
  START = [
16
- ["R", "P", "_", "_", "_", "_", "p", "r"],
17
- ["N", "P", "_", "_", "_", "_", "p", "n"],
18
- ["B", "P", "_", "_", "_", "_", "p", "b"],
19
- ["Q", "P", "_", "_", "_", "_", "p", "q"],
20
- ["K", "P", "_", "_", "_", "_", "p", "k"],
21
- ["B", "P", "_", "_", "_", "_", "p", "b"],
22
- ["N", "P", "_", "_", "_", "_", "p", "n"],
23
- ["R", "P", "_", "_", "_", "_", "p", "r"],
16
+ ["R", "P", nil, nil, nil, nil, "p", "r"],
17
+ ["N", "P", nil, nil, nil, nil, "p", "n"],
18
+ ["B", "P", nil, nil, nil, nil, "p", "b"],
19
+ ["Q", "P", nil, nil, nil, nil, "p", "q"],
20
+ ["K", "P", nil, nil, nil, nil, "p", "k"],
21
+ ["B", "P", nil, nil, nil, nil, "p", "b"],
22
+ ["N", "P", nil, nil, nil, nil, "p", "n"],
23
+ ["R", "P", nil, nil, nil, nil, "p", "r"],
24
24
  ]
25
25
 
26
26
  FILE_TO_INDEX = {
@@ -62,7 +62,7 @@ module PGN
62
62
  'B' => "\u{2657}",
63
63
  'N' => "\u{2658}",
64
64
  'P' => "\u{2659}",
65
- '_' => '_',
65
+ nil => '_',
66
66
  }
67
67
 
68
68
  attr_accessor :squares
@@ -77,14 +77,14 @@ module PGN
77
77
  # @example
78
78
  # PGN::Board.new(
79
79
  # [
80
- # ["R", "P", "_", "_", "_", "_", "p", "r"],
81
- # ["N", "P", "_", "_", "_", "_", "p", "n"],
82
- # ["B", "P", "_", "_", "_", "_", "p", "b"],
83
- # ["Q", "P", "_", "_", "_", "_", "p", "q"],
84
- # ["K", "P", "_", "_", "_", "_", "p", "k"],
85
- # ["B", "P", "_", "_", "_", "_", "p", "b"],
86
- # ["N", "P", "_", "_", "_", "_", "p", "n"],
87
- # ["R", "P", "_", "_", "_", "_", "p", "r"],
80
+ # ["R", "P", nil, nil, nil, nil, "p", "r"],
81
+ # ["N", "P", nil, nil, nil, nil, "p", "n"],
82
+ # ["B", "P", nil, nil, nil, nil, "p", "b"],
83
+ # ["Q", "P", nil, nil, nil, nil, "p", "q"],
84
+ # ["K", "P", nil, nil, nil, nil, "p", "k"],
85
+ # ["B", "P", nil, nil, nil, nil, "p", "b"],
86
+ # ["N", "P", nil, nil, nil, nil, "p", "n"],
87
+ # ["R", "P", nil, nil, nil, nil, "p", "r"],
88
88
  # ]
89
89
  # )
90
90
  #
@@ -106,14 +106,12 @@ module PGN
106
106
  # board.at("e4") #=> "P"
107
107
  #
108
108
  def at(*args)
109
- str = case args.length
109
+ case args.length
110
110
  when 1
111
111
  self.at(*coordinates_for(args.first))
112
112
  when 2
113
113
  self.squares[args[0]][args[1]]
114
114
  end
115
-
116
- str == "_" ? nil : str
117
115
  end
118
116
 
119
117
  # @param changes [Hash<String, <String, nil>>] changes to make to the board
@@ -136,7 +134,7 @@ module PGN
136
134
  #
137
135
  def update(square, piece)
138
136
  coords = coordinates_for(square)
139
- self.squares[coords[0]][coords[1]] = piece || "_"
137
+ self.squares[coords[0]][coords[1]] = piece
140
138
  self
141
139
  end
142
140
 
data/lib/pgn/fen.rb CHANGED
@@ -87,6 +87,7 @@ module PGN
87
87
  squares = board_fen.gsub(/\d/) {|match| "_" * match.to_i }
88
88
  .split("/")
89
89
  .map {|row| row.split('') }
90
+ .map {|row| row.map {|e| e == "_" ? nil : e } }
90
91
  .reverse
91
92
  .transpose
92
93
  self.board = PGN::Board.new(squares)
@@ -101,6 +102,7 @@ module PGN
101
102
  .squares
102
103
  .transpose
103
104
  .reverse
105
+ .map {|row| row.map {|e| e.nil? ? "_" : e } }
104
106
  .map {|row| row.join }
105
107
  .join("/")
106
108
  .gsub(/_+/) {|match| match.length }
@@ -90,13 +90,12 @@ module PGN
90
90
  def initialize(board, move)
91
91
  self.board = board
92
92
  self.move = move
93
+ self.origin = compute_origin
93
94
  end
94
95
 
95
96
  # @return [PGN::Board] the board after the move is made
96
97
  #
97
98
  def result_board
98
- compute_origin
99
-
100
99
  new_board = self.board.dup
101
100
  new_board.change!(changes)
102
101
 
@@ -106,8 +105,6 @@ module PGN
106
105
  # @return [Array<String>] which castling moves are no longer available
107
106
  #
108
107
  def castling_restrictions
109
- compute_origin
110
-
111
108
  restrict = case self.move.piece
112
109
  when "K" then "KQ"
113
110
  when "k" then "kq"
@@ -140,8 +137,6 @@ module PGN
140
137
  # @return [String, nil] the en passant square if applicable
141
138
  #
142
139
  def en_passant_square
143
- compute_origin
144
-
145
140
  return nil if move.castle
146
141
 
147
142
  if self.move.pawn? && (self.origin[1].to_i - self.move.destination[1].to_i).abs == 2
@@ -154,8 +149,6 @@ module PGN
154
149
  private
155
150
 
156
151
  def changes
157
- compute_origin
158
-
159
152
  changes = {}
160
153
  changes.merge!(CASTLING[self.move.castle]) if self.move.castle
161
154
  changes.merge!(
@@ -178,19 +171,17 @@ module PGN
178
171
  def compute_origin
179
172
  return nil if move.castle
180
173
 
181
- @origin ||= begin
182
- possibilities = case move.piece
183
- when /[brq]/i then direction_origins
184
- when /[kn]/i then move_origins
185
- when /p/i then pawn_origins
186
- end
187
-
188
- if possibilities.length > 1
189
- possibilities = disambiguate(possibilities)
190
- end
174
+ possibilities = case move.piece
175
+ when /[brq]/i then direction_origins
176
+ when /[kn]/i then move_origins
177
+ when /p/i then pawn_origins
178
+ end
191
179
 
192
- self.board.position_for(possibilities.first)
180
+ if possibilities.length > 1
181
+ possibilities = disambiguate(possibilities)
193
182
  end
183
+
184
+ self.board.position_for(possibilities.first)
194
185
  end
195
186
 
196
187
  # From the destination square, move in each direction stopping if we
data/lib/pgn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module PGN
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stacey Touset