ND_Studio_Game_Coursework 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 +21 -0
- data/README +6 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +31 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +28 -0
- data/lib/studio_game/clumsy_player.rb +9 -0
- data/lib/studio_game/die.rb +19 -0
- data/lib/studio_game/game.rb +85 -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 +18 -0
- data/lib/studio_game/player.rb +47 -0
- data/lib/studio_game/treasure_trove.rb +17 -0
- data/spec/spec_helper.rb +100 -0
- data/spec/studio_game/berserk_player_spec.rb +32 -0
- data/spec/studio_game/clumsy_player_spec.rb +35 -0
- data/spec/studio_game/game_spec.rb +51 -0
- data/spec/studio_game/player_spec.rb +124 -0
- data/spec/studio_game/treasure_trove_spec.rb +46 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: da9c86aacf0e51da48260ce5cccd377a44c0cc3c7f6b7a3f4b0c6138d39cdf57
|
4
|
+
data.tar.gz: b4f3ebd8f5332eb275839217ce70c3fa22a01cf86302bccc98d5cc231ca2ec94
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf4304467aa50345dfcce75bf83b99e2c73242e979f15d6d38ef974d77cf56bdad03748f3c2d7b57bafa2e87b8b9d7978d7823d555d9730734bacc1bbe31b118
|
7
|
+
data.tar.gz: 2716d9e692b4d23085d13000c89db6cad266edd09b3cdc80fb83b9d7777acc499577ac2c6d622e2fec59f5a8ac605c334233f6409c05fae974534348be8e4287
|
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/players.csv
ADDED
data/bin/studio_game
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/studio_game/game'
|
4
|
+
require_relative '../lib/studio_game/berserk_player'
|
5
|
+
require_relative '../lib/studio_game/clumsy_player'
|
6
|
+
|
7
|
+
knuckleheads = StudioGame::Game.new('Knuckleheads')
|
8
|
+
default_player_file = File.join(File.dirname(__FILE__ ), 'players.csv')
|
9
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
10
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
|
11
|
+
knuckleheads.add_player(klutz)
|
12
|
+
|
13
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
14
|
+
knuckleheads.add_player(berserker)
|
15
|
+
loop do
|
16
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
17
|
+
answer = gets.chomp.downcase
|
18
|
+
case answer
|
19
|
+
when /^\d+$/
|
20
|
+
knuckleheads.play(Integer(answer))
|
21
|
+
when 'quit', 'exit'
|
22
|
+
knuckleheads.print_stats
|
23
|
+
break
|
24
|
+
else puts "Please enter a number or 'quit'"
|
25
|
+
end
|
26
|
+
knuckleheads.save_high_scores
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
knuckleheads.print_stats
|
31
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
module StudioGame
|
3
|
+
class BerserkPlayer < Player
|
4
|
+
def initialize(name, health = 100)
|
5
|
+
super(name, health)
|
6
|
+
@w00t_count = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def berserk?
|
10
|
+
@w00t_count > 5
|
11
|
+
end
|
12
|
+
|
13
|
+
def w00t
|
14
|
+
super
|
15
|
+
@w00t_count += 1
|
16
|
+
puts "#{@name} is Berserk!" if berserk?
|
17
|
+
end
|
18
|
+
|
19
|
+
def blam
|
20
|
+
if berserk?
|
21
|
+
w00t
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'game_turn'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
module StudioGame
|
6
|
+
class Game
|
7
|
+
attr_reader :title
|
8
|
+
def initialize(title)
|
9
|
+
@title = title
|
10
|
+
@players = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_player(player)
|
14
|
+
@players << player
|
15
|
+
end
|
16
|
+
|
17
|
+
def total_points
|
18
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
19
|
+
end
|
20
|
+
|
21
|
+
def load_players(filename)
|
22
|
+
File.readlines(filename).each do |line|
|
23
|
+
add_player(Player.from_csv(line))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def save_high_scores(to_file='.high_scores.txt')
|
28
|
+
File.open(to_file, "w") do |file|
|
29
|
+
file.puts "#{@title} High Scores"
|
30
|
+
@players.sort.each do |p|
|
31
|
+
file.puts high_score_entry(p)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def high_score_entry(player)
|
37
|
+
"#{player.name.ljust(30,'.')} #{player.score}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def print_stats
|
41
|
+
strong_players, wimpy_players = @players.partition { |p| p.strong?}
|
42
|
+
puts "\n#{@title} Statistics"
|
43
|
+
puts "\n #{strong_players.count} Strong Players"
|
44
|
+
strong_players.each do |p|
|
45
|
+
puts p
|
46
|
+
end
|
47
|
+
puts "\n #{wimpy_players.count} Wimpy Players"
|
48
|
+
wimpy_players.each do |p|
|
49
|
+
puts p
|
50
|
+
end
|
51
|
+
@players.each do |player|
|
52
|
+
puts "\n#{player.name}'s point totals:"
|
53
|
+
player.each_found_treasure do |treasure|
|
54
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
55
|
+
end
|
56
|
+
puts "#{player.points} grand total points"
|
57
|
+
end
|
58
|
+
puts "#{@title} High Scores"
|
59
|
+
@players.sort.each do |p|
|
60
|
+
puts high_score_entry(p)
|
61
|
+
end
|
62
|
+
puts "#{total_points} total points from treasures found"
|
63
|
+
end
|
64
|
+
|
65
|
+
def play(rounds)
|
66
|
+
@players.each do |p|
|
67
|
+
puts p
|
68
|
+
end
|
69
|
+
treasures = TreasureTrove::TREASURES
|
70
|
+
puts "\nThere are #{treasures.size} treasures to be found."
|
71
|
+
treasures.each do |t|
|
72
|
+
puts "#{t.name}: #{t.points}pts"
|
73
|
+
end
|
74
|
+
1.upto(rounds) do |r|
|
75
|
+
if block_given?
|
76
|
+
break if yield
|
77
|
+
end
|
78
|
+
puts "\nRound #{r}"
|
79
|
+
@players.each do |p|
|
80
|
+
GameTurn.take_turn(p)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module StudioGame
|
2
|
+
module GameTurn
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'die'
|
5
|
+
require_relative 'loaded_die'
|
6
|
+
|
7
|
+
def self.take_turn(player)
|
8
|
+
die = Die.new
|
9
|
+
number_rolled = die.roll
|
10
|
+
case number_rolled
|
11
|
+
when 5..6
|
12
|
+
player.w00t
|
13
|
+
when 3..4
|
14
|
+
puts "#{player.name} got skipped"
|
15
|
+
else
|
16
|
+
player.blam
|
17
|
+
end
|
18
|
+
t_found = TreasureTrove.random
|
19
|
+
player.found_treasure(t_found)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative 'treasure_trove'
|
2
|
+
require_relative 'playable'
|
3
|
+
module StudioGame
|
4
|
+
class Player
|
5
|
+
include Playable
|
6
|
+
attr_accessor :name, :health
|
7
|
+
def initialize(name, health = 100)
|
8
|
+
@name = name.capitalize
|
9
|
+
@health = health
|
10
|
+
@found_treasures = Hash.new(0)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
"I'm #{@name}. health = #{@health}, points = #{points}, score = #{score}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def <=>(other)
|
18
|
+
other.score <=> score
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.from_csv(string)
|
22
|
+
name, health = string.split(',')
|
23
|
+
Player.new(name, Integer(health))
|
24
|
+
end
|
25
|
+
|
26
|
+
def found_treasure(treasure)
|
27
|
+
@found_treasures[treasure.name] += treasure.points
|
28
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points}pts!"
|
29
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def each_found_treasure
|
33
|
+
@found_treasures.each do |name, points|
|
34
|
+
yield Treasure.new(name, points)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def points
|
39
|
+
@found_treasures.values.reduce(0, :+)
|
40
|
+
end
|
41
|
+
|
42
|
+
def score
|
43
|
+
health + points
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
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
|
+
TREASURES.sample
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -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,32 @@
|
|
1
|
+
require 'studio_game/berserk_player'
|
2
|
+
module StudioGame
|
3
|
+
describe BerserkPlayer do
|
4
|
+
before(:each) do
|
5
|
+
$stdout = StringIO.new
|
6
|
+
end
|
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
|
+
expect(@player.berserk?).to be(false)
|
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(true)
|
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
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'studio_game/clumsy_player'
|
2
|
+
module StudioGame
|
3
|
+
describe ClumsyPlayer do
|
4
|
+
before do
|
5
|
+
@player = ClumsyPlayer.new("klutz")
|
6
|
+
end
|
7
|
+
before(:each) do
|
8
|
+
$stdout = StringIO.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it "only gets half the point value for each treasure" do
|
12
|
+
expect(@player.points).to eq(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
|
+
expect(@player.points).to eq(75)
|
20
|
+
|
21
|
+
crowbar = Treasure.new(:crowbar, 400)
|
22
|
+
@player.found_treasure(crowbar)
|
23
|
+
|
24
|
+
expect(@player.points).to eq(275)
|
25
|
+
|
26
|
+
yielded = []
|
27
|
+
@player.each_found_treasure do |treasure|
|
28
|
+
yielded << treasure
|
29
|
+
end
|
30
|
+
|
31
|
+
expect(yielded).to eq([Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)])
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
module StudioGame
|
3
|
+
describe Game do
|
4
|
+
before do
|
5
|
+
$stdout = StringIO.new
|
6
|
+
@game = Game.new("Knuckleheads")
|
7
|
+
|
8
|
+
@initial_health = 100
|
9
|
+
@player = Player.new("moe", @initial_health)
|
10
|
+
|
11
|
+
@game.add_player(@player)
|
12
|
+
end
|
13
|
+
it "A die roll of a high number w00ts the player" do
|
14
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
15
|
+
@game.play(2)
|
16
|
+
expect(@player.health).to eq(@initial_health + 15*2)
|
17
|
+
end
|
18
|
+
it "A die roll of a middle number skips the player" do
|
19
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(3)
|
20
|
+
@game.play(2)
|
21
|
+
expect(@player.health).to eq(@initial_health)
|
22
|
+
end
|
23
|
+
it "A die roll of a low number blams the player" do
|
24
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(1)
|
25
|
+
@game.play(2)
|
26
|
+
expect(@player.health).to eq(@initial_health - 10*2)
|
27
|
+
end
|
28
|
+
it "assigns a treasure for points during a player's turn" do
|
29
|
+
game = Game.new("Knuckleheads")
|
30
|
+
player = Player.new("moe")
|
31
|
+
game.add_player(player)
|
32
|
+
game.play(1)
|
33
|
+
expect(player.points).not_to be_zero
|
34
|
+
end
|
35
|
+
it "computes total points as the sum of all player points" do
|
36
|
+
game = Game.new("Knuckleheads")
|
37
|
+
|
38
|
+
player1 = Player.new("moe")
|
39
|
+
player2 = Player.new("larry")
|
40
|
+
|
41
|
+
game.add_player(player1)
|
42
|
+
game.add_player(player2)
|
43
|
+
|
44
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
45
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
46
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
47
|
+
|
48
|
+
expect(game.total_points).to eq(500)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'studio_game/player'
|
3
|
+
require 'studio_game/treasure_trove'
|
4
|
+
module StudioGame
|
5
|
+
describe Player do
|
6
|
+
before(:each) do
|
7
|
+
$stdout = StringIO.new
|
8
|
+
@initial_health = 150
|
9
|
+
@player = Player.new('larry', @initial_health)
|
10
|
+
end
|
11
|
+
it 'has a capitalized name' do
|
12
|
+
expect(@player.name).to eq('Larry')
|
13
|
+
end
|
14
|
+
it 'has an initial health' do
|
15
|
+
expect(@player.health).to eq(150)
|
16
|
+
end
|
17
|
+
it 'has a string representation' do
|
18
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
19
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
20
|
+
expect(@player.to_s).to eq("I'm Larry. health = 150, points = 100, score = 250")
|
21
|
+
end
|
22
|
+
it 'computes a score as the sum of its health and points' do
|
23
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
24
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
25
|
+
expect(@player.score).to eq(250)
|
26
|
+
end
|
27
|
+
it 'increases health by 15 when w00ted' do
|
28
|
+
@player.w00t
|
29
|
+
expect(@player.health).to eq(@initial_health + 15)
|
30
|
+
end
|
31
|
+
it 'decreases health by 10 when blammed' do
|
32
|
+
@player.blam
|
33
|
+
expect(@player.health).to eq(@initial_health - 10)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "computes points as the sum of all treasure points" do
|
37
|
+
expect(@player.points).to eq(0)
|
38
|
+
|
39
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
40
|
+
|
41
|
+
expect(@player.points).to eq(50)
|
42
|
+
|
43
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
44
|
+
|
45
|
+
expect(@player.points).to eq(450)
|
46
|
+
|
47
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
48
|
+
|
49
|
+
expect(@player.points).to eq(500)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'can be created from a CSV string' do
|
53
|
+
player = Player.from_csv('larry,150')
|
54
|
+
|
55
|
+
expect(player.name).to eq('Larry')
|
56
|
+
expect(player.health).to eq(150)
|
57
|
+
end
|
58
|
+
|
59
|
+
let(:expected_treasure) do
|
60
|
+
[
|
61
|
+
Treasure.new(:skillet, 200),
|
62
|
+
Treasure.new(:hammer, 50),
|
63
|
+
Treasure.new(:bottle, 25)
|
64
|
+
]
|
65
|
+
end
|
66
|
+
|
67
|
+
it "yields each found treasure and its total points" do
|
68
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
69
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
70
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
71
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
72
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
73
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
74
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
75
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
76
|
+
|
77
|
+
yielded = []
|
78
|
+
@player.each_found_treasure do |treasure|
|
79
|
+
yielded << treasure
|
80
|
+
end
|
81
|
+
|
82
|
+
expect(yielded).to eq(expected_treasure)
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'created with a default health' do
|
86
|
+
before do
|
87
|
+
@player = Player.new('larry')
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'has a health of 100' do
|
91
|
+
expect(@player.health).to eq(100)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
context 'A player with health of more than 100' do
|
95
|
+
before(:each) do
|
96
|
+
@player = Player.new('larry', 150)
|
97
|
+
end
|
98
|
+
it 'A player with health 150 is strong' do
|
99
|
+
expect(@player.strong?).to be true
|
100
|
+
end
|
101
|
+
end
|
102
|
+
context 'A player with health less than or equal to 100' do
|
103
|
+
before(:each) do
|
104
|
+
@player = Player.new('larry', 100)
|
105
|
+
end
|
106
|
+
it 'A player with health of 100 is not strong' do
|
107
|
+
expect(@player.strong?).to be false
|
108
|
+
end
|
109
|
+
end
|
110
|
+
context "in a collection of players" do
|
111
|
+
before do
|
112
|
+
@player1 = Player.new("moe", 100)
|
113
|
+
@player2 = Player.new("larry", 200)
|
114
|
+
@player3 = Player.new("curly", 300)
|
115
|
+
|
116
|
+
@players = [@player1, @player2, @player3]
|
117
|
+
end
|
118
|
+
|
119
|
+
it "is sorted by decreasing score" do
|
120
|
+
expect(@players.sort).to eq([@player3, @player2, @player1])
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'studio_game/treasure_trove'
|
2
|
+
module StudioGame
|
3
|
+
describe Treasure do
|
4
|
+
before do
|
5
|
+
$stdout = StringIO.new
|
6
|
+
@treasure = Treasure.new(:hammer, 50)
|
7
|
+
end
|
8
|
+
it "has a name attribute" do
|
9
|
+
expect(@treasure.name).to eq(:hammer)
|
10
|
+
end
|
11
|
+
it "has a points attribute" do
|
12
|
+
expect(@treasure.points).to eq(50)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
describe TreasureTrove do
|
16
|
+
before(:each) do
|
17
|
+
$stdout = StringIO.new
|
18
|
+
end
|
19
|
+
it "has six treasures" do
|
20
|
+
expect(TreasureTrove::TREASURES.size).to eq(6)
|
21
|
+
end
|
22
|
+
it "has a pie worth 5 points" do
|
23
|
+
expect(TreasureTrove::TREASURES[0]).to eq(Treasure.new(:pie, 5))
|
24
|
+
end
|
25
|
+
it "has a bottle worth 25 points" do
|
26
|
+
expect(TreasureTrove::TREASURES[1]).to eq(Treasure.new(:bottle, 25))
|
27
|
+
end
|
28
|
+
it "has a hammer worth 50 points" do
|
29
|
+
expect(TreasureTrove::TREASURES[2]).to eq(Treasure.new(:hammer, 50))
|
30
|
+
end
|
31
|
+
it "has a skillet worth 100 points" do
|
32
|
+
expect(TreasureTrove::TREASURES[3]).to eq(Treasure.new(:skillet, 100))
|
33
|
+
end
|
34
|
+
it "has a broomstick worth 200 points" do
|
35
|
+
expect(TreasureTrove::TREASURES[4]).to eq(Treasure.new(:broomstick, 200))
|
36
|
+
end
|
37
|
+
it "has a crowbar worth 400 points" do
|
38
|
+
expect(TreasureTrove::TREASURES[5]).to eq(Treasure.new(:crowbar, 400))
|
39
|
+
end
|
40
|
+
it "returns a random treasure" do
|
41
|
+
treasure = TreasureTrove.random
|
42
|
+
expect(TreasureTrove::TREASURES).to include(treasure)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ND_Studio_Game_Coursework
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nik Doran
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-23 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: |-
|
34
|
+
This is an application made for The Pragmatic Studio's
|
35
|
+
Ruby Programming course, as described at
|
36
|
+
|
37
|
+
http://pragmaticstudio.com
|
38
|
+
|
39
|
+
This code is Copyright 2012 The Pragmatic Studio. See the LICENSE file.
|
40
|
+
email: ndoran@illustrativemathematics.org
|
41
|
+
executables:
|
42
|
+
- studio_game
|
43
|
+
extensions: []
|
44
|
+
extra_rdoc_files: []
|
45
|
+
files:
|
46
|
+
- LICENSE
|
47
|
+
- README
|
48
|
+
- bin/players.csv
|
49
|
+
- bin/studio_game
|
50
|
+
- lib/studio_game/auditable.rb
|
51
|
+
- lib/studio_game/berserk_player.rb
|
52
|
+
- lib/studio_game/clumsy_player.rb
|
53
|
+
- lib/studio_game/die.rb
|
54
|
+
- lib/studio_game/game.rb
|
55
|
+
- lib/studio_game/game_turn.rb
|
56
|
+
- lib/studio_game/loaded_die.rb
|
57
|
+
- lib/studio_game/playable.rb
|
58
|
+
- lib/studio_game/player.rb
|
59
|
+
- lib/studio_game/treasure_trove.rb
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
- spec/studio_game/berserk_player_spec.rb
|
62
|
+
- spec/studio_game/clumsy_player_spec.rb
|
63
|
+
- spec/studio_game/game_spec.rb
|
64
|
+
- spec/studio_game/player_spec.rb
|
65
|
+
- spec/studio_game/treasure_trove_spec.rb
|
66
|
+
homepage: http://illustrativemathematics.org
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '1.9'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.0.3
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Coursework for Prag Studio Ruby Course
|
89
|
+
test_files:
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
- spec/studio_game/clumsy_player_spec.rb
|
92
|
+
- spec/studio_game/treasure_trove_spec.rb
|
93
|
+
- spec/studio_game/berserk_player_spec.rb
|
94
|
+
- spec/studio_game/player_spec.rb
|
95
|
+
- spec/studio_game/game_spec.rb
|