greed_game 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +97 -0
- data/Rakefile +2 -0
- data/greed_game.gemspec +19 -0
- data/lib/greed_game/dice_set.rb +8 -0
- data/lib/greed_game/player.rb +94 -0
- data/lib/greed_game/version.rb +3 -0
- data/lib/greed_game.rb +47 -0
- data/spec/dice_set_spec.rb +45 -0
- data/spec/greed_game_spec.rb +77 -0
- data/spec/player_spec.rb +234 -0
- data/spec/spec_helper.rb +5 -0
- metadata +77 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3-head@greed_game-gem
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 natalili
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# GreedGame
|
2
|
+
|
3
|
+
= Playing Greed
|
4
|
+
|
5
|
+
Greed is a dice game played among 2 or more players, using 5
|
6
|
+
six-sided dice.
|
7
|
+
|
8
|
+
== Playing Greed
|
9
|
+
|
10
|
+
Each player takes a turn consisting of one or more rolls of the dice.
|
11
|
+
On the first roll of the game, a player rolls all five dice which are
|
12
|
+
scored according to the following:
|
13
|
+
|
14
|
+
Three 1's => 1000 points
|
15
|
+
Three 6's => 600 points
|
16
|
+
Three 5's => 500 points
|
17
|
+
Three 4's => 400 points
|
18
|
+
Three 3's => 300 points
|
19
|
+
Three 2's => 200 points
|
20
|
+
One 1 => 100 points
|
21
|
+
One 5 => 50 points
|
22
|
+
|
23
|
+
A single die can only be counted once in each roll. For example,
|
24
|
+
a "5" can only count as part of a triplet (contributing to the 500
|
25
|
+
points) or as a single 50 points, but not both in the same roll.
|
26
|
+
|
27
|
+
Example Scoring
|
28
|
+
|
29
|
+
Throw Score
|
30
|
+
--------- ------------------
|
31
|
+
5 1 3 4 1 50 + 2 * 100 = 250
|
32
|
+
1 1 1 3 1 1000 + 100 = 1100
|
33
|
+
2 4 4 5 4 400 + 50 = 450
|
34
|
+
|
35
|
+
The dice not contributing to the score are called the non-scoring
|
36
|
+
dice. "3" and "4" are non-scoring dice in the first example. "3" is
|
37
|
+
a non-scoring die in the second, and "2" is a non-score die in the
|
38
|
+
final example.
|
39
|
+
|
40
|
+
After a player rolls and the score is calculated, the scoring dice are
|
41
|
+
removed and the player has the option of rolling again using only the
|
42
|
+
non-scoring dice. If all of the thrown dice are scoring, then the
|
43
|
+
player may roll all 5 dice in the next roll.
|
44
|
+
|
45
|
+
The player may continue to roll as long as each roll scores points. If
|
46
|
+
a roll has zero points, then the player loses not only their turn, but
|
47
|
+
also accumulated score for that turn. If a player decides to stop
|
48
|
+
rolling before rolling a zero-point roll, then the accumulated points
|
49
|
+
for the turn is added to his total score.
|
50
|
+
|
51
|
+
== Getting "In The Game"
|
52
|
+
|
53
|
+
Before a player is allowed to accumulate points, they must get at
|
54
|
+
least 300 points in a single turn. Once they have achieved 300 points
|
55
|
+
in a single turn, the points earned in that turn and each following
|
56
|
+
turn will be counted toward their total score.
|
57
|
+
|
58
|
+
== End Game
|
59
|
+
|
60
|
+
Once a player reaches 3000 (or more) points, the game enters the final
|
61
|
+
round where each of the other players gets one more turn. The winner
|
62
|
+
is the player with the highest score after the final round.
|
63
|
+
|
64
|
+
## Installation
|
65
|
+
|
66
|
+
Add this line to your application's Gemfile:
|
67
|
+
|
68
|
+
gem 'greed_game'
|
69
|
+
|
70
|
+
And then execute:
|
71
|
+
|
72
|
+
$ bundle
|
73
|
+
|
74
|
+
Or install it yourself as:
|
75
|
+
|
76
|
+
$ gem install greed_game
|
77
|
+
|
78
|
+
## Usage
|
79
|
+
|
80
|
+
1. Include this gem in class.
|
81
|
+
2. Create class's object:
|
82
|
+
`game = My_class.new("player1", "player2",...)`
|
83
|
+
"player1", "player2",... - player's names
|
84
|
+
3. Begin the game:
|
85
|
+
`game.in_progress`
|
86
|
+
4. Get a winner:
|
87
|
+
`game.finished`
|
88
|
+
5. `next_roll?` method can accept/reject (return true/false) it next roll
|
89
|
+
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
1. Fork it
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
95
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
96
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/greed_game.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/greed_game/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["natalili"]
|
6
|
+
gem.email = ["natali.li@list.ru"]
|
7
|
+
gem.description = %q{Greed is a dice game played among 2 or more players, using 5 six-sided dice.}
|
8
|
+
gem.summary = %q{Greed is a dice game}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "greed_game"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = GreedGame::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
class Player
|
2
|
+
|
3
|
+
attr_reader :score_roll, :score_total
|
4
|
+
attr_accessor :name
|
5
|
+
|
6
|
+
def initialize(name)
|
7
|
+
@name = name
|
8
|
+
@score_roll = 0
|
9
|
+
@score_total = 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def calculate_non_scoring_dice(dice)
|
13
|
+
@count = dice.count
|
14
|
+
dice.uniq.each do |n|
|
15
|
+
if n == 1
|
16
|
+
@count = @count - dice.count(1)
|
17
|
+
elsif n == 5
|
18
|
+
@count = @count - dice.count(5)
|
19
|
+
else
|
20
|
+
@count = @count - 3 if dice.count(n) >= 3
|
21
|
+
end
|
22
|
+
end
|
23
|
+
@count
|
24
|
+
end
|
25
|
+
|
26
|
+
def calculate_dice_for_next_roll(dice)
|
27
|
+
calculate_non_scoring_dice(dice)
|
28
|
+
if @count == 0 and dice.count > 0
|
29
|
+
@count = 5
|
30
|
+
elsif @count == dice.count
|
31
|
+
@count = 0
|
32
|
+
end
|
33
|
+
@count = 0 unless next_roll?
|
34
|
+
@count
|
35
|
+
end
|
36
|
+
|
37
|
+
def next_roll?
|
38
|
+
if @count > 0
|
39
|
+
puts "Player #{name}, You have #{@score} score and You can use #{@count} dice. \n" +
|
40
|
+
"If You want do roll, enter 'y'"
|
41
|
+
answer = STDIN.gets.chop!
|
42
|
+
answer == "y"
|
43
|
+
else
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def score(dice)
|
49
|
+
@score = 0
|
50
|
+
dice.uniq.each do |n|
|
51
|
+
if n == 1
|
52
|
+
c = dice.count(1)
|
53
|
+
@score += 100 * dice.count(1) if c < 3
|
54
|
+
@score += 1000 + 100 * (c - 3) if c >= 3
|
55
|
+
elsif n == 5
|
56
|
+
c = dice.count(5)
|
57
|
+
@score += 50 * c if c < 3
|
58
|
+
@score += 5 * 100 + 50 * (c - 3) if c >= 3
|
59
|
+
else
|
60
|
+
c = dice.count(n)
|
61
|
+
@score += n * 100 if c >= 3
|
62
|
+
end
|
63
|
+
end
|
64
|
+
@score
|
65
|
+
end
|
66
|
+
|
67
|
+
def calculate_score_roll(score)
|
68
|
+
score == 0 ? @score_roll = 0 : @score_roll += score
|
69
|
+
@score = 0
|
70
|
+
@score_roll
|
71
|
+
end
|
72
|
+
|
73
|
+
def calculate_score_total(score_roll)
|
74
|
+
if @score_total == 0
|
75
|
+
@score_total += score_roll if score_roll >= 300
|
76
|
+
else
|
77
|
+
@score_total += score_roll
|
78
|
+
end
|
79
|
+
@score_roll = 0
|
80
|
+
@score_total
|
81
|
+
end
|
82
|
+
|
83
|
+
def roll(dices)
|
84
|
+
while dices > 0
|
85
|
+
dice_set = DiceSet.new
|
86
|
+
dice_roll = dice_set.roll(dices)
|
87
|
+
score = score(dice_roll)
|
88
|
+
dices = calculate_dice_for_next_roll(dice_roll)
|
89
|
+
score_roll = calculate_score_roll(score)
|
90
|
+
calculate_score_total(score_roll) if dices == 0
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
data/lib/greed_game.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "greed_game/version"
|
2
|
+
require "greed_game/dice_set"
|
3
|
+
require "greed_game/player"
|
4
|
+
|
5
|
+
module GreedGame
|
6
|
+
|
7
|
+
def self.name
|
8
|
+
"Greed Game"
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(*names)
|
12
|
+
raise "It should be 2 or more players in the game" if names.size < 2
|
13
|
+
@dices = 5
|
14
|
+
@players = Array.new
|
15
|
+
names.each do |name|
|
16
|
+
@players << Player.new(name)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def in_progress
|
21
|
+
max_score = 0
|
22
|
+
last_round = false
|
23
|
+
while max_score < 3000 or not last_round
|
24
|
+
@players.each do |player|
|
25
|
+
player.roll(@dices)
|
26
|
+
end
|
27
|
+
player_whith_max_score = @players.max_by {|players| players.score_total }
|
28
|
+
last_round = true if max_score >= 3000
|
29
|
+
max_score = player_whith_max_score.score_total
|
30
|
+
end
|
31
|
+
@winner = Array.new
|
32
|
+
@players.each do |player|
|
33
|
+
@winner << player if player.score_total == max_score
|
34
|
+
end
|
35
|
+
@winner
|
36
|
+
end
|
37
|
+
|
38
|
+
def finished
|
39
|
+
if @winner.count == 1
|
40
|
+
text = "#{@winner[0].name} is winner"
|
41
|
+
else
|
42
|
+
text = "Winner is absent"
|
43
|
+
end
|
44
|
+
text
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DiceSet do
|
4
|
+
before(:each) {@dice = DiceSet.new; @dice.roll(5)}
|
5
|
+
|
6
|
+
|
7
|
+
it "should create a dice set" do
|
8
|
+
@dice.should_not be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should be an array" do
|
12
|
+
@dice.values.should be_is_a(Array)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have valid array size" do
|
16
|
+
@dice.values.size.should == 5
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return a set of integers between 1 and 6" do
|
20
|
+
@dice.values.each do |value|
|
21
|
+
value.should >= 1 && value.should <= 6
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it "values should not change unless dice explicitly rolled" do
|
26
|
+
first_time = @dice.values
|
27
|
+
second_time = @dice.values
|
28
|
+
second_time.should == first_time
|
29
|
+
end
|
30
|
+
|
31
|
+
it "rolls should change dice values" do
|
32
|
+
first_time = @dice.values
|
33
|
+
@dice.roll(5)
|
34
|
+
second_time = @dice.values
|
35
|
+
second_time.should_not == first_time
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should can roll different numbers of dice" do
|
39
|
+
@dice.values.size.should == 5
|
40
|
+
@dice.roll(3)
|
41
|
+
@dice.values.size.should == 3
|
42
|
+
@dice.roll(1)
|
43
|
+
@dice.values.size.should == 1
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GreedGame do
|
4
|
+
before(:each) do
|
5
|
+
class Game
|
6
|
+
include GreedGame
|
7
|
+
end
|
8
|
+
@game = Game.new("player1", "player2")
|
9
|
+
end
|
10
|
+
|
11
|
+
context "#name" do
|
12
|
+
it "shuld return gem name" do
|
13
|
+
GreedGame.name.should == "Greed Game"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "New" do
|
18
|
+
it "should create game with 2 or more players" do
|
19
|
+
@game.instance_variable_get(:@players).size.should == 2
|
20
|
+
@game = Game.new("player1", "player2", "player3")
|
21
|
+
@game.instance_variable_get(:@players).size.should == 3
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should rise exception with 1 player" do
|
25
|
+
expect { Game.new("player1") }.to raise_error('It should be 2 or more players in the game')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should use 5 dices" do
|
29
|
+
@game.instance_variable_get(:@dices).should == 5
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should have score total is zero" do
|
33
|
+
@game.instance_variable_get(:@players).max_by {|players| players.score_total }.score_total. should == 0
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "In progress" do
|
38
|
+
before(:each) {@game.instance_variable_get(:@players)[0].stub!(:next_roll?).and_return(false)
|
39
|
+
@game.instance_variable_get(:@players)[1].stub!(:next_roll?).and_return(false)}
|
40
|
+
|
41
|
+
it "should have a player with score total is more 3000" do
|
42
|
+
@game.in_progress
|
43
|
+
@game.instance_variable_get(:@players).max_by {|players| players.score_total }.score_total. should >= 3000
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return array with winners" do
|
47
|
+
@game.in_progress.should_not == []
|
48
|
+
@game.instance_variable_get(:@winner).count.should > 0
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return player with maximal score total" do
|
52
|
+
@game.instance_variable_get(:@players)[0].stub!(:score_total).and_return(30000)
|
53
|
+
@game.in_progress[0].name.should == "player1"
|
54
|
+
@game.instance_variable_get(:@winner)[0].name.should == "player1"
|
55
|
+
@game.instance_variable_get(:@winner).count.should == 1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "Finished" do
|
60
|
+
|
61
|
+
it "should return who is winner if one player have maximal score total" do
|
62
|
+
@game.instance_variable_set(:@winner , Array.new << Player.new("player1"))
|
63
|
+
@game.finished.should == "player1 is winner"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should return 'Winner is absent' if array with winners is empty" do
|
67
|
+
@game.instance_variable_set(:@winner, Array.new)
|
68
|
+
@game.finished.should == "Winner is absent"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should return 'Winner is absent' if more than one player have maximal score total" do
|
72
|
+
@game.instance_variable_set(:@winner, @game.instance_variable_get(:@players))
|
73
|
+
@game.finished.should == "Winner is absent"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
data/spec/player_spec.rb
ADDED
@@ -0,0 +1,234 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Player do
|
4
|
+
before(:each) {@player = Player.new("player1")}
|
5
|
+
|
6
|
+
context "New" do
|
7
|
+
|
8
|
+
it "should have a name" do
|
9
|
+
@player.instance_variables.include?(:@name).should == true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return a name" do
|
13
|
+
@player.name.should == "player1"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should set name" do
|
17
|
+
@player.name = "Player 1"
|
18
|
+
@player.name.should == "Player 1"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should have a current roll's score" do
|
22
|
+
@player.instance_variables.include?(:@score_roll).should == true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should have zero roll's score" do
|
26
|
+
@player.score_roll.should == 0
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should rise exception if roll's score is set" do
|
30
|
+
expect { @player.score_roll = 300 }.to raise_error(NoMethodError)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have total score" do
|
34
|
+
@player.instance_variables.include?(:@score_total).should == true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should have zero total score" do
|
38
|
+
@player.score_total.should == 0
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should rise exception if total score is set" do
|
42
|
+
expect { @player.score_total = 3000 }.to raise_error(NoMethodError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "Calculate non-scoring dices" do
|
47
|
+
|
48
|
+
it "should be zero if dices are absent" do
|
49
|
+
@player.calculate_non_scoring_dice([]).should == 0
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should be zero if all of the thrown dices are scoring" do
|
53
|
+
@player.calculate_non_scoring_dice([5]).should == 0
|
54
|
+
@player.calculate_non_scoring_dice([1]).should == 0
|
55
|
+
@player.calculate_non_scoring_dice([2,2,2]).should == 0
|
56
|
+
@player.calculate_non_scoring_dice([3,3,3]).should == 0
|
57
|
+
@player.calculate_non_scoring_dice([4,4,4]).should == 0
|
58
|
+
@player.calculate_non_scoring_dice([6,6,6]).should == 0
|
59
|
+
@player.calculate_non_scoring_dice([1,5,3,3,3]).should == 0
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return the count of 2s, 3s, 4s, and 6s" do
|
63
|
+
@player.calculate_non_scoring_dice([2,3,4,6]).should == 4
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should return the count of non-scoring dices for mixed dice sets" do
|
67
|
+
@player.calculate_non_scoring_dice([2,2,2,3,4]).should == 2
|
68
|
+
@player.calculate_non_scoring_dice([1,5,6,4,2]).should == 3
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
context "Calculate count of dices for next roll" do
|
74
|
+
before(:each) {@player.stub!(:next_roll?).and_return(true)}
|
75
|
+
|
76
|
+
it "should be zero if dices are absent" do
|
77
|
+
@player.calculate_dice_for_next_roll([]).should == 0
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should be 5 if all of the thrown dices are scoring" do
|
81
|
+
@player.calculate_dice_for_next_roll([1]).should == 5
|
82
|
+
@player.calculate_dice_for_next_roll([3,3,3]).should == 5
|
83
|
+
@player.calculate_dice_for_next_roll([5,5,4,4,4]).should == 5
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should be zero for 2s, 3s, 4s, and 6s" do
|
87
|
+
@player.calculate_dice_for_next_roll([2,3,4,6,4]).should == 0
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return the count of dices for mixed dice sets" do
|
91
|
+
@player.calculate_dice_for_next_roll([3,2,2,2]).should == 1
|
92
|
+
@player.calculate_dice_for_next_roll([4,2,5,1,1]).should == 2
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
context "Next roll?" do
|
98
|
+
|
99
|
+
it "should return false if player not have dices for next roll" do
|
100
|
+
@player.instance_variable_set("@count", 0)
|
101
|
+
@player.should_not be_next_roll
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should return true if player enter 'y'" do
|
105
|
+
@player.instance_variable_set("@count", 1)
|
106
|
+
STDIN.stub!(:gets).and_return("y\n")
|
107
|
+
@player.should be_next_roll
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should return false if player enter not 'y'" do
|
111
|
+
@player.instance_variable_set("@count", 1)
|
112
|
+
STDIN.stub!(:gets).and_return("")
|
113
|
+
@player.should_not be_next_roll
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "Score" do
|
119
|
+
context "For dice" do
|
120
|
+
|
121
|
+
it "should be zero if dices are absent" do
|
122
|
+
@player.score([]).should == 0
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should be 50 for single roll of 5" do
|
126
|
+
@player.score([5]).should == 50
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should be 100 for single roll of 1" do
|
130
|
+
@player.score([1]).should == 100
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should return the sum of individual scores for multiple 1s and 5s" do
|
134
|
+
@player.score([1,5,5,1]).should == 300
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should be zero for single 2s, 3s, 4s, and 6s" do
|
138
|
+
@player.score([2,3,4,6]).should == 0
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should be 1000 for a triple 1" do
|
142
|
+
@player.score([1,1,1]).should == 1000
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should return 100x for other triples" do
|
146
|
+
@player.score([2,2,2]).should == 200
|
147
|
+
@player.score([3,3,3]).should == 300
|
148
|
+
@player.score([4,4,4]).should == 400
|
149
|
+
@player.score([5,5,5]).should == 500
|
150
|
+
@player.score([6,6,6]).should == 600
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should return sum for mixed dice sets" do
|
154
|
+
@player.score([2,5,2,2,3]).should == 250
|
155
|
+
@player.score([5,5,5,5]).should == 550
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
context "Roll's" do
|
161
|
+
|
162
|
+
it "should be zero for new player" do
|
163
|
+
@player.score_roll.should == 0
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should be zero if score is zero" do
|
167
|
+
@player.calculate_score_roll(0).should == 0
|
168
|
+
@player.score_roll.should == 0
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should increase by score for dice" do
|
172
|
+
score = @player.score([1])
|
173
|
+
expect { @player.calculate_score_roll(score) }.to change{ @player.score_roll }.by(score)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should set zero for score" do
|
177
|
+
@player.calculate_score_roll(0)
|
178
|
+
@player.instance_variable_get("@score").should == 0
|
179
|
+
@player.calculate_score_roll(100)
|
180
|
+
@player.instance_variable_get("@score").should == 0
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
context "Total" do
|
186
|
+
|
187
|
+
it "should be zero for new player" do
|
188
|
+
@player.score_total.should == 0
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should not increase if total score is zero and roll's score less than 300" do
|
192
|
+
expect { @player.calculate_score_total(250) }.to_not change{ @player.score_total }
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should increase if total score is zero and roll's score equal 300" do
|
196
|
+
expect { @player.calculate_score_total(300) }.to change{ @player.score_total }.by(300)
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should increase if total score is zero and roll's score more than 300" do
|
200
|
+
expect { @player.calculate_score_total(400) }.to change{ @player.score_total }.by(400)
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should increase if total score more than zero and roll's score more than zero" do
|
204
|
+
@player.instance_variable_set(:@score_total, 500)
|
205
|
+
expect{ @player.calculate_score_total(200) }.to change{ @player.score_total }.from(500).to(700)
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should set zero for roll's score" do
|
209
|
+
@player.calculate_score_total(0)
|
210
|
+
@player.instance_variable_get("@score_roll").should == 0
|
211
|
+
@player.calculate_score_total(300)
|
212
|
+
@player.instance_variable_get("@score_roll").should == 0
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "Roll" do
|
219
|
+
|
220
|
+
it "should not increase score total if dices are absent" do
|
221
|
+
dices = 0
|
222
|
+
expect { @player.roll(dices) }.to_not change{ @player.score_total }
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should increase score total if amount dices more than zero" do
|
226
|
+
dices = 5
|
227
|
+
@player.instance_variable_set(:@score_total, 500)
|
228
|
+
@player.stub!(:calculate_dice_for_next_roll).and_return(0)
|
229
|
+
@player.stub!(:calculate_score_roll).and_return(200)
|
230
|
+
expect{ @player.roll(dices) }.to change{ @player.score_total }.from(500).to(700)
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: greed_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- natalili
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &77735400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *77735400
|
25
|
+
description: Greed is a dice game played among 2 or more players, using 5 six-sided
|
26
|
+
dice.
|
27
|
+
email:
|
28
|
+
- natali.li@list.ru
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .rspec
|
35
|
+
- .rvmrc
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- greed_game.gemspec
|
41
|
+
- lib/greed_game.rb
|
42
|
+
- lib/greed_game/dice_set.rb
|
43
|
+
- lib/greed_game/player.rb
|
44
|
+
- lib/greed_game/version.rb
|
45
|
+
- spec/dice_set_spec.rb
|
46
|
+
- spec/greed_game_spec.rb
|
47
|
+
- spec/player_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
homepage: ''
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.15
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Greed is a dice game
|
73
|
+
test_files:
|
74
|
+
- spec/dice_set_spec.rb
|
75
|
+
- spec/greed_game_spec.rb
|
76
|
+
- spec/player_spec.rb
|
77
|
+
- spec/spec_helper.rb
|