bchess 0.1.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46328051cb5767623ccfc2b65865008bfd3cab4a
4
- data.tar.gz: 0d53222053172c2c710a343d19f0738470b8dac0
3
+ metadata.gz: 370d493302b50f3cd02153f733ab46e43fd27727
4
+ data.tar.gz: 0436a659709ec9710be92eaa3f54747f6359eda6
5
5
  SHA512:
6
- metadata.gz: 89624bd2c2b9b8860d3f45cae48264f85010496081a7d8becf9bce98491004912734c582e4f87ee737e4c54ec2de81ce6d8ec8e7464240bb7a6c751ffeeddd95
7
- data.tar.gz: 401035c5490e48d066b15871db2e082b4336e93d56df9bcaa523f6de1a1bd9c3985f4a44aa92d3261adcf70460a563121c61424acaa4263011a1c41ba2f26e6d
6
+ metadata.gz: d824f7dd402d8c83a327f31ab0d63acd88fd0063277897395a402720fd0f23e2def762d0a182747fae6b36a6818fc67806ed8e63738ac55b1967dd106c4c801f
7
+ data.tar.gz: c3758e3249590c239c537fc36f1a3b526e7089067685cc136bdbb3fa5f612a099299151584fe03f3fe33f910b74c72925e0a8a5df123aeadcff3d6faaf4a6f59
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in bchess.gemspec
6
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bchess (0.1.1)
4
+ bchess (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bchess.gemspec CHANGED
@@ -1,29 +1,28 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "bchess/version"
3
+ require 'bchess/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "bchess"
6
+ spec.name = 'bchess'
8
7
  spec.version = Bchess::VERSION
9
- spec.authors = ["Staszek Zawadzki"]
10
- spec.email = ["s.zawadzki@visuality.pl"]
8
+ spec.authors = ['Staszek Zawadzki']
9
+ spec.email = ['s.zawadzki@visuality.pl']
11
10
 
12
- spec.summary = %q{Parsing chess PGN games.}
13
- spec.description = %q{Parsing and validating a chess games and returning as chess game objects}
14
- spec.homepage = ""
15
- spec.license = "MIT"
11
+ spec.summary = 'Parsing chess PGN games.'
12
+ spec.description = 'Parsing and validating a chess games and returning as chess game objects'
13
+ spec.homepage = 'https://github.com/visualitypl/bchess'
14
+ spec.license = 'MIT'
16
15
 
17
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
17
  f.match(%r{^(test|spec|features)/})
19
18
  end
20
- spec.bindir = "exe"
19
+ spec.bindir = 'exe'
21
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ["lib"]
21
+ spec.require_paths = ['lib']
23
22
 
24
- spec.add_development_dependency "bundler", "~> 1.16"
25
- spec.add_development_dependency "rake", "~> 10.0"
26
- spec.add_development_dependency "rspec", "~> 3.0"
27
- spec.add_development_dependency "treetop", "~> 1.6"
28
- spec.add_development_dependency "pry"
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'pry'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'treetop', '~> 1.6'
29
28
  end
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "bchess"
3
+ require 'bundler/setup'
4
+ require 'bchess'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "bchess"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
data/lib/bchess.rb CHANGED
@@ -1,33 +1,33 @@
1
- require "bchess/helpers/board_helpers"
2
- require "bchess/helpers/fen_helpers"
3
- require "bchess/helpers/board_helpers"
4
- require "bchess/helpers/castle_helpers"
5
- require "bchess/helpers/validations"
6
- require "bchess/helpers/en_passant_helpers"
7
- require "bchess/helpers/field_between_helpers"
1
+ require 'bchess/helpers/board_helpers'
2
+ require 'bchess/helpers/fen_helpers'
3
+ require 'bchess/helpers/board_helpers'
4
+ require 'bchess/helpers/castle_helpers'
5
+ require 'bchess/helpers/validations'
6
+ require 'bchess/helpers/en_passant_helpers'
7
+ require 'bchess/helpers/field_between_helpers'
8
8
 
9
- require "bchess/version"
10
- require "bchess/piece"
11
- require "bchess/king"
12
- require "bchess/rook"
13
- require "bchess/bishop"
14
- require "bchess/queen"
15
- require "bchess/knight"
16
- require "bchess/pawn"
17
- require "bchess/board"
9
+ require 'bchess/version'
10
+ require 'bchess/piece'
11
+ require 'bchess/king'
12
+ require 'bchess/rook'
13
+ require 'bchess/bishop'
14
+ require 'bchess/queen'
15
+ require 'bchess/knight'
16
+ require 'bchess/pawn'
17
+ require 'bchess/board'
18
18
 
19
- require "pgn/parser"
20
- require "pgn/pgn_file"
21
- require "pgn/pgn_nodes"
22
- require "pgn/game"
23
- require "pgn/game_header"
24
- require "pgn/game_body"
25
- require "pgn/move_info_parser"
19
+ require 'pgn/parser'
20
+ require 'pgn/pgn_file'
21
+ require 'pgn/pgn_nodes'
22
+ require 'pgn/game'
23
+ require 'pgn/game_header'
24
+ require 'pgn/game_body'
25
+ require 'pgn/move_info_parser'
26
26
 
27
27
  module Bchess
28
28
  class InvalidMoveException < StandardError; end
29
29
 
30
- WHITE = 'w'
31
- BLACK = 'b'
32
- START_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
30
+ WHITE = 'w'.freeze
31
+ BLACK = 'b'.freeze
32
+ START_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'.freeze
33
33
  end
data/lib/bchess/board.rb CHANGED
@@ -20,6 +20,7 @@ module Bchess
20
20
 
21
21
  def move(piece, column, row, promoted_piece = nil)
22
22
  return false if invalid_data?(piece, column, row)
23
+
23
24
  @en_passant = '-' unless en_passant_detected?(piece, column, row)
24
25
 
25
26
  if castle_detected?(piece, column)
@@ -30,9 +31,9 @@ module Bchess
30
31
  validate_en_passant(piece, column, row) && execute_en_passant(piece, column, row)
31
32
  elsif pawn_long_move_detected?(piece, row)
32
33
  long_pawn_move(piece, column, row)
33
- elsif piece.can_move_to_field?(column,row)
34
+ elsif piece.can_move_to_field?(column, row)
34
35
  piece.move(column, row)
35
- elsif piece.can_take_on_field?(column,row)
36
+ elsif piece.can_take_on_field?(column, row)
36
37
  piece.move(column, row)
37
38
  else
38
39
  return false
@@ -45,7 +46,7 @@ module Bchess
45
46
  end
46
47
 
47
48
  def get_possible_pieces(info)
48
- @pieces.select{|p| p.can_make_move?(info, self)}
49
+ @pieces.select { |p| p.can_make_move?(info, self) }
49
50
  end
50
51
 
51
52
  def update_info(piece, column, row)
@@ -57,7 +58,8 @@ module Bchess
57
58
  end
58
59
 
59
60
  def execute_promotion(piece, column, row, promoted_piece)
60
- raise RuntimeError.new("Promotion Not Specified") if promoted_piece.nil? || !(promoted_piece < Bchess::Piece)
61
+ raise 'Promotion Not Specified' if promoted_piece.nil? || !(promoted_piece < Bchess::Piece)
62
+
61
63
  pieces.delete(piece)
62
64
  promoted = promoted_piece.new(piece.color, column, row)
63
65
  promoted.moved = true if promoted_piece == Bchess::Rook
@@ -69,11 +71,11 @@ module Bchess
69
71
  end
70
72
 
71
73
  def at(column, row)
72
- pieces.select{|p| p.row == row && p.column == column}.first
74
+ pieces.select { |p| p.row == row && p.column == column }.first
73
75
  end
74
76
 
75
77
  def read_fen
76
- fen_pieces, fen_colors, fen_castles, fen_en_passant, fen_halfmove_clock, fen_move_number = fen.strip.split(" ")
78
+ fen_pieces, fen_colors, fen_castles, fen_en_passant, fen_halfmove_clock, fen_move_number = fen.strip.split(' ')
77
79
  set_pieces(fen_pieces)
78
80
  set_to_move(fen_colors)
79
81
  set_castles(fen_castles)
@@ -84,13 +86,13 @@ module Bchess
84
86
 
85
87
  def print
86
88
  puts 'Pieces WHITE: '
87
- puts _pieces(Bchess::WHITE).map{|p| p.to_s }.join(', ')
89
+ puts _pieces(Bchess::WHITE).map(&:to_s).join(', ')
88
90
  puts 'Pieces BLACK: '
89
- puts _pieces(Bchess::BLACK).map{|p| p.to_s }.join(', ')
91
+ puts _pieces(Bchess::BLACK).map(&:to_s).join(', ')
90
92
  end
91
93
 
92
94
  def king(color)
93
- @pieces.select{ |p| p.class == Bchess::King && p.color == color }.first
95
+ @pieces.select { |p| p.class == Bchess::King && p.color == color }.first
94
96
  end
95
97
 
96
98
  def change_to_move
@@ -100,7 +102,7 @@ module Bchess
100
102
  private
101
103
 
102
104
  def remove_old_piece(column, row, color)
103
- taken_piece = _other_pieces(color).select{|p| p.row == row && p.column == column}.first
105
+ taken_piece = _other_pieces(color).select { |p| p.row == row && p.column == column }.first
104
106
  pieces.delete(taken_piece)
105
107
  end
106
108
 
@@ -112,20 +114,20 @@ module Bchess
112
114
  _king = king(color)
113
115
  _pieces = _other_pieces(color)
114
116
 
115
- _pieces.any?{|p| attacks?(p, _king)}
117
+ _pieces.any? { |p| attacks?(p, _king) }
116
118
  end
117
119
 
118
120
  def attacks?(piece, king)
119
121
  piece.can_take_on_field?(king.column, king.row) &&
120
- piece.fields_between(king.column, king.row).none?{|f| at(*f)}
122
+ piece.fields_between(king.column, king.row).none? { |f| at(*f) }
121
123
  end
122
124
 
123
125
  def _pieces(color)
124
- @pieces.select{ |p| p.color == color }
126
+ @pieces.select { |p| p.color == color }
125
127
  end
126
128
 
127
129
  def _other_pieces(color)
128
- @pieces.select{ |p| p.color != color }
130
+ @pieces.reject { |p| p.color == color }
129
131
  end
130
132
  end
131
133
  end
data/lib/bchess/game.rb CHANGED
@@ -2,7 +2,7 @@ module Bchess
2
2
  class Game
3
3
  attr_reader :fen, :board, :moves
4
4
 
5
- def initialize(fen='')
5
+ def initialize(fen = '')
6
6
  @fen = fen
7
7
  @board = Board.new(fen)
8
8
  @moves = []
@@ -1,27 +1,27 @@
1
1
  module Bchess
2
2
  module BoardHelpers
3
3
  def field(column, row)
4
- (column+97).chr + (row+1).to_s
4
+ (column + 97).chr + (row + 1).to_s
5
5
  end
6
6
 
7
7
  def invalid_data?(piece, column, row)
8
- piece.nil? || !(0..7).include?(column) || !(0..7).include?(row)
8
+ piece.nil? || !(0..7).cover?(column) || !(0..7).cover?(row)
9
9
  end
10
10
 
11
11
  def castle_detected?(piece, column)
12
- piece.kind_of?(Bchess::King) && piece.column == 4 && (column - piece.column).abs == 2
12
+ piece.is_a?(Bchess::King) && piece.column == 4 && (column - piece.column).abs == 2
13
13
  end
14
14
 
15
15
  def promotion_detected?(piece, row)
16
- piece.kind_of?(Bchess::Pawn) && (row == 0 || row == 7)
16
+ piece.is_a?(Bchess::Pawn) && (row == 0 || row == 7)
17
17
  end
18
18
 
19
19
  def en_passant_detected?(piece, column, row)
20
- piece.kind_of?(Bchess::Pawn) && piece.row != row && piece.column != column && at(column, row).nil? && piece.can_take_on_field?(column, row)
20
+ piece.is_a?(Bchess::Pawn) && piece.row != row && piece.column != column && at(column, row).nil? && piece.can_take_on_field?(column, row)
21
21
  end
22
22
 
23
23
  def pawn_long_move_detected?(piece, row)
24
- piece.kind_of?(Bchess::Pawn) && (piece.row - row).abs == 2
24
+ piece.is_a?(Bchess::Pawn) && (piece.row - row).abs == 2
25
25
  end
26
26
 
27
27
  def kings_present?
@@ -37,7 +37,7 @@ module Bchess
37
37
  end
38
38
 
39
39
  def field(column, row)
40
- (column+97).chr + (row+1).to_s
40
+ (column + 97).chr + (row + 1).to_s
41
41
  end
42
42
  end
43
43
  end
@@ -1,6 +1,6 @@
1
1
  module EnPassantHelpers
2
- def validate_en_passant(piece, column, row)
3
- #TODO
2
+ def validate_en_passant(_piece, _column, _row)
3
+ # TODO
4
4
  true
5
5
  end
6
6
 
@@ -17,6 +17,6 @@ module EnPassantHelpers
17
17
  def long_pawn_move(piece, column, row)
18
18
  piece.move(column, row)
19
19
  direction = piece.color == Bchess::WHITE ? 1 : -1
20
- @en_passant = field(column, (row + piece.row)/2 - direction)
20
+ @en_passant = field(column, (row + piece.row) / 2 - direction)
21
21
  end
22
22
  end
@@ -2,25 +2,25 @@ module Bchess
2
2
  module FenHelpers
3
3
  def fen_hash
4
4
  {
5
- 'k': {klass: Bchess::King, color: Bchess::BLACK},
6
- 'q': {klass: Bchess::Queen, color: Bchess::BLACK},
7
- 'r': {klass: Bchess::Rook, color: Bchess::BLACK},
8
- 'b': {klass: Bchess::Bishop, color: Bchess::BLACK},
9
- 'n': {klass: Bchess::Knight, color: Bchess::BLACK},
10
- 'p': {klass: Bchess::Pawn, color: Bchess::BLACK},
11
- 'K': {klass: Bchess::King, color: Bchess::WHITE},
12
- 'Q': {klass: Bchess::Queen, color: Bchess::WHITE},
13
- 'R': {klass: Bchess::Rook, color: Bchess::WHITE},
14
- 'B': {klass: Bchess::Bishop, color: Bchess::WHITE},
15
- 'N': {klass: Bchess::Knight, color: Bchess::WHITE},
16
- 'P': {klass: Bchess::Pawn, color: Bchess::WHITE},
5
+ 'k': { klass: Bchess::King, color: Bchess::BLACK },
6
+ 'q': { klass: Bchess::Queen, color: Bchess::BLACK },
7
+ 'r': { klass: Bchess::Rook, color: Bchess::BLACK },
8
+ 'b': { klass: Bchess::Bishop, color: Bchess::BLACK },
9
+ 'n': { klass: Bchess::Knight, color: Bchess::BLACK },
10
+ 'p': { klass: Bchess::Pawn, color: Bchess::BLACK },
11
+ 'K': { klass: Bchess::King, color: Bchess::WHITE },
12
+ 'Q': { klass: Bchess::Queen, color: Bchess::WHITE },
13
+ 'R': { klass: Bchess::Rook, color: Bchess::WHITE },
14
+ 'B': { klass: Bchess::Bishop, color: Bchess::WHITE },
15
+ 'N': { klass: Bchess::Knight, color: Bchess::WHITE },
16
+ 'P': { klass: Bchess::Pawn, color: Bchess::WHITE }
17
17
  }
18
18
  end
19
19
 
20
20
  def write_fen
21
21
  result = ''
22
22
  7.downto(0) do |i|
23
- line_pieces = pieces.select{ |p| p.row == i }
23
+ line_pieces = pieces.select { |p| p.row == i }
24
24
  one_line = create_fen_line(line_pieces)
25
25
  result << one_line
26
26
  result << '/' unless i == 0
@@ -33,7 +33,7 @@ module Bchess
33
33
  counter = 0
34
34
 
35
35
  0.upto(7) do |i|
36
- piece = pieces.select{ |p| p.column == i }.first
36
+ piece = pieces.select { |p| p.column == i }.first
37
37
  if !!piece
38
38
  if counter > 0
39
39
  line.concat(counter.to_s)
@@ -52,13 +52,13 @@ module Bchess
52
52
 
53
53
  def set_pieces(board)
54
54
  pieces.clear
55
- board.split("/").each_with_index do |line, index|
55
+ board.split('/').each_with_index do |line, index|
56
56
  column = 0
57
57
  line.each_char do |char|
58
58
  if char.to_i != 0
59
59
  column += char.to_i - 1
60
60
  else
61
- pieces << fen_hash[char.to_sym][:klass].new(fen_hash[char.to_sym][:color], column, 7-index)
61
+ pieces << fen_hash[char.to_sym][:klass].new(fen_hash[char.to_sym][:color], column, 7 - index)
62
62
  end
63
63
  column += 1
64
64
  end
@@ -82,15 +82,15 @@ module Bchess
82
82
  end
83
83
 
84
84
  def to_fen(piece)
85
- fen_hash.key({ :klass => piece.class, :color => piece.color }).to_s
85
+ fen_hash.key(klass: piece.class, color: piece.color).to_s
86
86
  end
87
87
 
88
88
  def change_halfmove_clock(piece)
89
- if piece.kind_of?(Bchess::Pawn)
90
- @halfmove_clock = 0
91
- else
92
- @halfmove_clock = halfmove_clock + 1
93
- end
89
+ @halfmove_clock = if piece.is_a?(Bchess::Pawn)
90
+ 0
91
+ else
92
+ halfmove_clock + 1
93
+ end
94
94
  end
95
95
 
96
96
  def update_castles_after_move(piece)
@@ -104,9 +104,9 @@ module Bchess
104
104
 
105
105
  def update_castles_after_king_move(color)
106
106
  if color == Bchess::WHITE
107
- @castles.gsub!('K', '').gsub!('Q', '')
107
+ @castles.delete!('K').delete!('Q')
108
108
  else
109
- @castles.gsub!('k', '').gsub!('q', '')
109
+ @castles.delete!('k').delete!('q')
110
110
  end
111
111
  end
112
112
 
@@ -1,12 +1,12 @@
1
1
  module FieldBetweenHelpers
2
- def row_fields(dcolumn, drow)
2
+ def row_fields(dcolumn, _drow)
3
3
  smaller, bigger = [column, dcolumn].sort
4
- (smaller+1..bigger-1).map{|c| [c, row] }
4
+ (smaller + 1..bigger - 1).map { |c| [c, row] }
5
5
  end
6
6
 
7
- def column_fields(dcolumn, drow)
7
+ def column_fields(_dcolumn, drow)
8
8
  smaller, bigger = [row, drow].sort
9
- (smaller+1..bigger-1).map{|r| [column, r] }
9
+ (smaller + 1..bigger - 1).map { |r| [column, r] }
10
10
  end
11
11
 
12
12
  def diagonal_fields(dcolumn, drow)
@@ -14,21 +14,21 @@ module FieldBetweenHelpers
14
14
  if dcolumn > column
15
15
  if drow > row
16
16
  (dcolumn - column - 1).times do |i|
17
- fields << [dcolumn-(i+1), drow-(i+1)]
17
+ fields << [dcolumn - (i + 1), drow - (i + 1)]
18
18
  end
19
19
  else
20
20
  (dcolumn - column - 1).times do |i|
21
- fields << [dcolumn-(i+1), drow+(i+1)]
21
+ fields << [dcolumn - (i + 1), drow + (i + 1)]
22
22
  end
23
23
  end
24
24
  else
25
25
  if drow > row
26
26
  (column - dcolumn - 1).times do |i|
27
- fields << [dcolumn+(i+1), drow-(i+1)]
27
+ fields << [dcolumn + (i + 1), drow - (i + 1)]
28
28
  end
29
29
  else
30
30
  (column - dcolumn - 1).times do |i|
31
- fields << [dcolumn+(i+1), drow+(i+1)]
31
+ fields << [dcolumn + (i + 1), drow + (i + 1)]
32
32
  end
33
33
  end
34
34
  end
@@ -37,6 +37,7 @@ module FieldBetweenHelpers
37
37
 
38
38
  def fields_between(dcolumn, drow)
39
39
  return [] unless can_move_to_field?(dcolumn, drow)
40
+
40
41
  if same_row?(drow)
41
42
  row_fields(dcolumn, drow)
42
43
  elsif same_column?(dcolumn)
data/lib/bchess/king.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Bchess
2
2
  class King < Piece
3
-
4
3
  KINGS_REACH = 1
5
4
 
6
5
  attr_reader :moved
@@ -24,7 +23,7 @@ module Bchess
24
23
  (
25
24
  by_line(dcolumn, drow, KINGS_REACH) ||
26
25
  by_diagonal(dcolumn, drow, KINGS_REACH)
27
- )
26
+ )
28
27
  end
