studio_game_btihen 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 +0 -0
- data/README +0 -0
- data/bin/studio_game.rb +52 -0
- data/data/players.csv +4 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +36 -0
- data/lib/studio_game/clumsy_player.rb +38 -0
- data/lib/studio_game/die.rb +20 -0
- data/lib/studio_game/game.rb +113 -0
- data/lib/studio_game/game_turn.rb +23 -0
- data/lib/studio_game/loaded_die.rb +20 -0
- data/lib/studio_game/playable.rb +17 -0
- data/lib/studio_game/player.rb +80 -0
- data/lib/studio_game/treasure_trove.rb +17 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/studio_game/berserk_player_spec.rb +38 -0
- data/spec/studio_game/clumsy_player_spec.rb +52 -0
- data/spec/studio_game/die_spec.rb +22 -0
- data/spec/studio_game/game_spec.rb +45 -0
- data/spec/studio_game/player_spec.rb +114 -0
- data/spec/studio_game/treasure_trove_spec.rb +59 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3b4b8472be97a255f2c04ac05aaa62cd09fc10b0
|
4
|
+
data.tar.gz: 9198c183f3fca89d6e4d3a4f9201c323ad914bde
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 500e3e4561dc9f6920b3bc13cea81ee9b73998d1f3840daa2b0babd0997e7b87194bbf9a128f795e13441b698e4cc3c2a62338ae942b459a4aa559a19102aa7d
|
7
|
+
data.tar.gz: 314ce33f03e3da23cd406d3f84ac21bf131af471d258a75deae10e0c79d8a25cffd224663840216daa4f3c5feb70c92f340eebe8e56a5347d52672b8a59a7193
|
data/LICENSE
ADDED
File without changes
|
data/README
ADDED
File without changes
|
data/bin/studio_game.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require './lib/studio_game/game'
|
4
|
+
require './lib/studio_game/clumsy_player'
|
5
|
+
require './lib/studio_game/berserk_player'
|
6
|
+
|
7
|
+
|
8
|
+
# player1 = Player.new("moe")
|
9
|
+
# player2 = Player.new("larry", 60)
|
10
|
+
# player3 = Player.new("curly", 125)
|
11
|
+
# player4 = Player.new("Shemp", 90)
|
12
|
+
|
13
|
+
game=StudioGame::Game.new("knuckleheads")
|
14
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105, 3)
|
15
|
+
game.add_player(klutz)
|
16
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
17
|
+
game.add_player(berserker)
|
18
|
+
|
19
|
+
# game.add_player(player1)
|
20
|
+
# game.add_player(player2)
|
21
|
+
# game.add_player(player3)
|
22
|
+
|
23
|
+
# puts File.join(File.dirname(__FILE__),'../data/')
|
24
|
+
default_players_file = File.join(File.dirname(__FILE__),'../data/', 'players.csv')
|
25
|
+
game.load(ARGV.shift || default_players_file)
|
26
|
+
loop do
|
27
|
+
puts "How many rounds to play? ('quit' or 'q' to end)"
|
28
|
+
rounds = gets.chomp.downcase
|
29
|
+
case rounds
|
30
|
+
when /^\d+$/
|
31
|
+
game.play( rounds.to_i )
|
32
|
+
when 'q','quit','e','end','exit'
|
33
|
+
game.print_stats
|
34
|
+
break
|
35
|
+
else
|
36
|
+
puts "Enter a number or 'quit'"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
game.save
|
40
|
+
|
41
|
+
|
42
|
+
clumsy = StudioGame::ClumsyPlayer.new("klutz")
|
43
|
+
hammer = StudioGame::Treasure.new(:hammer, 50)
|
44
|
+
clumsy.found_treasure(hammer)
|
45
|
+
clumsy.found_treasure(hammer)
|
46
|
+
clumsy.found_treasure(hammer)
|
47
|
+
crowbar = StudioGame::Treasure.new(:crowbar, 400)
|
48
|
+
clumsy.found_treasure(crowbar)
|
49
|
+
clumsy.each_found_treasure do |treasure|
|
50
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
51
|
+
end
|
52
|
+
puts "#{clumsy.points} grand total points"
|
data/data/players.csv
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
attr_reader :woot_count
|
6
|
+
|
7
|
+
def initialize(name, health)
|
8
|
+
super(name, health)
|
9
|
+
@w00t_count = 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def w00t
|
13
|
+
super
|
14
|
+
@w00t_count += 1
|
15
|
+
puts "#{@name} is berserk!" if berserk?
|
16
|
+
end
|
17
|
+
|
18
|
+
def blam
|
19
|
+
# berserk? ? w00t : super
|
20
|
+
w00t if berserk?
|
21
|
+
super unless berserk?
|
22
|
+
end
|
23
|
+
|
24
|
+
def berserk?
|
25
|
+
return true if @w00t_count > 5
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if __FILE__ == $0
|
32
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
33
|
+
6.times { berserker.w00t }
|
34
|
+
2.times { berserker.blam }
|
35
|
+
puts berserker.health
|
36
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class ClumsyPlayer < Player
|
5
|
+
attr_reader :boost_factor
|
6
|
+
|
7
|
+
def initialize(name, health=100, boost_factor=1)
|
8
|
+
super(name, health)
|
9
|
+
@boost_factor = boost_factor
|
10
|
+
end
|
11
|
+
|
12
|
+
def w00t
|
13
|
+
@boost_factor.times { super }
|
14
|
+
end
|
15
|
+
|
16
|
+
def found_treasure(treasure)
|
17
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2.0)
|
18
|
+
super(damaged_treasure)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if __FILE__ == $0
|
24
|
+
clumsy = ClumsyPlayer.new("klutz", 105, 3)
|
25
|
+
|
26
|
+
hammer = Treasure.new(:hammer, 50)
|
27
|
+
clumsy.found_treasure(hammer)
|
28
|
+
clumsy.found_treasure(hammer)
|
29
|
+
clumsy.found_treasure(hammer)
|
30
|
+
|
31
|
+
crowbar = Treasure.new(:crowbar, 400)
|
32
|
+
clumsy.found_treasure(crowbar)
|
33
|
+
|
34
|
+
clumsy.each_found_treasure do |treasure|
|
35
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
36
|
+
end
|
37
|
+
puts "#{clumsy.points} grand total points"
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'auditable'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class Die
|
5
|
+
include Auditable
|
6
|
+
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@range = (1..6)
|
11
|
+
roll
|
12
|
+
end
|
13
|
+
|
14
|
+
def roll
|
15
|
+
@number = rand(@range)
|
16
|
+
audit
|
17
|
+
return @number
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'game_turn'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
class Game
|
7
|
+
def initialize(title)
|
8
|
+
@die = Die.new
|
9
|
+
@title = title
|
10
|
+
@players = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def load(file_name)
|
14
|
+
default_players_file = File.join(File.dirname(__FILE__),
|
15
|
+
'../../data/', 'players.csv')
|
16
|
+
puts default_players_file
|
17
|
+
file = file_name || default_players_file
|
18
|
+
File.readlines(file).each do |line|
|
19
|
+
# name,score = line.split(",")
|
20
|
+
# player = Player.new(name, score.to_i)
|
21
|
+
# add_player(player)
|
22
|
+
add_player( Player.from_csv(line) )
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def save(file_name='player_rankings.csv')
|
27
|
+
File.open(file_name,'w') do |file|
|
28
|
+
file.puts "GAME: #{@title}"
|
29
|
+
file.puts "=" * "GAME: #{@title}".length
|
30
|
+
file.puts
|
31
|
+
file.puts "High Scores:"
|
32
|
+
file.puts "-" * "High Scores:".length
|
33
|
+
|
34
|
+
@players.sort.each do |player|
|
35
|
+
# formatted_name = player.name.ljust(20, '.')
|
36
|
+
# file.puts "#{formatted_name} #{player.score}"
|
37
|
+
file.puts player.format_score
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def add_player(player)
|
43
|
+
@players << player
|
44
|
+
end
|
45
|
+
|
46
|
+
def status_report
|
47
|
+
puts
|
48
|
+
puts "Player Status Report"
|
49
|
+
@players.each do |p|
|
50
|
+
puts p
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def start_report
|
55
|
+
puts "There are #{@players.length} players in #{@title}:"
|
56
|
+
status_report
|
57
|
+
end
|
58
|
+
|
59
|
+
def print_name_health(player)
|
60
|
+
puts "#{player.name} (#{player.health})"
|
61
|
+
end
|
62
|
+
|
63
|
+
def print_stats
|
64
|
+
sorted = @players.sort{ |a,b| b.score <=> a.score }
|
65
|
+
strong = @players.select{ |p| p.strong? }.sort
|
66
|
+
wimpy = @players.reject{ |p| p.strong? }.sort
|
67
|
+
|
68
|
+
puts "\n#{@title} Stats:"
|
69
|
+
puts "\n#{strong.count} strong players:"
|
70
|
+
strong.each do |player|
|
71
|
+
# puts "#{player.name} (#{player.health})"
|
72
|
+
print_name_health(player)
|
73
|
+
end
|
74
|
+
puts "\n#{wimpy.count} wimpy players:"
|
75
|
+
wimpy.each do |player|
|
76
|
+
puts "#{player.name} (#{player.health})"
|
77
|
+
end
|
78
|
+
puts "\n#{@title} high scores"
|
79
|
+
@players.sort.each do |player|
|
80
|
+
# formatted_name = player.name.ljust(20,'.')
|
81
|
+
# puts "#{formatted_name} #{player.score}"
|
82
|
+
player.format_score
|
83
|
+
end
|
84
|
+
|
85
|
+
@players.each do |player|
|
86
|
+
puts "\n#{player.name}'s point totals:"
|
87
|
+
player.each_found_treasure do |treasure|
|
88
|
+
puts "* #{treasure.points} total #{treasure.name} points"
|
89
|
+
end
|
90
|
+
puts "#{player.points} grand total points"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def play( rounds )
|
95
|
+
|
96
|
+
treasures = TreasureTrove::TREASURES
|
97
|
+
|
98
|
+
puts "There are #{@players.size} players in"
|
99
|
+
status_report
|
100
|
+
1.upto( rounds) do |round|
|
101
|
+
@players.each do |player|
|
102
|
+
GameTurn.take_turn(player)
|
103
|
+
puts player
|
104
|
+
end
|
105
|
+
end
|
106
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
107
|
+
treasures.each do |treasure|
|
108
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
109
|
+
end
|
110
|
+
status_report
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'die'
|
2
|
+
require_relative 'loaded_die'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
module GameTurn
|
7
|
+
def self.roll
|
8
|
+
Die.new
|
9
|
+
# LoadedDie.new
|
10
|
+
end
|
11
|
+
def self.take_turn(player)
|
12
|
+
case roll
|
13
|
+
when 5..6
|
14
|
+
player.w00t
|
15
|
+
when 1..2
|
16
|
+
player.blam
|
17
|
+
end
|
18
|
+
|
19
|
+
treasure = TreasureTrove.random
|
20
|
+
player.found_treasure(treasure)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative 'auditable'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class LoadedDie
|
5
|
+
include Auditable
|
6
|
+
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@range = [1,2,3,5,6,6]
|
11
|
+
roll
|
12
|
+
end
|
13
|
+
|
14
|
+
def roll
|
15
|
+
@number = @range.sample
|
16
|
+
audit
|
17
|
+
return @number
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require_relative 'playable'
|
2
|
+
require_relative 'treasure_trove'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
class Player
|
6
|
+
include Playable
|
7
|
+
# attr_reader :health
|
8
|
+
attr_accessor :health
|
9
|
+
attr_accessor :name, :snacks, :found_treasures
|
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 name=(new_name)
|
18
|
+
@name = new_name.capitalize
|
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
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def each_found_treasure
|
32
|
+
@found_treasures.each do |name, points|
|
33
|
+
yield Treasure.new(name, points)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def score
|
38
|
+
puts "Health: #{@health} + Points: #{points}"
|
39
|
+
@health + points
|
40
|
+
end
|
41
|
+
|
42
|
+
# making a class method allows it to be called when there is no object yet
|
43
|
+
def self.from_csv(line)
|
44
|
+
name,score = line.split(",")
|
45
|
+
score ||= 0
|
46
|
+
# player = Player.new(name, score.to_i)
|
47
|
+
# below is preferred to create an exception
|
48
|
+
player = Player.new( name, Integer(score) )
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_csv
|
53
|
+
"#{@name},#{@score}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def format_score
|
57
|
+
formatted_name = @name.ljust(20, '.')
|
58
|
+
return "#{formatted_name} #{score}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_s
|
62
|
+
return "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
|
63
|
+
end
|
64
|
+
|
65
|
+
def <=>(other)
|
66
|
+
other.score <=> score
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
if __FILE__ == $0
|
72
|
+
player = Player.new("moe")
|
73
|
+
puts player.name
|
74
|
+
puts player.health
|
75
|
+
player.w00t
|
76
|
+
puts player.health
|
77
|
+
player.blam
|
78
|
+
puts player.health
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,17 @@
|
|
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
|
+
def self.random
|
14
|
+
return TREASURES.sample
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
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
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# require '../spec_helper'
|
2
|
+
require './lib/studio_game/berserk_player'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
describe BerserkPlayer do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@initial_health = 50
|
9
|
+
@player = BerserkPlayer.new("berserker", @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "does not go berserk when w00ted up to 5 times" do
|
13
|
+
1.upto(5) { @player.w00t }
|
14
|
+
|
15
|
+
@player.berserk?.should be_false
|
16
|
+
|
17
|
+
# or if using Rspec 3.0:
|
18
|
+
# @player.berserk?.should be_falsey
|
19
|
+
end
|
20
|
+
|
21
|
+
it "goes berserk when w00ted more than 5 times" do
|
22
|
+
1.upto(6) { @player.w00t }
|
23
|
+
|
24
|
+
@player.berserk?.should be_true
|
25
|
+
|
26
|
+
# or if using Rspec 3.0:
|
27
|
+
# @player.berserk?.should be_truthy
|
28
|
+
end
|
29
|
+
|
30
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
31
|
+
1.upto(6) { @player.w00t }
|
32
|
+
1.upto(2) { @player.blam }
|
33
|
+
|
34
|
+
@player.health.should == @initial_health + (8 * 15)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require './lib/studio_game/clumsy_player'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
describe ClumsyPlayer do
|
6
|
+
before do
|
7
|
+
@player = ClumsyPlayer.new("klutz")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "only gets half the point value for each treasure" do
|
11
|
+
@player.points.should == 0
|
12
|
+
|
13
|
+
hammer = Treasure.new(:hammer, 50)
|
14
|
+
@player.found_treasure(hammer)
|
15
|
+
@player.found_treasure(hammer)
|
16
|
+
@player.found_treasure(hammer)
|
17
|
+
|
18
|
+
@player.points.should == 75
|
19
|
+
|
20
|
+
crowbar = Treasure.new(:crowbar, 400)
|
21
|
+
@player.found_treasure(crowbar)
|
22
|
+
|
23
|
+
@player.points.should == 275
|
24
|
+
|
25
|
+
yielded = []
|
26
|
+
@player.each_found_treasure do |treasure|
|
27
|
+
yielded << treasure
|
28
|
+
end
|
29
|
+
|
30
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
31
|
+
end
|
32
|
+
|
33
|
+
context "with a boost factor" do
|
34
|
+
before do
|
35
|
+
@initial_health = 100
|
36
|
+
@boost_factor = 5
|
37
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "has a boost factor" do
|
41
|
+
@player.boost_factor.should == 5
|
42
|
+
end
|
43
|
+
|
44
|
+
it "gets boost factor number of w00ts when w00ted" do
|
45
|
+
@player.w00t
|
46
|
+
|
47
|
+
@player.health.should == @initial_health + (15 * @boost_factor)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# require '../spec_helper'
|
2
|
+
require './lib/studio_game/die'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
RSpec::Matchers.define :be_between do |expected|
|
6
|
+
match do |actual|
|
7
|
+
actual >= expected[:min] && actual <= expected[:max]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Die do
|
12
|
+
before do
|
13
|
+
@die = Die.new
|
14
|
+
@range = (1..6).map{|i| i}
|
15
|
+
end
|
16
|
+
it "should roll a number between 1 & 6" do
|
17
|
+
|
18
|
+
expect( @die.roll).to be_between(min: 1, max: 6)
|
19
|
+
expect( @range.include? @die.roll ).to be_true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# require '../spec_helper'
|
2
|
+
require './lib/studio_game/die'
|
3
|
+
require './lib/studio_game/game'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe Game do
|
7
|
+
before do
|
8
|
+
@game = Game.new('Knuchleheads')
|
9
|
+
|
10
|
+
@initial_health = 100
|
11
|
+
@player = Player.new('moe',@initial_health)
|
12
|
+
@rounds = 2
|
13
|
+
@game.add_player(@player)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "w00ts player if a high number is rolled (5 or 6)" do
|
17
|
+
GameTurn.stub(:roll).and_return(5)
|
18
|
+
@game.play(@rounds)
|
19
|
+
@player.health.should == @initial_health + (15 * @rounds)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "ignores player if a medium number is rolled (3 or 4)" do
|
23
|
+
GameTurn.stub(:roll).and_return(3)
|
24
|
+
@game.play(@rounds)
|
25
|
+
@player.health.should == @initial_health
|
26
|
+
end
|
27
|
+
|
28
|
+
it "blams player if a low number is rolled (1 or 2)" do
|
29
|
+
GameTurn.stub(:roll).and_return(1)
|
30
|
+
@game.play(@rounds)
|
31
|
+
@player.health.should == @initial_health - (10 * @rounds)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "assigns a treasure for points during a player's turn" do
|
35
|
+
game = Game.new("Knuckleheads")
|
36
|
+
player = Player.new("moe")
|
37
|
+
game.add_player(player)
|
38
|
+
game.play(1)
|
39
|
+
player.points.should_not be_zero
|
40
|
+
# or use alternate expectation syntax:
|
41
|
+
# expect(player.points).not_to be_zero
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
require './lib/studio_game/player'
|
3
|
+
require './lib/studio_game/treasure_trove'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe Player do
|
7
|
+
before do
|
8
|
+
$stdout = StringIO.new
|
9
|
+
@start_health = 150
|
10
|
+
@player = Player.new("larry", @start_health)
|
11
|
+
end
|
12
|
+
it "can create a player from a CSV string" do
|
13
|
+
player = Player.from_csv("bill,20")
|
14
|
+
player.name.should == "Bill"
|
15
|
+
player.health.should == 20
|
16
|
+
end
|
17
|
+
it "has a capitalized name" do
|
18
|
+
@player.name.should == "Larry"
|
19
|
+
end
|
20
|
+
it "has an initial health" do
|
21
|
+
@player.health.should == @start_health
|
22
|
+
end
|
23
|
+
it "has a string representation" do
|
24
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
25
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
26
|
+
|
27
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
|
28
|
+
end
|
29
|
+
it "computes a score as the sum of the health and points" do
|
30
|
+
@player.score.should == @player.health + @player.points
|
31
|
+
end
|
32
|
+
it "increases health by 15 when w00ted" do
|
33
|
+
@player.w00t
|
34
|
+
@player.health.should == @start_health + 15
|
35
|
+
end
|
36
|
+
it "decreases health by 10 when blammed" do
|
37
|
+
@player.blam
|
38
|
+
@player.health.should == @start_health - 10
|
39
|
+
end
|
40
|
+
it "yields each found treasure and its total points" do
|
41
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
42
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
43
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
44
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
45
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
46
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
47
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
48
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
49
|
+
|
50
|
+
yielded = []
|
51
|
+
@player.each_found_treasure do |treasure|
|
52
|
+
yielded << treasure
|
53
|
+
end
|
54
|
+
|
55
|
+
yielded.should == [
|
56
|
+
Treasure.new(:skillet, 200),
|
57
|
+
Treasure.new(:hammer, 50),
|
58
|
+
Treasure.new(:bottle, 25)
|
59
|
+
]
|
60
|
+
end
|
61
|
+
|
62
|
+
context "player health greater than 100" do
|
63
|
+
before do
|
64
|
+
@player = Player.new("larry",150)
|
65
|
+
end
|
66
|
+
it "is strong" do
|
67
|
+
@player.should be_strong
|
68
|
+
@player.strong?.should == true
|
69
|
+
@player.strong?.should be_true
|
70
|
+
# @player.strong?.should be_truthy
|
71
|
+
expect(@player.strong?).to eq(true)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "a player with health less than or equal to 100" do
|
76
|
+
before do
|
77
|
+
@player = Player.new('larry', 100)
|
78
|
+
end
|
79
|
+
it "is weak" do
|
80
|
+
@player.should_not be_strong
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "a collection of players" do
|
85
|
+
before do
|
86
|
+
@player1 = Player.new('moe', 100)
|
87
|
+
@player2 = Player.new('larry', 200)
|
88
|
+
@player3 = Player.new('curly', 300)
|
89
|
+
|
90
|
+
@players = [@player1, @player2, @player3]
|
91
|
+
end
|
92
|
+
it "sorts by decreasing score" do
|
93
|
+
@players.sort.should == [@player3, @player2, @player1]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it "computes points as the sum of all treasure points" do
|
98
|
+
@player.points.should == 0
|
99
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
100
|
+
@player.points.should == 50
|
101
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
102
|
+
@player.points.should == 450
|
103
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
104
|
+
@player.points.should == 500
|
105
|
+
end
|
106
|
+
|
107
|
+
it "computes a score as the sum of its health and points" do
|
108
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
109
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
110
|
+
@player.score.should == 250
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# require '../spec_helper'
|
2
|
+
require './lib/studio_game/treasure_trove'
|
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
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe TreasureTrove do
|
22
|
+
|
23
|
+
it "has six treasures" do
|
24
|
+
TreasureTrove::TREASURES.size.should == 6
|
25
|
+
end
|
26
|
+
|
27
|
+
it "has a pie worth 5 points" do
|
28
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "has a bottle worth 25 points" do
|
32
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "has a hammer worth 50 points" do
|
36
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has a skillet worth 100 points" do
|
40
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "has a broomstick worth 200 points" do
|
44
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "has a crowbar worth 400 points" do
|
48
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "returns a random treasure" do
|
52
|
+
treasure = TreasureTrove.random
|
53
|
+
TreasureTrove::TREASURES.should include(treasure)
|
54
|
+
# or use alternate expectation syntax:
|
55
|
+
# expect(TreasureTrove::TREASURES).to include(treasure)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_btihen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bill Tihen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-30 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: ''
|
28
|
+
email: btihen@las.ch
|
29
|
+
executables:
|
30
|
+
- studio_game.rb
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE
|
35
|
+
- README
|
36
|
+
- bin/studio_game.rb
|
37
|
+
- data/players.csv
|
38
|
+
- lib/studio_game/auditable.rb
|
39
|
+
- lib/studio_game/berserk_player.rb
|
40
|
+
- lib/studio_game/clumsy_player.rb
|
41
|
+
- lib/studio_game/die.rb
|
42
|
+
- lib/studio_game/game.rb
|
43
|
+
- lib/studio_game/game_turn.rb
|
44
|
+
- lib/studio_game/loaded_die.rb
|
45
|
+
- lib/studio_game/playable.rb
|
46
|
+
- lib/studio_game/player.rb
|
47
|
+
- lib/studio_game/treasure_trove.rb
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
- spec/studio_game/berserk_player_spec.rb
|
50
|
+
- spec/studio_game/clumsy_player_spec.rb
|
51
|
+
- spec/studio_game/die_spec.rb
|
52
|
+
- spec/studio_game/game_spec.rb
|
53
|
+
- spec/studio_game/player_spec.rb
|
54
|
+
- spec/studio_game/treasure_trove_spec.rb
|
55
|
+
homepage: http://www.las.ch
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.9'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.4.5.1
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: INSERT SUMMARY HERE
|
79
|
+
test_files:
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/studio_game/berserk_player_spec.rb
|
82
|
+
- spec/studio_game/clumsy_player_spec.rb
|
83
|
+
- spec/studio_game/die_spec.rb
|
84
|
+
- spec/studio_game/game_spec.rb
|
85
|
+
- spec/studio_game/player_spec.rb
|
86
|
+
- spec/studio_game/treasure_trove_spec.rb
|