Class: Pgn2Fen::Game
- Inherits:
-
Object
- Object
- Pgn2Fen::Game
- Defined in:
- lib/pgn2fen.rb
Constant Summary collapse
- START_FEN =
constants
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
- HEADER_KEY_REGEX =
/^\[([A-Z][A-Za-z]*)\s.*\]$/
- HEADER_VALUE_REGEX =
/^\[[A-Za-z]+\s"(.*)"\]$/
- OPEN_PAREN =
"("
- CLOSE_PAREN =
")"
- OPEN_BRACE =
"{"
- CLOSE_BRACE =
"}"
- @@pgn_squares =
static initializers
["a8","b8","c8","d8","e8","f8","g8","h8","a7","b7","c7","d7","e7","f7","g7","h7","a6","b6","c6","d6","e6","f6","g6","h6","a5","b5","c5","d5","e5","f5","g5","h5","a4","b4","c4","d4","e4","f4","g4","h4","a3","b3","c3","d3","e3","f3","g3","h3","a2","b2","c2","d2","e2","f2","g2","h2","a1","b1","c1","d1","e1","f1","g1","h1"]
- @@number_squares =
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
- @@pgn_number_hash =
- @@number_pgn_hash =
- @@aFile =
Set.new
- @@bFile =
Set.new
- @@cFile =
Set.new
- @@dFile =
Set.new
- @@eFile =
Set.new
- @@fFile =
Set.new
- @@gFile =
Set.new
- @@hFile =
Set.new
- @@firstRank =
Set.new
- @@secondRank =
Set.new
- @@thirdRank =
Set.new
- @@fourthRank =
Set.new
- @@fifthRank =
Set.new
- @@sixthRank =
Set.new
- @@seventhRank =
Set.new
- @@eighthRank =
Set.new
- @@light_squares =
Set.new
- @@dark_squares =
Set.new
- @@one_thru_eight =
Set.new
- @@a_thru_h =
Set.new
Instance Attribute Summary collapse
-
#board_color_from_fen ⇒ Object
Returns the value of attribute board_color_from_fen.
-
#can_black_castle_kingside ⇒ Object
Returns the value of attribute can_black_castle_kingside.
-
#can_black_castle_queenside ⇒ Object
Returns the value of attribute can_black_castle_queenside.
-
#can_white_castle_kingside ⇒ Object
Returns the value of attribute can_white_castle_kingside.
-
#fen_array ⇒ Object
attr_accessor :event, :site, :date, :eventdate, :round, :white, :black, :whiteelo, :blackelo, :result, :eco, :plycount, :fen.
-
#fullmove ⇒ Object
Returns the value of attribute fullmove.
-
#halfmove ⇒ Object
Returns the value of attribute halfmove.
-
#pgn ⇒ Object
attr_accessor :event, :site, :date, :eventdate, :round, :white, :black, :whiteelo, :blackelo, :result, :eco, :plycount, :fen.
-
#potential_enpassent_ply ⇒ Object
Returns the value of attribute potential_enpassent_ply.
-
#promotion ⇒ Object
Returns the value of attribute promotion.
-
#promotion_piece ⇒ Object
Returns the value of attribute promotion_piece.
Instance Method Summary collapse
-
#board_to_fen ⇒ Object
return fen representation from board.
- #clean_comments(pgn) ⇒ Object
-
#clean_pgn(pgn) ⇒ Object
clean pgn - remove comments and other unrequired text.
- #clean_subvariations(pgn) ⇒ Object
- #dark_square_by_number(square_number) ⇒ Object
- #dark_square_by_pgn(square_pgn) ⇒ Object
-
#fen_array_from_plies(plies) ⇒ Object
get FEN array from plies.
-
#fen_castle_text ⇒ Object
get FEN castle text based on castling availability.
-
#fen_for_ply(ply, ply_number) ⇒ Object
get FEN from a ply and ply number.
- #file_for_hint(hint) ⇒ Object
- #games_from_pgn(pgn) ⇒ Object
- #init_board ⇒ Object
-
#initialize(pgn_data) ⇒ Game
constructor
A new instance of Game.
-
#is_single_game ⇒ Object
check to verify PGN includes only a single game.
- #light_square_by_number(square_number) ⇒ Object
- #light_square_by_pgn(square_pgn) ⇒ Object
-
#make_ply_on_board(long_ply) ⇒ Object
make a ply on the board.
- #parse_headers(headers) ⇒ Object
- #parse_pgn ⇒ Object
-
#plies_from_pgn(pgn) ⇒ Object
get plies from pgn.
- #pp_board ⇒ Object
- #rank_for_hint(hint) ⇒ Object
- #remove_text_between_tokens_inclusive(text, open_token, close_token) ⇒ Object
- #reset_castle_options(option) ⇒ Object
-
#short_ply_to_long_ply(ply, is_white) ⇒ Object
convert short ply to long ply.
- #short_ply_to_long_ply_for_bishop(to_idx, to_pgn, hint, is_white) ⇒ Object
- #short_ply_to_long_ply_for_king(to_idx, to_pgn, hint, is_white) ⇒ Object
- #short_ply_to_long_ply_for_knight(to_idx, to_pgn, hint, is_white) ⇒ Object
- #short_ply_to_long_ply_for_queen(to_idx, to_pgn, hint, is_white) ⇒ Object
- #short_ply_to_long_ply_for_rook(to_idx, to_pgn, hint, is_white) ⇒ Object
- #to_s ⇒ Object
-
#update_castle_options_from_fen(castle_options) ⇒ Object
update castling options from FEN header.
Constructor Details
#initialize(pgn_data) ⇒ Game
Returns a new instance of Game.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pgn2fen.rb', line 98 def initialize(pgn_data) @pgn_data = pgn_data @board = [] @board_start_fen = START_FEN @fen = nil @potential_enpassent_ply = nil @halfmove = 0 @fullmove = 1 @promotion = false @promotion_piece = nil @board_color_from_fen = nil end |
Instance Attribute Details
#board_color_from_fen ⇒ Object
Returns the value of attribute board_color_from_fen.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def board_color_from_fen @board_color_from_fen end |
#can_black_castle_kingside ⇒ Object
Returns the value of attribute can_black_castle_kingside.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def can_black_castle_kingside @can_black_castle_kingside end |
#can_black_castle_queenside ⇒ Object
Returns the value of attribute can_black_castle_queenside.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def can_black_castle_queenside @can_black_castle_queenside end |
#can_white_castle_kingside ⇒ Object
Returns the value of attribute can_white_castle_kingside.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def can_white_castle_kingside @can_white_castle_kingside end |
#fen_array ⇒ Object
attr_accessor :event, :site, :date, :eventdate, :round, :white, :black, :whiteelo, :blackelo, :result, :eco, :plycount, :fen
18 19 20 |
# File 'lib/pgn2fen.rb', line 18 def fen_array @fen_array end |
#fullmove ⇒ Object
Returns the value of attribute fullmove.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def fullmove @fullmove end |
#halfmove ⇒ Object
Returns the value of attribute halfmove.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def halfmove @halfmove end |
#pgn ⇒ Object
attr_accessor :event, :site, :date, :eventdate, :round, :white, :black, :whiteelo, :blackelo, :result, :eco, :plycount, :fen
18 19 20 |
# File 'lib/pgn2fen.rb', line 18 def pgn @pgn end |
#potential_enpassent_ply ⇒ Object
Returns the value of attribute potential_enpassent_ply.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def potential_enpassent_ply @potential_enpassent_ply end |
#promotion ⇒ Object
Returns the value of attribute promotion.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def promotion @promotion end |
#promotion_piece ⇒ Object
Returns the value of attribute promotion_piece.
19 20 21 |
# File 'lib/pgn2fen.rb', line 19 def promotion_piece @promotion_piece end |
Instance Method Details
#board_to_fen ⇒ Object
return fen representation from board
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/pgn2fen.rb', line 514 def board_to_fen fen = "" empty_square_counter = 0; @board.each_with_index { |tok, idx| if (idx % 8 == 0 && idx > 0) if empty_square_counter != 0 fen.concat(empty_square_counter.to_s) empty_square_counter = 0 end fen.concat("/") end if tok.empty? empty_square_counter = empty_square_counter + 1 else if empty_square_counter != 0 fen.concat(empty_square_counter.to_s) empty_square_counter = 0 end fen.concat(tok) end } # last squares could be empty if empty_square_counter != 0 fen.concat(empty_square_counter.to_s) empty_square_counter = 0 end fen end |
#clean_comments(pgn) ⇒ Object
347 348 349 |
# File 'lib/pgn2fen.rb', line 347 def clean_comments(pgn) remove_text_between_tokens_inclusive(pgn, OPEN_BRACE, CLOSE_BRACE) end |
#clean_pgn(pgn) ⇒ Object
clean pgn - remove comments and other unrequired text
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/pgn2fen.rb', line 331 def clean_pgn(pgn) pgn.strip! # clean result pgn.sub!("1-0", ""); pgn.sub!("0-1", ""); pgn.sub!("1/2-1/2", "") # clean mate and incomplete game marker pgn.gsub!("#", ""); pgn.gsub!("*", "") #remove all chessbase $ characters pgn.gsub!(/\$(\w+)/, "") #remove all comments - content within {} #pgn.gsub!(/(\{[^}]+\})+?/, "") pgn = clean_comments(pgn) #remove all subvariations - content within () #pgn.gsub!(/(\([^}]+\))+?/, "") pgn = clean_subvariations(pgn) end |
#clean_subvariations(pgn) ⇒ Object
351 352 353 |
# File 'lib/pgn2fen.rb', line 351 def clean_subvariations(pgn) remove_text_between_tokens_inclusive(pgn, OPEN_PAREN, CLOSE_PAREN) end |
#dark_square_by_number(square_number) ⇒ Object
257 258 259 |
# File 'lib/pgn2fen.rb', line 257 def dark_square_by_number square_number @@dark_squares.include?(square_number) end |
#dark_square_by_pgn(square_pgn) ⇒ Object
249 250 251 |
# File 'lib/pgn2fen.rb', line 249 def dark_square_by_pgn square_pgn @@dark_squares.include?(@@pgn_number_hash[square_pgn]) end |
#fen_array_from_plies(plies) ⇒ Object
get FEN array from plies
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/pgn2fen.rb', line 374 def fen_array_from_plies(plies) fen_array = [] fen_array << @board_start_fen ply_number = 1 #ply_number starts at 1 for regular game unless @fen.nil? if @board_color_from_fen == "w" ply_number = (@fullmove.to_i * 2) + 1 else ply_number = (@fullmove.to_i * 2) end end plies.each{|ply| #puts "ply=#{ply}, ply_number=#{ply_number}, move_number=#{ply_number/2 + 1}" fen_array << fen_for_ply(ply, ply_number) ply_number += 1 } fen_array end |
#fen_castle_text ⇒ Object
get FEN castle text based on castling availability
458 459 460 461 462 463 464 465 466 |
# File 'lib/pgn2fen.rb', line 458 def fen_castle_text text = "" if @can_white_castle_kingside; text.concat("K"); end if @can_white_castle_queenside; text.concat("Q"); end if @can_black_castle_kingside; text.concat("k"); end if @can_black_castle_queenside; text.concat("q"); end if text.empty?; text = "-"; end text end |
#fen_for_ply(ply, ply_number) ⇒ Object
get FEN from a ply and ply number
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/pgn2fen.rb', line 395 def fen_for_ply(ply, ply_number) #puts ply is_white = (ply_number % 2 != 0) @halfmove = @halfmove + 1 long_ply = short_ply_to_long_ply(ply, is_white) if long_ply.eql? "O-O" if is_white make_ply_on_board("e1g1") make_ply_on_board("h1f1") @can_white_castle_kingside = false @can_white_castle_queenside = false else make_ply_on_board("e8g8") make_ply_on_board("h8f8") @can_black_castle_kingside = false @can_black_castle_queenside = false end elsif long_ply.eql? "O-O-O" if is_white make_ply_on_board("e1c1") make_ply_on_board("a1d1") @can_white_castle_kingside = false @can_white_castle_queenside = false else make_ply_on_board("e8c8") make_ply_on_board("a8d8") @can_black_castle_kingside = false @can_black_castle_queenside = false end else # rest of moves if is_white if @can_white_castle_kingside && long_ply[0,2] == "h1"; @can_white_castle_kingside = false; end if @can_white_castle_queenside && long_ply[0,2] == "a1"; @can_white_castle_queenside = false; end else if @can_black_castle_kingside && long_ply[0,2] == "h8"; @can_white_castle_kingside = false; end if @can_black_castle_queenside && long_ply[0,2] == "a8"; @can_white_castle_queenside = false; end end make_ply_on_board(long_ply) end fen = board_to_fen fen.concat(" ").concat(is_white ? "b": "w") fen.concat(" ").concat(fen_castle_text) #enpassent square unless @potential_enpassent_ply.nil? fen.concat(" ").concat(@potential_enpassent_ply) @potential_enpassent_ply = nil else fen.concat(" ").concat("-") end fen.concat(" ").concat(@halfmove.to_s) if is_white fen.concat(" ").concat((@fullmove).to_s) else fen.concat(" ").concat((@fullmove += 1).to_s) end #pp_board fen end |
#file_for_hint(hint) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/pgn2fen.rb', line 222 def file_for_hint(hint) case hint when 'a' return @@aFile when 'b' return @@bFile when 'c' return @@cFile when 'd' return @@dFile when 'e' return @@eFile when 'f' return @@fFile when 'g' return @@gFile when 'h' return @@hFile else return nil end end |
#games_from_pgn(pgn) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/pgn2fen.rb', line 276 def games_from_pgn(pgn) games = [] pos_array = pgn.enum_for(:scan, /\[Event/).map{Regexp.last_match.begin(0)} pos_array.each_with_index {|pos, index| if (pos_array.size > index + 1) # all elements but last single_game_pgn = pgn[pos..pos_array[index+1]-1] else # last element single_game_pgn = pgn[pos..-1] end games << game_from_pgn(single_game_pgn) } games end |
#init_board ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/pgn2fen.rb', line 261 def init_board @board_start_fen.split(" ").first.each_char {|c| case c when 'r', 'n', 'b', 'q', 'k', 'p' @board << c when 'R', 'N', 'B', 'Q', 'K', 'P' @board << c when '1', '2', '3', '4', '5', '6', '7', '8' (0...c.to_i).each {|n| @board << "" } else # do nothing end } end |
#is_single_game ⇒ Object
check to verify PGN includes only a single game
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/pgn2fen.rb', line 292 def is_single_game count = @pgn_data.scan(/\[Event /).count #puts "Event count:#{count}" if count > 1; return false; end # result header also includes result, hence compare with 2 count = @pgn_data.scan(/1-0/).count #puts "1-0 count:#{count}" if count > 2; return false; end count = @pgn_data.scan(/0-1/).count #puts "0-1 count:#{count}" if count > 2; return false; end count = @pgn_data.scan(/1\/2-1\/2/).count #puts "1/2-1/2 count:#{count}" if count > 2; return false; end true end |
#light_square_by_number(square_number) ⇒ Object
253 254 255 |
# File 'lib/pgn2fen.rb', line 253 def light_square_by_number square_number @@light_squares.include?(square_number) end |
#light_square_by_pgn(square_pgn) ⇒ Object
245 246 247 |
# File 'lib/pgn2fen.rb', line 245 def light_square_by_pgn square_pgn @@light_squares.include?(@@pgn_number_hash[square_pgn]) end |
#make_ply_on_board(long_ply) ⇒ Object
make a ply on the board
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/pgn2fen.rb', line 492 def make_ply_on_board(long_ply) from_pgn = long_ply[0,2] to_pgn = long_ply[2,2] from_idx = @@pgn_number_hash[from_pgn] to_idx = @@pgn_number_hash[to_pgn] #puts "from_pgn:#{from_pgn}, to_pgn:#{to_pgn}, from_idx:#{from_idx}, to_idx:#{to_idx}" if @promotion if "P" == @board[from_idx] #white @board[to_idx] = @promotion_piece.upcase else #black @board[to_idx] = @promotion_piece.downcase end @promotion = false @promotion_piece = nil else #general case @board[to_idx] = @board[from_idx] end @board[from_idx] = "" end |
#parse_headers(headers) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/pgn2fen.rb', line 119 def parse_headers(headers) #puts headers headers.each {|header| key = HEADER_KEY_REGEX.match(header)[1] value = HEADER_VALUE_REGEX.match(header)[1] instance_variable_set(:"@#{key.downcase.to_sym}", value) } end |
#parse_pgn ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/pgn2fen.rb', line 128 def parse_pgn unless is_single_game raise Pgn2FenError, 'Only a single game PGN is supported right now.' end headers = [] @pgn_data.strip! pgn_data_array = @pgn_data.split("\n") pgn_data_array.each {|line| if line.index("[") == 0 headers << line else break end } parse_headers(headers) pgn_data_array = pgn_data_array[headers.size..-1] # get pgn @pgn = "" pgn_data_array.each {|line| @pgn = pgn.concat(line).concat(" ") } @pgn = clean_pgn(@pgn) #if fen does exist, use that as start board unless @fen.nil? tokens = @fen.split unless tokens.size == 6 raise Pgn2FenError, "Invalid FEN header #{@fen}" end @board_start_fen = @fen @board_color_from_fen = tokens[1] @fullmove = tokens[5].to_i = tokens[3] () else (true) end init_board @plies = plies_from_pgn(@pgn) @fen_array = fen_array_from_plies(@plies) self # allow chaining end |
#plies_from_pgn(pgn) ⇒ Object
get plies from pgn
315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/pgn2fen.rb', line 315 def plies_from_pgn(pgn) moves = pgn.split(/[0-9]+\./) moves.shift # remove first "" plies = [] moves.each {|move| move.strip! move.gsub!(".", "") move.split(" ").each {|ply| plies << ply } } plies end |
#pp_board ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/pgn2fen.rb', line 543 def pp_board str = "" @board.each_with_index { |sq, idx| if sq.empty?; sq = "*"; end if (idx % 8 == 0 && idx != 0); str << "\n"; end str << sq << " " } puts "= = = = = = = =" puts str puts "= = = = = = = =\n\n\n" end |
#rank_for_hint(hint) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/pgn2fen.rb', line 199 def rank_for_hint(hint) case hint when '1' return @@firstRank when '2' return @@secondRank when '3' return @@thirdRank when '4' return @@fourthRank when '5' return @@fifthRank when '6' return @@sixthRank when '7' return @@seventhRank when '8' return @@eighthRank else return nil end end |
#remove_text_between_tokens_inclusive(text, open_token, close_token) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/pgn2fen.rb', line 355 def remove_text_between_tokens_inclusive(text, open_token, close_token) open_token_count = 0 new_text = "" text.split("").each {|c| if c == open_token open_token_count += 1 elsif c == close_token open_token_count -= 1 else if open_token_count == 0 new_text << c end end } new_text end |
#reset_castle_options(option) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/pgn2fen.rb', line 111 def (option) @can_white_castle_kingside = option @can_white_castle_queenside = option @can_black_castle_kingside = option @can_black_castle_queenside = option end |
#short_ply_to_long_ply(ply, is_white) ⇒ Object
convert short ply to long ply
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'lib/pgn2fen.rb', line 557 def short_ply_to_long_ply(ply, is_white) from_idx = -1 to_idx = -1 from_pgn = "" to_pgn = "" hint = nil if ply.eql?("O-O"); return ply; end if ply.eql?("O-O-O"); return ply; end unless ply.index('+').nil?; ply.sub!("+", ""); end unless ply.index('x').nil?; ply.sub!("x", ""); @halfmove = 0; end unless ply.index('=').nil? @promotion = true @promotion_piece = ply[-1] ply = ply.chop.chop end if ply.length == 2 # pawn non-capture to_pgn = ply to_idx = @@pgn_number_hash[to_pgn] if is_white if !@board[to_idx+8].empty? from_idx = to_idx+8 else from_idx = to_idx+16 # if @board[to_idx-1].eql?"p" or @board[to_idx+1].eql?"p" @potential_enpassent_ply = @@number_pgn_hash[from_idx-8] # end end else # isBlack if !@board[to_idx-8].empty? from_idx = to_idx-8 else from_idx = to_idx-16 # if @board[to_idx-1].eql?"P" or @board[to_idx+1].eql?"P" @potential_enpassent_ply = @@number_pgn_hash[from_idx+8] # end end end from_pgn = @@number_pgn_hash[from_idx] @halfmove = 0 return from_pgn + to_pgn end if ('a'..'h').include?(ply[0]) && ply.length == 3 #pawn capture, non-enpassent to_pgn = ply[1..-1] to_idx = @@pgn_number_hash[to_pgn] if is_white if @board[to_idx+7].eql?("P") && file_for_hint(ply[0]).include?(to_idx+7) from_idx = to_idx+7 elsif @board[to_idx+9].eql?("P")&& file_for_hint(ply[0]).include?(to_idx+9) from_idx = to_idx+9 end else #is_black if @board[to_idx-7].eql?("p") && file_for_hint(ply[0]).include?(to_idx-7) from_idx = to_idx-7 elsif @board[to_idx-9].eql?("p") && file_for_hint(ply[0]).include?(to_idx-9) from_idx = to_idx-9 end end if from_idx == -1; raise Pgn2FenError, "Error parsing pawn capture at ply #{ply}"; end from_pgn = @@number_pgn_hash[from_idx] @halfmove = 0 return from_pgn + to_pgn end if ply.length == 3; to_pgn = ply[1,2]; end if ply.length() == 4 to_pgn = ply[2,2] hint = ply[1] end to_idx = @@pgn_number_hash[to_pgn] if ply[0].downcase.eql?('r'); return short_ply_to_long_ply_for_rook(to_idx, to_pgn, hint, is_white); end if ply[0].downcase.eql?('n'); return short_ply_to_long_ply_for_knight(to_idx, to_pgn, hint, is_white); end if ply[0].downcase.eql?('b'); return short_ply_to_long_ply_for_bishop(to_idx, to_pgn, hint, is_white); end if ply[0].downcase.eql?('q'); return short_ply_to_long_ply_for_queen(to_idx, to_pgn, hint, is_white); end if ply[0].downcase.eql?('k'); return short_ply_to_long_ply_for_king(to_idx, to_pgn, hint, is_white); end return from_pgn + to_pgn end |
#short_ply_to_long_ply_for_bishop(to_idx, to_pgn, hint, is_white) ⇒ Object
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 |
# File 'lib/pgn2fen.rb', line 763 def short_ply_to_long_ply_for_bishop(to_idx, to_pgn, hint, is_white) from_idx = -1 from_pgn = "" piece = is_white ? "B" : "b" is_light = light_square_by_number(to_idx) ? true : false if (!hint.nil?) if @@one_thru_eight.include?(hint) from_pgn = to_pgn[0,1] + h end if @@a_thru_h.include?(hint) from_pgn = h + to_pgn[1,1] end return from_pgn + to_pgn else # check nw direction nw = to_idx while(nw > -1) do nw = nw - 9 if (nw < 0) break end # puts "nw=#{nw}" if light_square_by_number(nw) != is_light # square colors don't match - overflow break elsif @board[nw].eql?(piece) from_idx = nw from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[nw].eql?("") next else break end end # check ne direction ne = to_idx while(ne > 0) do ne = ne - 7 if (ne < 1) break end # puts "ne=#{ne}" if light_square_by_number(ne) != is_light # square colors don't match - overflow break elsif @board[ne].eql?(piece) from_idx = ne from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[ne].eql?("") next else break end end # check sw direction sw = to_idx while(sw < 63) do sw = sw + 7 if (sw > 62) break end # puts "sw=#{sw}" if light_square_by_number(sw) != is_light # square colors don't match - overflow break elsif @board[sw].eql?(piece) from_idx = sw from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[sw].eql?("") next else break end end # check se direction se = to_idx while(se < 64) do se = se + 9 if (se > 63) break end # puts "se=#{se}" if light_square_by_number(se) != is_light # square colors don't match - overflow break elsif @board[se].eql?(piece) from_idx = se from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[se].eql?("") next else break end end end return from_pgn + to_pgn end |
#short_ply_to_long_ply_for_king(to_idx, to_pgn, hint, is_white) ⇒ Object
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
# File 'lib/pgn2fen.rb', line 1037 def short_ply_to_long_ply_for_king(to_idx, to_pgn, hint, is_white) from_idx = -1 from_pgn = "" if is_white @board.reverse.each_with_index {|i,idx| if i.eql?("K"); from_idx = 63 - idx; break; end } else @board.each_with_index {|i,idx| if i.eql?("k"); from_idx = idx; break; end } end from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn end |
#short_ply_to_long_ply_for_knight(to_idx, to_pgn, hint, is_white) ⇒ Object
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/pgn2fen.rb', line 722 def short_ply_to_long_ply_for_knight(to_idx, to_pgn, hint, is_white) from_idx = -1 from_pgn = "" piece = is_white ? "N" : "n" if !hint.nil? # -17,-15, 10, -6, 6, 10, 15, 17 are possible knight moves knight_moves = [17+to_idx, 17+to_idx, -15+to_idx, 15+to_idx, -10+to_idx, 10+to_idx, -6+to_idx, 6+to_idx] if @@one_thru_eight.include?(hint) rank = rank_for_hint(hint) knight_moves = knight_moves & rank.to_a # intersection end if @@a_thru_h.include?(hint) file = file_for_hint(hint) knight_moves = knight_moves & file.to_a #intersection end knight_moves.each {|idx| if @board[idx].eql?(piece) from_idx = idx from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn end } else # -17,-15, 10, -6, 6, 10, 15, 17 are possible knight moves knight_moves = [-17, 17, -15, 15, -10, 10, -6, 6] knight_moves.each {|i| idx = to_idx + i if (idx < 0 && idx > 63) next end if @board[idx].eql?(piece) from_idx = idx from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn end } end #return from_pgn + to_pgn raise Pgn2FenError, "Error parsing knight move to square #{to_pgn}" end |
#short_ply_to_long_ply_for_queen(to_idx, to_pgn, hint, is_white) ⇒ Object
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 |
# File 'lib/pgn2fen.rb', line 861 def short_ply_to_long_ply_for_queen(to_idx, to_pgn, hint, is_white) # check bishop type moves from_idx = -1 from_pgn = "" piece = is_white ? "Q" : "q" is_light = light_square_by_number(to_idx) ? true : false if (!hint.nil?) if @@one_thru_eight.include?(hint) from_pgn = to_pgn[0,1] + hint end if @@a_thru_h.include?(hint) from_pgn = hint + to_pgn[1,1] end return from_pgn + to_pgn else # check nw direction nw = to_idx while(nw > -1) do nw = nw - 9 if (nw < 0) break end if light_square_by_number(nw) != is_light # square colors don't match - overflow break elsif @board[nw].eql?(piece) from_idx = nw from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[nw].eql?("") next else break end end # check ne direction ne = to_idx while(ne > 0) do ne = ne - 7 if (ne < 1) break end if light_square_by_number(ne) != is_light # square colors don't match - overflow break elsif @board[ne].eql?(piece) from_idx = ne from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[ne].eql?("") next else break end end # check sw direction sw = to_idx while(sw < 63) do sw = sw + 7 if (sw > 62) break end if light_square_by_number(sw) != is_light # square colors don't match - overflow break elsif @board[sw].eql?(piece) from_idx = sw from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[sw].eql?("") next else break end end # check se direction se = to_idx while(se < 64) do se = se + 9 if (se > 63) break end if light_square_by_number(se) != is_light # square colors don't match - overflow break elsif @board[se].eql?(piece) from_idx = se from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[se].eql?("") next else break end end end if (from_pgn.length == 2) return from_pgn + to_pgn end # check rook type moves if !hint.nil? if @@one_thru_eight.include?(hint) from_pgn = to_pgn[0,1] + hint end if @@a_thru_h.include?(hint) from_pgn = hint + to_pgn[1,1] end return from_pgn + to_pgn else # no hint # check file up = to_idx while up > -1 do up = up - 8 if (up < 0) break end if @board[up].eql?(piece) from_idx = up from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[up].eql?("") next else break end end down = to_idx while down < 64 do down = down + 8 if down > 63 break end if @board[down].eql?(piece) from_idx = down from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[down].eql?("") next else break end end # check rank left = to_idx while left > -1 do left = left - 1 if left % 8 == 7 break end if @board[left].eql?(piece) from_idx = left from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[left].eql?("") next else break end end right = to_idx while right < 64 do right = right + 1 if right % 8 == 0 break end if @board[right].eql?(piece) from_idx = right from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[right].eql?("") next else break end end end from_pgn + to_pgn end |
#short_ply_to_long_ply_for_rook(to_idx, to_pgn, hint, is_white) ⇒ Object
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
# File 'lib/pgn2fen.rb', line 638 def short_ply_to_long_ply_for_rook(to_idx, to_pgn, hint, is_white) from_idx = -1 from_pgn = "" piece = is_white ? "R" : "r" if !hint.nil? if @@one_thru_eight.include?(hint) from_pgn = to_pgn[0,1] + hint end if @@a_thru_h.include?(hint) from_pgn = hint + to_pgn[1,1] end return from_pgn + to_pgn else # no hint # check file up = to_idx while up > -1 do up = up - 8 if (up < 0) break end if @board[up].eql?(piece) from_idx = up from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[up].eql?("") next else break end end down = to_idx while down < 64 do down = down + 8 if down > 63 break end if @board[down].eql?(piece) from_idx = down from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[down].eql?("") next else break end end # check rank left = to_idx while left > -1 do left = left - 1 if left % 8 == 7 break end if @board[left].eql?(piece) from_idx = left from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[left].eql?("") next else break end end right = to_idx while right < 64 do right = right + 1 if right % 8 == 0 break end if @board[right].eql?(piece) from_idx = right from_pgn = @@number_pgn_hash[from_idx] return from_pgn + to_pgn elsif @board[right].eql?("") next else break end end end from_pgn + to_pgn end |
#to_s ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/pgn2fen.rb', line 172 def to_s str = "" str << "Event: #{@event}\n" str << "Site: #{@site}\n" str << "Date: #{@date}\n" str << "EventDate: #{@eventdate}\n" str << "Round: #{@round}\n" str << "White: #{@white}\n" str << "Black: #{@black}\n" str << "WhiteElo: #{@whiteElo}\n" str << "BlackElo: #{@blackElo}\n" str << "Result: #{@result}\n" str << "Eco: #{@eco}\n" str << "Plycount: #{@plycount}\n" str << "FEN: #{@fen}\n" str << "PGN: #{@pgn}\n" str << "Plies: #{@plies}\n" str << "FEN Array:\n" unless @fen_array.nil? @fen_array.each {|fen| str << fen << "\n" } end str end |
#update_castle_options_from_fen(castle_options) ⇒ Object
update castling options from FEN header
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/pgn2fen.rb', line 470 def (false) if == "-" return end tokens = .split("") tokens.each {|token| case token when "K" @can_white_castle_kingside = true when "Q" @can_white_castle_queenside = true when "k" @can_black_castle_kingside = true when "q" @can_black_castle_queenside = true end } end |