29
28
  end
30
29
  end
data/lib/bchess/knight.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Bchess
2
2
  class Knight < Piece
3
-
4
3
  def initiialize(*args)
5
4
  super(args)
6
5
  end
@@ -13,7 +12,7 @@ module Bchess
13
12
  super && by_jump(dcolumn, drow)
14
13
  end
15
14
 
16
- def fields_between(column, row)
15
+ def fields_between(_column, _row)
17
16
  []
18
17
  end
19
18
 
data/lib/bchess/pawn.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Bchess
2
2
  class Pawn < Piece
3
-
4
3
  def initiialize(*args)
5
4
  super(args)
6
5
  end
@@ -35,11 +34,11 @@ module Bchess
35
34
  column == dcolumn
36
35
  end
37
36
 
38
- def direction_kept?(dcolumn, drow)
37
+ def direction_kept?(_dcolumn, drow)
39
38
  white? ? drow > row : row > drow
40
39
  end
41
40
 
42
- def row_diff?(dcolumn, drow)
41
+ def row_diff?(_dcolumn, drow)
43
42
  (row - drow).abs <= (starting_position? ? 2 : 1)
44
43
  end
45
44
 
@@ -48,7 +47,7 @@ module Bchess
48
47
  end
49
48
 
50
49
  def pawn_position?
