studio_game_umueller 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README +3 -0
- data/bin/high_scores.txt +6 -0
- data/bin/my_favorite_players.csv +3 -0
- data/bin/player.csv +3 -0
- data/bin/studio_game +32 -0
- data/lib/studio_game/auditable.rb +5 -0
- data/lib/studio_game/berserk_player.rb +31 -0
- data/lib/studio_game/clumsy_player.rb +41 -0
- data/lib/studio_game/die.rb +18 -0
- data/lib/studio_game/game.rb +107 -0
- data/lib/studio_game/game_turn.rb +23 -0
- data/lib/studio_game/loaded_die.rb +15 -0
- data/lib/studio_game/playable.rb +16 -0
- data/lib/studio_game/player.rb +64 -0
- data/lib/studio_game/treasure_trove.rb +17 -0
- data/spec/studio_game/berserk_player_spec.rb +30 -0
- data/spec/studio_game/clumsy_player_spec.rb +43 -0
- data/spec/studio_game/game_spec.rb +62 -0
- data/spec/studio_game/player_spec.rb +123 -0
- data/spec/studio_game/spec_helper.rb +8 -0
- data/spec/studio_game/treasure_trove_spec.rb +53 -0
- metadata +94 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6daa3e22d8a1bf5f6ae17fabcee46a02c26745f15933314a202f727aef6c4b67
|
|
4
|
+
data.tar.gz: 4a790663706d7e41f490942c3cca6771c9ad2ef9eec56efbfbfe6b53b65563c2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ae3f8a0160b677be62a310f850079bb7ed02db520e332732745a2f5d21c58571a9ea70e95efc59d257214154765703e393fd5329cdc4b678498f61fb9f5d97ea
|
|
7
|
+
data.tar.gz: 5f87bb99ee6866666d87f01a8b41d70a868a5d6727dfc80ff8a20923e0d7af4c9f52fe165762929bd66b00ccffa412f438817c35f622f0c6ca56340273cf2ae8
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Copyright (c) 2012 The Pragmatic Studio
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
- You may not use this Software in other training contexts.
|
|
11
|
+
|
|
12
|
+
- The above copyright notice and this permission notice shall be
|
|
13
|
+
included in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README
ADDED
data/bin/high_scores.txt
ADDED
data/bin/player.csv
ADDED
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
|
+
|
|
8
|
+
player4 = StudioGame::ClumsyPlayer.new('klutz', 105)
|
|
9
|
+
player5 = StudioGame::BerserkPlayer.new("berserker", 50)
|
|
10
|
+
|
|
11
|
+
knuckelheads = StudioGame::Game.new("Knuckleheads")
|
|
12
|
+
knuckelheads.load_players(ARGV.shift || File.join(File.dirname(__FILE__), 'player.csv'))
|
|
13
|
+
knuckelheads.add_player(player4)
|
|
14
|
+
knuckelheads.add_player(player5)
|
|
15
|
+
|
|
16
|
+
loop do
|
|
17
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
|
18
|
+
resp = gets.chomp.downcase
|
|
19
|
+
case resp
|
|
20
|
+
when /^\d+$/
|
|
21
|
+
|
|
22
|
+
knuckelheads.play(resp.to_i)
|
|
23
|
+
when 'quit', 'exit'
|
|
24
|
+
knuckelheads.print_stats
|
|
25
|
+
break
|
|
26
|
+
else
|
|
27
|
+
puts "\nPlease enter a number or 'quit'"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
knuckelheads.save_high_scores()
|
|
31
|
+
|
|
32
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
|
|
3
|
+
module StudioGame
|
|
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
|
+
berserk? ? w00t : super
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
if __FILE__ == $0
|
|
27
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
|
28
|
+
6.times { berserker.w00t }
|
|
29
|
+
2.times { berserker.blam }
|
|
30
|
+
puts berserker.health
|
|
31
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
|
|
3
|
+
module StudioGame
|
|
4
|
+
class ClumsyPlayer < Player
|
|
5
|
+
attr_reader :boost
|
|
6
|
+
|
|
7
|
+
def initialize (name, health=100)
|
|
8
|
+
super(name, health)
|
|
9
|
+
@boost = (1..10).to_a.sample
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def w00t
|
|
13
|
+
@boost.times { super }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def found_treasure(treasure)
|
|
17
|
+
damaged_treasue = Treasure.new(treasure.name, treasure.points / 2)
|
|
18
|
+
super (damaged_treasue)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
if __FILE__ == $0
|
|
26
|
+
clumsy = ClumsyPlayer.new("klutz")
|
|
27
|
+
|
|
28
|
+
hammer = Treasure.new(:hammer, 50)
|
|
29
|
+
clumsy.found_treasure(hammer)
|
|
30
|
+
clumsy.found_treasure(hammer)
|
|
31
|
+
clumsy.found_treasure(hammer)
|
|
32
|
+
puts clumsy.boost
|
|
33
|
+
|
|
34
|
+
crowbar = Treasure.new(:crowbar, 400)
|
|
35
|
+
clumsy.found_treasure(crowbar)
|
|
36
|
+
|
|
37
|
+
clumsy.each_found_treasure do |treasure|
|
|
38
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
39
|
+
end
|
|
40
|
+
puts "#{clumsy.points} grand total points"
|
|
41
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
def initialize(title)
|
|
11
|
+
@title = title
|
|
12
|
+
@players = []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def load_players(filename)
|
|
16
|
+
CSV.foreach(filename) do |row|
|
|
17
|
+
player = Player.new(row[0], row[1].to_i)
|
|
18
|
+
add_player(player)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def high_score_etnry(player)
|
|
23
|
+
formatted_name = player.name.ljust(20,'.')
|
|
24
|
+
"#{formatted_name } #{player.score}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def save_high_scores(filname = "high_scores.txt")
|
|
28
|
+
File.open(filname, "w") do |file|
|
|
29
|
+
file.puts "#{@title} High Score"
|
|
30
|
+
@players.sort.each do |player|
|
|
31
|
+
file.puts high_score_etnry(player)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def add_player(player)
|
|
38
|
+
@players << player
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def play(rounds)
|
|
42
|
+
puts @players
|
|
43
|
+
treasures = TreasureTrove::TREASURES
|
|
44
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
|
45
|
+
treasures.each do |treasure|
|
|
46
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
|
47
|
+
end
|
|
48
|
+
1.upto(rounds) do |round|
|
|
49
|
+
if block_given?
|
|
50
|
+
break if yield
|
|
51
|
+
end
|
|
52
|
+
puts "\nRound #{round}:"
|
|
53
|
+
@players.each do |player|
|
|
54
|
+
GameTurn.take_turn(player)
|
|
55
|
+
puts player
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def print_name_and_health(player)
|
|
61
|
+
puts "#{player.name} (#{player.health})"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def total_points
|
|
65
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def print_stats
|
|
69
|
+
@players.sort.each do |player|
|
|
70
|
+
puts "\n#{player.name}'s point totals:"
|
|
71
|
+
player.each_found_treasure do |treasure|
|
|
72
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
73
|
+
end
|
|
74
|
+
puts "#{player.points} grand total points"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
strong, wimpy = @players.partition { |player| player.strong? }
|
|
78
|
+
puts "\n#{title} Statistics:"
|
|
79
|
+
puts "\n#{strong.size} strong players:"
|
|
80
|
+
strong.each do |player|
|
|
81
|
+
print_name_and_health(player)
|
|
82
|
+
end
|
|
83
|
+
puts "\n#{wimpy.size} wimpy players:"
|
|
84
|
+
wimpy.each do |player|
|
|
85
|
+
print_name_and_health(player)
|
|
86
|
+
end
|
|
87
|
+
puts "\n#{title} High Scores:"
|
|
88
|
+
@players.sort.each do |player|
|
|
89
|
+
puts high_score_etnry(player)
|
|
90
|
+
end
|
|
91
|
+
puts "\nTotal treasure points found: #{total_points}"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
if __FILE__ == $0
|
|
97
|
+
player1 = Player.new('moe')
|
|
98
|
+
player2 = Player.new('larry', 60)
|
|
99
|
+
knuckelheads = Game.new("Knuckleheads")
|
|
100
|
+
knuckelheads.add_player(player1)
|
|
101
|
+
knuckelheads.add_player(player2)
|
|
102
|
+
knuckelheads.play(5)
|
|
103
|
+
knuckelheads.print_stats
|
|
104
|
+
knuckelheads.play(5) do
|
|
105
|
+
knuckelheads.total_points >= 2000
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require_relative 'die'
|
|
2
|
+
require_relative 'player'
|
|
3
|
+
require_relative 'treasure_trove'
|
|
4
|
+
require_relative 'loaded_die'
|
|
5
|
+
|
|
6
|
+
module StudioGame
|
|
7
|
+
module GameTurn
|
|
8
|
+
|
|
9
|
+
def self.take_turn(player)
|
|
10
|
+
die = Die.new
|
|
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
|
+
treasure = TreasureTrove.random
|
|
20
|
+
player.found_treasure(treasure)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
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
|
+
attr_accessor :health
|
|
8
|
+
attr_accessor :name
|
|
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(str)
|
|
17
|
+
name, health = str.split(',')
|
|
18
|
+
Player.new(name, Integer(health))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def found_treasure(treasure)
|
|
22
|
+
@found_treasures[treasure.name] += treasure.points
|
|
23
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points}"
|
|
24
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def points
|
|
28
|
+
@found_treasures.values.reduce(0,:+)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def name=(new_n)
|
|
32
|
+
@name = new_n.capitalize
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def to_s
|
|
36
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def <=>(other)
|
|
40
|
+
other.score <=> score
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def score
|
|
44
|
+
@health + points
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def each_found_treasure
|
|
48
|
+
@found_treasures.each do |name, points|
|
|
49
|
+
treasure = Treasure.new(name, points)
|
|
50
|
+
yield treasure
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
if __FILE__ == $0
|
|
57
|
+
player = Player.new("moe")
|
|
58
|
+
puts player.name
|
|
59
|
+
puts player.health
|
|
60
|
+
player.w00t
|
|
61
|
+
puts player.health
|
|
62
|
+
player.blam
|
|
63
|
+
puts player.health
|
|
64
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module StudioGame
|
|
2
|
+
Treasure = Struct.new(:name, :points)
|
|
3
|
+
|
|
4
|
+
module TreasureTrove
|
|
5
|
+
TREASURES =[ Treasure.new(:pie, 5),
|
|
6
|
+
Treasure.new(:bottle, 25),
|
|
7
|
+
Treasure.new(:hammer, 50),
|
|
8
|
+
Treasure.new(:skillet, 100),
|
|
9
|
+
Treasure.new(:broomstick, 200),
|
|
10
|
+
Treasure.new(:crowbar, 400)
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
def self.random
|
|
14
|
+
TREASURES.sample
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/player'
|
|
3
|
+
require 'studio_game/berserk_player'
|
|
4
|
+
|
|
5
|
+
module StudioGame
|
|
6
|
+
describe BerserkPlayer do
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
@inital_health = 50
|
|
10
|
+
@berserk_player = BerserkPlayer.new('berserker', @inital_health)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "does not go berserk when w00ted up to 5 times" do
|
|
14
|
+
1.upto(5) {@berserk_player.w00t}
|
|
15
|
+
@berserk_player.berserk?.should be_falsey
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "goes berserk when w00ted more then 5 times" do
|
|
19
|
+
1.upto(6) {@berserk_player.w00t}
|
|
20
|
+
@berserk_player.berserk?.should be_truthy
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "get's w00ted instead of blammed when it's gone berserk" do
|
|
24
|
+
1.upto(6) {@berserk_player.w00t}
|
|
25
|
+
1.upto(2) {@berserk_player.blam}
|
|
26
|
+
|
|
27
|
+
@berserk_player.health.should == @inital_health + (8 * 15)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/player'
|
|
3
|
+
require 'studio_game/clumsy_player'
|
|
4
|
+
|
|
5
|
+
module StudioGame
|
|
6
|
+
describe ClumsyPlayer do
|
|
7
|
+
before do
|
|
8
|
+
@player = ClumsyPlayer.new("Klutz")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "only get's half the point value for each treasure" do
|
|
12
|
+
@player.points.should == 0
|
|
13
|
+
|
|
14
|
+
hammer = Treasure.new(:hammer, 50)
|
|
15
|
+
@player.found_treasure(hammer)
|
|
16
|
+
@player.found_treasure(hammer)
|
|
17
|
+
@player.found_treasure(hammer)
|
|
18
|
+
|
|
19
|
+
@player.points.should == 75
|
|
20
|
+
|
|
21
|
+
crowbar = Treasure.new(:crowbar, 400)
|
|
22
|
+
@player.found_treasure(crowbar)
|
|
23
|
+
|
|
24
|
+
@player.points.should == 275
|
|
25
|
+
|
|
26
|
+
yielded = []
|
|
27
|
+
@player.each_found_treasure do |treasure|
|
|
28
|
+
yielded << treasure
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "should get a random boost when w00t" do
|
|
35
|
+
@player.health.should == 100
|
|
36
|
+
puts @player.health
|
|
37
|
+
|
|
38
|
+
2.times { @player.w00t }
|
|
39
|
+
|
|
40
|
+
@player.health.should == 100 + (@player.boost * 15 * 2)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require 'studio_game/game'
|
|
2
|
+
require_relative 'spec_helper'
|
|
3
|
+
|
|
4
|
+
module StudioGame
|
|
5
|
+
describe Game do
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
$stdout = StringIO.new
|
|
9
|
+
@game = Game.new("Knuckleheads")
|
|
10
|
+
|
|
11
|
+
@initial_health = 100
|
|
12
|
+
@player = Player.new("moe", @initial_health)
|
|
13
|
+
|
|
14
|
+
@game.add_player(@player)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "w00t's all the player at a >4 number" do
|
|
18
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
|
19
|
+
@game.play(2)
|
|
20
|
+
@player.health.should == @initial_health + ( 15 * 2)
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "does noting with a medium number 3 or 4" do
|
|
25
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(4)
|
|
26
|
+
@game.play (2)
|
|
27
|
+
@player.health.should == @initial_health
|
|
28
|
+
end
|
|
29
|
+
it "blam for a nmber 2 or 1" do
|
|
30
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(2)
|
|
31
|
+
@game.play(2)
|
|
32
|
+
@player.health.should == @initial_health - (10 * 2)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "assigns a treasure for points during a player's turn" do
|
|
36
|
+
game = Game.new("Knuckleheads")
|
|
37
|
+
player = Player.new("moe")
|
|
38
|
+
|
|
39
|
+
game.add_player(player)
|
|
40
|
+
|
|
41
|
+
game.play(1)
|
|
42
|
+
|
|
43
|
+
player.points.should_not be_zero
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "computes total points as the sum of all player points" do
|
|
47
|
+
game = Game.new("Knuckleheads")
|
|
48
|
+
|
|
49
|
+
player1 = Player.new("moe")
|
|
50
|
+
player2 = Player.new("larry")
|
|
51
|
+
|
|
52
|
+
game.add_player(player1)
|
|
53
|
+
game.add_player(player2)
|
|
54
|
+
|
|
55
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
|
56
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
|
57
|
+
player1.found_treasure(Treasure.new(:crowbar, 400))
|
|
58
|
+
|
|
59
|
+
game.total_points.should == 500
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/player'
|
|
3
|
+
require 'studio_game/treasure_trove.rb'
|
|
4
|
+
|
|
5
|
+
module StudioGame
|
|
6
|
+
describe Player do
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
$stdout = StringIO.new
|
|
10
|
+
@health = 150
|
|
11
|
+
@player = Player.new("larry", @health)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "has a capitalized name" do
|
|
15
|
+
@player.name.should == "Larry"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "has an initial health" do
|
|
19
|
+
@player.health.should == 150
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "has a string representation" do
|
|
23
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
24
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
25
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
it "increases health by 15 when w00ted" do
|
|
30
|
+
@player.w00t
|
|
31
|
+
@player.health.should == 165
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "decreases health by 10 when blammed" do
|
|
35
|
+
@player.blam
|
|
36
|
+
@player.health.should == 140
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
context "with a helath greater then 100" do
|
|
40
|
+
before do
|
|
41
|
+
@player = Player.new("Larry",150)
|
|
42
|
+
end
|
|
43
|
+
it "is strong" do
|
|
44
|
+
@player.should be_strong
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
context "with a helath of <= 100" do
|
|
49
|
+
before do
|
|
50
|
+
@player = Player.new("Larry",100)
|
|
51
|
+
end
|
|
52
|
+
it "is strong" do
|
|
53
|
+
@player.should_not be_strong
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
context "in a collection of players" do
|
|
58
|
+
before do
|
|
59
|
+
@player1 = Player.new("moe", 100)
|
|
60
|
+
@player2 = Player.new("larry", 200)
|
|
61
|
+
@player3 = Player.new("curly", 300)
|
|
62
|
+
|
|
63
|
+
@players = [@player1, @player2, @player3]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "is sorted by decreasing score" do
|
|
67
|
+
@players.sort.should == [@player3, @player2, @player1]
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "computes points as the sum of all treasure points" do
|
|
72
|
+
@player.points.should == 0
|
|
73
|
+
|
|
74
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
75
|
+
|
|
76
|
+
@player.points.should == 50
|
|
77
|
+
|
|
78
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
|
79
|
+
|
|
80
|
+
@player.points.should == 450
|
|
81
|
+
|
|
82
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
83
|
+
|
|
84
|
+
@player.points.should == 500
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "computes a score as the sum of its health and points" do
|
|
88
|
+
@player.found_treasure(Treasure.new(:hammer,50))
|
|
89
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
90
|
+
|
|
91
|
+
@player.score.should == 250
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "yields each found treasure and its total points" do
|
|
95
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
96
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
97
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
98
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
99
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
100
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
101
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
102
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
103
|
+
|
|
104
|
+
yielded = []
|
|
105
|
+
@player.each_found_treasure do |treasure|
|
|
106
|
+
yielded << treasure
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
yielded.should == [
|
|
110
|
+
Treasure.new(:skillet, 200),
|
|
111
|
+
Treasure.new(:hammer, 50),
|
|
112
|
+
Treasure.new(:bottle, 25)
|
|
113
|
+
]
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it "converts a CSV string into a new player object" do
|
|
117
|
+
player = Player.from_CSV("Tom,18")
|
|
118
|
+
|
|
119
|
+
player.name.should == "Tom"
|
|
120
|
+
player.health.should == 18
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'studio_game/treasure_trove'
|
|
2
|
+
require_relative 'spec_helper'
|
|
3
|
+
|
|
4
|
+
module StudioGame
|
|
5
|
+
describe Treasure do
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
@treasure = Treasure.new(:hammer, 50)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "has a name attribute" do
|
|
12
|
+
@treasure.name.should == :hammer
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "has a points attribute" do
|
|
16
|
+
@treasure.points.should == 50
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe TreasureTrove do
|
|
21
|
+
it "has six treasures" do
|
|
22
|
+
TreasureTrove::TREASURES.size.should == 6
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "has a pie worth 5 points" do
|
|
26
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "has a pie worth 5 points" do
|
|
30
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "has a hammer worht 50 points" do
|
|
34
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "has a hammer worht 50 points" do
|
|
38
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet ,100)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "has a hammer worht 50 points" do
|
|
42
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "has a hammer worht 50 points" do
|
|
46
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "returns a random treasure" do
|
|
50
|
+
TreasureTrove::TREASURES.should include(TreasureTrove.random)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: studio_game_umueller
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Uwe Mueller
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-07-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rspec
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 2.8.0
|
|
20
|
+
- - "~>"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '2.8'
|
|
23
|
+
type: :development
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: 2.8.0
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '2.8'
|
|
33
|
+
description: "This is an example application that simulates a game as descirbed at
|
|
34
|
+
\n http://pragmaticstudio.com\nThis code is Copyright 2012 The Pragmatic Studio.
|
|
35
|
+
See the LICENSE file."
|
|
36
|
+
email: mueller.uwe@gmail.com
|
|
37
|
+
executables:
|
|
38
|
+
- studio_game
|
|
39
|
+
extensions: []
|
|
40
|
+
extra_rdoc_files: []
|
|
41
|
+
files:
|
|
42
|
+
- LICENSE
|
|
43
|
+
- README
|
|
44
|
+
- bin/high_scores.txt
|
|
45
|
+
- bin/my_favorite_players.csv
|
|
46
|
+
- bin/player.csv
|
|
47
|
+
- bin/studio_game
|
|
48
|
+
- lib/studio_game/auditable.rb
|
|
49
|
+
- lib/studio_game/berserk_player.rb
|
|
50
|
+
- lib/studio_game/clumsy_player.rb
|
|
51
|
+
- lib/studio_game/die.rb
|
|
52
|
+
- lib/studio_game/game.rb
|
|
53
|
+
- lib/studio_game/game_turn.rb
|
|
54
|
+
- lib/studio_game/loaded_die.rb
|
|
55
|
+
- lib/studio_game/playable.rb
|
|
56
|
+
- lib/studio_game/player.rb
|
|
57
|
+
- lib/studio_game/treasure_trove.rb
|
|
58
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
59
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
60
|
+
- spec/studio_game/game_spec.rb
|
|
61
|
+
- spec/studio_game/player_spec.rb
|
|
62
|
+
- spec/studio_game/spec_helper.rb
|
|
63
|
+
- spec/studio_game/treasure_trove_spec.rb
|
|
64
|
+
homepage: http://pragmaticstudio.com
|
|
65
|
+
licenses:
|
|
66
|
+
- MIT
|
|
67
|
+
metadata: {}
|
|
68
|
+
post_install_message:
|
|
69
|
+
rdoc_options: []
|
|
70
|
+
require_paths:
|
|
71
|
+
- lib
|
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '1.9'
|
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
requirements: []
|
|
83
|
+
rubyforge_project:
|
|
84
|
+
rubygems_version: 2.7.9
|
|
85
|
+
signing_key:
|
|
86
|
+
specification_version: 4
|
|
87
|
+
summary: A fun, and entirely random, text-based game
|
|
88
|
+
test_files:
|
|
89
|
+
- spec/studio_game/spec_helper.rb
|
|
90
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
91
|
+
- spec/studio_game/treasure_trove_spec.rb
|
|
92
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
93
|
+
- spec/studio_game/player_spec.rb
|
|
94
|
+
- spec/studio_game/game_spec.rb
|