just_chess 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +74 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/just_chess.gemspec +29 -0
- data/lib/just_chess.rb +8 -0
- data/lib/just_chess/direction.rb +35 -0
- data/lib/just_chess/errors/causes_check_error.rb +22 -0
- data/lib/just_chess/errors/error.rb +20 -0
- data/lib/just_chess/errors/invalid_move_error.rb +22 -0
- data/lib/just_chess/errors/invalid_promotion_error.rb +22 -0
- data/lib/just_chess/errors/moved_into_check_error.rb +22 -0
- data/lib/just_chess/errors/no_piece_error.rb +22 -0
- data/lib/just_chess/errors/not_players_turn_error.rb +22 -0
- data/lib/just_chess/errors/off_board_error.rb +22 -0
- data/lib/just_chess/game_state.rb +323 -0
- data/lib/just_chess/piece_factory.rb +68 -0
- data/lib/just_chess/pieces/bishop.rb +23 -0
- data/lib/just_chess/pieces/king.rb +97 -0
- data/lib/just_chess/pieces/knight.rb +23 -0
- data/lib/just_chess/pieces/pawn.rb +103 -0
- data/lib/just_chess/pieces/piece.rb +121 -0
- data/lib/just_chess/pieces/queen.rb +23 -0
- data/lib/just_chess/pieces/rook.rb +23 -0
- data/lib/just_chess/point.rb +52 -0
- data/lib/just_chess/square.rb +124 -0
- data/lib/just_chess/square_set.rb +407 -0
- data/lib/just_chess/vector.rb +86 -0
- data/lib/just_chess/version.rb +4 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6272f4c1c746d2ef9c3dd9ca9569c4564f714a3b
|
4
|
+
data.tar.gz: 96f5372a8b9220f720abac49e2a7d8c586a81556
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 71e99a15d466599fd0e286e9c51afe765c04b50191e5588c616b925e34520bd5e78fb230e2fdf9d09df9ff664644f196711b8326450c2aebd14a52f989d2cdec
|
7
|
+
data.tar.gz: a0cbf2a6cd01e4540d31020570fa0f8ac306d911b97fe113e022184022eb0148cc04a432dee72cacf08e863ff81bb0742ce2b033fdacebeccecb01e44a8a7dd4
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 mark@mrlhumphreys.com. 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
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Mark Humphreys
|
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,74 @@
|
|
1
|
+
# JustChess
|
2
|
+
|
3
|
+
A chess engine written in ruby. It provides a representation of a chess game complete with rules enforcement and serialisation.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'just_chess'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install just_chess
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To start, a new game state can be instantiated with the default state:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
game_state = JustChess::GameState.default
|
27
|
+
```
|
28
|
+
|
29
|
+
Moves can be made by passing in the player number and the from and to rank and file co-ordinates. It will return true if the move is valid, otherwise it will return false.
|
30
|
+
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
game_state.move(1, 'e4', 'e5')
|
34
|
+
```
|
35
|
+
|
36
|
+
The last change with all its details are found in the `last_change` attribute
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
game_state.last_change
|
40
|
+
```
|
41
|
+
|
42
|
+
If something happens errors may be found in the errors attribute
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
game_state.errors
|
46
|
+
```
|
47
|
+
|
48
|
+
The Winner can be found by calling winner on the object.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
game_state.winner
|
52
|
+
```
|
53
|
+
|
54
|
+
Also, the game can be serialized into a hash.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
game_state.as_json
|
58
|
+
```
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mrlhumphreys/just_chess. 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.
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
71
|
+
|
72
|
+
## Code of Conduct
|
73
|
+
|
74
|
+
Everyone interacting in the JustChess project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mrlhumphreys/just_chess/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "just_chess"
|
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
data/just_chess.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "just_chess/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "just_chess"
|
8
|
+
spec.version = JustChess::VERSION
|
9
|
+
spec.authors = ["Mark Humphreys"]
|
10
|
+
spec.email = ["mark@mrlhumphreys.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A chess engine written in ruby}
|
13
|
+
spec.description = %q{Provides a representation of a chess game complete with rules enforcement and serialisation.}
|
14
|
+
spec.homepage = "https://github.com/mrlhumphreys/just_chess"
|
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.required_ruby_version = '>= 2.1'
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
29
|
+
end
|
data/lib/just_chess.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module JustChess
|
2
|
+
|
3
|
+
# = Direction
|
4
|
+
#
|
5
|
+
# The Direction that something is moving on a 2d plane
|
6
|
+
class Direction
|
7
|
+
|
8
|
+
# New objects can be instantiated with
|
9
|
+
#
|
10
|
+
# @param [Fixnum] dx
|
11
|
+
# the dx magnitude.
|
12
|
+
#
|
13
|
+
# @param [Fixnum] dy
|
14
|
+
# the dy magnitude.
|
15
|
+
#
|
16
|
+
# ==== Example:
|
17
|
+
# # Instantiates a new Direction
|
18
|
+
# JustChess::Direction.new({
|
19
|
+
# dx: 1,
|
20
|
+
# dy: 1
|
21
|
+
# })
|
22
|
+
def initialize(dx, dy)
|
23
|
+
x = dx == 0 ? dx : dx/dx.abs
|
24
|
+
y = dy == 0 ? dy : dy/dy.abs
|
25
|
+
|
26
|
+
@x, @y = x, y
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Fixnum] the x magnitude.
|
30
|
+
attr_reader :x
|
31
|
+
|
32
|
+
# @return [Fixnum] the y magnitude.
|
33
|
+
attr_reader :y
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'just_chess/errors/error'
|
2
|
+
|
3
|
+
module JustChess
|
4
|
+
|
5
|
+
# = CausesCheckError
|
6
|
+
#
|
7
|
+
# A causes check error with a message
|
8
|
+
class CausesCheckError < Error
|
9
|
+
|
10
|
+
# New causes check errors can be instantiated with
|
11
|
+
#
|
12
|
+
# @option [String] message
|
13
|
+
# the message to display.
|
14
|
+
#
|
15
|
+
# ==== Example:
|
16
|
+
# # Instantiates a new CausesCheckError
|
17
|
+
# JustChess::CausesCheckError.new("Custom Message")
|
18
|
+
def initialize(message="That move would put the king in check.")
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module JustChess
|
2
|
+
|
3
|
+
# = Error
|
4
|
+
#
|
5
|
+
# An error with a message
|
6
|
+
class Error
|
7
|
+
|
8
|
+
# New errors can be instantiated with
|
9
|
+
#
|
10
|
+
# @option [String] message
|
11
|
+
# the message to display.
|
12
|
+
#
|
13
|
+
# ==== Example:
|
14
|
+
# # Instantiates a new Error
|
15
|
+
# JustChess::Error.new("Custom Message")
|
16
|
+
def initialize(message="Generic Error")
|
17
|
+
@message = message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'just_chess/errors/error'
|
2
|
+
|
3
|
+
module JustChess
|
4
|
+
|
5
|
+
# = InvalidMoveError
|
6
|
+
#
|
7
|
+
# An invalid move error with a message
|
8
|
+
class InvalidMoveError < Error
|
9
|
+
|
10
|
+
# New invalid move errors can be instantiated with
|
11
|
+
#
|
12
|
+
# @option [String] message
|
13
|
+
# the message to display.
|
14
|
+
#
|
15
|
+
# ==== Example:
|
16
|
+
# # Instantiates a new InvalidMoveError
|
17
|
+
# JustChess::InvalidMoveError.new("Custom Message")
|
18
|
+
def initialize(message="Move is invalid.")
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'just_chess/errors/error'
|
2
|
+
|
3
|
+
module JustChess
|
4
|
+
|
5
|
+
# = InvalidPromotionError
|
6
|
+
#
|
7
|
+
# An invalid promotion error with a message
|
8
|
+
class InvalidPromotionError < Error
|
9
|
+
|
10
|
+
# New invalid promotion errors can be instantiated with
|
11
|
+
#
|
12
|
+
# @option [String] message
|
13
|
+
# the message to display.
|
14
|
+
#
|
15
|
+
# ==== Example:
|
16
|
+
# # Instantiates a new InvalidPromotionError
|
17
|
+
# JustChess::InvalidPromotionError.new("Custom Message")
|
18
|
+
def initialize(message="Promotion is invalid.")
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'just_chess/errors/error'
|
2
|
+
|
3
|
+
module JustChess
|
4
|
+
|
5
|
+
# = MovedIntoCheckError
|
6
|
+
#
|
7
|
+
# A moved into check error with a message
|
8
|
+
class MovedIntoCheckError < Error
|
9
|
+
|
10
|
+
# New moved into check errors can be instantiated with
|
11
|
+
#
|
12
|
+
# @option [String] message
|
13
|
+
# the message to display.
|
14
|
+
#
|
15
|
+
# ==== Example:
|
16
|
+
# # Instantiates a new MovedIntoCheckError
|
17
|
+
# JustChess::MovedIntoCheckError.new("Custom Message")
|
18
|
+
def initialize(message="This move would leave the king in check.")
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'just_chess/errors/error'
|
2
|
+
|
3
|
+
module JustChess
|
4
|
+
|
5
|
+
# = NoPieceError
|
6
|
+
#
|
7
|
+
# A no piece error with a message
|
8
|
+
class NoPieceError < Error
|
9
|
+
|
10
|
+
# New no piece errors can be instantiated with
|
11
|
+
#
|
12
|
+
# @option [String] message
|
13
|
+
# the message to display.
|
14
|
+
#
|
15
|
+
# ==== Example:
|
16
|
+
# # Instantiates a new NoPieceError
|
17
|
+
# JustChess::NoPieceError.new("Custom Message")
|
18
|
+
def initialize(message="There is no piece to move.")
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|