51
- (1..6).include?(row)
50
+ (1..6).cover?(row)
52
51
  end
53
52
  end
54
53
  end
data/lib/bchess/queen.rb CHANGED
@@ -14,8 +14,8 @@ module Bchess
14
14
  super &&
15
15
  (
16
16
  by_line(dcolumn, drow, QUEEN_REACH) ||
17
- by_diagonal(dcolumn, drow, QUEEN_REACH)
18
- )
17
+ by_diagonal(dcolumn, drow, QUEEN_REACH)
18
+ )
19
19
  end
20
20
  end
21
21
  end
@@ -1,3 +1,3 @@
1
1
  module Bchess
2
- VERSION = "0.1.1"
2
+ VERSION = '0.1.2'.freeze
3
3
  end
@@ -8,43 +8,43 @@ module Bchess
8
8
  end
9
9
 
10
10
  def player_white
11
- @player_white ||= values["White"]
11
+ @player_white ||= values['White']
12
12
  end
13
13
 
14
14
  def player_black
15
- @player_black ||= values["Black"]
15
+ @player_black ||= values['Black']
16
16
  end
17
17
 
18
18
  def elo_white
19
- @elo_white ||= values["WhiteElo"]
19
+ @elo_white ||= values['WhiteElo']
20
20
  end
