cb_studio_game 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 +10 -0
- data/README +24 -0
- data/bin/cb_studio_game +40 -0
- data/bin/players.csv +3 -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 +28 -0
- data/lib/studio_game/die.rb +19 -0
- data/lib/studio_game/game.rb +103 -0
- data/lib/studio_game/game_turn.rb +24 -0
- data/lib/studio_game/loaded_die.rb +18 -0
- data/lib/studio_game/playable.rb +16 -0
- data/lib/studio_game/player.rb +64 -0
- data/lib/studio_game/treasure_trove.rb +18 -0
- data/spec/studio_game/berserk_player_spec.rb +30 -0
- data/spec/studio_game/clumsy_player_spec.rb +32 -0
- data/spec/studio_game/game_spec.rb +64 -0
- data/spec/studio_game/player_spec.rb +127 -0
- data/spec/studio_game/spec_helper.rb +8 -0
- data/spec/studio_game/treasure_trove_spec.rb +52 -0
- metadata +108 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 6de72b0c499c999638d74293cefd09e3314b757e
|
|
4
|
+
data.tar.gz: 03debe5455854c7b3dbe402ad2aa0c51b092cc0e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: da978467c795e737c2edf7e08a128bc9d378b5f5dbd814eb634553a3dd4617ceaa80bf6799dc3edc046e99955c4f9bc4a228a93d930b51c88848d6ecac010fb9
|
|
7
|
+
data.tar.gz: 340246ef770ecdf405363e79dfdf22d6a8214fb46a7444f5eb3c3036bc66762af590fcf3173bdbcf27f04c52b727a80e0db7a80702b08c1b21fa680e436247c2
|
data/LICENSE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2015 Curtis Browning
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
cb_studio_game v1.0.0
|
|
2
|
+
|
|
3
|
+
This gem contains the the program 'cb_studio_game' that was written as part of the Pragmatic Studio
|
|
4
|
+
Ruby Programming course.
|
|
5
|
+
|
|
6
|
+
Start the game by entering 'cb_studio_game' at the command prompt.
|
|
7
|
+
|
|
8
|
+
The game requires the user to input the number of rounds to be played. The game's players are
|
|
9
|
+
are housed within the 'players.csv' file which may be modified as desired to add/remove players and their initial health.
|
|
10
|
+
|
|
11
|
+
Players can also be added within the 'cb_studio_game' file by removing the '#' from the appropriate lines and changing the player names and health values as needed.
|
|
12
|
+
|
|
13
|
+
When the desired number of rounds have been played, the game's stats can be viewed by entering 'quit'.
|
|
14
|
+
|
|
15
|
+
The name of the game can also be changed within the 'cb_studio_game' file.
|
|
16
|
+
|
|
17
|
+
High scores obtained during the game are saved and can be found in the 'high_scores.txt' file.
|
|
18
|
+
|
|
19
|
+
This gem is hosted at the the following URL: 'https://rubygems.org/gems/cb_studio_game'
|
|
20
|
+
|
|
21
|
+
This gem may be used as outlined in the included 'LICENSE' file.
|
|
22
|
+
|
|
23
|
+
A special thank you to the folks at Pramatic Studio for providing their Ruby Programming course
|
|
24
|
+
without which this gem could not have been written and published.
|
data/bin/cb_studio_game
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
require_relative '../lib/studio_game/clumsy_player'
|
|
3
|
+
require_relative '../lib/studio_game/berserk_player'
|
|
4
|
+
require_relative '../lib/studio_game/player'
|
|
5
|
+
require_relative '../lib/studio_game/game'
|
|
6
|
+
|
|
7
|
+
player1 = CbStudioGame::Player.new("moe")
|
|
8
|
+
player2 = CbStudioGame::Player.new("larry", 60)
|
|
9
|
+
player3 = CbStudioGame::Player.new("curly", 125)
|
|
10
|
+
player4 = CbStudioGame::Player.new("shemp", 90)
|
|
11
|
+
|
|
12
|
+
knuckleheads = CbStudioGame::Game.new("knuckleheads")
|
|
13
|
+
# knuckleheads.add_player(player1)
|
|
14
|
+
# knuckleheads.add_player(player2)
|
|
15
|
+
# knuckleheads.add_player(player3)
|
|
16
|
+
|
|
17
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
|
18
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
|
19
|
+
|
|
20
|
+
klutz = CbStudioGame::ClumsyPlayer.new("klutz", 105)
|
|
21
|
+
knuckleheads.add_player(klutz)
|
|
22
|
+
|
|
23
|
+
berserker = CbStudioGame::BerserkPlayer.new("berserker", 50)
|
|
24
|
+
knuckleheads.add_player(berserker)
|
|
25
|
+
|
|
26
|
+
loop do
|
|
27
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
|
28
|
+
answer = gets.chomp.downcase
|
|
29
|
+
case answer
|
|
30
|
+
when /^\d+$/
|
|
31
|
+
knuckleheads.play(answer.to_i)
|
|
32
|
+
when 'quit', 'exit'
|
|
33
|
+
knuckleheads.print_stats
|
|
34
|
+
break
|
|
35
|
+
else
|
|
36
|
+
puts "Please enter a number or 'quit'"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
knuckleheads.save_high_scores
|
data/bin/players.csv
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
|
|
3
|
+
module CbStudioGame
|
|
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,28 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
|
|
3
|
+
module CbStudioGame
|
|
4
|
+
class ClumsyPlayer < Player
|
|
5
|
+
|
|
6
|
+
def found_treasure(treasure)
|
|
7
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2)
|
|
8
|
+
super(damaged_treasure)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
if __FILE__ == $0
|
|
13
|
+
clumsy = ClumsyPlayer.new("klutz")
|
|
14
|
+
|
|
15
|
+
hammer = Treasure.new(:hammer, 50)
|
|
16
|
+
clumsy.found_treasure(hammer)
|
|
17
|
+
clumsy.found_treasure(hammer)
|
|
18
|
+
clumsy.found_treasure(hammer)
|
|
19
|
+
|
|
20
|
+
crowbar = Treasure.new(:crowbar, 400)
|
|
21
|
+
clumsy.found_treasure(crowbar)
|
|
22
|
+
|
|
23
|
+
clumsy.each_found_treasure do |treasure|
|
|
24
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
25
|
+
end
|
|
26
|
+
puts "#{clumsy.points} grand total points"
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
require_relative 'die'
|
|
3
|
+
require_relative 'game_turn'
|
|
4
|
+
require_relative 'treasure_trove'
|
|
5
|
+
|
|
6
|
+
module CbStudioGame
|
|
7
|
+
class Game
|
|
8
|
+
|
|
9
|
+
attr_reader :title
|
|
10
|
+
|
|
11
|
+
def initialize(title)
|
|
12
|
+
@title = title.capitalize
|
|
13
|
+
@players = []
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def load_players(from_file)
|
|
17
|
+
File.readlines(from_file).each do |line|
|
|
18
|
+
add_player(Player.from_csv(line))
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def save_high_scores(to_file="high_scores.txt")
|
|
23
|
+
File.open(to_file, "w") do |file|
|
|
24
|
+
file.puts "#{@title} High Scores:"
|
|
25
|
+
@players.sort.each do |player|
|
|
26
|
+
file.puts high_score_entry(player)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def high_score_entry(player)
|
|
32
|
+
formatted_name = player.name.ljust(20, '.')
|
|
33
|
+
"#{formatted_name} #{player.score}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def add_player(a_player)
|
|
37
|
+
@players << a_player
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def play(rounds)
|
|
41
|
+
puts "There are #{@players.size} in #{@title}:"
|
|
42
|
+
@players.each do |player|
|
|
43
|
+
puts player
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
treasures = TreasureTrove::TREASURES
|
|
47
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
|
48
|
+
treasures.each do |treasure|
|
|
49
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
1.upto(rounds) do |round|
|
|
53
|
+
if block_given?
|
|
54
|
+
break if yield
|
|
55
|
+
end
|
|
56
|
+
puts "\nRound #{round}:"
|
|
57
|
+
@players.each do |player|
|
|
58
|
+
GameTurn.take_turn(player)
|
|
59
|
+
puts player
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def print_name_and_health(player)
|
|
65
|
+
puts "#{player.name} (#{player.health})"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def total_points
|
|
69
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def print_stats
|
|
73
|
+
puts "\n#{@title} Statistics:"
|
|
74
|
+
|
|
75
|
+
puts "#{total_points} total points from treasures found"
|
|
76
|
+
|
|
77
|
+
@players.each do |player|
|
|
78
|
+
puts "\n#{player.name}'s point totals:"
|
|
79
|
+
player.each_found_treasure do |treasure|
|
|
80
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
81
|
+
end
|
|
82
|
+
puts "#{player.points} grand total points"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
strong_players, wimpy_players = @players.partition { |player| player.strong? }
|
|
86
|
+
|
|
87
|
+
puts "\n#{strong_players.size} Strong Players:"
|
|
88
|
+
strong_players.each do |player|
|
|
89
|
+
print_name_and_health(player)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
puts "\n#{wimpy_players.size} Wimpy Players:"
|
|
93
|
+
wimpy_players.each do |player|
|
|
94
|
+
print_name_and_health(player)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
puts "\n#{@title} High Scores:"
|
|
98
|
+
@players.sort.each do |player|
|
|
99
|
+
puts high_score_entry(player)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require_relative 'die'
|
|
2
|
+
require_relative 'player'
|
|
3
|
+
require_relative 'treasure_trove'
|
|
4
|
+
require_relative 'loaded_die'
|
|
5
|
+
|
|
6
|
+
module CbStudioGame
|
|
7
|
+
module GameTurn
|
|
8
|
+
def self.take_turn(player) # requires 'self'
|
|
9
|
+
die = Die.new
|
|
10
|
+
# die = LoadedDie.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
|
+
|
|
20
|
+
treasure = TreasureTrove.random
|
|
21
|
+
player.found_treasure(treasure)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require_relative 'auditable'
|
|
2
|
+
|
|
3
|
+
module CbStudioGame
|
|
4
|
+
module CbStudioGame
|
|
5
|
+
class LoadedDie
|
|
6
|
+
include Auditable
|
|
7
|
+
|
|
8
|
+
attr_reader :number
|
|
9
|
+
|
|
10
|
+
def roll
|
|
11
|
+
numbers = [1, 1, 2, 5, 6, 6]
|
|
12
|
+
@number = numbers.sample
|
|
13
|
+
audit
|
|
14
|
+
@number
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require_relative 'treasure_trove'
|
|
2
|
+
require_relative 'playable'
|
|
3
|
+
|
|
4
|
+
module CbStudioGame
|
|
5
|
+
class Player
|
|
6
|
+
include Playable
|
|
7
|
+
|
|
8
|
+
attr_accessor :health
|
|
9
|
+
attr_accessor :name
|
|
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 self.from_csv(string)
|
|
18
|
+
name, health = string.split(',')
|
|
19
|
+
Player.new(name, Integer(health))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def name=(new_name)
|
|
23
|
+
@name = new_name.capitalize
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_s
|
|
27
|
+
"I'm #{@name} with a health = #{@health}, points = #{points}, and score = #{score}."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def found_treasure(treasure)
|
|
31
|
+
@found_treasures[treasure.name] += treasure.points
|
|
32
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
|
33
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def each_found_treasure
|
|
37
|
+
@found_treasures.each do |name, points|
|
|
38
|
+
yield Treasure.new(name, points)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def points
|
|
43
|
+
@found_treasures.values.reduce(0, :+)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def score
|
|
47
|
+
@health + points
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def <=>(other)
|
|
51
|
+
other.score <=> score
|
|
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,18 @@
|
|
|
1
|
+
module CbStudioGame
|
|
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,30 @@
|
|
|
1
|
+
require 'studio_game/berserk_player'
|
|
2
|
+
|
|
3
|
+
module CbStudioGame
|
|
4
|
+
describe BerserkPlayer do
|
|
5
|
+
|
|
6
|
+
before do
|
|
7
|
+
@initial_health = 50
|
|
8
|
+
@player = BerserkPlayer.new("berserker", @initial_health)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "does not go berserk when w00ted up to 5 times" do
|
|
12
|
+
1.upto(5) { @player.w00t }
|
|
13
|
+
|
|
14
|
+
@player.berserk?.should be_falsey
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "goes berserk when w00ted more than 5 times" do
|
|
18
|
+
1.upto(6) { @player.w00t }
|
|
19
|
+
|
|
20
|
+
@player.berserk?.should be_truthy
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
|
24
|
+
1.upto(6) { @player.w00t }
|
|
25
|
+
1.upto(2) { @player.blam }
|
|
26
|
+
|
|
27
|
+
expect(@player.health).to eq(@initial_health + (8 * 15))
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'studio_game/clumsy_player'
|
|
2
|
+
|
|
3
|
+
module CbStudioGame
|
|
4
|
+
describe ClumsyPlayer do
|
|
5
|
+
before do
|
|
6
|
+
@player = ClumsyPlayer.new("klutz")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "only gets half the point value for each treasure" do
|
|
10
|
+
expect(@player.points).to eq(0)
|
|
11
|
+
|
|
12
|
+
hammer = Treasure.new(:hammer, 50)
|
|
13
|
+
@player.found_treasure(hammer)
|
|
14
|
+
@player.found_treasure(hammer)
|
|
15
|
+
@player.found_treasure(hammer)
|
|
16
|
+
|
|
17
|
+
expect(@player.points).to eq(75)
|
|
18
|
+
|
|
19
|
+
crowbar = Treasure.new(:crowbar, 400)
|
|
20
|
+
@player.found_treasure(crowbar)
|
|
21
|
+
|
|
22
|
+
expect(@player.points).to eq(275)
|
|
23
|
+
|
|
24
|
+
yielded = []
|
|
25
|
+
@player.each_found_treasure do |treasure|
|
|
26
|
+
yielded << treasure
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
expect(yielded).to eq([Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)])
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'studio_game/game'
|
|
2
|
+
|
|
3
|
+
module CbStudioGame
|
|
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
|
+
@game.add_player(@player)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "w00ts the player if a high number is rolled" do
|
|
14
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
|
15
|
+
|
|
16
|
+
@game.play(2)
|
|
17
|
+
|
|
18
|
+
expect(@player.health).to eq(@initial_health + (15 * 2))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "skips the player if a medium number is rolled" do
|
|
22
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(3)
|
|
23
|
+
|
|
24
|
+
@game.play(2)
|
|
25
|
+
|
|
26
|
+
expect(@player.health).to eq(@initial_health)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "blams the player is a low number is rolled" do
|
|
30
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(1)
|
|
31
|
+
|
|
32
|
+
@game.play(2)
|
|
33
|
+
|
|
34
|
+
expect(@player.health).to eq(@initial_health - (10 * 2))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "assigns a treasure for points during a player's turn" do
|
|
38
|
+
game = Game.new("Knuckleheads")
|
|
39
|
+
player = Player.new("moe")
|
|
40
|
+
|
|
41
|
+
game.add_player(player)
|
|
42
|
+
|
|
43
|
+
game.play(1)
|
|
44
|
+
|
|
45
|
+
expect(player.points).not_to be_zero
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "computes total points as the sum of all player points" do
|
|
49
|
+
game = Game.new("Knuckleheads")
|
|
50
|
+
|
|
51
|
+
player1 = Player.new("moe")
|
|
52
|
+
player2 = Player.new("larry")
|
|
53
|
+
|
|
54
|
+
game.add_player(player1)
|
|
55
|
+
game.add_player(player2)
|
|
56
|
+
|
|
57
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
|
58
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
|
59
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
|
60
|
+
|
|
61
|
+
expect(game.total_points).to eq(500)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
require 'studio_game/spec_helper'
|
|
2
|
+
require 'studio_game/player'
|
|
3
|
+
require 'studio_game/treasure_trove'
|
|
4
|
+
|
|
5
|
+
module CbStudioGame
|
|
6
|
+
describe Player do
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
@initial_health = 150
|
|
10
|
+
@player = Player.new("larry", @initial_health)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "can be created from a CSV string" do
|
|
14
|
+
player = Player.from_csv("larry,150")
|
|
15
|
+
expect(player.name).to eq("Larry")
|
|
16
|
+
expect(player.health).to eq(150)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "has a capitalized name" do
|
|
20
|
+
|
|
21
|
+
expect(@player.name).to eq("Larry")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
it "has an initial health" do
|
|
26
|
+
|
|
27
|
+
expect(@player.health).to eq(150)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "has a string representation" do
|
|
31
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
32
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
33
|
+
|
|
34
|
+
expect(@player.to_s).to eq("I'm Larry with a health = 150, points = 100, and score = 250.")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "computes a score as the sum of its health and points" do
|
|
38
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
39
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
40
|
+
|
|
41
|
+
expect(@player.score).to eq(250)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "increases health by 15 when w00ted" do
|
|
45
|
+
|
|
46
|
+
@player.w00t
|
|
47
|
+
|
|
48
|
+
expect(@player.health).to eq(@initial_health + 15)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "decreases health by 15 when blammed" do
|
|
52
|
+
|
|
53
|
+
@player.blam
|
|
54
|
+
|
|
55
|
+
expect(@player.health).to eq(@initial_health - 10)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "computes points as the total of all treasure points" do
|
|
59
|
+
expect(@player.points).to eq(0)
|
|
60
|
+
|
|
61
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
62
|
+
expect(@player.points).to eq(50)
|
|
63
|
+
|
|
64
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
|
65
|
+
expect(@player.points).to eq(450)
|
|
66
|
+
|
|
67
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
68
|
+
expect(@player.points).to eq(500)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "yields each treasure and its total points" do
|
|
72
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
73
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
74
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
75
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
76
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
77
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
78
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
79
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
80
|
+
|
|
81
|
+
yielded = []
|
|
82
|
+
@player.each_found_treasure do |treasure|
|
|
83
|
+
yielded << treasure
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
yielded.should == [
|
|
87
|
+
Treasure.new(:skillet, 200),
|
|
88
|
+
Treasure.new(:hammer, 50),
|
|
89
|
+
Treasure.new(:bottle, 25)
|
|
90
|
+
]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
context "with a health greated than 100" do
|
|
94
|
+
before do
|
|
95
|
+
@player = Player.new("larry", 150)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it "is strong" do
|
|
99
|
+
expect(@player).to be_strong
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
context "with a health less than or equal to 100" do
|
|
104
|
+
before do
|
|
105
|
+
@player = Player.new("larry", 100)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
it "is wimpy" do
|
|
109
|
+
expect(@player).not_to be_strong
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
context "in collection of players" do
|
|
114
|
+
before do
|
|
115
|
+
@player1 = Player.new("moe", 100)
|
|
116
|
+
@player2 = Player.new("larry", 200)
|
|
117
|
+
@player3 = Player.new("curly", 300)
|
|
118
|
+
|
|
119
|
+
@players = [@player1, @player2, @player3]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "is sorted by decreasing score" do
|
|
123
|
+
expect(@players.sort).to eq([@player3, @player2, @player1])
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'studio_game/treasure_trove'
|
|
2
|
+
|
|
3
|
+
module CbStudioGame
|
|
4
|
+
describe Treasure do
|
|
5
|
+
before do
|
|
6
|
+
@treasure = Treasure.new(:hammer, 50)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "has a name attribute" do
|
|
10
|
+
expect(@treasure.name).to eq(:hammer)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "has a points attribute" do
|
|
14
|
+
expect(@treasure.points).to eq(50)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe TreasureTrove do
|
|
19
|
+
it "has six treasures" do
|
|
20
|
+
expect(TreasureTrove::TREASURES.size).to eq(6)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "has a pie worth 5 points" do
|
|
24
|
+
expect(TreasureTrove::TREASURES[0]).to eq(Treasure.new(:pie, 5))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "has a bottle worth 25 points" do
|
|
28
|
+
expect(TreasureTrove::TREASURES[1]).to eq(Treasure.new(:bottle, 25))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "has a hammer worth 50 points" do
|
|
32
|
+
expect(TreasureTrove::TREASURES[2]).to eq(Treasure.new(:hammer, 50))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "has a skillet worth 100 points" do
|
|
36
|
+
expect(TreasureTrove::TREASURES[3]).to eq(Treasure.new(:skillet, 100))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "has a broomstick worth 200 points" do
|
|
40
|
+
expect(TreasureTrove::TREASURES[4]).to eq(Treasure.new(:broomstick, 200))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "has a crowbar worth 400 points" do
|
|
44
|
+
expect(TreasureTrove::TREASURES[5]).to eq(Treasure.new(:crowbar, 400))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "returns a random treasure" do
|
|
48
|
+
treasure = TreasureTrove.random
|
|
49
|
+
expect(TreasureTrove::TREASURES).to include(treasure)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cb_studio_game
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Curtis Browning
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-02-01 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
|
+
cb_studio_game v1.0.0
|
|
29
|
+
|
|
30
|
+
This gem contains the the program 'cb_studio_game' that was written as part of the Pragmatic Studio
|
|
31
|
+
Ruby Programming course.
|
|
32
|
+
|
|
33
|
+
Start the game by entering 'cb_studio_game' at the command prompt.
|
|
34
|
+
|
|
35
|
+
The game requires the user to input the number of rounds to be played. The game's players are
|
|
36
|
+
are housed within the 'players.csv' file which may be modified as desired to add/remove players and their initial health.
|
|
37
|
+
|
|
38
|
+
Players can also be added within the 'cb_studio_game' file by removing the '#' from the appropriate lines and changing the player names and health values as needed.
|
|
39
|
+
|
|
40
|
+
When the desired number of rounds have been played, the game's stats can be viewed by entering 'quit'.
|
|
41
|
+
|
|
42
|
+
The name of the game can also be changed within the 'cb_studio_game' file.
|
|
43
|
+
|
|
44
|
+
High scores obtained during the game are saved and can be found in the 'high_scores.txt' file.
|
|
45
|
+
|
|
46
|
+
This gem is hosted at the the following URL: 'https://rubygems.org/gems/cb_studio_game'
|
|
47
|
+
|
|
48
|
+
This gem may be used as outlined in the included 'LICENSE' file.
|
|
49
|
+
|
|
50
|
+
A special thank you to the folks at Pramatic Studio for providing their Ruby Programming course
|
|
51
|
+
without which this gem could not have been written and published.
|
|
52
|
+
email: clb12369@yahoo.com
|
|
53
|
+
executables:
|
|
54
|
+
- cb_studio_game
|
|
55
|
+
extensions: []
|
|
56
|
+
extra_rdoc_files: []
|
|
57
|
+
files:
|
|
58
|
+
- LICENSE
|
|
59
|
+
- README
|
|
60
|
+
- bin/cb_studio_game
|
|
61
|
+
- bin/players.csv
|
|
62
|
+
- lib/studio_game/auditable.rb
|
|
63
|
+
- lib/studio_game/berserk_player.rb
|
|
64
|
+
- lib/studio_game/clumsy_player.rb
|
|
65
|
+
- lib/studio_game/die.rb
|
|
66
|
+
- lib/studio_game/game.rb
|
|
67
|
+
- lib/studio_game/game_turn.rb
|
|
68
|
+
- lib/studio_game/loaded_die.rb
|
|
69
|
+
- lib/studio_game/playable.rb
|
|
70
|
+
- lib/studio_game/player.rb
|
|
71
|
+
- lib/studio_game/treasure_trove.rb
|
|
72
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
73
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
74
|
+
- spec/studio_game/game_spec.rb
|
|
75
|
+
- spec/studio_game/player_spec.rb
|
|
76
|
+
- spec/studio_game/spec_helper.rb
|
|
77
|
+
- spec/studio_game/treasure_trove_spec.rb
|
|
78
|
+
homepage: https://rubygems.org/gems/cb_studio_game
|
|
79
|
+
licenses:
|
|
80
|
+
- MIT
|
|
81
|
+
metadata: {}
|
|
82
|
+
post_install_message:
|
|
83
|
+
rdoc_options: []
|
|
84
|
+
require_paths:
|
|
85
|
+
- lib
|
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '1.9'
|
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
requirements: []
|
|
97
|
+
rubyforge_project:
|
|
98
|
+
rubygems_version: 2.2.2
|
|
99
|
+
signing_key:
|
|
100
|
+
specification_version: 4
|
|
101
|
+
summary: Game written as part of the Pragmatic Studio Ruby Course
|
|
102
|
+
test_files:
|
|
103
|
+
- spec/studio_game/player_spec.rb
|
|
104
|
+
- spec/studio_game/treasure_trove_spec.rb
|
|
105
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
106
|
+
- spec/studio_game/spec_helper.rb
|
|
107
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
108
|
+
- spec/studio_game/game_spec.rb
|