studio_game_peterpiper 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 +17 -0
- data/bin/my_favorite_players.csv +3 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +48 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +45 -0
- data/lib/studio_game/clumsy_player.rb +37 -0
- data/lib/studio_game/die.rb +24 -0
- data/lib/studio_game/game.rb +121 -0
- data/lib/studio_game/game_turn.rb +24 -0
- data/lib/studio_game/health_sensitive_player.rb +22 -0
- data/lib/studio_game/loaded_die.rb +16 -0
- data/lib/studio_game/playable.rb +17 -0
- data/lib/studio_game/player.rb +66 -0
- data/lib/studio_game/treasure_trove.rb +18 -0
- data/spec/studio_game/berserk_player_spec.rb +37 -0
- data/spec/studio_game/clumsy_player_spec.rb +46 -0
- data/spec/studio_game/game_spec.rb +107 -0
- data/spec/studio_game/health_sensitive_player_spec.rb +32 -0
- data/spec/studio_game/load_player_test.csv +3 -0
- data/spec/studio_game/player_spec.rb +129 -0
- data/spec/studio_game/treasure_trove_spec.rb +57 -0
- metadata +122 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (C) 2013 Peter Sumsion
|
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,17 @@
|
|
1
|
+
This program is a game written for the Ruby course at pragmaticstudio.com.
|
2
|
+
Excellent course by the way if you are considering learning Ruby.
|
3
|
+
I highly recommend both learning Ruby and this course.
|
4
|
+
|
5
|
+
The game is fairly straight forward. Players are loaded by default from a
|
6
|
+
players.csv file, however a different players file can be specified on the
|
7
|
+
command line. A custom players file would be a .csv with a player's name
|
8
|
+
and initial health being the two fields on each line.
|
9
|
+
The game starts when the main studio_game program is run.
|
10
|
+
The players take turns and are randomly w00ted, blammed, or skipped
|
11
|
+
depending on what number is rolled by a die. The one running the game can
|
12
|
+
choose how many rounds to play and then quit. When the game quits, the
|
13
|
+
stats are printed to the screen and the game exits.
|
14
|
+
|
15
|
+
Enjoy,
|
16
|
+
|
17
|
+
PeterPiper
|
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/studio_game/player'
|
4
|
+
require_relative '../lib/studio_game/clumsy_player'
|
5
|
+
require_relative '../lib/studio_game/berserk_player'
|
6
|
+
require_relative '../lib/studio_game/game'
|
7
|
+
|
8
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
9
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
10
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
11
|
+
#
|
12
|
+
# player_names = %w( peter breanne george ben joe marla heidi scott lela )
|
13
|
+
#
|
14
|
+
# player_names.each do |name|
|
15
|
+
# generated_score = rand(50..150)
|
16
|
+
# player = Player.new(name, generated_score)
|
17
|
+
# knuckleheads.add_player(player)
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# knuckleheads.play(10) do
|
21
|
+
# knuckleheads.total_points >= 500
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# knuckleheads.print_stats
|
25
|
+
|
26
|
+
clumsy = StudioGame::ClumsyPlayer.new("klutz", 105)
|
27
|
+
knuckleheads.add_player(clumsy)
|
28
|
+
|
29
|
+
berserk = StudioGame::BerserkPlayer.new("berserk", 50)
|
30
|
+
knuckleheads.add_player(berserk)
|
31
|
+
|
32
|
+
loop do
|
33
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
34
|
+
rounds = gets.chomp.downcase
|
35
|
+
|
36
|
+
case rounds
|
37
|
+
when /^\d+$/
|
38
|
+
knuckleheads.play(rounds.to_i)
|
39
|
+
when 'quit'
|
40
|
+
knuckleheads.print_stats
|
41
|
+
break
|
42
|
+
else
|
43
|
+
puts "Please enter a number or 'quit'"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
knuckleheads.save_high_scores
|
48
|
+
knuckleheads.save_high_scores("my_high_scores.txt")
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
def initialize(name, health=nil)
|
6
|
+
@w00ts = 0
|
7
|
+
if health
|
8
|
+
super(name, health)
|
9
|
+
else
|
10
|
+
super(name)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def w00t
|
15
|
+
super
|
16
|
+
@w00ts += 1
|
17
|
+
if berserk?
|
18
|
+
puts "#{@name} is berserk!"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def blam
|
23
|
+
if berserk?
|
24
|
+
w00t
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def berserk?
|
31
|
+
if @w00ts > 5
|
32
|
+
true
|
33
|
+
else
|
34
|
+
false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
if __FILE__ == $0
|
41
|
+
berserker = BerserkPlayer.new("berserker")
|
42
|
+
6.times { berserker.w00t }
|
43
|
+
2.times { berserker.blam }
|
44
|
+
puts berserker.health
|
45
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'player'
|
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
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if __FILE__ == $0
|
25
|
+
clumsy = ClumsyPlayer.new("klutz", 105, 3)
|
26
|
+
|
27
|
+
hammer = Treasure.new(:hammer, 50)
|
28
|
+
3.times { clumsy.found_treasure(hammer) }
|
29
|
+
|
30
|
+
crowbar = Treasure.new(:crowbar, 400)
|
31
|
+
clumsy.found_treasure(crowbar)
|
32
|
+
|
33
|
+
clumsy.each_found_treasure do |treasure|
|
34
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
35
|
+
end
|
36
|
+
puts "#{clumsy.points} grand total points"
|
37
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'auditable'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class Die
|
5
|
+
include Auditable
|
6
|
+
attr_reader :number
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@number = roll
|
10
|
+
end
|
11
|
+
|
12
|
+
def roll
|
13
|
+
@number = rand(1..6)
|
14
|
+
audit
|
15
|
+
@number
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
if __FILE__ == $0
|
21
|
+
die = Die.new
|
22
|
+
puts die.roll
|
23
|
+
puts die.number
|
24
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'game_turn'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
require 'csv'
|
6
|
+
|
7
|
+
module StudioGame
|
8
|
+
class Game
|
9
|
+
attr_reader :title
|
10
|
+
|
11
|
+
def initialize(title)
|
12
|
+
@title = title
|
13
|
+
@players = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def each_player
|
17
|
+
if block_given?
|
18
|
+
@players.each do |player|
|
19
|
+
yield player
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_player(player)
|
25
|
+
@players << player
|
26
|
+
end
|
27
|
+
|
28
|
+
def total_points
|
29
|
+
@players.reduce(0) do |points, player|
|
30
|
+
points += player.points
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def load_players(from_file)
|
35
|
+
CSV.foreach(from_file) do |row|
|
36
|
+
player = Player.new(row[0], Integer(row[1]))
|
37
|
+
add_player(player)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def save_high_scores(to_file="high_scores.txt")
|
42
|
+
File.open(to_file, "w") do |file|
|
43
|
+
file.puts "#{@title} High Scores:"
|
44
|
+
@players.sort.each do |player|
|
45
|
+
file.puts high_score_entry(player)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def high_score_entry(player)
|
51
|
+
"#{player.name.ljust(20, '.')} #{player.score}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def print_name_and_health(player)
|
55
|
+
puts "#{player.name} (#{player.score})"
|
56
|
+
end
|
57
|
+
|
58
|
+
def print_stats
|
59
|
+
strong_players, wimpy_players = @players.partition { |player| player.strong? }
|
60
|
+
|
61
|
+
puts "\n#{title} Statistics:"
|
62
|
+
puts "\n#{strong_players.size} strong players:"
|
63
|
+
strong_players.each do |player|
|
64
|
+
print_name_and_health(player)
|
65
|
+
end
|
66
|
+
|
67
|
+
puts "\n#{wimpy_players.size} wimpy players:"
|
68
|
+
wimpy_players.each do |player|
|
69
|
+
print_name_and_health(player)
|
70
|
+
end
|
71
|
+
|
72
|
+
@players.each do |player|
|
73
|
+
puts "\n#{player.name}'s point totals:"
|
74
|
+
player.each_found_treasure do |treasure|
|
75
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
76
|
+
end
|
77
|
+
puts "#{player.points} grand total points"
|
78
|
+
end
|
79
|
+
|
80
|
+
puts "\n#{total_points} total points from treasures found"
|
81
|
+
|
82
|
+
high_score_string = "\n#{title} High Score"
|
83
|
+
puts high_score_string
|
84
|
+
@players.sort.each do |player|
|
85
|
+
puts high_score_entry(player)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def play(rounds)
|
90
|
+
puts "There are #{@players.size} players in #{@title}:"
|
91
|
+
puts @players
|
92
|
+
|
93
|
+
treasures = TreasureTrove::TREASURES
|
94
|
+
|
95
|
+
# Symbols and Structs: 4. Print the Treasure Trove
|
96
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
97
|
+
treasures.each do |treasure|
|
98
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
99
|
+
end
|
100
|
+
|
101
|
+
1.upto(rounds) do |round|
|
102
|
+
if block_given?
|
103
|
+
break if yield
|
104
|
+
end
|
105
|
+
puts "\nRound: #{round}"
|
106
|
+
@players.each do |player|
|
107
|
+
GameTurn.take_turn(player)
|
108
|
+
puts player
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
if __FILE__ == $0
|
116
|
+
player = Player.new("curly")
|
117
|
+
|
118
|
+
knuckleheads = Game.new("Knuckleheads")
|
119
|
+
knuckleheads.add_player(player)
|
120
|
+
knuckleheads.play(3)
|
121
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'die'
|
2
|
+
require_relative 'loaded_die'
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
|
6
|
+
module StudioGame
|
7
|
+
module GameTurn
|
8
|
+
def self.take_turn(player)
|
9
|
+
die = Die.new
|
10
|
+
|
11
|
+
case die.roll
|
12
|
+
when 1..2
|
13
|
+
player.blam
|
14
|
+
when 3..4
|
15
|
+
puts "#{player.name} was skipped."
|
16
|
+
else
|
17
|
+
player.w00t
|
18
|
+
end
|
19
|
+
|
20
|
+
treasure = TreasureTrove.random
|
21
|
+
player.found_treasure(treasure)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class HealthSensitivePlayer < Player
|
5
|
+
|
6
|
+
attr_reader :sickness_factor
|
7
|
+
|
8
|
+
def initialize(name, health=100, sickness_factor=10)
|
9
|
+
super(name, health)
|
10
|
+
@sickness_factor = sickness_factor
|
11
|
+
end
|
12
|
+
|
13
|
+
def blam
|
14
|
+
super
|
15
|
+
@health -= @sickness_factor
|
16
|
+
end
|
17
|
+
|
18
|
+
def w00t
|
19
|
+
@sickness_factor -= 5
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,66 @@
|
|
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=100)
|
12
|
+
@name = name.capitalize
|
13
|
+
@health = health
|
14
|
+
@found_treasures = Hash.new(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
def found_treasure(treasure)
|
18
|
+
name = treasure.name
|
19
|
+
points = treasure.points
|
20
|
+
@found_treasures[name] += points
|
21
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
22
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def each_found_treasure
|
26
|
+
@found_treasures.each do |name, points|
|
27
|
+
yield Treasure.new(name, points)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def points
|
32
|
+
@found_treasures.values.reduce(0, :+)
|
33
|
+
end
|
34
|
+
|
35
|
+
def score
|
36
|
+
@health + points
|
37
|
+
end
|
38
|
+
|
39
|
+
def name=(new_name)
|
40
|
+
@name = new_name.capitalize
|
41
|
+
end
|
42
|
+
|
43
|
+
def <=>(other)
|
44
|
+
other.score <=> score
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_s
|
48
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.from_csv(csv_line)
|
52
|
+
name, health = csv_line.split(",")
|
53
|
+
Player.new(name, Integer(health))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
if __FILE__ == $0
|
59
|
+
player = Player.new("moe")
|
60
|
+
puts player.name
|
61
|
+
puts player.health
|
62
|
+
player.w00t
|
63
|
+
puts player.health
|
64
|
+
player.blam
|
65
|
+
puts player.health
|
66
|
+
end
|
@@ -0,0 +1,18 @@
|
|
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
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'studio_game/berserk_player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe BerserkPlayer do
|
5
|
+
before do
|
6
|
+
$stdout = StringIO.new
|
7
|
+
@player = BerserkPlayer.new("berserk")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "is not berserk if w00ted up to 5 times" do
|
11
|
+
5.times { @player.w00t }
|
12
|
+
@player.should_not be_berserk
|
13
|
+
end
|
14
|
+
|
15
|
+
it "gets blammed if not berserk" do
|
16
|
+
@player.blam
|
17
|
+
|
18
|
+
@player.health.should == (100 - 10)
|
19
|
+
end
|
20
|
+
|
21
|
+
context "w00ted 6 times or more" do
|
22
|
+
before do
|
23
|
+
6.times { @player.w00t }
|
24
|
+
end
|
25
|
+
|
26
|
+
it "is berserk" do
|
27
|
+
@player.should be_berserk
|
28
|
+
end
|
29
|
+
|
30
|
+
it "w00ts when blam is called" do
|
31
|
+
2.times { @player.blam }
|
32
|
+
|
33
|
+
@player.health.should == 220
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'studio_game/clumsy_player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe ClumsyPlayer do
|
5
|
+
before do
|
6
|
+
$stdout = StringIO.new
|
7
|
+
@player = ClumsyPlayer.new("klutz")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "has a boost factor of 1" do
|
11
|
+
@player.boost_factor.should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it "w00ts as many times as the boost factor when w00ted" do
|
15
|
+
@player.w00t
|
16
|
+
@player.health.should == 100 + 15
|
17
|
+
|
18
|
+
player2 = ClumsyPlayer.new("awkward", 100, 3)
|
19
|
+
player2.w00t
|
20
|
+
player2.health.should == 100 + (15 * 3)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "only gets half the point value for each treasure" do
|
24
|
+
@player.points.should == 0
|
25
|
+
|
26
|
+
hammer = Treasure.new(:hammer, 50)
|
27
|
+
@player.found_treasure(hammer)
|
28
|
+
@player.found_treasure(hammer)
|
29
|
+
@player.found_treasure(hammer)
|
30
|
+
|
31
|
+
@player.points.should == 75
|
32
|
+
|
33
|
+
crowbar = Treasure.new(:crowbar, 400)
|
34
|
+
@player.found_treasure(crowbar)
|
35
|
+
|
36
|
+
@player.points.should == 275
|
37
|
+
|
38
|
+
yielded = []
|
39
|
+
@player.each_found_treasure do |treasure|
|
40
|
+
yielded << treasure
|
41
|
+
end
|
42
|
+
|
43
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
require 'studio_game/die'
|
3
|
+
require 'csv'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe Game do
|
7
|
+
|
8
|
+
before do
|
9
|
+
$stdout = StringIO.new
|
10
|
+
@game = Game.new("Knuckleheads")
|
11
|
+
|
12
|
+
@initial_health = 100
|
13
|
+
@player = Player.new("moe", @initial_health)
|
14
|
+
|
15
|
+
@game.add_player(@player)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "w00ts the player if a high number is rolled" do
|
19
|
+
Die.any_instance.stub(:roll).and_return(5)
|
20
|
+
|
21
|
+
@game.play(2)
|
22
|
+
|
23
|
+
@player.health.should == @initial_health + (15 * 2)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "skips the player if a medium number is rolled" do
|
27
|
+
Die.any_instance.stub(:roll).and_return(3)
|
28
|
+
|
29
|
+
@game.play(2)
|
30
|
+
@player.health.should == @initial_health
|
31
|
+
end
|
32
|
+
|
33
|
+
it "blams the player if a low number is rolled" do
|
34
|
+
Die.any_instance.stub(:roll).and_return(1)
|
35
|
+
|
36
|
+
@game.play(2)
|
37
|
+
|
38
|
+
@player.health.should == @initial_health - (10 * 2)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "assigns a treasure for points during a player's turn" do
|
42
|
+
game = Game.new("Knuckleheads")
|
43
|
+
player = Player.new("moe")
|
44
|
+
|
45
|
+
game.add_player(player)
|
46
|
+
|
47
|
+
game.play(1)
|
48
|
+
|
49
|
+
player.points.should_not be_zero
|
50
|
+
end
|
51
|
+
|
52
|
+
it "computes total points as the sum of all player points" do
|
53
|
+
game = Game.new("Knuckleheads")
|
54
|
+
|
55
|
+
player1 = Player.new("moe")
|
56
|
+
player2 = Player.new("larry")
|
57
|
+
|
58
|
+
game.add_player(player1)
|
59
|
+
game.add_player(player2)
|
60
|
+
|
61
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
62
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
63
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
64
|
+
|
65
|
+
game.total_points.should == 500
|
66
|
+
end
|
67
|
+
|
68
|
+
it "returns each player" do
|
69
|
+
player2 = Player.new("curly", @initial_health)
|
70
|
+
player3 = Player.new("larry", @initial_health)
|
71
|
+
@game.add_player(player2)
|
72
|
+
@game.add_player(player3)
|
73
|
+
|
74
|
+
players = [@player, player2, player3]
|
75
|
+
expected_players = []
|
76
|
+
|
77
|
+
@game.each_player do |player|
|
78
|
+
expected_players << player
|
79
|
+
end
|
80
|
+
|
81
|
+
expected_players.should == players
|
82
|
+
end
|
83
|
+
|
84
|
+
it "loads players from a csv file" do
|
85
|
+
game = Game.new("Knuckleheads")
|
86
|
+
test_player_file =
|
87
|
+
File.join(File.dirname(__FILE__), "load_player_test.csv")
|
88
|
+
player_csv_file = File.open(test_player_file, "wb") do |file|
|
89
|
+
file << "moe,145\n"
|
90
|
+
file << "curly,160\n"
|
91
|
+
file << "larry,150\n"
|
92
|
+
end
|
93
|
+
|
94
|
+
expected_players = {"Moe" => 145, "Curly" => 160, "Larry" => 150}
|
95
|
+
|
96
|
+
players = {}
|
97
|
+
|
98
|
+
game.load_players(player_csv_file)
|
99
|
+
|
100
|
+
game.each_player do |player|
|
101
|
+
players[player.name] = player.health
|
102
|
+
end
|
103
|
+
|
104
|
+
players.should == expected_players
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'studio_game/player'
|
2
|
+
require 'studio_game/health_sensitive_player'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
describe HealthSensitivePlayer do
|
6
|
+
before do
|
7
|
+
@initial_health = 90
|
8
|
+
@sickness_factor = 10
|
9
|
+
@player = HealthSensitivePlayer.new("healthnut", @initial_health, @sickness_factor)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "is an instance of HealthSensitivePlayer" do
|
13
|
+
@player.should be_an_instance_of HealthSensitivePlayer
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has a sickness factor" do
|
17
|
+
@player.sickness_factor.should == 10
|
18
|
+
end
|
19
|
+
|
20
|
+
it "decreases health by sickness factor when blammed" do
|
21
|
+
@player.blam
|
22
|
+
|
23
|
+
@player.health.should == @initial_health - 10 - @sickness_factor
|
24
|
+
end
|
25
|
+
|
26
|
+
it "only decreases sickness factor by 5 when w00ted" do
|
27
|
+
@player.w00t
|
28
|
+
|
29
|
+
@player.sickness_factor.should == @sickness_factor - 5
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,129 @@
|
|
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
|
+
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 "has a string representation" do
|
21
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
22
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
23
|
+
|
24
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
|
25
|
+
end
|
26
|
+
|
27
|
+
it "computes a score as the sum of its health and points" do
|
28
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
29
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
30
|
+
|
31
|
+
@player.score.should == (@initial_health + 100)
|
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
|
+
it "computes points as the sum of all treasure points" do
|
47
|
+
@player.points.should == 0
|
48
|
+
|
49
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
50
|
+
|
51
|
+
@player.points.should == 50
|
52
|
+
|
53
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
54
|
+
|
55
|
+
@player.points.should == 450
|
56
|
+
|
57
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
58
|
+
|
59
|
+
@player.points.should == 500
|
60
|
+
end
|
61
|
+
|
62
|
+
it "yields each found treasure and its total points" do
|
63
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
64
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
65
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
66
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
67
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
68
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
69
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
70
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
71
|
+
|
72
|
+
yielded = []
|
73
|
+
@player.each_found_treasure do |treasure|
|
74
|
+
yielded << treasure
|
75
|
+
end
|
76
|
+
|
77
|
+
yielded.should == [
|
78
|
+
Treasure.new(:skillet, 200),
|
79
|
+
Treasure.new(:hammer, 50),
|
80
|
+
Treasure.new(:bottle, 25)
|
81
|
+
]
|
82
|
+
end
|
83
|
+
|
84
|
+
it "returns a new player from a csv string of name and health" do
|
85
|
+
player_csv = "moe,145"
|
86
|
+
player = Player.from_csv(player_csv)
|
87
|
+
|
88
|
+
player.name.should == "Moe"
|
89
|
+
player.health.should == 145
|
90
|
+
end
|
91
|
+
|
92
|
+
context "with a health greater than 100" do
|
93
|
+
before do
|
94
|
+
@initial_health = 150
|
95
|
+
@player = Player.new("curly", @initial_health)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "is strong" do
|
99
|
+
@player.should be_strong
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context "with a health of 100" do
|
104
|
+
before do
|
105
|
+
@initial_health = 100
|
106
|
+
@player = Player.new("curly", @initial_health)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "is wimpy" do
|
110
|
+
@player.should_not be_strong
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "in a collection of players" do
|
115
|
+
before do
|
116
|
+
@player1 = Player.new("moe", 100)
|
117
|
+
@player2 = Player.new("larry", 200)
|
118
|
+
@player3 = Player.new("curly", 300)
|
119
|
+
|
120
|
+
@players = [@player1, @player2, @player3]
|
121
|
+
end
|
122
|
+
|
123
|
+
it "is sorted by decreasing score" do
|
124
|
+
@players.sort.should == [@player3, @player2, @player1]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,57 @@
|
|
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
|
+
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_peterpiper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Sumsion
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-16 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 program is a game written for the Ruby course at pragmaticstudio.com.
|
31
|
+
|
32
|
+
Excellent course by the way if you are considering learning Ruby.
|
33
|
+
|
34
|
+
I highly recommend both learning Ruby and this course.
|
35
|
+
|
36
|
+
|
37
|
+
The game is fairly straight forward. Players are loaded by default from a
|
38
|
+
|
39
|
+
players.csv file, however a different players file can be specified on the
|
40
|
+
|
41
|
+
command line. A custom players file would be a .csv with a player''s name
|
42
|
+
|
43
|
+
and initial health being the two fields on each line.
|
44
|
+
|
45
|
+
The game starts when the main studio_game program is run.
|
46
|
+
|
47
|
+
The players take turns and are randomly w00ted, blammed, or skipped
|
48
|
+
|
49
|
+
depending on what number is rolled by a die. The one running the game can
|
50
|
+
|
51
|
+
choose how many rounds to play and then quit. When the game quits, the
|
52
|
+
|
53
|
+
stats are printed to the screen and the game exits.
|
54
|
+
|
55
|
+
|
56
|
+
Enjoy,
|
57
|
+
|
58
|
+
|
59
|
+
PeterPiper
|
60
|
+
|
61
|
+
'
|
62
|
+
email: sumsionp@gmail.com
|
63
|
+
executables:
|
64
|
+
- studio_game
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- bin/my_favorite_players.csv
|
69
|
+
- bin/players.csv
|
70
|
+
- bin/studio_game
|
71
|
+
- lib/studio_game/auditable.rb
|
72
|
+
- lib/studio_game/berserk_player.rb
|
73
|
+
- lib/studio_game/clumsy_player.rb
|
74
|
+
- lib/studio_game/die.rb
|
75
|
+
- lib/studio_game/game.rb
|
76
|
+
- lib/studio_game/game_turn.rb
|
77
|
+
- lib/studio_game/health_sensitive_player.rb
|
78
|
+
- lib/studio_game/loaded_die.rb
|
79
|
+
- lib/studio_game/playable.rb
|
80
|
+
- lib/studio_game/player.rb
|
81
|
+
- lib/studio_game/treasure_trove.rb
|
82
|
+
- spec/studio_game/berserk_player_spec.rb
|
83
|
+
- spec/studio_game/clumsy_player_spec.rb
|
84
|
+
- spec/studio_game/game_spec.rb
|
85
|
+
- spec/studio_game/health_sensitive_player_spec.rb
|
86
|
+
- spec/studio_game/load_player_test.csv
|
87
|
+
- spec/studio_game/player_spec.rb
|
88
|
+
- spec/studio_game/treasure_trove_spec.rb
|
89
|
+
- LICENSE
|
90
|
+
- README
|
91
|
+
homepage: http://psumsiontech.wordpress.com/2013/11/16/first-ruby-gem-built/
|
92
|
+
licenses: []
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '1.9'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 1.8.25
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: Multiple player random die roll game
|
115
|
+
test_files:
|
116
|
+
- spec/studio_game/berserk_player_spec.rb
|
117
|
+
- spec/studio_game/clumsy_player_spec.rb
|
118
|
+
- spec/studio_game/game_spec.rb
|
119
|
+
- spec/studio_game/health_sensitive_player_spec.rb
|
120
|
+
- spec/studio_game/load_player_test.csv
|
121
|
+
- spec/studio_game/player_spec.rb
|
122
|
+
- spec/studio_game/treasure_trove_spec.rb
|