21
21
 
22
22
  def elo_black
23
- @elo_black ||= values["BlackElo"]
23
+ @elo_black ||= values['BlackElo']
24
24
  end
25
25
 
26
26
  def event
27
- @event ||= values["Event"]
27
+ @event ||= values['Event']
28
28
  end
29
29
 
30
30
  def site
31
- @site ||= values["Site"]
31
+ @site ||= values['Site']
32
32
  end
33
33
 
34
34
  def date
35
- @date ||= values["Date"]
35
+ @date ||= values['Date']
36
36
  end
37
37
 
38
38
  def round
39
- @round ||= values["Round"]
39
+ @round ||= values['Round']
40
40
  end
41
41
 
42
42
  def eco
43
- @eco ||= values["Eco"]
43
+ @eco ||= values['Eco']
44
44
  end
45
45
 
46
46
  def result
47
- @result ||= values["Result"]
47
+ @result ||= values['Result']
48
48
  end
49
49
 
50
50
  def values
@@ -3,11 +3,11 @@ module Bchess
3
3
  class MoveInfoParser
4
4
  attr_accessor :info, :move, :castle, :move_string
5
5
 
6
- CHECK = "+"
7
- TAKE = "x"
8
- TAKE_2 = ":"
9
- MATE = "#"
10
- PROMOTION = "="
6
+ CHECK = '+'.freeze
7
+ TAKE = 'x'.freeze
8
+ TAKE_2 = ':'.freeze
9
+ MATE = '#'.freeze
10
+ PROMOTION = '='.freeze
11
11
 
