studio_game_riverron 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/high_scores.txt +6 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +32 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +34 -0
- data/lib/studio_game/clumsy_player.rb +39 -0
- data/lib/studio_game/die.rb +19 -0
- data/lib/studio_game/game.rb +110 -0
- data/lib/studio_game/game_turn.rb +23 -0
- data/lib/studio_game/iterators.rb +27 -0
- data/lib/studio_game/loaded_die.rb +16 -0
- data/lib/studio_game/playable.rb +17 -0
- data/lib/studio_game/player.rb +58 -0
- data/lib/studio_game/treasure_trove.rb +18 -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 +66 -0
- data/spec/studio_game/player_spec.rb +125 -0
- data/spec/studio_game/treasure_trove_spec.rb +57 -0
- metadata +91 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (C) <2012> <Ronald Rivera>
|
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
|
+
This is an example application used in The Pragmatic Studio's Ruby Programming course.
|
data/bin/high_scores.txt
ADDED
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
module StudioGameRR
|
4
|
+
require_relative '../lib/studio_game/game'
|
5
|
+
require_relative '../lib/studio_game/clumsy_player'
|
6
|
+
require_relative '../lib/studio_game/berserk_player'
|
7
|
+
|
8
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
9
|
+
knuckleheads = StudioGameRR::Game.new("Knuckleheads")
|
10
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
11
|
+
|
12
|
+
clumsy = ClumsyPlayer.new("klutz", 105, 3)
|
13
|
+
knuckleheads.add_player(clumsy)
|
14
|
+
|
15
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
16
|
+
knuckleheads.add_player(berserker)
|
17
|
+
|
18
|
+
loop do
|
19
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
20
|
+
answer = gets.chomp.downcase
|
21
|
+
case answer
|
22
|
+
when /^\d+$/
|
23
|
+
knuckleheads.play(answer.to_i)
|
24
|
+
when 'quit', 'exit'
|
25
|
+
knuckleheads.print_stats
|
26
|
+
knuckleheads.save_high_scores
|
27
|
+
break
|
28
|
+
else
|
29
|
+
puts "Please enter a number or 'quit'"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require_relative 'player'
|
3
|
+
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
def initialize(name, health=100)
|
6
|
+
super(name, health)
|
7
|
+
@w00t_count = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def berserk?
|
11
|
+
@w00t_count > 5
|
12
|
+
end
|
13
|
+
|
14
|
+
def w00t
|
15
|
+
super
|
16
|
+
@w00t_count += 1
|
17
|
+
puts "#{@name} is berserk!" if berserk?
|
18
|
+
end
|
19
|
+
|
20
|
+
def blam
|
21
|
+
if berserk?
|
22
|
+
w00t
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if __FILE__ == $0
|
30
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
31
|
+
6.times { berserker.w00t }
|
32
|
+
2.times { berserker.blam }
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require_relative 'player'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
|
5
|
+
class ClumsyPlayer < Player
|
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 w00t
|
14
|
+
@boost_factor.times { super }
|
15
|
+
end
|
16
|
+
|
17
|
+
def found_treasure(treasure)
|
18
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2)
|
19
|
+
super(damaged_treasure)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if __FILE__ == $0
|
24
|
+
clumsy = ClumsyPlayer.new("klutz", 105, 3)
|
25
|
+
|
26
|
+
hammer = Treasure.new(:hammer, 50)
|
27
|
+
clumsy.found_treasure(hammer)
|
28
|
+
clumsy.found_treasure(hammer)
|
29
|
+
clumsy.found_treasure(hammer)
|
30
|
+
|
31
|
+
crowbar = Treasure.new(:crowbar, 400)
|
32
|
+
clumsy.found_treasure(crowbar)
|
33
|
+
|
34
|
+
clumsy.each_found_treasure do |treasure|
|
35
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
36
|
+
end
|
37
|
+
puts "#{clumsy.points} grand total points"
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'game_turn'
|
5
|
+
require_relative 'treasure_trove'
|
6
|
+
require 'csv'
|
7
|
+
|
8
|
+
class Game
|
9
|
+
attr_reader :title
|
10
|
+
|
11
|
+
def initialize(title)
|
12
|
+
@title = title
|
13
|
+
@players = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_players(from_file)
|
17
|
+
CSV.foreach(from_file) do |row|
|
18
|
+
player = Player.new(row[0], row[1].to_i)
|
19
|
+
add_player(player)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def save_high_scores(to_file="high_scores.txt")
|
24
|
+
File.open(to_file,"w") do |file|
|
25
|
+
file.puts "#{@title} High Scores:"
|
26
|
+
@players.sort.each do |player|
|
27
|
+
file.puts high_score_entry(player)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def high_score_entry(player)
|
33
|
+
formatted_name = player.name.ljust(20, '.')
|
34
|
+
"#{formatted_name} #{player.score}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def add_player(player)
|
38
|
+
@players << player
|
39
|
+
end
|
40
|
+
|
41
|
+
def play(rounds)
|
42
|
+
puts "There are #{@players.size} players in Knuckleheads:"
|
43
|
+
@players.each do |player|
|
44
|
+
puts player
|
45
|
+
end
|
46
|
+
|
47
|
+
treasures = TreasureTrove::TREASURES
|
48
|
+
|
49
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
50
|
+
treasures.each do |treasure|
|
51
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
52
|
+
end
|
53
|
+
|
54
|
+
1.upto(rounds) do |round|
|
55
|
+
if block_given?
|
56
|
+
break if yield
|
57
|
+
end
|
58
|
+
puts "\nRound #{round}:"
|
59
|
+
@players.each do |player|
|
60
|
+
GameTurn.take_turn(player)
|
61
|
+
puts player
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def total_points
|
67
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
68
|
+
end
|
69
|
+
|
70
|
+
def print_stats
|
71
|
+
puts "\n#{@title} Statistics:"
|
72
|
+
|
73
|
+
strong_players = @players.select { |player| player.strong? }
|
74
|
+
wimpy_players = @players.reject { |player| player.strong? }
|
75
|
+
|
76
|
+
# or
|
77
|
+
# strong_players, wimply_players = @players.partition { |player| player.strong? }
|
78
|
+
|
79
|
+
puts "\n#{strong_players.size} strong players:"
|
80
|
+
strong_players.each do |player|
|
81
|
+
print_name_and_health(player)
|
82
|
+
end
|
83
|
+
|
84
|
+
puts "\n#{wimpy_players.size} wimpy players:"
|
85
|
+
wimpy_players.each do |player|
|
86
|
+
print_name_and_health(player)
|
87
|
+
end
|
88
|
+
|
89
|
+
@players.sort.each do |player|
|
90
|
+
puts "\n#{player.name}'s point totals:"
|
91
|
+
player.each_found_treasure do |treasure|
|
92
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
93
|
+
end
|
94
|
+
puts "#{player.points} grand total points"
|
95
|
+
end
|
96
|
+
|
97
|
+
puts "\n#{total_points} total points from treasures found"
|
98
|
+
|
99
|
+
puts "\n#{@title} High Scores:"
|
100
|
+
@players.sort.each do |player|
|
101
|
+
puts high_score_entry(player)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def print_name_and_health(player)
|
106
|
+
formatted_name = player.name.ljust(20, '.')
|
107
|
+
puts "#{formatted_name} #{player.score}"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
require_relative 'loaded_die'
|
6
|
+
|
7
|
+
module GameTurn
|
8
|
+
def self.take_turn(player)
|
9
|
+
die = Die.new
|
10
|
+
case die.roll
|
11
|
+
when 1..2
|
12
|
+
player.blam
|
13
|
+
when 3..4
|
14
|
+
puts "#{player.name} was skipped."
|
15
|
+
else
|
16
|
+
player.w00t
|
17
|
+
end
|
18
|
+
|
19
|
+
treasure = TreasureTrove.random
|
20
|
+
player.found_treasure(treasure)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
def conversation
|
2
|
+
puts "Hello"
|
3
|
+
yield
|
4
|
+
puts "Goodbye"
|
5
|
+
end
|
6
|
+
|
7
|
+
conversation { puts "Good to meet you!" }
|
8
|
+
|
9
|
+
def five_times
|
10
|
+
1.upto(5) { |count| yield count }
|
11
|
+
end
|
12
|
+
|
13
|
+
five_times do |n|
|
14
|
+
puts "#{n} situps"
|
15
|
+
puts "#{n} pushups"
|
16
|
+
puts "#{n} chinups"
|
17
|
+
end
|
18
|
+
|
19
|
+
def n_times(number)
|
20
|
+
1.upto(number) { |count| yield count }
|
21
|
+
end
|
22
|
+
|
23
|
+
n_times(5) do |n|
|
24
|
+
puts "#{n} situps"
|
25
|
+
puts "#{n} pushups"
|
26
|
+
puts "#{n} chinups"
|
27
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require_relative 'playable.rb'
|
3
|
+
|
4
|
+
class Player
|
5
|
+
include Playable
|
6
|
+
|
7
|
+
attr_accessor :name
|
8
|
+
attr_accessor :health
|
9
|
+
|
10
|
+
def initialize(name, health=100)
|
11
|
+
@name = name.capitalize
|
12
|
+
@health = health
|
13
|
+
@found_treasures = Hash.new(0)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_csv(string)
|
17
|
+
name, health = string.split(',')
|
18
|
+
new(name, Integer(health))
|
19
|
+
end
|
20
|
+
|
21
|
+
def points
|
22
|
+
@found_treasures.values.reduce(0, :+)
|
23
|
+
end
|
24
|
+
|
25
|
+
def found_treasure(treasure)
|
26
|
+
@found_treasures[treasure.name] += treasure.points
|
27
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_s
|
31
|
+
"I'm #{@name.capitalize} with health = #{@health}, points = #{points}, and score = #{score}."
|
32
|
+
end
|
33
|
+
|
34
|
+
def <=>(other)
|
35
|
+
other.score <=> score
|
36
|
+
end
|
37
|
+
|
38
|
+
def score
|
39
|
+
@health + points
|
40
|
+
end
|
41
|
+
|
42
|
+
def each_found_treasure
|
43
|
+
@found_treasures.each do |name, points|
|
44
|
+
yield Treasure.new(name, points)
|
45
|
+
end
|
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,18 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
Treasure = Struct.new(:name, :points)
|
3
|
+
|
4
|
+
module TreasureTrove
|
5
|
+
TREASURES = [
|
6
|
+
Treasure.new(:pie, 5),
|
7
|
+
Treasure.new(:bottle, 25),
|
8
|
+
Treasure.new(:hammer, 50),
|
9
|
+
Treasure.new(:skillet, 100),
|
10
|
+
Treasure.new(:broomstick, 200),
|
11
|
+
Treasure.new(:crowbar, 400),
|
12
|
+
]
|
13
|
+
|
14
|
+
def self.random
|
15
|
+
TREASURES.sample
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require 'studio_game/berserk_player'
|
3
|
+
|
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
|
+
module StudioGameRR
|
2
|
+
require 'studio_game/clumsy_player'
|
3
|
+
|
4
|
+
describe ClumsyPlayer do
|
5
|
+
before do
|
6
|
+
@player = ClumsyPlayer.new("klutz")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "only gets half the point value for each treasure" do
|
10
|
+
@player.points.should == 0
|
11
|
+
|
12
|
+
hammer = Treasure.new(:hammer, 50)
|
13
|
+
@player.found_treasure(hammer)
|
14
|
+
@player.found_treasure(hammer)
|
15
|
+
@player.found_treasure(hammer)
|
16
|
+
|
17
|
+
@player.points.should == 75
|
18
|
+
|
19
|
+
crowbar = Treasure.new(:crowbar, 400)
|
20
|
+
@player.found_treasure(crowbar)
|
21
|
+
|
22
|
+
@player.points.should == 275
|
23
|
+
|
24
|
+
yielded = []
|
25
|
+
@player.each_found_treasure do |treasure|
|
26
|
+
yielded << treasure
|
27
|
+
end
|
28
|
+
|
29
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
30
|
+
end
|
31
|
+
|
32
|
+
context "with a boost factor" do
|
33
|
+
before do
|
34
|
+
@initial_health = 100
|
35
|
+
@boost_factor = 5
|
36
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has a boost factor" do
|
40
|
+
@player.boost_factor.should == 5
|
41
|
+
end
|
42
|
+
|
43
|
+
it "gets boost factor number of w00ts when w00ted" do
|
44
|
+
@player.w00t
|
45
|
+
|
46
|
+
@player.health.should == @initial_health + (15 * @boost_factor)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require 'studio_game/game'
|
3
|
+
|
4
|
+
describe Game do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@game = Game.new("Knuckleheads")
|
8
|
+
|
9
|
+
@initial_health = 100
|
10
|
+
@player = Player.new("moe", @initial_health)
|
11
|
+
|
12
|
+
@game.add_player(@player)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "w00ts the player if a high number is rolled" do
|
16
|
+
Die.any_instance.stub(:roll).and_return(5)
|
17
|
+
|
18
|
+
@game.play(2)
|
19
|
+
|
20
|
+
@player.health.should == @initial_health + (15 * 2)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "skips the player if a medium number is rolled" do
|
24
|
+
Die.any_instance.stub(:roll).and_return(3)
|
25
|
+
|
26
|
+
@game.play(2)
|
27
|
+
|
28
|
+
@player.health.should == @initial_health
|
29
|
+
end
|
30
|
+
|
31
|
+
it "blams the player if a low number is rolled" do
|
32
|
+
Die.any_instance.stub(:roll).and_return(1)
|
33
|
+
|
34
|
+
@game.play(2)
|
35
|
+
|
36
|
+
@player.health.should == @initial_health - (10 * 2)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "assigns a treasure for points during a player's turn" do
|
40
|
+
game = Game.new("Knuckleheads")
|
41
|
+
player = Player.new("moe")
|
42
|
+
|
43
|
+
game.add_player(player)
|
44
|
+
|
45
|
+
game.play(1)
|
46
|
+
|
47
|
+
player.points.should_not be_zero
|
48
|
+
end
|
49
|
+
|
50
|
+
it "computes total points as the sum of all player points" do
|
51
|
+
game = Game.new("Knuckleheads")
|
52
|
+
|
53
|
+
player1 = Player.new("moe")
|
54
|
+
player2 = Player.new("larry")
|
55
|
+
|
56
|
+
game.add_player(player1)
|
57
|
+
game.add_player(player2)
|
58
|
+
|
59
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
60
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
61
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
62
|
+
|
63
|
+
game.total_points.should == 500
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require 'studio_game/player'
|
3
|
+
require 'studio_game/treasure_trove'
|
4
|
+
|
5
|
+
describe Player do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@initial_health = 150
|
9
|
+
@player = Player.new("larry", @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "has a capitalized name" do
|
13
|
+
@player.name.should == "Larry"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has an initial health" do
|
17
|
+
@player.health.should == 150
|
18
|
+
end
|
19
|
+
|
20
|
+
it "computes a score as the sum of its health and points" do
|
21
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
22
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
23
|
+
|
24
|
+
@player.score.should == 250
|
25
|
+
end
|
26
|
+
|
27
|
+
it "has a string representation" do
|
28
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
29
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
30
|
+
|
31
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
|
32
|
+
end
|
33
|
+
|
34
|
+
it "increases health by 15 when w00ted" do
|
35
|
+
@player.w00t
|
36
|
+
|
37
|
+
@player.health.should == @initial_health + 15
|
38
|
+
end
|
39
|
+
|
40
|
+
it "decreases health by 10 when blammed" do
|
41
|
+
@player.blam
|
42
|
+
|
43
|
+
@player.health.should == @initial_health - 10
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with a health greater than 100" do
|
47
|
+
before do
|
48
|
+
@player = Player.new("larry", 150)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "is strong" do
|
52
|
+
@player.should be_strong
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "with a health of 100 or less" do
|
57
|
+
before do
|
58
|
+
@player = Player.new("larry", 100)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "is wimpy" do
|
62
|
+
@player.should_not be_strong
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "in a collection of players" do
|
67
|
+
before do
|
68
|
+
@player1 = Player.new("moe", 100)
|
69
|
+
@player2 = Player.new("larry", 200)
|
70
|
+
@player3 = Player.new("curly", 300)
|
71
|
+
|
72
|
+
@players = [@player1, @player2, @player3]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "is sorted by decreasing score" do
|
76
|
+
@players.sort.should == [@player3, @player2, @player1]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it "computes points as the sum of all treasure points" do
|
81
|
+
@player.points.should == 0
|
82
|
+
|
83
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
84
|
+
|
85
|
+
@player.points.should == 50
|
86
|
+
|
87
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
88
|
+
|
89
|
+
@player.points.should == 450
|
90
|
+
|
91
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
92
|
+
|
93
|
+
@player.points.should == 500
|
94
|
+
end
|
95
|
+
|
96
|
+
it "yields each found treasure and its total points" do
|
97
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
98
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
99
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
100
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
101
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
102
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
103
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
104
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
105
|
+
|
106
|
+
yielded = []
|
107
|
+
@player.each_found_treasure do |treasure|
|
108
|
+
yielded << treasure
|
109
|
+
end
|
110
|
+
|
111
|
+
yielded.should == [
|
112
|
+
Treasure.new(:skillet, 200),
|
113
|
+
Treasure.new(:hammer, 50),
|
114
|
+
Treasure.new(:bottle, 25)
|
115
|
+
]
|
116
|
+
end
|
117
|
+
|
118
|
+
it "can be created from a CSV string" do
|
119
|
+
player = Player.from_csv("larry,150")
|
120
|
+
|
121
|
+
player.name.should == "Larry"
|
122
|
+
player.health.should == 150
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module StudioGameRR
|
2
|
+
require 'studio_game/treasure_trove'
|
3
|
+
|
4
|
+
describe Treasure do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@treasure = Treasure.new(:hammer, 50)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has a name attribute" do
|
11
|
+
@treasure.name.should == :hammer
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has a points attribute" do
|
15
|
+
@treasure.points.should == 50
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
describe TreasureTrove do
|
21
|
+
|
22
|
+
it "has six treasures" do
|
23
|
+
TreasureTrove::TREASURES.size.should == 6
|
24
|
+
end
|
25
|
+
|
26
|
+
it "has a pie worth 5 points" do
|
27
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "has a bottle worth 25 points" do
|
31
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "has a hammer worth 50 points" do
|
35
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "has a skillet worth 100 points" do
|
39
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "has a broomstick worth 200 points" do
|
43
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "has a crowbar worth 400 points" do
|
47
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns a random treasure" do
|
51
|
+
treasure = TreasureTrove.random
|
52
|
+
|
53
|
+
TreasureTrove::TREASURES.should include(treasure)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_riverron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ronald Rivera
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !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: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ! 'This is an example application used in The Pragmatic Studio''s Ruby
|
31
|
+
Programming course.
|
32
|
+
|
33
|
+
'
|
34
|
+
email: roncrivera@gmail.com
|
35
|
+
executables:
|
36
|
+
- studio_game
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- bin/high_scores.txt
|
41
|
+
- bin/players.csv
|
42
|
+
- bin/studio_game
|
43
|
+
- lib/studio_game/auditable.rb
|
44
|
+
- lib/studio_game/berserk_player.rb
|
45
|
+
- lib/studio_game/clumsy_player.rb
|
46
|
+
- lib/studio_game/die.rb
|
47
|
+
- lib/studio_game/game.rb
|
48
|
+
- lib/studio_game/game_turn.rb
|
49
|
+
- lib/studio_game/iterators.rb
|
50
|
+
- lib/studio_game/loaded_die.rb
|
51
|
+
- lib/studio_game/playable.rb
|
52
|
+
- lib/studio_game/player.rb
|
53
|
+
- lib/studio_game/treasure_trove.rb
|
54
|
+
- spec/studio_game/berserk_player_spec.rb
|
55
|
+
- spec/studio_game/clumsy_player_spec.rb
|
56
|
+
- spec/studio_game/game_spec.rb
|
57
|
+
- spec/studio_game/player_spec.rb
|
58
|
+
- spec/studio_game/treasure_trove_spec.rb
|
59
|
+
- LICENSE
|
60
|
+
- README
|
61
|
+
homepage: https://github.com/riverron
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '1.9'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.24
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: This is an example application used in The Pragmatic Studio's Ruby Programming
|
85
|
+
course.
|
86
|
+
test_files:
|
87
|
+
- spec/studio_game/berserk_player_spec.rb
|
88
|
+
- spec/studio_game/clumsy_player_spec.rb
|
89
|
+
- spec/studio_game/game_spec.rb
|
90
|
+
- spec/studio_game/player_spec.rb
|
91
|
+
- spec/studio_game/treasure_trove_spec.rb
|