bchess 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 46328051cb5767623ccfc2b65865008bfd3cab4a
4
+ data.tar.gz: 0d53222053172c2c710a343d19f0738470b8dac0
5
+ SHA512:
6
+ metadata.gz: 89624bd2c2b9b8860d3f45cae48264f85010496081a7d8becf9bce98491004912734c582e4f87ee737e4c54ec2de81ce6d8ec8e7464240bb7a6c751ffeeddd95
7
+ data.tar.gz: 401035c5490e48d066b15871db2e082b4336e93d56df9bcaa523f6de1a1bd9c3985f4a44aa92d3261adcf70460a563121c61424acaa4263011a1c41ba2f26e6d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at s.zawadzki@visuality.pl. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bchess.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bchess (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.0)
12
+ polyglot (0.3.5)
13
+ pry (0.11.3)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ rake (10.5.0)
17
+ rspec (3.7.0)
18
+ rspec-core (~> 3.7.0)
19
+ rspec-expectations (~> 3.7.0)
20
+ rspec-mocks (~> 3.7.0)
21
+ rspec-core (3.7.1)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-expectations (3.7.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.7.0)
26
+ rspec-mocks (3.7.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.7.0)
29
+ rspec-support (3.7.1)
30
+ treetop (1.6.10)
31
+ polyglot (~> 0.3)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bchess!
38
+ bundler (~> 1.16)
39
+ pry
40
+ rake (~> 10.0)
41
+ rspec (~> 3.0)
42
+ treetop (~> 1.6)
43
+
44
+ BUNDLED WITH
45
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Staszek Zawadzki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Bchess
2
+
3
+ Why bchess? Becasue BChess love chess :)
4
+
5
+ ## Installation
6
+
7
+ Standard instalation. Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bchess'
11
+ ```
12
+ And then execute:
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+ $ gem install bchess
17
+
18
+ ## Usage
19
+
20
+ ### Reading chess game from the file
21
+
22
+ Parsing a game file
23
+ ```ruby
24
+ file = Bchess::PGN::PGNFile.new('./games_to_read.pgn')
25
+ parser = Bchess::PGN::Parser.new(file)
26
+ parsed_games = parser.parse
27
+ ```
28
+
29
+ If the file is invalid you will get an error. Otherwise you'll receive an array of games containing games headers and moves in proper order.
30
+
31
+ Validating game on the board
32
+
33
+ ```ruby
34
+ parsed_game = parser.parse.first
35
+ Bchess::PGN::Game.new(parsed_game)
36
+
37
+ ## Getting moves
38
+ game.convert_body_to_moves
39
+
40
+ ## Accessing headers
41
+ game.header.player_white
42
+
43
+ ```
44
+
45
+ ### Testing
46
+ Due to the nature of the gem I tested it on a large amount of games to make sure it covers all or almost all edgecases. Full tests take a lot of time, so you can run a basic test with test.sh script. If you want to run the full suite the run full_test.sh or just rspec spec.
47
+
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/visualitypl/bchess. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ Everyone interacting in the Bchess project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bchess/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bchess.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bchess/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bchess"
8
+ spec.version = Bchess::VERSION
9
+ spec.authors = ["Staszek Zawadzki"]
10
+ spec.email = ["s.zawadzki@visuality.pl"]
11
+
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"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
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"
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bchess"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/full_test.sh ADDED
@@ -0,0 +1 @@
1
+ rspec spec/
data/lib/bchess.rb ADDED
@@ -0,0 +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"
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"
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"
26
+
27
+ module Bchess
28
+ class InvalidMoveException < StandardError; end
29
+
30
+ WHITE = 'w'
31
+ BLACK = 'b'
32
+ START_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
33
+ end
@@ -0,0 +1,17 @@
1
+ module Bchess
2
+ class Bishop < Piece
3
+ BISHOP_REACH = 7
4
+
5
+ def initiialize(*args)
6
+ super(args)
7
+ end
8
+
9
+ def name
10
+ 'B'
11
+ end
12
+
13
+ def can_move_to_field?(dcolumn, drow)
14
+ super && by_diagonal(dcolumn, drow, BISHOP_REACH)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,131 @@
1
+ module Bchess
2
+ class Board
3
+ include BoardHelpers
4
+ include FenHelpers
5
+ include CastleHelpers
6
+ include EnPassantHelpers
7
+ include Validations
8
+
9
+ attr_accessor :fen, :to_move, :pieces, :castles, :en_passant, :halfmove_clock, :move_number
10
+
11
+ def initialize(fen = Bchess::START_FEN, to_move = Bchess::WHITE)
12
+ @fen = fen
13
+ @to_move = to_move
14
+ @move_number = 0
15
+ @halfmove_clock = 0
16
+ @pieces = []
17
+ @en_passant = '-'
18
+ @castles = '-'
19
+ end
20
+
21
+ def move(piece, column, row, promoted_piece = nil)
22
+ return false if invalid_data?(piece, column, row)
23
+ @en_passant = '-' unless en_passant_detected?(piece, column, row)
24
+
25
+ if castle_detected?(piece, column)
26
+ castle(piece, column, row)
27
+ elsif promotion_detected?(piece, row)
28
+ execute_promotion(piece, column, row, promoted_piece)
29
+ elsif en_passant_detected?(piece, column, row)
30
+ validate_en_passant(piece, column, row) && execute_en_passant(piece, column, row)
31
+ elsif pawn_long_move_detected?(piece, row)
32
+ long_pawn_move(piece, column, row)
33
+ elsif piece.can_move_to_field?(column,row)
34
+ piece.move(column, row)
35
+ elsif piece.can_take_on_field?(column,row)
36
+ piece.move(column, row)
37
+ else
38
+ return false
39
+ end
40
+
41
+ update_info(piece, column, row)
42
+ validate_move
43
+ rescue RuntimeError
44
+ false
45
+ end
46
+
47
+ def get_possible_pieces(info)
48
+ @pieces.select{|p| p.can_make_move?(info, self)}
49
+ end
50
+
51
+ def update_info(piece, column, row)
52
+ update_castles_after_move(piece) if piece.moved
53
+ change_halfmove_clock(piece)
54
+ change_to_move
55
+ change_move_number if piece.color == Bchess::BLACK
56
+ remove_old_piece(column, row, piece.color) if !!at(column, row)
57
+ end
58
+
59
+ def execute_promotion(piece, column, row, promoted_piece)
60
+ raise RuntimeError.new("Promotion Not Specified") if promoted_piece.nil? || !(promoted_piece < Bchess::Piece)
61
+ pieces.delete(piece)
62
+ promoted = promoted_piece.new(piece.color, column, row)
63
+ promoted.moved = true if promoted_piece == Bchess::Rook
64
+ pieces << promoted
65
+ end
66
+
67
+ def transform_field(field)
68
+ [field.chars.first.ord - 97, field.chars.last.to_i - 1]
69
+ end
70
+
71
+ def at(column, row)
72
+ pieces.select{|p| p.row == row && p.column == column}.first
73
+ end
74
+
75
+ def read_fen
76
+ fen_pieces, fen_colors, fen_castles, fen_en_passant, fen_halfmove_clock, fen_move_number = fen.strip.split(" ")
77
+ set_pieces(fen_pieces)
78
+ set_to_move(fen_colors)
79
+ set_castles(fen_castles)
80
+ set_en_passant(fen_en_passant)
81
+ set_halfmove_clock(fen_halfmove_clock)
82
+ set_move_number(fen_move_number)
83
+ end
84
+
85
+ def print
86
+ puts 'Pieces WHITE: '
87
+ puts _pieces(Bchess::WHITE).map{|p| p.to_s }.join(', ')
88
+ puts 'Pieces BLACK: '
89
+ puts _pieces(Bchess::BLACK).map{|p| p.to_s }.join(', ')
90
+ end
91
+
92
+ def king(color)
93
+ @pieces.select{ |p| p.class == Bchess::King && p.color == color }.first
94
+ end
95
+
96
+ def change_to_move
97
+ @to_move = to_move == Bchess::WHITE ? Bchess::BLACK : Bchess::WHITE
98
+ end
99
+
100
+ private
101
+
102
+ def remove_old_piece(column, row, color)
103
+ taken_piece = _other_pieces(color).select{|p| p.row == row && p.column == column}.first
104
+ pieces.delete(taken_piece)
105
+ end
106
+
107
+ def just_moved
108
+ to_move == Bchess::WHITE ? Bchess::BLACK : Bchess::WHITE
109
+ end
110
+
111
+ def king_attacked(color)
112
+ _king = king(color)
113
+ _pieces = _other_pieces(color)
114
+
115
+ _pieces.any?{|p| attacks?(p, _king)}
116
+ end
117
+
118
+ def attacks?(piece, king)
119
+ piece.can_take_on_field?(king.column, king.row) &&
120
+ piece.fields_between(king.column, king.row).none?{|f| at(*f)}
121
+ end
122
+
123
+ def _pieces(color)
124
+ @pieces.select{ |p| p.color == color }
125
+ end
126
+
127
+ def _other_pieces(color)
128
+ @pieces.select{ |p| p.color != color }
129
+ end
130
+ end
131
+ end