12
12
  def initialize(move, castle)
13
13
  @move = move
@@ -38,7 +38,7 @@ module Bchess
38
38
  end
39
39
 
40
40
  def extract_piece_type(move_string)
41
- if 2 == move_string.size || move_string.size && pawn_taking?(move_string)
41
+ if move_string.size == 2 || move_string.size && pawn_taking?(move_string)
42
42
  Bchess::Pawn
43
43
  else
44
44
  get_piece_from_letter(move_string[0])
@@ -58,9 +58,9 @@ module Bchess
58
58
  end
59
59
 
60
60
  def extract_additional_info(move_string)
61
- if (3 == move_string.size) && pawn_taking?(move_string)
61
+ if (move_string.size == 3) && pawn_taking?(move_string)
62
62
  move_string[0]
63
- elsif (4 == move_string.size)
63
+ elsif move_string.size == 4
64
64
  move_string[1]
65
65
  end
66
66
  end
@@ -79,26 +79,26 @@ module Bchess
79
79
  def basic_move_data(move_split)
80
80
  @move_string = move_split.last.strip
81
81
 
82
- raise InvalidMoveException.new("Wrong move description") if move_string.size < 2
82
+ raise InvalidMoveException, 'Wrong move description' if move_string.size < 2
83
+
83
84
  color = move_split.size == 2 ? Bchess::WHITE : Bchess::BLACK
