studio_game_ps 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 +4 -0
- data/bin/high_scores.txt +10 -0
- data/bin/players.csv +7 -0
- data/bin/players_kevin.csv +1 -0
- data/bin/studio_game +32 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +32 -0
- data/lib/studio_game/calculator.rb +21 -0
- data/lib/studio_game/clumsy_player.rb +29 -0
- data/lib/studio_game/die.rb +28 -0
- data/lib/studio_game/game.rb +101 -0
- data/lib/studio_game/game_turn.rb +22 -0
- data/lib/studio_game/iterators.rb +17 -0
- data/lib/studio_game/loaded_die.rb +21 -0
- data/lib/studio_game/playable.rb +17 -0
- data/lib/studio_game/player.rb +64 -0
- data/lib/studio_game/treasure_trove.rb +25 -0
- data/spec/studio_game/berserk_player_spec.rb +31 -0
- data/spec/studio_game/clumsy_player_spec.rb +33 -0
- data/spec/studio_game/game_spec.rb +60 -0
- data/spec/studio_game/player_spec.rb +121 -0
- data/spec/studio_game/treasure_trove_spec.rb +56 -0
- metadata +90 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (C) 2013 The Pragmatic Studio
|
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
data/bin/high_scores.txt
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Knuckleheads High Scores:
|
2
|
+
Curly............... (1970)
|
3
|
+
Larry............... (1690)
|
4
|
+
Theo................ (1685)
|
5
|
+
Berserker........... (1410)
|
6
|
+
Kevin............... (1367)
|
7
|
+
Alvin............... (1340)
|
8
|
+
Simon............... (1335)
|
9
|
+
Moe................. (1090)
|
10
|
+
Klutz............... (403)
|
data/bin/players.csv
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Kevin,42
|
data/bin/studio_game
ADDED
@@ -0,0 +1,32 @@
|
|
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
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
11
|
+
|
12
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz")
|
13
|
+
knuckleheads.add_player(klutz)
|
14
|
+
|
15
|
+
berserker = StudioGame::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
|
+
break
|
27
|
+
else
|
28
|
+
puts "\nPlease enter a number or 'quit'"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
knuckleheads.save_high_scores
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
|
6
|
+
def initialize(name, health=42)
|
7
|
+
@wOOt_count = 0
|
8
|
+
super(name, health)
|
9
|
+
end
|
10
|
+
|
11
|
+
def wOOt
|
12
|
+
super
|
13
|
+
@wOOt_count += 1
|
14
|
+
puts "#{@name} is berserk!" if berserk?
|
15
|
+
end
|
16
|
+
|
17
|
+
def blam
|
18
|
+
berserk? ? wOOt : super
|
19
|
+
end
|
20
|
+
|
21
|
+
def berserk?
|
22
|
+
@wOOt_count > 5
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if __FILE__ == $0
|
28
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
29
|
+
6.times { berserker.wOOt }
|
30
|
+
2.times { berserker.blam }
|
31
|
+
puts berserker.health
|
32
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module StudioGame
|
2
|
+
class Calculator
|
3
|
+
def initialize(number1, number2)
|
4
|
+
@number1 = number1
|
5
|
+
@number2 = number2
|
6
|
+
end
|
7
|
+
|
8
|
+
def add
|
9
|
+
@number1 + @number2
|
10
|
+
end
|
11
|
+
|
12
|
+
def subtract
|
13
|
+
@number1 - @number2
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
calc = Calculator.new(20, 11)
|
20
|
+
puts calc.add
|
21
|
+
puts calc.subtract
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class ClumsyPlayer < Player
|
5
|
+
|
6
|
+
def found_treasure(treasure)
|
7
|
+
damaged_treasure = StudioGame::Treasure.new(treasure.name, treasure.points / 2)
|
8
|
+
super(damaged_treasure)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
if __FILE__ == $0
|
15
|
+
clumsy = StudioGame::ClumsyPlayer.new("klutz")
|
16
|
+
|
17
|
+
hammer = StudioGame::Treasure.new(:hammer, 50)
|
18
|
+
clumsy.found_treasure(hammer)
|
19
|
+
clumsy.found_treasure(hammer)
|
20
|
+
clumsy.found_treasure(hammer)
|
21
|
+
|
22
|
+
crowbar = StudioGame::Treasure.new(:crowbar, 400)
|
23
|
+
clumsy.found_treasure(crowbar)
|
24
|
+
|
25
|
+
clumsy.each_found_treasure do |treasure|
|
26
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
27
|
+
end
|
28
|
+
puts "#{clumsy.points} grand total points"
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative "auditable"
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class Die
|
5
|
+
include Auditable
|
6
|
+
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
roll
|
11
|
+
end
|
12
|
+
|
13
|
+
def roll
|
14
|
+
@number = rand 1..6
|
15
|
+
audit
|
16
|
+
@number
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
if __FILE__ == $0
|
23
|
+
die = StudioGame::Die.new
|
24
|
+
puts die.roll
|
25
|
+
puts die.roll
|
26
|
+
puts die.roll
|
27
|
+
puts die.roll
|
28
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'game_turn'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
|
6
|
+
module StudioGame
|
7
|
+
class Game
|
8
|
+
attr_reader :title
|
9
|
+
|
10
|
+
def initialize(title)
|
11
|
+
@title = title
|
12
|
+
@players = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def load_players(from_file)
|
16
|
+
File.readlines(from_file).each do |line|
|
17
|
+
add_player(Player.from_csv(line))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_player(player)
|
22
|
+
@players << player
|
23
|
+
end
|
24
|
+
|
25
|
+
def play(rounds)
|
26
|
+
puts "There are #{@players.size} players in the game #{@title}"
|
27
|
+
puts @players
|
28
|
+
|
29
|
+
treasures = StudioGame::TreasureTrove::TREASURES
|
30
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
31
|
+
treasures.each do |treasure|
|
32
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
33
|
+
end
|
34
|
+
|
35
|
+
1.upto(rounds) do |r|
|
36
|
+
puts "\nRounds #{r}"
|
37
|
+
@players.each do |p|
|
38
|
+
GameTurn.take_turn(p)
|
39
|
+
puts p
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def total_points
|
45
|
+
@players.reduce(0) { |sum, p| sum += p.points }
|
46
|
+
end
|
47
|
+
|
48
|
+
def high_score_entry(player)
|
49
|
+
"#{player.name.ljust(20, '.')} (#{player.score})"
|
50
|
+
end
|
51
|
+
|
52
|
+
def print_stats
|
53
|
+
strong_players, weak_players = @players.partition { | p | p.strong? }
|
54
|
+
puts "\n#{@title} Statistics"
|
55
|
+
|
56
|
+
@players.sort.each do |p|
|
57
|
+
puts "\n#{p.name}'s point totals:"
|
58
|
+
p.each_found_treasure do |treasure|
|
59
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
60
|
+
end
|
61
|
+
puts "#{p.points} grand total points"
|
62
|
+
end
|
63
|
+
|
64
|
+
puts "\n#{total_points} total points from treasures found"
|
65
|
+
|
66
|
+
puts "\n#{strong_players.size} strong players:"
|
67
|
+
strong_players.each do |p|
|
68
|
+
puts p.print_name_and_health
|
69
|
+
end
|
70
|
+
|
71
|
+
puts "\n#{weak_players.size} weak players:"
|
72
|
+
weak_players.each do |p|
|
73
|
+
puts p.print_name_and_health
|
74
|
+
end
|
75
|
+
|
76
|
+
puts "\n#{@title} High Scores:"
|
77
|
+
@players.sort.each do |p|
|
78
|
+
puts high_score_entry(p)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
def save_high_scores(to_file="high_scores.txt")
|
84
|
+
File.open(to_file, "w") do |file|
|
85
|
+
file.puts "#{@title} High Scores:"
|
86
|
+
@players.sort.each do |p|
|
87
|
+
file.puts high_score_entry(p)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
if __FILE__ == $0
|
95
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
96
|
+
player0 = StudioGame::Player.new("kevin")
|
97
|
+
puts knuckleheads.title
|
98
|
+
knuckleheads.add_player(player0)
|
99
|
+
knuckleheads.play(1)
|
100
|
+
knuckleheads.print_stats
|
101
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'loaded_die'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
|
6
|
+
module GameTurn
|
7
|
+
def self.take_turn(player)
|
8
|
+
die = StudioGame::Die.new
|
9
|
+
# die = StudioGame::LoadedDie.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.wOOt
|
17
|
+
end
|
18
|
+
|
19
|
+
treasure = StudioGame::TreasureTrove.random
|
20
|
+
player.found_treasure(treasure)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
def conversation
|
2
|
+
puts "Hello"
|
3
|
+
yield
|
4
|
+
puts "Goodbye"
|
5
|
+
end
|
6
|
+
|
7
|
+
conversation { puts "Nice to meet you" }
|
8
|
+
|
9
|
+
def n_times(count=0)
|
10
|
+
1.upto(count) { |i| yield i }
|
11
|
+
end
|
12
|
+
|
13
|
+
n_times() do |n|
|
14
|
+
puts "#{n} situps"
|
15
|
+
puts "#{n} pushups"
|
16
|
+
puts "#{n} chinups"
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative "auditable"
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class LoadedDie
|
5
|
+
include Auditable
|
6
|
+
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
roll
|
11
|
+
end
|
12
|
+
|
13
|
+
def roll
|
14
|
+
numbers = [1, 1, 2, 5, 6, 6]
|
15
|
+
@number = numbers.sample
|
16
|
+
audit
|
17
|
+
@number
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require_relative 'treasure_trove'
|
2
|
+
require_relative 'playable'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
class Player
|
6
|
+
include Playable
|
7
|
+
|
8
|
+
attr_accessor :name
|
9
|
+
attr_accessor :health
|
10
|
+
|
11
|
+
def initialize(name, health=42)
|
12
|
+
@name = name.capitalize
|
13
|
+
@health = health
|
14
|
+
@found_treasures = Hash.new(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
def points
|
18
|
+
@found_treasures.values.reduce(0, :+)
|
19
|
+
end
|
20
|
+
|
21
|
+
def each_found_treasure
|
22
|
+
@found_treasures.each do | name, points |
|
23
|
+
yield StudioGame::Treasure.new(name, points)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def found_treasure(treasure)
|
28
|
+
@found_treasures[treasure.name] += treasure.points
|
29
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points"
|
30
|
+
puts "#{@name} treasures: #{@found_treasures}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def score
|
34
|
+
@health + points
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
|
39
|
+
end
|
40
|
+
|
41
|
+
def <=>(other)
|
42
|
+
other.score <=> score
|
43
|
+
end
|
44
|
+
|
45
|
+
def print_name_and_health
|
46
|
+
"#{@name} (#{@health})"
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.from_csv(string)
|
50
|
+
name, health = string.split(",")
|
51
|
+
new(name, Integer(health))
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
if __FILE__ == $0
|
58
|
+
player0 = StudioGame::Player.new("kevin")
|
59
|
+
puts player0.name
|
60
|
+
puts player0.health
|
61
|
+
puts player0.blam
|
62
|
+
puts player0.wOOt
|
63
|
+
puts player0
|
64
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module StudioGame
|
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
|
19
|
+
|
20
|
+
if __FILE__ == $0
|
21
|
+
treasure = StudioGame::Treasure.new(:pie, 5)
|
22
|
+
puts treasure.name
|
23
|
+
puts treasure.points
|
24
|
+
puts StudioGame::TreasureTrove.random
|
25
|
+
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 wOOted up to 5 times" do
|
12
|
+
1.upto(5) { @player.wOOt }
|
13
|
+
|
14
|
+
@player.berserk?.should be_false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "goes berserk when wOOted more than 5 times" do
|
18
|
+
1.upto(6) { @player.wOOt }
|
19
|
+
|
20
|
+
@player.berserk?.should be_true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "gets wOOted instead of blammed when it's gone berserk" do
|
24
|
+
1.upto(6) { @player.wOOt }
|
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,33 @@
|
|
1
|
+
require 'studio_game/clumsy_player'
|
2
|
+
|
3
|
+
module StudioGame
|
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 eq(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 eq(75)
|
18
|
+
|
19
|
+
crowbar = Treasure.new(:crowbar, 400)
|
20
|
+
@player.found_treasure(crowbar)
|
21
|
+
|
22
|
+
@player.points.should eq(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
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe Game do
|
5
|
+
|
6
|
+
before do
|
7
|
+
$stdout = StringIO.new # standard output will be written to the StringIO object instead of to the console
|
8
|
+
|
9
|
+
@game = Game.new("Knuckleheads")
|
10
|
+
|
11
|
+
@initial_health = 100
|
12
|
+
@game_rounds = 2
|
13
|
+
@player = Player.new("kevin", @initial_health)
|
14
|
+
|
15
|
+
@game.add_player(@player)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "woots the player if a high number is called" do
|
19
|
+
Die.any_instance.stub(:roll).and_return(5)
|
20
|
+
@game.play(@game_rounds)
|
21
|
+
@player.health.should == @initial_health + (15 * @game_rounds)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "skips the player if a medium number is called" do
|
25
|
+
Die.any_instance.stub(:roll).and_return(3)
|
26
|
+
@game.play(@game_rounds)
|
27
|
+
@player.health.should == @initial_health
|
28
|
+
end
|
29
|
+
|
30
|
+
it "blams the player if a low number is called" do
|
31
|
+
Die.any_instance.stub(:roll).and_return(1)
|
32
|
+
@game.play(@game_rounds)
|
33
|
+
@player.health.should == @initial_health - (10 * @game_rounds)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "assigns a treasure for points during a player's turn" do
|
37
|
+
@game.play(@game_rounds)
|
38
|
+
@player.points.should_not be_zero
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with 2 players" do
|
42
|
+
before do
|
43
|
+
@game = Game.new("Knuckleheads")
|
44
|
+
@player1 = Player.new("moe")
|
45
|
+
@player2 = Player.new("larry")
|
46
|
+
@game.add_player(@player1)
|
47
|
+
@game.add_player(@player2)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "computes total points as the sum of all player points" do
|
51
|
+
@player1.found_treasure(Treasure.new(:hammer, 50))
|
52
|
+
@player1.found_treasure(Treasure.new(:hammer, 50))
|
53
|
+
@player2.found_treasure(Treasure.new(:crowbar, 400))
|
54
|
+
|
55
|
+
@game.total_points.should == 500
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'studio_game/player'
|
2
|
+
require 'studio_game/treasure_trove'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
describe Player do
|
6
|
+
|
7
|
+
before do
|
8
|
+
$stdout = StringIO.new # standard output will be written to the StringIO object instead of to the console
|
9
|
+
@initial_health = 150
|
10
|
+
@player = Player.new("kevin", @initial_health)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has a capitalized name" do
|
14
|
+
@player.name.should == "Kevin"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has an initial health" do
|
18
|
+
@player.health.should == 150
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has a string representation" do
|
22
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
23
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
24
|
+
|
25
|
+
@player.to_s.should eq("I'm Kevin with health = 150, points = 100, and score = 250.")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "computes a score as the sum of its health and points" do
|
29
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
30
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
31
|
+
|
32
|
+
@player.score.should == 250
|
33
|
+
end
|
34
|
+
|
35
|
+
it "increases health by 15 when wOOted" do
|
36
|
+
@player.wOOt
|
37
|
+
@player.health.should == @initial_health + 15
|
38
|
+
end
|
39
|
+
|
40
|
+
it "decreases health by 10 when blammed" do
|
41
|
+
@player.blam
|
42
|
+
@player.health.should == @initial_health - 10
|
43
|
+
end
|
44
|
+
|
45
|
+
it "computes points as the sum of all treasure points" do
|
46
|
+
@player.points.should eq(0)
|
47
|
+
|
48
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
49
|
+
@player.points.should eq(50)
|
50
|
+
|
51
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
52
|
+
@player.points.should eq(450)
|
53
|
+
|
54
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
55
|
+
@player.points.should eq(500)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "yields each found treasure and its total points" do
|
59
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
60
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
61
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
62
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
63
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
64
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
65
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
66
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
67
|
+
|
68
|
+
yielded = []
|
69
|
+
@player.each_found_treasure do |treasure|
|
70
|
+
yielded << treasure
|
71
|
+
end
|
72
|
+
|
73
|
+
yielded.should == [
|
74
|
+
Treasure.new(:skillet, 200),
|
75
|
+
Treasure.new(:hammer, 50),
|
76
|
+
Treasure.new(:bottle, 25)
|
77
|
+
]
|
78
|
+
end
|
79
|
+
|
80
|
+
context "with a health greater than 100" do
|
81
|
+
before do
|
82
|
+
@player = Player.new("kevin", 150)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "is a strong player" do
|
86
|
+
@player.should be_strong
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context "with a health of 100 or less" do
|
91
|
+
before do
|
92
|
+
@player = Player.new("kevin", 100)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "is not a strong player" do
|
96
|
+
@player.should_not be_strong
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "in a collection of players" do
|
101
|
+
before do
|
102
|
+
@player1 = Player.new("moe", 100)
|
103
|
+
@player2 = Player.new("larry", 200)
|
104
|
+
@player3 = Player.new("curly", 300)
|
105
|
+
|
106
|
+
@players = [@player1, @player2, @player3]
|
107
|
+
end
|
108
|
+
|
109
|
+
it "is sorted by decreasing score" do
|
110
|
+
@players.sort.should == [@player3, @player2, @player1]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
it "can be created from a CSV string" do
|
115
|
+
player = Player.from_csv("larry,150")
|
116
|
+
|
117
|
+
player.name.should eq("Larry")
|
118
|
+
player.health.should eq(150)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'studio_game/treasure_trove'
|
2
|
+
|
3
|
+
module StudioGame
|
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
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_ps
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kevin McGill
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-21 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: ! "Course files from The Pragmatic Studio's Ruby Programming course.\n\thttp://online.pragmaticstudio.com/courses/ruby\n\nSimilar,
|
31
|
+
but not exactly the same as he documented course code."
|
32
|
+
email: kevmcgill@gmail.com
|
33
|
+
executables:
|
34
|
+
- studio_game
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- bin/high_scores.txt
|
39
|
+
- bin/players.csv
|
40
|
+
- bin/players_kevin.csv
|
41
|
+
- bin/studio_game
|
42
|
+
- lib/studio_game/auditable.rb
|
43
|
+
- lib/studio_game/berserk_player.rb
|
44
|
+
- lib/studio_game/calculator.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/kevinmcgill
|
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: The Pragmatic Studio's Ruby Programming course
|
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/treasure_trove_spec.rb
|