just_backgammon 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/just_backgammon.gemspec +27 -0
- data/lib/just_backgammon/bar.rb +107 -0
- data/lib/just_backgammon/combined_move.rb +55 -0
- data/lib/just_backgammon/common.rb +38 -0
- data/lib/just_backgammon/dice_set.rb +74 -0
- data/lib/just_backgammon/die.rb +54 -0
- data/lib/just_backgammon/errors/bear_off_error.rb +20 -0
- data/lib/just_backgammon/errors/blocked_error.rb +20 -0
- data/lib/just_backgammon/errors/dice_mismatch_error.rb +20 -0
- data/lib/just_backgammon/errors/empty_bar_error.rb +20 -0
- data/lib/just_backgammon/errors/empty_point_error.rb +20 -0
- data/lib/just_backgammon/errors/moves_possible_error.rb +20 -0
- data/lib/just_backgammon/errors/not_players_turn_error.rb +20 -0
- data/lib/just_backgammon/errors/pieces_on_bar_error.rb +20 -0
- data/lib/just_backgammon/errors/point_not_found_error.rb +20 -0
- data/lib/just_backgammon/errors/point_ownership_error.rb +20 -0
- data/lib/just_backgammon/errors/wrong_direction_error.rb +20 -0
- data/lib/just_backgammon/errors/wrong_phase_error.rb +20 -0
- data/lib/just_backgammon/game_state.rb +332 -0
- data/lib/just_backgammon/move.rb +112 -0
- data/lib/just_backgammon/move_list.rb +151 -0
- data/lib/just_backgammon/off_board.rb +73 -0
- data/lib/just_backgammon/piece.rb +33 -0
- data/lib/just_backgammon/point.rb +104 -0
- data/lib/just_backgammon/point_set.rb +95 -0
- data/lib/just_backgammon/version.rb +4 -0
- data/lib/just_backgammon.rb +6 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4d93c6c0bcf97a4d75323ad95881347cbe992215
|
4
|
+
data.tar.gz: 9e02aba6744f4057d1cf367abce89be027a747ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c69f8fa340a095e98d8ae1bd7078fa3ea957d10879246186cb61d71663ede027a345f4b38437183258b0d9c0058db69b7b5d69b4b6f10b000d23e96d24d8e1e4
|
7
|
+
data.tar.gz: 03fbae1226c27cb7ce5760803b4352ffca72b41bce6df98c18a26e38c51e9e57780545e60078481c691509118332f9c40f8d40f53ed3ecac5529080e655acc46
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at mark@mrlhumphreys.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 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,68 @@
|
|
1
|
+
# JustBackgammon
|
2
|
+
|
3
|
+
A backgammon engine written in ruby. It provides a representation of a backgammon 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_backgammon'
|
11
|
+
```
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install just_backgammon
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
To start, a new game state can be instantiated with the default state:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
game_state = JustBackgammon::GameState.default
|
23
|
+
```
|
24
|
+
|
25
|
+
Rolls can be made by calling the roll method, passing in the player number.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
game_state.roll(1)
|
29
|
+
```
|
30
|
+
|
31
|
+
Moves can be made by passing in the player number, and an array of moves each consisting of the from point number and to point number. It will return true if the move is valid, otherwise it will return false.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
game_state.move(1, [{from: 1, to: 2}]])
|
35
|
+
```
|
36
|
+
|
37
|
+
If something happens errors may be found in the errors attribute
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
game_state.errors
|
41
|
+
```
|
42
|
+
|
43
|
+
The Winner can be found by calling winner on the object.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
game_state.winner
|
47
|
+
```
|
48
|
+
|
49
|
+
Also, the game can be serialized into a hash.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
game_state.as_json
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mrlhumphreys/just_backgammon. 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.
|
63
|
+
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
68
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "just_backgammon"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'just_backgammon/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "just_backgammon"
|
8
|
+
spec.version = JustBackgammon::VERSION
|
9
|
+
spec.authors = ["Mark Humphreys"]
|
10
|
+
spec.email = ["mark@mrlhumphreys.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A backgammon engine written in ruby}
|
13
|
+
spec.description = %q{Provides a representation of a backgammon game complete with rules enforcement and serialisation.}
|
14
|
+
spec.homepage = "https://github.com/mrlhumphreys/just_backgammon"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.1'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'just_backgammon/common'
|
2
|
+
require 'just_backgammon/piece'
|
3
|
+
|
4
|
+
module JustBackgammon
|
5
|
+
|
6
|
+
# = Bar
|
7
|
+
#
|
8
|
+
# The bar is where hit pieces go. Contains an array of pieces.
|
9
|
+
class Bar
|
10
|
+
extend Common
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
# A new instance of Bar.
|
14
|
+
#
|
15
|
+
# @param [Array<Hash>] pieces
|
16
|
+
# All the pieces on the bar, each piece has an owner.
|
17
|
+
#
|
18
|
+
# ==== Example:
|
19
|
+
# # Instantiates a new Bar
|
20
|
+
# JustBackgammon::Bar.new({
|
21
|
+
# pieces: [{owner: 1}, {owner: 1}]
|
22
|
+
# })
|
23
|
+
def initialize(pieces:)
|
24
|
+
@pieces = JustBackgammon::Piece.load(pieces)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Array<Piece>] all the pieces on the bar, each piece has an owner
|
28
|
+
attr_reader :pieces
|
29
|
+
|
30
|
+
def_delegator :pieces, :empty?
|
31
|
+
|
32
|
+
# The identifier of the bar, returns the string 'bar'.
|
33
|
+
#
|
34
|
+
# @return [String]
|
35
|
+
def number
|
36
|
+
'bar'
|
37
|
+
end
|
38
|
+
|
39
|
+
# ALl the pieces owned by the specified player.
|
40
|
+
#
|
41
|
+
# @param [Fixnum] player_number
|
42
|
+
# the specified player number.
|
43
|
+
#
|
44
|
+
# @return [Array<Piece>]
|
45
|
+
def pieces_owned_by_player(player_number)
|
46
|
+
pieces.select { |p| p.owner == player_number }
|
47
|
+
end
|
48
|
+
|
49
|
+
# Number of pieces owned by the specified player.
|
50
|
+
#
|
51
|
+
# @param [Fixnum] player_number
|
52
|
+
# the specified player number.
|
53
|
+
#
|
54
|
+
# @return [Fixnum]
|
55
|
+
def number_of_pieces_owned_by_player(player_number)
|
56
|
+
pieces_owned_by_player(player_number).size
|
57
|
+
end
|
58
|
+
|
59
|
+
# If the player has any pieces.
|
60
|
+
#
|
61
|
+
# @param [Fixnum] player_number
|
62
|
+
# the specified player number.
|
63
|
+
#
|
64
|
+
# @return [Boolean]
|
65
|
+
def any_pieces_for_player?(player_number)
|
66
|
+
pieces_owned_by_player(player_number).any?
|
67
|
+
end
|
68
|
+
|
69
|
+
# If the player has no pieces.
|
70
|
+
#
|
71
|
+
# @param [Fixnum] player_number
|
72
|
+
# the specified player number.
|
73
|
+
#
|
74
|
+
# @return [Boolean]
|
75
|
+
def empty_for_player?(player_number)
|
76
|
+
pieces_owned_by_player(player_number).none?
|
77
|
+
end
|
78
|
+
|
79
|
+
# Removes a piece owned by the specified player and return it.
|
80
|
+
#
|
81
|
+
# @param [Fixnum] player_number
|
82
|
+
# the specified player number.
|
83
|
+
#
|
84
|
+
# @return [Piece,NilClass]
|
85
|
+
def pop_for_player(player_number)
|
86
|
+
p = pieces.find { |p| p.owner == player_number }
|
87
|
+
pieces.delete(p)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Adds a piece to the bar.
|
91
|
+
#
|
92
|
+
# @param [Piece] piece
|
93
|
+
# the piece to push onto the bar.
|
94
|
+
#
|
95
|
+
# @return [Array<Piece>]
|
96
|
+
def push(piece)
|
97
|
+
pieces.push(piece)
|
98
|
+
end
|
99
|
+
|
100
|
+
# A hashed serialized representation of the bar.
|
101
|
+
#
|
102
|
+
# @return [Hash]
|
103
|
+
def as_json
|
104
|
+
{ pieces: pieces.map(&:as_json) }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module JustBackgammon
|
2
|
+
|
3
|
+
# = CombinedMove
|
4
|
+
#
|
5
|
+
# A combined move is a move where one piece moves multiple times.
|
6
|
+
class CombinedMove
|
7
|
+
extend Forwardable
|
8
|
+
|
9
|
+
# A new instance of CombinedMove.
|
10
|
+
#
|
11
|
+
# @param [Array<Move>] legs
|
12
|
+
# The legs of the combined move.
|
13
|
+
#
|
14
|
+
# ==== Example:
|
15
|
+
# # Instantiates a new CombinedMove
|
16
|
+
# JustBackgammon::CombinedMove.new({
|
17
|
+
# legs: [move_a, move_b]
|
18
|
+
# })
|
19
|
+
def initialize(legs:)
|
20
|
+
@legs = legs
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [Array<Move>] the legs of the combined move.
|
24
|
+
attr_reader :legs
|
25
|
+
|
26
|
+
def_delegator :legs, :size
|
27
|
+
|
28
|
+
# Does the combined move start from a point?
|
29
|
+
#
|
30
|
+
# @return [Boolean]
|
31
|
+
def from_point?
|
32
|
+
first_leg.instance_of?(JustBackgammon::Point) if first_leg
|
33
|
+
end
|
34
|
+
|
35
|
+
# Does the combined move start with an empty point?
|
36
|
+
#
|
37
|
+
# @return [Boolean]
|
38
|
+
def empty?
|
39
|
+
first_leg.empty? if first_leg
|
40
|
+
end
|
41
|
+
|
42
|
+
# Does the combined move have pieces owned by the opponent?
|
43
|
+
#
|
44
|
+
# @return [Boolean]
|
45
|
+
def owned_by_opponent?(player_number)
|
46
|
+
first_leg.owned_by_opponent?(player_number) if first_leg
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def first_leg # :nodoc:
|
52
|
+
legs.first
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module JustBackgammon
|
2
|
+
|
3
|
+
# = Common
|
4
|
+
#
|
5
|
+
# Mixin that allows the class to have a custom load method
|
6
|
+
# It allows initializing by arrays, hashes or objects with the same class.
|
7
|
+
module Common
|
8
|
+
|
9
|
+
# Method that initializing an object by arrays, hashes or objects with the same class.
|
10
|
+
# Returns the object or array of objects.
|
11
|
+
# Will raise error if elements of array are not all the same Class.
|
12
|
+
# Will raise error if argument is not Hash, Array or the same Class.
|
13
|
+
#
|
14
|
+
# @param [Array<Hash>, Hash, Object] argument
|
15
|
+
# The initialization data.
|
16
|
+
#
|
17
|
+
# @return [Array<Object>, Object]
|
18
|
+
def load(argument)
|
19
|
+
case argument
|
20
|
+
when Hash
|
21
|
+
self.new(argument)
|
22
|
+
when Array
|
23
|
+
case
|
24
|
+
when argument.all? { |o| o.instance_of?(Hash) }
|
25
|
+
argument.map { |o| self.new(o) }
|
26
|
+
when argument.all? { |o| o.instance_of?(self) }
|
27
|
+
argument
|
28
|
+
else
|
29
|
+
raise ArgumentError, "elements of array must have the same class"
|
30
|
+
end
|
31
|
+
when self
|
32
|
+
argument
|
33
|
+
else
|
34
|
+
raise ArgumentError, "argument needs to be a Hash, Array or #{self}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'just_backgammon/common'
|
2
|
+
require 'just_backgammon/die'
|
3
|
+
|
4
|
+
module JustBackgammon
|
5
|
+
|
6
|
+
# = DiceSet
|
7
|
+
#
|
8
|
+
# The collections of dice.
|
9
|
+
class DiceSet
|
10
|
+
extend Common
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
# A new instance of DiceSet.
|
14
|
+
#
|
15
|
+
# @param [Array<Hash>] dice
|
16
|
+
# All the dice in the set, each with a number.
|
17
|
+
#
|
18
|
+
# ==== Example:
|
19
|
+
# # Instantiates a new DiceSet
|
20
|
+
# JustBackgammon::DiceSet.new({
|
21
|
+
# dice: [{number: 1}, {number: 2}]
|
22
|
+
# })
|
23
|
+
def initialize(dice:)
|
24
|
+
@dice = Die.load(dice)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Array<Dice>] allthe dice in the set, each with a number
|
28
|
+
attr_reader :dice
|
29
|
+
|
30
|
+
def_delegator :dice, :map
|
31
|
+
def_delegator :dice, :each
|
32
|
+
|
33
|
+
# finds a die that matches the specified number.
|
34
|
+
#
|
35
|
+
# @return [Die]
|
36
|
+
def find_by_number(number)
|
37
|
+
@dice.find { |d| d.number == number }
|
38
|
+
end
|
39
|
+
|
40
|
+
# returns an array of the numbers on the dice.
|
41
|
+
#
|
42
|
+
# @return [Array<Fixnum>]
|
43
|
+
def numbers
|
44
|
+
@dice.map(&:number)
|
45
|
+
end
|
46
|
+
|
47
|
+
# randomizes each die and duplicates if they are the same number.
|
48
|
+
#
|
49
|
+
# @return [Array<Die>]
|
50
|
+
def roll
|
51
|
+
@dice.each(&:roll)
|
52
|
+
if @dice.first == @dice.last
|
53
|
+
@dice.concat(@dice)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# sets all dice numbers to nil and reduces the number of dice to two.
|
58
|
+
#
|
59
|
+
# @return [Array<Die>]
|
60
|
+
def reset
|
61
|
+
@dice.each(&:reset)
|
62
|
+
if @dice.size > 2
|
63
|
+
@dice.slice!(-2..-1)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# A hashed serialized representation of the dice set.
|
68
|
+
#
|
69
|
+
# @return [Hash]
|
70
|
+
def as_json
|
71
|
+
dice.map(&:as_json)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'just_backgammon/common'
|
2
|
+
|
3
|
+
module JustBackgammon
|
4
|
+
|
5
|
+
# = Die
|
6
|
+
#
|
7
|
+
# The die is a cube that be rolled to result in a number from 1 to 6.
|
8
|
+
class Die
|
9
|
+
extend Common
|
10
|
+
|
11
|
+
# A new instance of Die.
|
12
|
+
#
|
13
|
+
# @param [Fixnum, NilClass] number
|
14
|
+
# The number of the die. Returns nil if not yet rolled.
|
15
|
+
#
|
16
|
+
# ==== Example:
|
17
|
+
# # Instantiates a new Die
|
18
|
+
# JustBackgammon::Die.new(number: 1)
|
19
|
+
def initialize(number:)
|
20
|
+
@number = number
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [Fixnum] the number of the die. Returns nil if not yet rolled
|
24
|
+
attr_reader :number
|
25
|
+
|
26
|
+
# Rolls the die, the number will be between 1 and 6.
|
27
|
+
#
|
28
|
+
# @return [Fixnum]
|
29
|
+
def roll
|
30
|
+
@number = Random.new.rand(6) + 1
|
31
|
+
end
|
32
|
+
|
33
|
+
# Resets the die, the number will be nil.
|
34
|
+
#
|
35
|
+
# @return [NilClass]
|
36
|
+
def reset
|
37
|
+
@number = nil
|
38
|
+
end
|
39
|
+
|
40
|
+
# Equals operator compares the number to determine if the dice are equal.
|
41
|
+
#
|
42
|
+
# @return [Boolean]
|
43
|
+
def == (other)
|
44
|
+
self.number == other.number
|
45
|
+
end
|
46
|
+
|
47
|
+
# A hashed serialized representation of the die
|
48
|
+
#
|
49
|
+
# @return [Hash]
|
50
|
+
def as_json
|
51
|
+
{ number: number }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module JustBackgammon
|
2
|
+
|
3
|
+
# = BearOffError
|
4
|
+
#
|
5
|
+
# A bear off error with a message
|
6
|
+
class BearOffError
|
7
|
+
|
8
|
+
# A new instance of BearOffError.
|
9
|
+
#
|
10
|
+
# @option [String] message
|
11
|
+
# the message.
|
12
|
+
#
|
13
|
+
# ==== Example:
|
14
|
+
# # Instantiates a new BearOffError
|
15
|
+
# JustBackgammon::BearOffError.new("Custom Message")
|
16
|
+
def initialize(message="Cannot bear off while pieces are not home.")
|
17
|
+
@message = message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module JustBackgammon
|
2
|
+
|
3
|
+
# = BlockedError
|
4
|
+
#
|
5
|
+
# A blocked error with a message
|
6
|
+
class BlockedError
|
7
|
+
|
8
|
+
# A new instance of BlockedError.
|
9
|
+
#
|
10
|
+
# @option [String] message
|
11
|
+
# the message.
|
12
|
+
#
|
13
|
+
# ==== Example:
|
14
|
+
# # Instantiates a new BlockedError
|
15
|
+
# JustBackgammon::BlockedError.new("Custom Message")
|
16
|
+
def initialize(message="Point blocked by opponent.")
|
17
|
+
@message = message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module JustBackgammon
|
2
|
+
|
3
|
+
# = DiceMismatchError
|
4
|
+
#
|
5
|
+
# A dice mismatch error with a message
|
6
|
+
class DiceMismatchError
|
7
|
+
|
8
|
+
# A new instance of DiceMismatchError.
|
9
|
+
#
|
10
|
+
# @option [String] message
|
11
|
+
# the message.
|
12
|
+
#
|
13
|
+
# ==== Example:
|
14
|
+
# # Instantiates a new DiceMismatchError
|
15
|
+
# JustBackgammon::DiceMismatchError.new("Custom Message")
|
16
|
+
def initialize(message="Move does not match dice rolls.")
|
17
|
+
@message = message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module JustBackgammon
|
2
|
+
|
3
|
+
# = EmptyBarError
|
4
|
+
#
|
5
|
+
# An empty bar error with a message
|
6
|
+
class EmptyBarError
|
7
|
+
|
8
|
+
# A new instance of EmptyBarError.
|
9
|
+
#
|
10
|
+
# @option [String] message
|
11
|
+
# the message.
|
12
|
+
#
|
13
|
+
# ==== Example:
|
14
|
+
# # Instantiates a new EmptyBarError
|
15
|
+
# JustBackgammon::EmptyBarError.new("Custom Message")
|
16
|
+
def initialize(message="No pieces owned by player on bar.")
|
17
|
+
@message = message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module JustBackgammon
|
2
|
+
|
3
|
+
# = EmptyPointError
|
4
|
+
#
|
5
|
+
# An empty point error with a message
|
6
|
+
class EmptyPointError
|
7
|
+
|
8
|
+
# A new instance of EmptyPointError.
|
9
|
+
#
|
10
|
+
# @option [String] message
|
11
|
+
# the message.
|
12
|
+
#
|
13
|
+
# ==== Example:
|
14
|
+
# # Instantiates a new EmptyPointError
|
15
|
+
# JustBackgammon::EmptyPointError.new("Custom Message")
|
16
|
+
def initialize(message="Point is empty.")
|
17
|
+
@message = message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|