84
85
  number = move_split.first.strip if move_split.size == 2
85
86
  {
86
87
  piece_color: color,
87
- move_number: number,
88
- check: move_string.include?(CHECK),
88
+ move_number: number,
89
+ check: move_string.include?(CHECK),
89
90
  take: move_string.include?(TAKE) || move_string.include?(TAKE_2),
90
- mate: move_string.include?(MATE),
91
+ mate: move_string.include?(MATE)
91
92
  }
92
93
  end
93
94
 
94
-
95
95
  def get_piece_from_letter(letter)
96
96
  {
97
- "K" => Bchess::King,
98
- "Q" => Bchess::Queen,
99
- "R" => Bchess::Rook,
100
- "B" => Bchess::Bishop,
101
- "N" => Bchess::Knight
97
+ 'K' => Bchess::King,
98
+ 'Q' => Bchess::Queen,
99
+ 'R' => Bchess::Rook,
100
+ 'B' => Bchess::Bishop,
101
+ 'N' => Bchess::Knight
102
102
  }[letter]
103
103
  end
104
104
  end
data/lib/pgn/parser.rb CHANGED
@@ -2,7 +2,7 @@ require 'treetop'
2
2
 
3
3
  module Bchess
4
4
  module PGN
5
- class ParserException < Exception ;end
5
+ class ParserException < RuntimeError; end
6
6
 
