studio_game_alan_mccann 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +7 -0
- data/README +1 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +31 -0
- data/lib/studio_game/auditable.rb +8 -0
- data/lib/studio_game/berserk_player.rb +34 -0
- data/lib/studio_game/clumsy_player.rb +41 -0
- data/lib/studio_game/die.rb +19 -0
- data/lib/studio_game/game.rb +94 -0
- data/lib/studio_game/game_turn.rb +22 -0
- data/lib/studio_game/iterators.rb +21 -0
- data/lib/studio_game/loaded_die.rb +14 -0
- data/lib/studio_game/playable.rb +17 -0
- data/lib/studio_game/player.rb +58 -0
- data/lib/studio_game/treasure_trove.rb +20 -0
- data/spec/studio_game/berserk_player_spec.rb +31 -0
- data/spec/studio_game/clumsy_player_spec.rb +50 -0
- data/spec/studio_game/game_spec.rb +60 -0
- data/spec/studio_game/player_spec.rb +114 -0
- data/spec/studio_game/treasure_trove_spec.rb +56 -0
- metadata +84 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (C) 2012 Alan McCann
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gem file from Pragmatic Studio Ruby Course
|
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/studio_game/game'
|
4
|
+
require_relative '../lib/studio_game/player'
|
5
|
+
require_relative '../lib/studio_game/berserk_player'
|
6
|
+
|
7
|
+
game = StudioGame::Game.new("Knuckleheads")
|
8
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
9
|
+
game.load_players(ARGV.shift || default_player_file)
|
10
|
+
clumsy_player = StudioGame::ClumsyPlayer.new("klutz", 105)
|
11
|
+
game.add_player(clumsy_player)
|
12
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
13
|
+
game.add_player(berserker)
|
14
|
+
|
15
|
+
loop do
|
16
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
17
|
+
answer = gets.chomp.downcase
|
18
|
+
case answer
|
19
|
+
when /^\d+$/
|
20
|
+
game.play(answer.to_i)
|
21
|
+
when 'quit', 'exit'
|
22
|
+
game.print_stats
|
23
|
+
game.save_high_scores
|
24
|
+
break
|
25
|
+
else
|
26
|
+
puts "Please enter a number or 'quit'"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'game'
|
2
|
+
module StudioGame
|
3
|
+
class BerserkPlayer < Player
|
4
|
+
def initialize(name,health=100)
|
5
|
+
super(name,health)
|
6
|
+
@w00t_count = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def berserk?
|
10
|
+
@w00t_count > 5
|
11
|
+
end
|
12
|
+
|
13
|
+
def w00t
|
14
|
+
super
|
15
|
+
@w00t_count += 1
|
16
|
+
puts "#{@name} is berserk!" if berserk?
|
17
|
+
end
|
18
|
+
|
19
|
+
def blam
|
20
|
+
if berserk?
|
21
|
+
w00t
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
if __FILE__ == $0
|
29
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
30
|
+
6.times { berserker.w00t }
|
31
|
+
2.times { berserker.blam }
|
32
|
+
puts berserker.health
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'game'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class ClumsyPlayer < Player
|
5
|
+
|
6
|
+
attr_reader :boost_factor
|
7
|
+
|
8
|
+
def initialize(name, health=100, boost_factor=1)
|
9
|
+
super(name,health)
|
10
|
+
@boost_factor = boost_factor
|
11
|
+
end
|
12
|
+
|
13
|
+
def found_treasure(treasure)
|
14
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2)
|
15
|
+
super(damaged_treasure)
|
16
|
+
end
|
17
|
+
|
18
|
+
def w00t
|
19
|
+
@boost_factor.times { super }
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
if __FILE__ == $0
|
26
|
+
clumsy = ClumsyPlayer.new("klutz", 105, 3)
|
27
|
+
|
28
|
+
hammer = Treasure.new(:hammer, 50)
|
29
|
+
clumsy.found_treasure(hammer)
|
30
|
+
clumsy.found_treasure(hammer)
|
31
|
+
clumsy.found_treasure(hammer)
|
32
|
+
|
33
|
+
crowbar = Treasure.new(:crowbar, 400)
|
34
|
+
clumsy.found_treasure(crowbar)
|
35
|
+
|
36
|
+
clumsy.each_found_treasure do |treasure|
|
37
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
38
|
+
end
|
39
|
+
puts "#{clumsy.points} grand total points"
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'game_turn'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
require_relative 'clumsy_player'
|
6
|
+
module StudioGame
|
7
|
+
class Game
|
8
|
+
|
9
|
+
attr_reader :title
|
10
|
+
|
11
|
+
def initialize(title)
|
12
|
+
@title = title
|
13
|
+
@players = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_player(player)
|
17
|
+
@players << player
|
18
|
+
end
|
19
|
+
|
20
|
+
def load_players(from_file)
|
21
|
+
File.readlines(from_file).each do |line|
|
22
|
+
name, health = line.split(',')
|
23
|
+
player = Player.new(name, Integer(health))
|
24
|
+
add_player(player)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def save_high_scores(to_file="high_scores.txt")
|
30
|
+
File.open(to_file, "w") do |file|
|
31
|
+
file.puts "#{@title} High Scores:"
|
32
|
+
@players.sort.each do |player|
|
33
|
+
formatted_name = player.name.ljust(20, '.')
|
34
|
+
file.puts "#{formatted_name} #{player.score}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def play(rounds)
|
40
|
+
|
41
|
+
puts "There are #{@players.size} players in #{@title}:"
|
42
|
+
puts @players
|
43
|
+
treasures = TreasureTrove::TREASURES
|
44
|
+
puts "\nThere are #{treasures.count} treasures to be found."
|
45
|
+
treasures.each do |treasure|
|
46
|
+
puts "A #{treasure.name.to_s} is worth #{treasure.points} points."
|
47
|
+
end
|
48
|
+
1.upto rounds do |round|
|
49
|
+
if block_given?
|
50
|
+
break if yield
|
51
|
+
end
|
52
|
+
puts "\nRound #{round}:"
|
53
|
+
@players.each do |player|
|
54
|
+
GameTurn.take_turn(player)
|
55
|
+
puts player
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def print_stats
|
61
|
+
puts "#{@title} Statistics:"
|
62
|
+
strong, wimpy = @players.partition {|x| x.strong?}
|
63
|
+
puts "#{strong.count} strong players"
|
64
|
+
strong.each do |player|
|
65
|
+
puts "#{player.name} (#{player.health})"
|
66
|
+
end
|
67
|
+
puts "#{wimpy.count} wimpy players"
|
68
|
+
wimpy.each do |player|
|
69
|
+
puts "#{player.name} (#{player.health})"
|
70
|
+
end
|
71
|
+
puts "#{@title} High Scores:"
|
72
|
+
@players.sort.each do |player|
|
73
|
+
formatted_name = player.name.ljust(20, '.')
|
74
|
+
puts "#{formatted_name}: #{player.score}"
|
75
|
+
end
|
76
|
+
|
77
|
+
@players.sort.each do |player|
|
78
|
+
puts "\n#{player.name}'s point totals:"
|
79
|
+
player.each_found_treasure do |treasure|
|
80
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
81
|
+
end
|
82
|
+
puts "#{player.points} grand total points"
|
83
|
+
end
|
84
|
+
|
85
|
+
puts "\nTotal Treasure Points: #{total_points}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def total_points
|
89
|
+
@players.reduce(0) {|sum,player| sum + player.points}
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative 'game'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'loaded_die'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
module GameTurn
|
7
|
+
|
8
|
+
def self.take_turn(player)
|
9
|
+
die = Die.new
|
10
|
+
roll = die.roll
|
11
|
+
case roll
|
12
|
+
when 1..2
|
13
|
+
player.blam
|
14
|
+
when 3..4
|
15
|
+
else
|
16
|
+
player.w00t
|
17
|
+
end
|
18
|
+
treasure = TreasureTrove.random
|
19
|
+
player.found_treasure(treasure)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
def conversation
|
2
|
+
puts "Hello"
|
3
|
+
yield
|
4
|
+
puts "Goodbye"
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
conversation { puts "Good to meet you!" }
|
9
|
+
|
10
|
+
|
11
|
+
def n_times(maximum)
|
12
|
+
1.upto maximum do |count|
|
13
|
+
yield(count)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
n_times(6) do |n|
|
18
|
+
puts "#{n} situps"
|
19
|
+
puts "#{n} pushups"
|
20
|
+
puts "#{n} chinups"
|
21
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative 'playable'
|
2
|
+
module StudioGame
|
3
|
+
class Player
|
4
|
+
|
5
|
+
include Playable
|
6
|
+
|
7
|
+
attr_accessor :health
|
8
|
+
|
9
|
+
attr_accessor :name
|
10
|
+
|
11
|
+
def initialize(name,health=100)
|
12
|
+
@name = name.capitalize
|
13
|
+
@health = health
|
14
|
+
@found_treasures = Hash.new(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
"I'm #{@name.capitalize} with health = #{@health}, points = #{points}, and score = #{score}."
|
19
|
+
end
|
20
|
+
|
21
|
+
def score
|
22
|
+
@health + points
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
def <=>(other)
|
28
|
+
other.score <=> score
|
29
|
+
end
|
30
|
+
|
31
|
+
def found_treasure(treasure)
|
32
|
+
@found_treasures[treasure.name] += treasure.points
|
33
|
+
puts "#{name} found a #{treasure.name} worth #{treasure.points}."
|
34
|
+
puts "#{name}'s treasures: #{@found_treasures}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def points
|
38
|
+
@found_treasures.values.reduce(0,:+)
|
39
|
+
end
|
40
|
+
|
41
|
+
def each_found_treasure
|
42
|
+
@found_treasures.each do |name,points|
|
43
|
+
yield(Treasure.new(name, points))
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
if __FILE__ == $0
|
50
|
+
player = Player.new("moe")
|
51
|
+
puts player.name
|
52
|
+
puts player.health
|
53
|
+
player.w00t
|
54
|
+
puts player.health
|
55
|
+
player.blam
|
56
|
+
puts player.health
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module StudioGame
|
2
|
+
Treasure = Struct.new(:name, :points)
|
3
|
+
|
4
|
+
module TreasureTrove
|
5
|
+
|
6
|
+
TREASURES = [
|
7
|
+
Treasure.new(:pie,5),
|
8
|
+
Treasure.new(:bottle, 25),
|
9
|
+
Treasure.new(:hammer, 50),
|
10
|
+
Treasure.new(:skillet, 100),
|
11
|
+
Treasure.new(:broomstick, 200),
|
12
|
+
Treasure.new(:crowbar, 400)
|
13
|
+
]
|
14
|
+
|
15
|
+
def self.random
|
16
|
+
TREASURES.sample
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'studio_game/berserk_player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe BerserkPlayer do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@initial_health = 50
|
8
|
+
@player = BerserkPlayer.new("berserker", @initial_health)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "does not go berserk when w00ted up to 5 times" do
|
12
|
+
1.upto(5) { @player.w00t }
|
13
|
+
|
14
|
+
@player.berserk?.should be_false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "goes berserk when w00ted more than 5 times" do
|
18
|
+
1.upto(6) { @player.w00t }
|
19
|
+
|
20
|
+
@player.berserk?.should be_true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
24
|
+
1.upto(6) { @player.w00t }
|
25
|
+
1.upto(2) { @player.blam }
|
26
|
+
|
27
|
+
@player.health.should == @initial_health + (8 * 15)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'studio_game/clumsy_player'
|
2
|
+
module StudioGame
|
3
|
+
describe ClumsyPlayer do
|
4
|
+
before do
|
5
|
+
@player = ClumsyPlayer.new("klutz")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "only gets half the point value for each treasure" do
|
9
|
+
@player.points.should == 0
|
10
|
+
|
11
|
+
hammer = Treasure.new(:hammer, 50)
|
12
|
+
@player.found_treasure(hammer)
|
13
|
+
@player.found_treasure(hammer)
|
14
|
+
@player.found_treasure(hammer)
|
15
|
+
|
16
|
+
@player.points.should == 75
|
17
|
+
|
18
|
+
crowbar = Treasure.new(:crowbar, 400)
|
19
|
+
@player.found_treasure(crowbar)
|
20
|
+
|
21
|
+
@player.points.should == 275
|
22
|
+
|
23
|
+
yielded = []
|
24
|
+
@player.each_found_treasure do |treasure|
|
25
|
+
yielded << treasure
|
26
|
+
end
|
27
|
+
|
28
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with a boost factor" do
|
32
|
+
before do
|
33
|
+
@initial_health = 100
|
34
|
+
@boost_factor = 5
|
35
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "has a boost factor" do
|
39
|
+
@player.boost_factor.should == 5
|
40
|
+
end
|
41
|
+
|
42
|
+
it "gets boost factor number of w00ts when w00ted" do
|
43
|
+
@player.w00t
|
44
|
+
|
45
|
+
@player.health.should == @initial_health + (15 * @boost_factor)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
module StudioGame
|
3
|
+
describe Game do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@game = Game.new("Knuckleheads")
|
7
|
+
@rounds = 3
|
8
|
+
@initial_health = 100
|
9
|
+
@player = Player.new("moe", @initial_health)
|
10
|
+
|
11
|
+
@game.add_player(@player)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "w00t the player if a high number is rolled" do
|
15
|
+
Die.any_instance.stub(:roll).and_return(5)
|
16
|
+
@game.play(@rounds)
|
17
|
+
@player.health.should == @initial_health + (@rounds * 15)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "ignore the player if a medium number is rolled" do
|
21
|
+
Die.any_instance.stub(:roll).and_return(4)
|
22
|
+
@game.play(@rounds)
|
23
|
+
@player.health.should == @initial_health
|
24
|
+
end
|
25
|
+
|
26
|
+
it "blam the player if a low number is rolled" do
|
27
|
+
Die.any_instance.stub(:roll).and_return(2)
|
28
|
+
@game.play(@rounds)
|
29
|
+
@player.health.should == @initial_health - (10 * @rounds)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "assigns a treasure for points during a player's turn" do
|
33
|
+
game = Game.new("Knuckleheads")
|
34
|
+
player = Player.new("moe")
|
35
|
+
|
36
|
+
game.add_player(player)
|
37
|
+
|
38
|
+
game.play(1)
|
39
|
+
|
40
|
+
player.points.should_not be_zero
|
41
|
+
end
|
42
|
+
|
43
|
+
it "computes total points as the sum of all player points" do
|
44
|
+
game = Game.new("Knuckleheads")
|
45
|
+
|
46
|
+
player1 = Player.new("moe")
|
47
|
+
player2 = Player.new("larry")
|
48
|
+
|
49
|
+
game.add_player(player1)
|
50
|
+
game.add_player(player2)
|
51
|
+
|
52
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
53
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
54
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
55
|
+
|
56
|
+
game.total_points.should == 500
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'studio_game/player'
|
2
|
+
require 'studio_game/treasure_trove'
|
3
|
+
module StudioGame
|
4
|
+
describe Player do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@initial_health = 150
|
8
|
+
@player = Player.new("larry", @initial_health)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "has a capitalized name" do
|
12
|
+
@player.name.should == "Larry"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "has an initial health" do
|
16
|
+
@player.health.should == 150
|
17
|
+
end
|
18
|
+
|
19
|
+
it "has a string representation" do
|
20
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 0, and score = 150."
|
21
|
+
end
|
22
|
+
|
23
|
+
it "computes a score as the sum of its health and points" do
|
24
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
25
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
26
|
+
|
27
|
+
@player.score.should == 250
|
28
|
+
end
|
29
|
+
|
30
|
+
it "increases health by 15 when w00ted" do
|
31
|
+
@player.w00t
|
32
|
+
@player.health.should == @initial_health + 15
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
it "decreases health by 10 when blammed" do
|
37
|
+
@player.blam
|
38
|
+
@player.health.should == @initial_health - 10
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with a health greate than 100" do
|
42
|
+
before do
|
43
|
+
@player = Player.new("larry", 150)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "is strong" do
|
47
|
+
@player.should be_strong
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "with a health equal to 100" do
|
52
|
+
before do
|
53
|
+
@player = Player.new("larry", 100)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "is not strong" do
|
57
|
+
@player.should_not be_strong
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "in a collection of players" do
|
62
|
+
before do
|
63
|
+
@player1 = Player.new("moe", 100)
|
64
|
+
@player2 = Player.new("larry", 200)
|
65
|
+
@player3 = Player.new("curly", 300)
|
66
|
+
|
67
|
+
@players = [@player1, @player2, @player3]
|
68
|
+
end
|
69
|
+
|
70
|
+
it "is sorted by decreasing score" do
|
71
|
+
@players.sort.should == [@player3, @player2, @player1]
|
72
|
+
end
|
73
|
+
|
74
|
+
it "computes points as the sum of all treasure points" do
|
75
|
+
@player.points.should == 0
|
76
|
+
|
77
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
78
|
+
|
79
|
+
@player.points.should == 50
|
80
|
+
|
81
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
82
|
+
|
83
|
+
@player.points.should == 450
|
84
|
+
|
85
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
86
|
+
|
87
|
+
@player.points.should == 500
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
it "yields each found treasure and its total points" do
|
93
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
94
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
95
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
96
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
97
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
98
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
99
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
100
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
101
|
+
|
102
|
+
yielded = []
|
103
|
+
@player.each_found_treasure do |treasure|
|
104
|
+
yielded << treasure
|
105
|
+
end
|
106
|
+
|
107
|
+
yielded.should == [
|
108
|
+
Treasure.new(:skillet, 200),
|
109
|
+
Treasure.new(:hammer, 50),
|
110
|
+
Treasure.new(:bottle, 25)
|
111
|
+
]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'studio_game/treasure_trove'
|
2
|
+
module StudioGame
|
3
|
+
describe Treasure do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@treasure = Treasure.new(:hammer, 50)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has a name attribute" do
|
10
|
+
@treasure.name.should == :hammer
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has a points attribute" do
|
14
|
+
@treasure.points.should == 50
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
describe TreasureTrove do
|
20
|
+
|
21
|
+
it "has six treasures" do
|
22
|
+
TreasureTrove::TREASURES.size.should == 6
|
23
|
+
end
|
24
|
+
|
25
|
+
it "has a pie worth 5 points" do
|
26
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "has a bottle worth 25 points" do
|
30
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "has a hammer worth 50 points" do
|
34
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "has a skillet worth 100 points" do
|
38
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "has a broomstick worth 200 points" do
|
42
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "has a crowbar worth 400 points" do
|
46
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns a random treasure" do
|
50
|
+
treasure = TreasureTrove.random
|
51
|
+
|
52
|
+
TreasureTrove::TREASURES.should include(treasure)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_alan_mccann
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alan McCann
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70206378886060 !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: *70206378886060
|
25
|
+
description: ! 'Gem file from Pragmatic Studio Ruby Course
|
26
|
+
|
27
|
+
'
|
28
|
+
email: alan@imccann.com
|
29
|
+
executables:
|
30
|
+
- studio_game
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- bin/players.csv
|
35
|
+
- bin/studio_game
|
36
|
+
- lib/studio_game/auditable.rb
|
37
|
+
- lib/studio_game/berserk_player.rb
|
38
|
+
- lib/studio_game/clumsy_player.rb
|
39
|
+
- lib/studio_game/die.rb
|
40
|
+
- lib/studio_game/game.rb
|
41
|
+
- lib/studio_game/game_turn.rb
|
42
|
+
- lib/studio_game/iterators.rb
|
43
|
+
- lib/studio_game/loaded_die.rb
|
44
|
+
- lib/studio_game/playable.rb
|
45
|
+
- lib/studio_game/player.rb
|
46
|
+
- lib/studio_game/treasure_trove.rb
|
47
|
+
- spec/studio_game/berserk_player_spec.rb
|
48
|
+
- spec/studio_game/clumsy_player_spec.rb
|
49
|
+
- spec/studio_game/game_spec.rb
|
50
|
+
- spec/studio_game/player_spec.rb
|
51
|
+
- spec/studio_game/treasure_trove_spec.rb
|
52
|
+
- LICENSE
|
53
|
+
- README
|
54
|
+
homepage: http://www.alanmccann.com
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '1.9'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.16
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Pragmatic Studio Ruby Course Exercise Results
|
78
|
+
test_files:
|
79
|
+
- spec/studio_game/berserk_player_spec.rb
|
80
|
+
- spec/studio_game/clumsy_player_spec.rb
|
81
|
+
- spec/studio_game/game_spec.rb
|
82
|
+
- spec/studio_game/player_spec.rb
|
83
|
+
- spec/studio_game/treasure_trove_spec.rb
|
84
|
+
has_rdoc:
|