studio_game_2018_08_28 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +19 -0
- data/README +1 -0
- data/bin/high_scores.txt +6 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +36 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +35 -0
- data/lib/studio_game/clumsy_player.rb +27 -0
- data/lib/studio_game/die.rb +19 -0
- data/lib/studio_game/game.rb +107 -0
- data/lib/studio_game/game_turn.rb +28 -0
- data/lib/studio_game/loaded_die.rb +16 -0
- data/lib/studio_game/playable.rb +17 -0
- data/lib/studio_game/player.rb +60 -0
- data/lib/studio_game/treasure_trove.rb +18 -0
- data/spec/studio_game/berserk_player_spec.rb +31 -0
- data/spec/studio_game/clumsy_player_spec.rb +33 -0
- data/spec/studio_game/game_spec.rb +68 -0
- data/spec/studio_game/player_spec.rb +132 -0
- data/spec/studio_game/spec_helper.rb +100 -0
- data/spec/studio_game/treasure_trove_spec.rb +57 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0d1c16ebb3568a9369bf353d83169a9f260a2557f5ce6850865bab4ec0fcc411
|
4
|
+
data.tar.gz: ad7738a36088421ecd9db84b6cc4f24d4cee26d85aa48ce1346b183a7feb8d64
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b449444ad8d3bd3673913162dbe64f1732a4605433854a86c6e05db472a8719799d1ad2ae51d46b555a38577a05e911453ab9b04504da34a3f61f137d84820b
|
7
|
+
data.tar.gz: 72d70806828d31aca412bf24ed70f2fd10ce893b4d72ae69f18f15f10aec827a111f89cccc994b22733569d35f1b7990d86b95cf89444dc934bd1b7586b20e50
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) <2018> <John Kawahara>
|
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
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
This studio_game gem is my exercise solution from the Pragmatic Studio Ruby Programming online course.
|
data/bin/high_scores.txt
ADDED
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/player'
|
4
|
+
require_relative '../lib/studio_game/game'
|
5
|
+
require_relative '../lib/studio_game/clumsy_player'
|
6
|
+
require_relative '../lib/studio_game/berserk_player'
|
7
|
+
|
8
|
+
player1 = StudioGame::Player.new("moe")
|
9
|
+
player2 = StudioGame::Player.new("larry", 60)
|
10
|
+
player3 = StudioGame::Player.new("Curly", 125)
|
11
|
+
|
12
|
+
knuckleheads = StudioGame::Game.new("knuckleheads")
|
13
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
14
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
15
|
+
|
16
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
|
17
|
+
knuckleheads.add_player(klutz)
|
18
|
+
|
19
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
20
|
+
knuckleheads.add_player(berserker)
|
21
|
+
|
22
|
+
loop do
|
23
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
24
|
+
answer = gets.chomp.downcase
|
25
|
+
case answer
|
26
|
+
when /^\d+$/
|
27
|
+
knuckleheads.play(answer.to_i)
|
28
|
+
when 'quit', 'exit'
|
29
|
+
knuckleheads.print_stats
|
30
|
+
break
|
31
|
+
else
|
32
|
+
puts "Please enter a number or 'quit'"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
knuckleheads.save_high_scores
|
@@ -0,0 +1,35 @@
|
|
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
|
+
if berserk?
|
22
|
+
w00t
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if __FILE__ == $0
|
31
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
32
|
+
6.times { berserker.w00t }
|
33
|
+
2.times { berserker.blam }
|
34
|
+
puts berserker.health
|
35
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class ClumsyPlayer < Player
|
5
|
+
def found_treasure(treasure)
|
6
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2.0)
|
7
|
+
super(damaged_treasure)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
if __FILE__ == $0
|
12
|
+
clumsy = ClumsyPlayer.new("klutz")
|
13
|
+
|
14
|
+
hammer = Treasure.new(:hammer, 50)
|
15
|
+
clumsy.found_treasure(hammer)
|
16
|
+
clumsy.found_treasure(hammer)
|
17
|
+
clumsy.found_treasure(hammer)
|
18
|
+
|
19
|
+
crowbar = Treasure.new(:crowbar, 400)
|
20
|
+
clumsy.found_treasure(crowbar)
|
21
|
+
|
22
|
+
clumsy.each_found_treasure do |treasure|
|
23
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
24
|
+
end
|
25
|
+
puts "#{clumsy.points} grand total points"
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'game_turn'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
class Game
|
7
|
+
attr_reader :title
|
8
|
+
|
9
|
+
def initialize(title)
|
10
|
+
@title = title
|
11
|
+
@players = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_player (a_player)
|
15
|
+
@players << a_player
|
16
|
+
end
|
17
|
+
|
18
|
+
def play(rounds)
|
19
|
+
puts "There are #{@players.size} players in #{@title.capitalize}:"
|
20
|
+
|
21
|
+
@players.each do |player|
|
22
|
+
puts player
|
23
|
+
end
|
24
|
+
|
25
|
+
treasures = TreasureTrove::TREASURES
|
26
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
27
|
+
treasures.each do |treasure|
|
28
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
29
|
+
end
|
30
|
+
|
31
|
+
1.upto(rounds) do |round|
|
32
|
+
puts "\nRound #{round}:"
|
33
|
+
@players.each do |player|
|
34
|
+
GameTurn.take_turn(player)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def print_name_and_health(player)
|
40
|
+
puts "#{player.name} (#{player.health})"
|
41
|
+
end
|
42
|
+
|
43
|
+
def high_score_entry(player)
|
44
|
+
formatted_name = player.name.ljust(20, '.')
|
45
|
+
"#{formatted_name} #{player.score}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def total_points
|
49
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
50
|
+
end
|
51
|
+
|
52
|
+
def print_stats
|
53
|
+
puts "\n#{title.capitalize} Statistics:"
|
54
|
+
|
55
|
+
strong_players, wimpy_players = @players.partition { |player| player.strong? }
|
56
|
+
|
57
|
+
puts "#{strong_players.size} strong player(s):"
|
58
|
+
strong_players.each do |player|
|
59
|
+
print_name_and_health(player)
|
60
|
+
end
|
61
|
+
|
62
|
+
puts "#{wimpy_players.size} wimpy player(s):"
|
63
|
+
wimpy_players.each do |player|
|
64
|
+
print_name_and_health(player)
|
65
|
+
end
|
66
|
+
|
67
|
+
puts "\n#{total_points} total points from treasures found"
|
68
|
+
@players.each do |player|
|
69
|
+
puts "\n#{player.name}'s point totals:"
|
70
|
+
player.each_found_treasure do |treasure|
|
71
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
72
|
+
end
|
73
|
+
puts "#{player.points} grand total points"
|
74
|
+
end
|
75
|
+
|
76
|
+
puts "\n#{title.capitalize} High Scores:"
|
77
|
+
@players.sort.each do |player|
|
78
|
+
puts high_score_entry(player)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def load_players(from_file)
|
83
|
+
File.readlines(from_file).each do |line|
|
84
|
+
add_player(Player.from_csv(line))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def save_high_scores(to_file="high_scores.txt")
|
89
|
+
File.open(to_file, "w") do |file|
|
90
|
+
file.puts "#{title.capitalize} High Scores:"
|
91
|
+
@players.sort.each do |player|
|
92
|
+
file.puts high_score_entry(player)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
if __FILE__ == $0
|
100
|
+
player = Player.new("larry")
|
101
|
+
puts player.name
|
102
|
+
puts player.health
|
103
|
+
player.w00t
|
104
|
+
puts player.health
|
105
|
+
player.blam
|
106
|
+
puts player.health
|
107
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
require_relative 'loaded_die'
|
5
|
+
|
6
|
+
module StudioGame
|
7
|
+
module GameTurn
|
8
|
+
def self.take_turn(player)
|
9
|
+
die = Die.new
|
10
|
+
case die.roll
|
11
|
+
when 1..2
|
12
|
+
player.blam
|
13
|
+
when 3..4
|
14
|
+
puts "#{player.name} was skipped."
|
15
|
+
when 5..6
|
16
|
+
player.w00t
|
17
|
+
end
|
18
|
+
|
19
|
+
treasure = TreasureTrove.random
|
20
|
+
player.found_treasure(treasure)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if __FILE__ == $0
|
26
|
+
player = Player.new("curly", 125)
|
27
|
+
GameTurn.take_turn(player)
|
28
|
+
end
|
@@ -0,0 +1,60 @@
|
|
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_reader :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 score
|
18
|
+
@health + points
|
19
|
+
end
|
20
|
+
|
21
|
+
def points
|
22
|
+
@found_treasures.values.reduce(0, :+)
|
23
|
+
end
|
24
|
+
|
25
|
+
def found_treasure(treasure)
|
26
|
+
@found_treasures[treasure.name] += treasure.points
|
27
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
28
|
+
end
|
29
|
+
|
30
|
+
def each_found_treasure
|
31
|
+
@found_treasures.each do |name, points|
|
32
|
+
yield Treasure.new(name, points)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def <=>(other)
|
37
|
+
other.score <=> score
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.from_csv(string)
|
45
|
+
name, health = string.split(',')
|
46
|
+
Player.new(name, Integer(health))
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
if __FILE__ == $0
|
53
|
+
player = Player.new("moe")
|
54
|
+
puts player.name
|
55
|
+
puts player.health
|
56
|
+
player.w00t
|
57
|
+
puts player.health
|
58
|
+
player.blam
|
59
|
+
puts player.health
|
60
|
+
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,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 = StudioGame::BerserkPlayer.new("berserker", @initial_health)
|
9
|
+
$stdout = StringIO.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "does not go berserk when w00ted up to 5 times" do
|
13
|
+
1.upto(5) { @player.w00t }
|
14
|
+
|
15
|
+
expect(@player.berserk?).not_to be_truthy
|
16
|
+
end
|
17
|
+
|
18
|
+
it "goes berserk when w00ted more than 5 times" do
|
19
|
+
1.upto(6) { @player.w00t }
|
20
|
+
|
21
|
+
expect(@player.berserk?).to be_truthy
|
22
|
+
end
|
23
|
+
|
24
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
25
|
+
1.upto(6) { @player.w00t }
|
26
|
+
1.upto(2) { @player.blam }
|
27
|
+
|
28
|
+
expect(@player.health).to eq(@initial_health + (8 * 15))
|
29
|
+
end
|
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 = StudioGame::ClumsyPlayer.new("klutz")
|
7
|
+
$stdout = StringIO.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "only gets half the point value for each treasure" do
|
11
|
+
expect(@player.points).to eq(0)
|
12
|
+
|
13
|
+
hammer = StudioGame::Treasure.new(:hammer, 50)
|
14
|
+
@player.found_treasure(hammer)
|
15
|
+
@player.found_treasure(hammer)
|
16
|
+
@player.found_treasure(hammer)
|
17
|
+
|
18
|
+
expect(@player.points).to eq(75)
|
19
|
+
|
20
|
+
crowbar = StudioGame::Treasure.new(:crowbar, 400)
|
21
|
+
@player.found_treasure(crowbar)
|
22
|
+
|
23
|
+
expect(@player.points).to eq(275)
|
24
|
+
|
25
|
+
yielded = []
|
26
|
+
@player.each_found_treasure do |treasure|
|
27
|
+
yielded << treasure
|
28
|
+
end
|
29
|
+
|
30
|
+
expect(yielded).to eq([StudioGame::Treasure.new(:hammer, 75), StudioGame::Treasure.new(:crowbar, 200)])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe Game do
|
5
|
+
before do
|
6
|
+
@game = Game.new("Knuckleheads")
|
7
|
+
|
8
|
+
@initial_health = 100
|
9
|
+
@player = Player.new("moe", @initial_health)
|
10
|
+
|
11
|
+
@game.add_player(@player)
|
12
|
+
|
13
|
+
$stdout = StringIO.new
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
it "w00ts the player if a high number is rolled" do
|
18
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
19
|
+
|
20
|
+
@game.play(2)
|
21
|
+
|
22
|
+
expect(@player.health).to eq(@initial_health + (15 * 2))
|
23
|
+
end
|
24
|
+
|
25
|
+
it "skips the player if a medium number is rolled" do
|
26
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(3)
|
27
|
+
|
28
|
+
@game.play(2)
|
29
|
+
|
30
|
+
expect(@player.health).to eq(@initial_health)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "blams the player if a low number is rolled" do
|
34
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(1)
|
35
|
+
|
36
|
+
@game.play(2)
|
37
|
+
|
38
|
+
expect(@player.health).to eq(@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
|
+
expect(player.points).not_to 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
|
+
expect(game.total_points).to eq(500)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'studio_game/player'
|
2
|
+
require 'studio_game/treasure_trove'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
describe Player do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@initial_health = 150
|
9
|
+
@player = Player.new("larry", @initial_health)
|
10
|
+
$stdout = StringIO.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has a captizalized name" do
|
14
|
+
expect(@player.name).to eq("Larry")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has an initial health" do
|
18
|
+
expect(@player.health).to eq(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
|
+
expect(@player.to_s).to eq("I'm Larry 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
|
+
expect(@player.score).to eq(250)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "increases health by 15 when w00ted" do
|
36
|
+
@player.w00t
|
37
|
+
|
38
|
+
expect(@player.health).to eq(@initial_health + 15)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "decreases health by 10 when blammed" do
|
42
|
+
@player.blam
|
43
|
+
|
44
|
+
expect(@player.health).to eq(@initial_health - 10)
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with a health greater than 100" do
|
48
|
+
before do
|
49
|
+
@player = Player.new("larry", 150)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "is strong" do
|
53
|
+
expect(@player).to be_strong
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "with a health of 100 or less" do
|
58
|
+
before do
|
59
|
+
@player = Player.new("larry", 100)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "is wimpy" do
|
63
|
+
expect(@player).not_to be_strong
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "in a collection of players" do
|
68
|
+
before do
|
69
|
+
@player1 = Player.new("moe", 100)
|
70
|
+
@player2 = Player.new("larry", 200)
|
71
|
+
@player3 = Player.new("curly", 300)
|
72
|
+
@players = [@player1, @player2, @player3]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "is sorted by decreasing score" do
|
76
|
+
expect(@players.sort).to eq(@players.sort)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it "computes points as the sum of all treasure points" do
|
81
|
+
expect(@player.points).to eq(0)
|
82
|
+
|
83
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
84
|
+
|
85
|
+
expect(@player.points).to eq(50)
|
86
|
+
|
87
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
88
|
+
|
89
|
+
expect(@player.points).to eq(450)
|
90
|
+
|
91
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
92
|
+
|
93
|
+
expect(@player.points).to eq(500)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "computes a score as the sum of its health and points" do
|
97
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
98
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
99
|
+
|
100
|
+
expect(@player.score).to eq(250)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "yields each found treasure and its total points" do
|
104
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
105
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
106
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
107
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
108
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
109
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
110
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
111
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
112
|
+
|
113
|
+
yielded = []
|
114
|
+
@player.each_found_treasure do |treasure|
|
115
|
+
yielded << treasure
|
116
|
+
end
|
117
|
+
|
118
|
+
expect(yielded).to eq([
|
119
|
+
Treasure.new(:skillet, 200),
|
120
|
+
Treasure.new(:hammer, 50),
|
121
|
+
Treasure.new(:bottle, 25)
|
122
|
+
])
|
123
|
+
end
|
124
|
+
|
125
|
+
it "can be created from a CSV string" do
|
126
|
+
player = Player.from_csv("larry,150")
|
127
|
+
|
128
|
+
expect(player.name).to eq("Larry")
|
129
|
+
expect(player.health).to eq(150)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
16
|
+
RSpec.configure do |config|
|
17
|
+
# rspec-expectations config goes here. You can use an alternate
|
18
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
19
|
+
# assertions if you prefer.
|
20
|
+
config.expect_with :rspec do |expectations|
|
21
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
22
|
+
# and `failure_message` of custom matchers include text for helper methods
|
23
|
+
# defined using `chain`, e.g.:
|
24
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
25
|
+
# # => "be bigger than 2 and smaller than 4"
|
26
|
+
# ...rather than:
|
27
|
+
# # => "be bigger than 2"
|
28
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
29
|
+
end
|
30
|
+
|
31
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
32
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
33
|
+
config.mock_with :rspec do |mocks|
|
34
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
35
|
+
# a real object. This is generally recommended, and will default to
|
36
|
+
# `true` in RSpec 4.
|
37
|
+
mocks.verify_partial_doubles = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
41
|
+
# have no way to turn it off -- the option exists only for backwards
|
42
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
43
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
44
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
45
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
46
|
+
|
47
|
+
# The settings below are suggested to provide a good initial experience
|
48
|
+
# with RSpec, but feel free to customize to your heart's content.
|
49
|
+
=begin
|
50
|
+
# This allows you to limit a spec run to individual examples or groups
|
51
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
52
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
53
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
54
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
55
|
+
config.filter_run_when_matching :focus
|
56
|
+
|
57
|
+
# Allows RSpec to persist some state between runs in order to support
|
58
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
59
|
+
# you configure your source control system to ignore this file.
|
60
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
61
|
+
|
62
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
63
|
+
# recommended. For more details, see:
|
64
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
65
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
66
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
67
|
+
config.disable_monkey_patching!
|
68
|
+
|
69
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
70
|
+
# be too noisy due to issues in dependencies.
|
71
|
+
config.warnings = true
|
72
|
+
|
73
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
74
|
+
# file, and it's useful to allow more verbose output when running an
|
75
|
+
# individual spec file.
|
76
|
+
if config.files_to_run.one?
|
77
|
+
# Use the documentation formatter for detailed output,
|
78
|
+
# unless a formatter has already been configured
|
79
|
+
# (e.g. via a command-line flag).
|
80
|
+
config.default_formatter = "doc"
|
81
|
+
end
|
82
|
+
|
83
|
+
# Print the 10 slowest examples and example groups at the
|
84
|
+
# end of the spec run, to help surface which specs are running
|
85
|
+
# particularly slow.
|
86
|
+
config.profile_examples = 10
|
87
|
+
|
88
|
+
# Run specs in random order to surface order dependencies. If you find an
|
89
|
+
# order dependency and want to debug it, you can fix the order by providing
|
90
|
+
# the seed, which is printed after each run.
|
91
|
+
# --seed 1234
|
92
|
+
config.order = :random
|
93
|
+
|
94
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
95
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
96
|
+
# test failures related to randomization by passing the same `--seed` value
|
97
|
+
# as the one that triggered the failure.
|
98
|
+
Kernel.srand config.seed
|
99
|
+
=end
|
100
|
+
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
|
+
expect(@treasure.name).to eq(:hammer)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has a points attribute" do
|
15
|
+
expect(@treasure.points).to eq(50)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module StudioGame
|
21
|
+
describe TreasureTrove do
|
22
|
+
|
23
|
+
it "has six treasures" do
|
24
|
+
expect(TreasureTrove::TREASURES.size).to eq(6)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "has a pie worth 5 points" do
|
28
|
+
expect(TreasureTrove::TREASURES[0]).to eq(Treasure.new(:pie, 5))
|
29
|
+
end
|
30
|
+
|
31
|
+
it "has a bottle worth 25 points" do
|
32
|
+
expect(TreasureTrove::TREASURES[1]).to eq(Treasure.new(:bottle, 25))
|
33
|
+
end
|
34
|
+
|
35
|
+
it "has a hammer worth 50 points" do
|
36
|
+
expect(TreasureTrove::TREASURES[2]).to eq(Treasure.new(:hammer, 50))
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has a skillet worth 100 points" do
|
40
|
+
expect(TreasureTrove::TREASURES[3]).to eq(Treasure.new(:skillet, 100))
|
41
|
+
end
|
42
|
+
|
43
|
+
it "has a broomstick worth 200 points" do
|
44
|
+
expect(TreasureTrove::TREASURES[4]).to eq(Treasure.new(:broomstick, 200))
|
45
|
+
end
|
46
|
+
|
47
|
+
it "has a crowbar worth 400 points" do
|
48
|
+
expect(TreasureTrove::TREASURES[5]).to eq(Treasure.new(:crowbar, 400))
|
49
|
+
end
|
50
|
+
|
51
|
+
it "returns a random treasure" do
|
52
|
+
treasure = TreasureTrove.random
|
53
|
+
|
54
|
+
expect(TreasureTrove::TREASURES).to include(treasure)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_2018_08_28
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Kawahara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-28 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'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.8.0
|
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'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.8.0
|
33
|
+
description: 'This studio_game gem is my exercise solution from the Pragmatic Studio
|
34
|
+
Ruby Programming online course. '
|
35
|
+
email: kawaharajohn@gmail.com
|
36
|
+
executables:
|
37
|
+
- studio_game
|
38
|
+
extensions: []
|
39
|
+
extra_rdoc_files: []
|
40
|
+
files:
|
41
|
+
- LICENSE
|
42
|
+
- README
|
43
|
+
- bin/high_scores.txt
|
44
|
+
- bin/players.csv
|
45
|
+
- bin/studio_game
|
46
|
+
- lib/studio_game/auditable.rb
|
47
|
+
- lib/studio_game/berserk_player.rb
|
48
|
+
- lib/studio_game/clumsy_player.rb
|
49
|
+
- lib/studio_game/die.rb
|
50
|
+
- lib/studio_game/game.rb
|
51
|
+
- lib/studio_game/game_turn.rb
|
52
|
+
- lib/studio_game/loaded_die.rb
|
53
|
+
- lib/studio_game/playable.rb
|
54
|
+
- lib/studio_game/player.rb
|
55
|
+
- lib/studio_game/treasure_trove.rb
|
56
|
+
- spec/studio_game/berserk_player_spec.rb
|
57
|
+
- spec/studio_game/clumsy_player_spec.rb
|
58
|
+
- spec/studio_game/game_spec.rb
|
59
|
+
- spec/studio_game/player_spec.rb
|
60
|
+
- spec/studio_game/spec_helper.rb
|
61
|
+
- spec/studio_game/treasure_trove_spec.rb
|
62
|
+
homepage: https://rubygems.org/gems/studio_game_2018_08_28
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.9'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.7.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Pragmatic Studios studio_game exercise
|
86
|
+
test_files:
|
87
|
+
- spec/studio_game/spec_helper.rb
|
88
|
+
- spec/studio_game/clumsy_player_spec.rb
|
89
|
+
- spec/studio_game/treasure_trove_spec.rb
|
90
|
+
- spec/studio_game/berserk_player_spec.rb
|
91
|
+
- spec/studio_game/player_spec.rb
|
92
|
+
- spec/studio_game/game_spec.rb
|