7
7
  class Parser
8
8
  attr_reader :input, :output, :error, :tree
@@ -15,13 +15,13 @@ module Bchess
15
15
  end
16
16
 
17
17
  def parse
18
- if input.kind_of?(String)
18
+ if input.is_a?(String)
19
19
  @tree = @@parser.parse(input)
20
- elsif input.kind_of?(Bchess::PGN::PGNFile)
20
+ elsif input.is_a?(Bchess::PGN::PGNFile)
21
21
  @tree = @@parser.parse(input.load_games)
22
22
  end
23
23
 
24
- if !tree
24
+ unless tree
25
25
  raise PGN::ParserException, "Parse error at offset: #{@@parser.index}"
26
26
  end
27
27
 
@@ -34,8 +34,8 @@ module Bchess
34
34
  root_elements = root_node.elements
35
35
  return unless root_elements
36
36
 
37
- root_elements.delete_if{ |node| node.class.name == "Treetop::Runtime::SyntaxNode" }
38
- root_elements.each{ |node| sanitize_tree(node) }
37
+ root_elements.delete_if { |node| node.class.name == 'Treetop::Runtime::SyntaxNode' }
38
+ root_elements.each { |node| sanitize_tree(node) }
39
39
  end
40
40
  end
41
41
  end
data/lib/pgn/pgn_file.rb CHANGED
@@ -8,7 +8,7 @@ module Bchess
8
8
  end
