studio_game_bdu 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 +36 -0
- data/lib/studio_game/auditable.rb +9 -0
- data/lib/studio_game/berserk_player.rb +26 -0
- data/lib/studio_game/clumsy_player.rb +40 -0
- data/lib/studio_game/die.rb +20 -0
- data/lib/studio_game/game.rb +120 -0
- data/lib/studio_game/game_turn.rb +25 -0
- data/lib/studio_game/high_score_file.txt +1 -0
- data/lib/studio_game/high_scores.txt +7 -0
- data/lib/studio_game/loaded_die.rb +18 -0
- data/lib/studio_game/my_favorite_players.csv +5 -0
- data/lib/studio_game/playable.rb +23 -0
- data/lib/studio_game/player.rb +79 -0
- data/lib/studio_game/treasure_trove.rb +20 -0
- data/spec/studio_game/berserk_player_spec.rb +33 -0
- data/spec/studio_game/clumsy_player_spec.rb +32 -0
- data/spec/studio_game/game_spec.rb +101 -0
- data/spec/studio_game/player_spec.rb +153 -0
- data/spec/studio_game/rspect_player_file.csv +1 -0
- data/spec/studio_game/studio_game.rb +32 -0
- data/spec/studio_game/treasure_trove_spec.rb +56 -0
- metadata +92 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (C) 2013 Pragmatic Studios
|
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
|
+
Pragmatic Studios ruby programing course
|
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/studio_game/game'
|
4
|
+
require_relative '../lib/studio_game/clumsy_player'
|
5
|
+
require_relative '../lib/studio_game/berserk_player'
|
6
|
+
|
7
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
8
|
+
|
9
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
10
|
+
|
11
|
+
players = ARGV.shift || default_player_file
|
12
|
+
knuckleheads.load_players(players)
|
13
|
+
|
14
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105, 5)
|
15
|
+
knuckleheads.add_player(klutz)
|
16
|
+
|
17
|
+
berzerker = StudioGame::BerserkPlayer.new("berzerker", 50)
|
18
|
+
knuckleheads.add_player(berzerker)
|
19
|
+
|
20
|
+
loop do
|
21
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
22
|
+
rounds = gets.chomp.downcase
|
23
|
+
case rounds
|
24
|
+
when /^\d+$/
|
25
|
+
knuckleheads.play(rounds.to_i)
|
26
|
+
when 'quit', 'exit'
|
27
|
+
knuckleheads.print_stats
|
28
|
+
knuckleheads.save_high_scores
|
29
|
+
break
|
30
|
+
else
|
31
|
+
puts "Please put a legitimate response, an integer or 'quit'."
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module StudioGame
|
2
|
+
require_relative 'player'
|
3
|
+
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
|
6
|
+
def initialize(name, health=100)
|
7
|
+
super
|
8
|
+
@w00t_count = 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def berserk?
|
12
|
+
@w00t_count > 5
|
13
|
+
end
|
14
|
+
|
15
|
+
def w00t
|
16
|
+
super
|
17
|
+
@w00t_count += 1
|
18
|
+
puts "#{name} is berserk!" if berserk?
|
19
|
+
end
|
20
|
+
|
21
|
+
def blam
|
22
|
+
berserk? ? w00t : super
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module StudioGame
|
2
|
+
require_relative 'player'
|
3
|
+
|
4
|
+
class ClumsyPlayer < Player
|
5
|
+
|
6
|
+
def initialize(name, health=100, boost_factor=1)
|
7
|
+
super(name, health)
|
8
|
+
@boost_factor = boost_factor
|
9
|
+
end
|
10
|
+
|
11
|
+
def w00t
|
12
|
+
@boost_factor.times { super }
|
13
|
+
end
|
14
|
+
|
15
|
+
def found_treasure(treasure)
|
16
|
+
clumsy_treasure = Treasure.new(treasure.name, treasure.points/2.0)
|
17
|
+
super(clumsy_treasure)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
if __FILE__ == $0
|
25
|
+
clumsy = ClumsyPlayer.new("klutz")
|
26
|
+
|
27
|
+
hammer = Treasure.new(:hammer, 50)
|
28
|
+
clumsy.found_treasure(hammer)
|
29
|
+
clumsy.found_treasure(hammer)
|
30
|
+
clumsy.found_treasure(hammer)
|
31
|
+
|
32
|
+
crowbar = Treasure.new(:crowbar, 400)
|
33
|
+
clumsy.found_treasure(crowbar)
|
34
|
+
|
35
|
+
clumsy.each_found_treasure do |treasure|
|
36
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
37
|
+
end
|
38
|
+
puts "#{clumsy.points} grand total points"
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module StudioGame
|
2
|
+
require_relative 'player'
|
3
|
+
require_relative 'game_turn'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
|
6
|
+
class Game
|
7
|
+
|
8
|
+
attr_accessor :title
|
9
|
+
attr_reader :players
|
10
|
+
|
11
|
+
def initialize(title)
|
12
|
+
@title = title
|
13
|
+
@players = []
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def load_players(from_file)
|
18
|
+
File.readlines(from_file).each do |line|
|
19
|
+
add_player(Player.from_csv(line))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_player(player)
|
24
|
+
@players << player
|
25
|
+
end
|
26
|
+
|
27
|
+
def print_players
|
28
|
+
players.each do |player|
|
29
|
+
puts player
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def total_points
|
34
|
+
@players.reduce(0) {|sum, player| sum + player.points }
|
35
|
+
end
|
36
|
+
|
37
|
+
def play(rounds)
|
38
|
+
puts "There are #{@players.size} players in #{@title}:"
|
39
|
+
|
40
|
+
|
41
|
+
@players.each do |player|
|
42
|
+
puts player
|
43
|
+
end
|
44
|
+
|
45
|
+
treasures = TreasureTrove::TREASURES
|
46
|
+
puts "\nThere are #{treasures.length} treasures to be found:"
|
47
|
+
treasures.each { |treasure| puts "A #{treasure.name} is worth #{treasure.points} points" }
|
48
|
+
|
49
|
+
1.upto(rounds) do |round|
|
50
|
+
if block_given?
|
51
|
+
break if yield
|
52
|
+
end
|
53
|
+
puts "\nRound #{round}:"
|
54
|
+
@players.each do |player|
|
55
|
+
GameTurn.take_turn(player)
|
56
|
+
puts player
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def high_score_title
|
62
|
+
"\n#{title} High Scores:"
|
63
|
+
end
|
64
|
+
|
65
|
+
def high_score_entry(player)
|
66
|
+
formatted_name = player.name.ljust(30, '.')
|
67
|
+
"#{formatted_name} #{player.score}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def print_high_scores
|
71
|
+
puts high_score_title
|
72
|
+
players.sort.each do |player|
|
73
|
+
puts high_score_entry(player)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def save_high_scores(file_name="high_scores.txt")
|
78
|
+
File.open(file_name, "w") do |file|
|
79
|
+
file.puts high_score_title
|
80
|
+
players.sort.each do |player|
|
81
|
+
file.puts high_score_entry(player)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def print_stats
|
87
|
+
print_high_scores
|
88
|
+
save_high_scores
|
89
|
+
|
90
|
+
strong_players, wimpy_players = @players.partition {|player| player.strong? }
|
91
|
+
|
92
|
+
puts "\n#{@title} Statistics:"
|
93
|
+
|
94
|
+
# print out strong players
|
95
|
+
puts "\n#{strong_players.size} strong players:"
|
96
|
+
strong_players.each { |player| puts player.print_name_and_health }
|
97
|
+
|
98
|
+
# print out wimpy players
|
99
|
+
puts "\n#{wimpy_players.size} wimpy players:"
|
100
|
+
wimpy_players.each { |player| puts player.print_name_and_health }
|
101
|
+
|
102
|
+
players.sort.each do |player|
|
103
|
+
puts "\n#{player.name}'s point totals:"
|
104
|
+
player.each_found_treasure do |treasure|
|
105
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
106
|
+
end
|
107
|
+
puts "#{player.points} grand total points"
|
108
|
+
end
|
109
|
+
|
110
|
+
puts "\n#{total_points} collected from treasure found."
|
111
|
+
|
112
|
+
end # print_stats
|
113
|
+
|
114
|
+
if __FILE__ == $0
|
115
|
+
game = Game.new("Chimpmonks")
|
116
|
+
game.save_high_scores("high_score_file.txt")
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module StudioGame
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'loaded_die'
|
4
|
+
require_relative 'player'
|
5
|
+
require_relative 'treasure_trove'
|
6
|
+
|
7
|
+
module GameTurn
|
8
|
+
|
9
|
+
def self.take_turn(player)
|
10
|
+
die = Die.new
|
11
|
+
@number_rolled = die.roll
|
12
|
+
case @number_rolled
|
13
|
+
when 1, 2
|
14
|
+
player.blam
|
15
|
+
when 3, 4
|
16
|
+
puts "#{player} skipped."
|
17
|
+
when 5, 6
|
18
|
+
player.w00t
|
19
|
+
end
|
20
|
+
treasure = TreasureTrove.random
|
21
|
+
player.found_treasure(treasure)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Chimpmonks High Scores
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module StudioGame
|
2
|
+
module Playable
|
3
|
+
|
4
|
+
def w00t
|
5
|
+
self.health += 15
|
6
|
+
puts "#{name} got w00ted!"
|
7
|
+
end
|
8
|
+
|
9
|
+
def blam
|
10
|
+
self.health -= 10
|
11
|
+
puts "#{name} got blammed!"
|
12
|
+
end
|
13
|
+
|
14
|
+
def strong?
|
15
|
+
self.health > 100
|
16
|
+
end
|
17
|
+
|
18
|
+
def whimpy?
|
19
|
+
self.health <= 100
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module StudioGame
|
2
|
+
require_relative 'treasure_trove'
|
3
|
+
require_relative 'playable'
|
4
|
+
|
5
|
+
class Player
|
6
|
+
|
7
|
+
include Playable
|
8
|
+
|
9
|
+
attr_reader :found_treasures
|
10
|
+
attr_accessor :name, :health
|
11
|
+
|
12
|
+
def initialize(name, health=100)
|
13
|
+
@name = name.capitalize
|
14
|
+
@health = health
|
15
|
+
@found_treasures = Hash.new(0)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.from_csv(line)
|
19
|
+
name, health = line.split(",")
|
20
|
+
new(name, Integer(health))
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def <=>(other)
|
25
|
+
other.score <=> score
|
26
|
+
end
|
27
|
+
|
28
|
+
def points
|
29
|
+
@found_treasures.values.reduce(0, :+)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
"I'm #{@name} with a health of #{@health} and a score of #{score}."
|
36
|
+
end
|
37
|
+
|
38
|
+
def score
|
39
|
+
@health + points
|
40
|
+
end
|
41
|
+
|
42
|
+
def print_name_and_health
|
43
|
+
"#{name} (#{health})"
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
def found_treasure(treasure)
|
49
|
+
@found_treasures[treasure.name] += treasure.points
|
50
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
51
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def each_found_treasure
|
55
|
+
@found_treasures.each do |name, points|
|
56
|
+
treasure = Treasure.new(name, points)
|
57
|
+
yield(treasure)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
if __FILE__ == $0
|
64
|
+
player = Player.new("moe")
|
65
|
+
puts player.name
|
66
|
+
puts player.health
|
67
|
+
player.w00t
|
68
|
+
puts player.health
|
69
|
+
player.blam
|
70
|
+
puts player.health
|
71
|
+
10.times do
|
72
|
+
player.found_treasure
|
73
|
+
puts player.found_treasures
|
74
|
+
end
|
75
|
+
player.found_treasures.each do |treasure, points|
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
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,33 @@
|
|
1
|
+
require 'studio_game/berserk_player'
|
2
|
+
$stdout = StringIO.new
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
describe BerserkPlayer do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@initial_health = 50
|
9
|
+
@player = BerserkPlayer.new("berserker", @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not be bezerk when w00ted <= 5 times" do
|
13
|
+
5.times { @player.w00t }
|
14
|
+
|
15
|
+
@player.should_not be_berserk
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be bezerk when w00ted over 5 times" do
|
19
|
+
6.times { @player.w00t }
|
20
|
+
|
21
|
+
@player.should be_berserk
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be get w00ted and not blammed when berzerk" do
|
25
|
+
6.times { @player.w00t }
|
26
|
+
2.times { @player.blam }
|
27
|
+
|
28
|
+
@player.health.should == @initial_health + (15*8)
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,32 @@
|
|
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
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
$stdout = StringIO.new
|
3
|
+
module StudioGame
|
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
|
+
|
14
|
+
@number_rounds = 2
|
15
|
+
end
|
16
|
+
|
17
|
+
context "is being played" do
|
18
|
+
|
19
|
+
it "the specified number of rounds" do
|
20
|
+
Die.any_instance.stub(:roll).and_return(1)
|
21
|
+
|
22
|
+
@game.play(@number_rounds)
|
23
|
+
@player.health.should == @initial_health - (10*@number_rounds)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "blams a player when 1 is rolled" do
|
27
|
+
Die.any_instance.stub(:roll).and_return(1)
|
28
|
+
|
29
|
+
@game.play(@number_rounds)
|
30
|
+
@player.health.should == @initial_health - (10*@number_rounds)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "blams a player when 2 is rolled" do
|
34
|
+
Die.any_instance.stub(:roll).and_return(2)
|
35
|
+
|
36
|
+
@game.play(@number_rounds)
|
37
|
+
@player.health.should == @initial_health - (10*@number_rounds)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "skips a player when 3 is rolled" do
|
41
|
+
Die.any_instance.stub(:roll).and_return(3)
|
42
|
+
|
43
|
+
@game.play(@number_rounds)
|
44
|
+
@player.health.should == @initial_health
|
45
|
+
end
|
46
|
+
|
47
|
+
it "skips a player when 4 is rolled" do
|
48
|
+
Die.any_instance.stub(:roll).and_return(4)
|
49
|
+
|
50
|
+
@game.play(@number_rounds)
|
51
|
+
@player.health.should == @initial_health
|
52
|
+
end
|
53
|
+
|
54
|
+
it "w00ts a player when 5 is rolled" do
|
55
|
+
Die.any_instance.stub(:roll).and_return(5)
|
56
|
+
|
57
|
+
@game.play(@number_rounds)
|
58
|
+
@player.health.should == @initial_health + (15*@number_rounds)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "w00ts a player when 6 is rolled" do
|
62
|
+
Die.any_instance.stub(:roll).and_return(6)
|
63
|
+
|
64
|
+
@game.play(@number_rounds)
|
65
|
+
@player.health.should == @initial_health + (15*@number_rounds)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "earns a player treasure points when found_treasure" do
|
69
|
+
game = Game.new("Knuckleheads")
|
70
|
+
player = Player.new("moe")
|
71
|
+
|
72
|
+
game.add_player(player)
|
73
|
+
|
74
|
+
game.play(1)
|
75
|
+
|
76
|
+
player.points.should_not be_zero
|
77
|
+
end
|
78
|
+
|
79
|
+
it "sums all of the points of all of the players when total_points" do
|
80
|
+
game2 = Game.new("Studges")
|
81
|
+
|
82
|
+
player1 = Player.new("moe")
|
83
|
+
player2 = Player.new("larry")
|
84
|
+
|
85
|
+
game2.add_player(player1)
|
86
|
+
game2.add_player(player2)
|
87
|
+
|
88
|
+
treasure1 = Treasure.new(:pickax, 100)
|
89
|
+
player1.found_treasure(treasure1)
|
90
|
+
|
91
|
+
treasure2 = Treasure.new(:ladder, 400)
|
92
|
+
player2.found_treasure(treasure2)
|
93
|
+
|
94
|
+
game2.total_points.should == 500
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'studio_game/player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe Player do
|
5
|
+
|
6
|
+
before do
|
7
|
+
$stdout = StringIO.new
|
8
|
+
@initial_health = 150
|
9
|
+
@player = Player.new("larry", @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
context "attributes" do
|
13
|
+
it "has a capitalized name" do
|
14
|
+
@player.name.should == "Larry"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has an initial health" do
|
18
|
+
@player.health.should == 150
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has a found_treasures hash" do
|
22
|
+
@player.found_treasures.should == {}
|
23
|
+
end
|
24
|
+
end # context
|
25
|
+
|
26
|
+
context "methods" do
|
27
|
+
|
28
|
+
it "has a string representation" do
|
29
|
+
score = @player.health + @player.points
|
30
|
+
@player.to_s.should == "I'm #{@player.name} with a health of #{@player.health} and a score of #{score}."
|
31
|
+
end
|
32
|
+
|
33
|
+
it "computes a score as the sum of its health and points" do
|
34
|
+
@player.score.should == (150 + 0)
|
35
|
+
|
36
|
+
treasure = Treasure.new(:broomstick, 200)
|
37
|
+
@player.found_treasure(treasure)
|
38
|
+
@player.score.should == (150 + 200)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "increases health by 15 when w00ted" do
|
42
|
+
@player.w00t
|
43
|
+
@player.health.should == @initial_health + 15
|
44
|
+
end
|
45
|
+
|
46
|
+
it "decreases health by 10 when blammed" do
|
47
|
+
@player.blam
|
48
|
+
@player.health.should == @initial_health - 10
|
49
|
+
end
|
50
|
+
|
51
|
+
it "creates a formatted string when print_health" do
|
52
|
+
@player.print_name_and_health.should == "#{@player.name} (#{@player.health})"
|
53
|
+
end
|
54
|
+
|
55
|
+
it "computes points as the sum of all treasure points" do
|
56
|
+
@player.points.should == 0
|
57
|
+
|
58
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
59
|
+
@player.points.should == 50
|
60
|
+
|
61
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
62
|
+
@player.points.should == 450
|
63
|
+
|
64
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
65
|
+
@player.points.should == 500
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
it "yields each_found_treasure and its total points" do
|
70
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
71
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
72
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
73
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
74
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
75
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
76
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
77
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
78
|
+
|
79
|
+
yielded = []
|
80
|
+
@player.each_found_treasure do |treasure|
|
81
|
+
yielded << treasure
|
82
|
+
end
|
83
|
+
|
84
|
+
yielded.should == [
|
85
|
+
Treasure.new(:skillet, 200),
|
86
|
+
Treasure.new(:hammer, 50),
|
87
|
+
Treasure.new(:bottle, 25)
|
88
|
+
]
|
89
|
+
end
|
90
|
+
|
91
|
+
it "can create a new player from a csv string" do
|
92
|
+
|
93
|
+
player = Player.from_csv("larry,150")
|
94
|
+
|
95
|
+
player.name.should == "Larry"
|
96
|
+
player.health.should == 150
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end # methods context
|
101
|
+
|
102
|
+
context "with health > 100" do
|
103
|
+
|
104
|
+
it "is strong" do
|
105
|
+
@strong_player = Player.new("strong", 101)
|
106
|
+
@strong_player.should be_strong
|
107
|
+
end
|
108
|
+
|
109
|
+
it "is not whimpy" do
|
110
|
+
@not_whimpy_player = Player.new("not_whimpy", 101)
|
111
|
+
@not_whimpy_player.should_not be_whimpy
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "with health <= 100" do
|
116
|
+
|
117
|
+
it "is not strong when health = 100" do
|
118
|
+
@not_strong_player = Player.new("strong", 100)
|
119
|
+
@not_strong_player.should_not be_strong
|
120
|
+
end
|
121
|
+
|
122
|
+
it "is not strong when health < 100" do
|
123
|
+
@not_strong_player2 = Player.new("strong", 99)
|
124
|
+
@not_strong_player2.should_not be_strong
|
125
|
+
end
|
126
|
+
|
127
|
+
it "is whimpy when health = 100" do
|
128
|
+
@whimpy_player = Player.new("whimpy", 100)
|
129
|
+
@whimpy_player.should be_whimpy
|
130
|
+
end
|
131
|
+
|
132
|
+
it "is whimpy when health < 100" do
|
133
|
+
@whimpy_player2 = Player.new("whimpy", 99)
|
134
|
+
@whimpy_player2.should be_whimpy
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context "in a collection of players" do
|
139
|
+
|
140
|
+
before do
|
141
|
+
@player1 = Player.new("moe", 100)
|
142
|
+
@player2 = Player.new("larry", 200)
|
143
|
+
@player3 = Player.new("curly", 300)
|
144
|
+
|
145
|
+
@players = [@player1, @player2, @player3]
|
146
|
+
end
|
147
|
+
|
148
|
+
it "is sorted by decreasing score" do
|
149
|
+
@players.sort.should == [@player3, @player2, @player1]
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
test_player,100
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative 'game'
|
2
|
+
require_relative 'clumsy_player'
|
3
|
+
require_relative 'berserk_player'
|
4
|
+
|
5
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
6
|
+
|
7
|
+
players = ARGV.shift || "players.csv"
|
8
|
+
knuckleheads.load_players(players)
|
9
|
+
|
10
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105, 5)
|
11
|
+
knuckleheads.add_player(klutz)
|
12
|
+
|
13
|
+
berzerker = StudioGame::BerserkPlayer.new("berzerker", 50)
|
14
|
+
knuckleheads.add_player(berzerker)
|
15
|
+
|
16
|
+
loop do
|
17
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
18
|
+
rounds = gets.chomp.downcase
|
19
|
+
case rounds
|
20
|
+
when /^\d+$/
|
21
|
+
knuckleheads.play(rounds.to_i)
|
22
|
+
when 'quit', 'exit'
|
23
|
+
knuckleheads.print_stats
|
24
|
+
knuckleheads.save_high_scores
|
25
|
+
break
|
26
|
+
else
|
27
|
+
puts "Please put a legitimate response, an integer or 'quit'."
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'studio_game/treasure_trove'
|
2
|
+
module StudioGame
|
3
|
+
describe Treasure do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@hammer = Treasure.new(:hammer, 50)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should have a name attribute" do
|
10
|
+
@hammer.name.should == :hammer
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should have a points attribute" do
|
14
|
+
@hammer.points.should == 50
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe TreasureTrove do
|
19
|
+
|
20
|
+
it "should contain 6 Treasures" do
|
21
|
+
TreasureTrove::TREASURES.size.should == 6
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should contain a pie worth 5 points" do
|
25
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should contain a bottle worth 25 points" do
|
29
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should contain a hammer worth 50 points" do
|
33
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should contain a skillet worth 100 points" do
|
37
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should contain a broomstick worth 200 points" do
|
41
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should contain a crowbar worth 400 points" do
|
45
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have a random method that returns one random treasure" do
|
49
|
+
treasure = TreasureTrove.random
|
50
|
+
|
51
|
+
TreasureTrove::TREASURES.should include(treasure)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_bdu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Unger
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-03 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: Pragmatic Studios ruby programing course
|
31
|
+
email: benjamin.d.unger@gmail.com
|
32
|
+
executables:
|
33
|
+
- studio_game
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- bin/players.csv
|
38
|
+
- bin/studio_game
|
39
|
+
- lib/studio_game/auditable.rb
|
40
|
+
- lib/studio_game/berserk_player.rb
|
41
|
+
- lib/studio_game/clumsy_player.rb
|
42
|
+
- lib/studio_game/die.rb
|
43
|
+
- lib/studio_game/game.rb
|
44
|
+
- lib/studio_game/game_turn.rb
|
45
|
+
- lib/studio_game/high_score_file.txt
|
46
|
+
- lib/studio_game/high_scores.txt
|
47
|
+
- lib/studio_game/loaded_die.rb
|
48
|
+
- lib/studio_game/my_favorite_players.csv
|
49
|
+
- lib/studio_game/playable.rb
|
50
|
+
- lib/studio_game/player.rb
|
51
|
+
- lib/studio_game/treasure_trove.rb
|
52
|
+
- spec/studio_game/berserk_player_spec.rb
|
53
|
+
- spec/studio_game/clumsy_player_spec.rb
|
54
|
+
- spec/studio_game/game_spec.rb
|
55
|
+
- spec/studio_game/player_spec.rb
|
56
|
+
- spec/studio_game/rspect_player_file.csv
|
57
|
+
- spec/studio_game/studio_game.rb
|
58
|
+
- spec/studio_game/treasure_trove_spec.rb
|
59
|
+
- LICENSE
|
60
|
+
- README
|
61
|
+
homepage: http://pragmaticstudio.com/ruby/
|
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.25
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Fun game with health scores and treasure.
|
85
|
+
test_files:
|
86
|
+
- spec/studio_game/berserk_player_spec.rb
|
87
|
+
- spec/studio_game/clumsy_player_spec.rb
|
88
|
+
- spec/studio_game/game_spec.rb
|
89
|
+
- spec/studio_game/player_spec.rb
|
90
|
+
- spec/studio_game/rspect_player_file.csv
|
91
|
+
- spec/studio_game/studio_game.rb
|
92
|
+
- spec/studio_game/treasure_trove_spec.rb
|