9
9
 
10
10
  def load_games
11
- file = File.open(filepath, "rt")
11
+ file = File.open(filepath, 'rt')
12
12
  @content = file.read
13
13
  end
14
14
  end
data/lib/pgn/pgn_nodes.rb CHANGED
@@ -10,7 +10,7 @@ module Sexp
10
10
 
11
11
  class PHeader < Treetop::Runtime::SyntaxNode
12
12
  def parse(value)
13
- val = value.split(" ", 2)
13
+ val = value.split(' ', 2)
14
14
 
15
15
  [val[0].delete('['), val[1].delete('"]').rstrip]
16
16
  end
@@ -72,11 +72,11 @@ class Treetop::Runtime::SyntaxNode
72
72
  def create_value_hash
73
73
  hash = {}
74
74
 
75
- self.elements.each do |element|
75
+ elements.each do |element|
76
76
  key = element.elements.first.text_value
77
77
  value = element.elements.last.text_value
78
78
 
79
- hash[key] = value.gsub('"','')
79
+ hash[key] = value.delete('"')
80
80
  end
81
81
 
82
82
  hash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bchess
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Staszek Zawadzki
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +80,6 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '1.6'
69
- - !ruby/object:Gem::Dependency
70
- name: pry
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
83
  description: Parsing and validating a chess games and returning as chess game objects
84
84
  email:
85
85
  - s.zawadzki@visuality.pl
@@ -126,7 +126,7 @@ files:
126
126
  - lib/pgn/pgn_nodes.rb
127
127
  - lib/pgn/pgn_rules.treetop
128
128
  - test.sh
129
- homepage: ''
129
+ homepage: https://github.com/visualitypl/bchess
130
130
  licenses:
131
131
  - MIT
132
132
  metadata: {}