studio_game_tr 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 +7 -0
- data/README +9 -0
- data/bin/knuckleheads.csv +3 -0
- data/bin/pool_pros.csv +4 -0
- data/bin/studio_game +50 -0
- data/lib/studio_game/auditable.rb +9 -0
- data/lib/studio_game/berserk_player.rb +34 -0
- data/lib/studio_game/clumsy_player.rb +38 -0
- data/lib/studio_game/die.rb +19 -0
- data/lib/studio_game/game.rb +92 -0
- data/lib/studio_game/game_turn.rb +29 -0
- data/lib/studio_game/loaded_die.rb +16 -0
- data/lib/studio_game/playable.rb +19 -0
- data/lib/studio_game/player.rb +61 -0
- data/lib/studio_game/treasure_trove.rb +19 -0
- data/spec/studio_game/berserk_player_spec.rb +31 -0
- data/spec/studio_game/clumsy_player_spec.rb +51 -0
- data/spec/studio_game/game_spec.rb +67 -0
- data/spec/studio_game/player_spec.rb +126 -0
- data/spec/studio_game/treasure_trove_spec.rb +57 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7785f2c6c9df6125fb88e984ef06d43eb634244e
|
4
|
+
data.tar.gz: 087397f1c001fe4ae6d864920edfdbfc4a0354c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42fa59867badbc7cf66cd81683dfdb00b51b735fde670a05308d3897470ddc7c9dc64d4488555ed6bebd9d82fe8d4c824e05211b640f46a1235219f61e6e44ac
|
7
|
+
data.tar.gz: 6b01190532d2bbe998a7b18336e829e776e900793dd402e9109a6476d56c798605336a373ca590faf29e672aa0fd43009e14dc4d3402b44e0f2baf98227a708a
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2015 Todd Ruhlen
|
2
|
+
|
3
|
+
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:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
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,9 @@
|
|
1
|
+
A simple text-based game that you can run from the command line. Runs a list of
|
2
|
+
players (either those provided, or that you can provide via a '.csv' file that
|
3
|
+
you then name as a command line argument when loading the program), through a
|
4
|
+
series of 'rounds', the number of which you can choose as the game runs. Each
|
5
|
+
player is 'w00ted', 'blammed', or 'skipped' and receives a treasure. High-scores
|
6
|
+
are printed out to the console upon exiting the game. They are also saved to a
|
7
|
+
file ('high_scores.txt') in the top-level folder of the application.
|
8
|
+
|
9
|
+
Created while doing the Ruby Programming course from Pragmatic Studio.
|
data/bin/pool_pros.csv
ADDED
data/bin/studio_game
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# start_time = Time.new
|
4
|
+
# puts "The game started on #{start_time.strftime("%A %m/%d/%Y at %I:%M%p")}"
|
5
|
+
#
|
6
|
+
# def say_hello(name, health=100)
|
7
|
+
# "I'm #{name.capitalize} with a health of #{health} as of #{time}."
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# def time
|
11
|
+
# current_time = Time.new
|
12
|
+
# current_time.strftime("%I:%M:%S")
|
13
|
+
# end
|
14
|
+
|
15
|
+
|
16
|
+
require_relative '../lib/studio_game/game'
|
17
|
+
require_relative '../lib/studio_game/player'
|
18
|
+
require_relative '../lib/studio_game/clumsy_player'
|
19
|
+
require_relative '../lib/studio_game/berserk_player'
|
20
|
+
|
21
|
+
|
22
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
23
|
+
default_player_file = File.join(File.dirname(__FILE__), 'knuckleheads.csv')
|
24
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
25
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
|
26
|
+
knuckleheads.add_player(klutz)
|
27
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
28
|
+
knuckleheads.add_player(berserker)
|
29
|
+
knuckleheads.play(3)
|
30
|
+
|
31
|
+
# pool_pros = StudioGame::Game.new("Pool Pros")
|
32
|
+
# pool_pros.load_players(ARGV.shift || "pool_pros.csv")
|
33
|
+
# pool_pros.play(1) { pool_pros.total_points >= 12_000 }
|
34
|
+
# pool_pros.print_stats
|
35
|
+
|
36
|
+
loop do
|
37
|
+
puts "How many game rounds? ('quit' to exit)"
|
38
|
+
answer = gets.chomp.downcase
|
39
|
+
case answer
|
40
|
+
when /^\d+$/
|
41
|
+
knuckleheads.play(answer.to_i)
|
42
|
+
when 'quit', 'exit'
|
43
|
+
knuckleheads.print_stats
|
44
|
+
break
|
45
|
+
else
|
46
|
+
puts "Please enter a number or 'quit'"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
knuckleheads.save_high_scores
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
|
6
|
+
def initialize(name, health=100)
|
7
|
+
super(name, health)
|
8
|
+
@w00t_count = 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def berserk?
|
12
|
+
@w00t_count > 5
|
13
|
+
end
|
14
|
+
|
15
|
+
def w00t
|
16
|
+
super
|
17
|
+
@w00t_count += 1
|
18
|
+
puts "#{name} is berserk!" if berserk?
|
19
|
+
end
|
20
|
+
|
21
|
+
def blam
|
22
|
+
berserk? ? w00t : super
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
if __FILE__ == $0
|
29
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
30
|
+
6.times { berserker.w00t }
|
31
|
+
2.times { berserker.blam }
|
32
|
+
puts berserker.health
|
33
|
+
end
|
34
|
+
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=10)
|
8
|
+
super(name, health)
|
9
|
+
@boost_factor = boost_factor
|
10
|
+
end
|
11
|
+
|
12
|
+
def found_treasure(treasure)
|
13
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2.0)
|
14
|
+
super(damaged_treasure)
|
15
|
+
end
|
16
|
+
|
17
|
+
def w00t
|
18
|
+
boost_factor.times { super }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if __FILE__ == $0
|
24
|
+
clumsy = ClumsyPlayer.new("klutz")
|
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,92 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'game_turn'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
|
6
|
+
module StudioGame
|
7
|
+
class Game
|
8
|
+
attr_reader :name
|
9
|
+
def initialize(name)
|
10
|
+
@name = name
|
11
|
+
@players = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_player(player)
|
15
|
+
@players << player
|
16
|
+
end
|
17
|
+
|
18
|
+
def load_players(from_file)
|
19
|
+
File.readlines(from_file).each do |line|
|
20
|
+
add_player(Player.from_csv(line))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def play(rounds)
|
25
|
+
|
26
|
+
puts "There are #{@players.count} players in #{name}:"
|
27
|
+
puts @players
|
28
|
+
puts "There are #{TreasureTrove::TREASURES.size} treasures to be found."
|
29
|
+
TreasureTrove::TREASURES.each { |treasure| puts "A #{treasure.name} is worth #{treasure.points} points"}
|
30
|
+
1.upto(rounds) do |round_number|
|
31
|
+
if block_given?
|
32
|
+
if yield
|
33
|
+
break
|
34
|
+
end
|
35
|
+
end
|
36
|
+
puts "\nRound #{round_number}:"
|
37
|
+
@players.each { |player|
|
38
|
+
GameTurn.take_turn(player)
|
39
|
+
puts player
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def print_name_and_health(player)
|
45
|
+
puts "#{player.name} (#{player.health})"
|
46
|
+
end
|
47
|
+
|
48
|
+
def print_stats
|
49
|
+
puts "\n#{@name} High Scores:"
|
50
|
+
@players.sort.each { |player| puts high_score_entry(player) }
|
51
|
+
strongs, wimpys = @players.partition { |player| player.strong? }
|
52
|
+
puts "\n#{@name} Statistics:"
|
53
|
+
puts "\n#{strongs.size} strong players:"
|
54
|
+
strongs.sort.each { |strong| print_name_and_health(strong) }
|
55
|
+
puts "\n#{wimpys.size} wimpy players:"
|
56
|
+
wimpys.sort.each { |wimpy| print_name_and_health(wimpy) }
|
57
|
+
@players.sort.each { |player|
|
58
|
+
puts "\n#{player.name}'s treasure point totals:"
|
59
|
+
player.each_found_treasure { |treasure| puts "#{treasure.points} total #{treasure.name} points" }
|
60
|
+
puts "#{player.points} grand total treasure points"
|
61
|
+
}
|
62
|
+
puts "\n#{total_points} total points from treasures found"
|
63
|
+
end
|
64
|
+
|
65
|
+
def total_points
|
66
|
+
@players.reduce(0) { |total, player| total += player.points }
|
67
|
+
end
|
68
|
+
|
69
|
+
def high_score_entry(player)
|
70
|
+
"#{player.name.ljust(20,".")} #{player.score}"
|
71
|
+
end
|
72
|
+
|
73
|
+
def save_high_scores
|
74
|
+
to_file = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "high_scores.txt")
|
75
|
+
File.open(to_file, "w") do |file|
|
76
|
+
file.puts "\n#{@name} High Scores:"
|
77
|
+
@players.sort.each { |player| file.puts high_score_entry(player) }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
if __FILE__ == $0
|
84
|
+
player1 = Player.new("moe")
|
85
|
+
player2 = Player.new("larry", 60)
|
86
|
+
|
87
|
+
two_knuckleheads = Game.new("Two Knuckleheads")
|
88
|
+
two_knuckleheads.add_player(player1)
|
89
|
+
two_knuckleheads.add_player(player2)
|
90
|
+
two_knuckleheads.play(3)
|
91
|
+
two_knuckleheads.print_stats
|
92
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'die'
|
2
|
+
require_relative 'player'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
require_relative 'loaded_die'
|
5
|
+
|
6
|
+
module StudioGame
|
7
|
+
module GameTurn
|
8
|
+
|
9
|
+
def self.take_turn(player)
|
10
|
+
die = Die.new
|
11
|
+
number_rolled = die.roll
|
12
|
+
case number_rolled
|
13
|
+
when 5..6
|
14
|
+
player.w00t
|
15
|
+
when 1..2
|
16
|
+
player.blam
|
17
|
+
else
|
18
|
+
puts "#{player.name} was skipped."
|
19
|
+
end
|
20
|
+
treasure = TreasureTrove.random
|
21
|
+
player.found_treasure(treasure)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
if __FILE__ == $0
|
27
|
+
player = Player.new("larry", 60)
|
28
|
+
GameTurn.take_turn(player)
|
29
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require_relative 'treasure_trove'
|
2
|
+
require_relative 'playable'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
class Player
|
6
|
+
include Playable
|
7
|
+
|
8
|
+
attr_accessor :name, :health
|
9
|
+
|
10
|
+
def initialize(name, health=100)
|
11
|
+
@name = name.capitalize
|
12
|
+
@health = health
|
13
|
+
@found_treasures = Hash.new(0)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_csv(string)
|
17
|
+
name, health = string.split(",")
|
18
|
+
Player.new(name,Integer(health))
|
19
|
+
end
|
20
|
+
|
21
|
+
def name=(new_name)
|
22
|
+
@name = new_name.capitalize
|
23
|
+
end
|
24
|
+
|
25
|
+
def <=>(other_player)
|
26
|
+
other_player.score <=> score
|
27
|
+
end
|
28
|
+
|
29
|
+
def score
|
30
|
+
@health + points
|
31
|
+
end
|
32
|
+
|
33
|
+
def found_treasure(treasure)
|
34
|
+
@found_treasures[treasure.name] += treasure.points
|
35
|
+
puts "#{name} found a #{treasure.name} worth #{treasure.points} points."
|
36
|
+
puts "#{name}'s treasure points: #{@found_treasures}'"
|
37
|
+
end
|
38
|
+
|
39
|
+
def each_found_treasure
|
40
|
+
@found_treasures.each { |name, points| yield Treasure.new(name, points) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def points
|
44
|
+
@found_treasures.values.reduce(0, :+)
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_s
|
48
|
+
"I'm #{@name} with health = #{@health}, and points = #{points}, and score = #{score}."
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
if __FILE__ == $0
|
54
|
+
player = Player.new("moe")
|
55
|
+
puts player.name
|
56
|
+
puts player.health
|
57
|
+
player.w00t
|
58
|
+
puts player.health
|
59
|
+
player.blam
|
60
|
+
puts player.health
|
61
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module StudioGame
|
2
|
+
Treasure = Struct.new(:name, :points)
|
3
|
+
|
4
|
+
module TreasureTrove
|
5
|
+
|
6
|
+
TREASURES = [
|
7
|
+
Treasure.new(:pie, 5),
|
8
|
+
Treasure.new(:bottle, 25),
|
9
|
+
Treasure.new(:hammer, 50),
|
10
|
+
Treasure.new(:skillet, 100),
|
11
|
+
Treasure.new(:broomstick, 200),
|
12
|
+
Treasure.new(:crowbar, 400)
|
13
|
+
]
|
14
|
+
|
15
|
+
def self.random
|
16
|
+
TREASURES.sample
|
17
|
+
end
|
18
|
+
end
|
19
|
+
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 = 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
|
+
expect(@player.berserk?).to be_falsey
|
15
|
+
end
|
16
|
+
|
17
|
+
it "goes berserk when w00ted more than 5 times" do
|
18
|
+
1.upto(6) { @player.w00t }
|
19
|
+
|
20
|
+
expect(@player.berserk?).to 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
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'studio_game/clumsy_player'
|
2
|
+
|
3
|
+
module StudioGame
|
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
|
+
|
32
|
+
context "with a boost factor" do
|
33
|
+
before do
|
34
|
+
@initial_health = 100
|
35
|
+
@boost_factor = 5
|
36
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has a boost factor" do
|
40
|
+
expect(@player.boost_factor).to eq(5)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "gets boost factor number of w00ts when w00ted" do
|
44
|
+
@player.w00t
|
45
|
+
|
46
|
+
expect(@player.health).to eq(@initial_health + (15 * @boost_factor))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe Game do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@game = Game.new("Knuckleheads")
|
8
|
+
|
9
|
+
@initial_health = 100
|
10
|
+
@player = Player.new("moe", @initial_health)
|
11
|
+
|
12
|
+
@game.add_player(@player)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "adds 15 health when a high number is rolled" do
|
16
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
17
|
+
|
18
|
+
@game.play(2)
|
19
|
+
|
20
|
+
expect(@player.health).to eq(@initial_health + (15 * 2))
|
21
|
+
end
|
22
|
+
|
23
|
+
it "does not change the player's health when a medium number is rolled" do
|
24
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(3)
|
25
|
+
|
26
|
+
@game.play(2)
|
27
|
+
|
28
|
+
expect(@player.health).to eq(@initial_health)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "decreases the player's health by 10 when a low number is rolled" do
|
32
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(1)
|
33
|
+
|
34
|
+
@game.play(2)
|
35
|
+
|
36
|
+
expect(@player.health).to eq(@initial_health - (10 * 2))
|
37
|
+
end
|
38
|
+
|
39
|
+
it "assigns a treasure for points during a player's turn" do
|
40
|
+
game = Game.new("Knuckleheads")
|
41
|
+
player = Player.new("moe")
|
42
|
+
|
43
|
+
game.add_player(player)
|
44
|
+
|
45
|
+
game.play(1)
|
46
|
+
|
47
|
+
expect(player.points).not_to be_zero
|
48
|
+
end
|
49
|
+
|
50
|
+
it "computes total points as the sum of all player points" do
|
51
|
+
game = Game.new("Knuckleheads")
|
52
|
+
|
53
|
+
player1 = Player.new("moe")
|
54
|
+
player2 = Player.new("larry")
|
55
|
+
|
56
|
+
game.add_player(player1)
|
57
|
+
game.add_player(player2)
|
58
|
+
|
59
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
60
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
61
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
62
|
+
|
63
|
+
expect(game.total_points).to eq(500)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'studio_game/player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe Player do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@initial_health = 150
|
8
|
+
@player = Player.new("larry", @initial_health)
|
9
|
+
$stdout = StringIO.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "has a capitalized name" do
|
13
|
+
expect(@player.name).to eq("Larry")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has an initial health" do
|
17
|
+
expect(@player.health).to eq(150)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can be created from a CSV string" do
|
21
|
+
player = Player.from_csv("larry,150")
|
22
|
+
|
23
|
+
expect(player.name).to eq("Larry")
|
24
|
+
expect(player.health).to eq(150)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "has a string representation" do
|
28
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
29
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
30
|
+
|
31
|
+
expect(@player.to_s).to eq("I'm Larry with health = 150, and points = 100, and score = 250.")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "computes a score as the sum of its health and points" do
|
35
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
36
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
37
|
+
|
38
|
+
expect(@player.score).to eq(250)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "increases health by 15 when w00ted" do
|
42
|
+
@player.w00t
|
43
|
+
|
44
|
+
expect(@player.health).to eq(@initial_health + 15)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "decreases health by 10 when blammed" do
|
48
|
+
@player.blam
|
49
|
+
|
50
|
+
expect(@player.health).to eq(@initial_health - 10)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "computes points as the sum of all treasure poitns" do
|
54
|
+
expect(@player.points).to eq(0)
|
55
|
+
|
56
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
57
|
+
|
58
|
+
expect(@player.points).to eq(50)
|
59
|
+
|
60
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
61
|
+
|
62
|
+
expect(@player.points).to eq(450)
|
63
|
+
|
64
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
65
|
+
|
66
|
+
expect(@player.points).to eq(500)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "yields each found treasure and its total points" do
|
70
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
71
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
72
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
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
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
77
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
78
|
+
|
79
|
+
yielded = []
|
80
|
+
@player.each_found_treasure do |treasure|
|
81
|
+
yielded << treasure
|
82
|
+
end
|
83
|
+
|
84
|
+
expect(yielded).to eq([
|
85
|
+
Treasure.new(:skillet, 200),
|
86
|
+
Treasure.new(:hammer, 50),
|
87
|
+
Treasure.new(:bottle, 25)
|
88
|
+
])
|
89
|
+
end
|
90
|
+
|
91
|
+
context "has an initial health of 150" do
|
92
|
+
before do
|
93
|
+
@initial_health = 150
|
94
|
+
@player = Player.new("larry", @initial_health)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "'strong?' returns 'true'" do
|
98
|
+
expect(@player).to be_strong
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "has an initial health <= 100" do
|
103
|
+
before do
|
104
|
+
@player = Player.new("larry", 100)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "'strong?' returns 'false'" do
|
108
|
+
expect(@player).not_to be_strong
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context "in a collection of players" do
|
113
|
+
before do
|
114
|
+
@player1 = Player.new("moe", 100)
|
115
|
+
@player2 = Player.new("larry", 200)
|
116
|
+
@player3 = Player.new("curly", 300)
|
117
|
+
|
118
|
+
@players = [@player1, @player2, @player3]
|
119
|
+
end
|
120
|
+
|
121
|
+
it "is sorted by decreasing score" do
|
122
|
+
expect(@players.sort).to eq([@player3, @player2, @player1])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
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
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
describe TreasureTrove do
|
21
|
+
|
22
|
+
it "has six treasures" do
|
23
|
+
expect(TreasureTrove::TREASURES.size).to eq(6)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "has a pie worth 5 points" do
|
27
|
+
expect(TreasureTrove::TREASURES[0]).to eq(Treasure.new(:pie, 5))
|
28
|
+
end
|
29
|
+
|
30
|
+
it "has a bottle worth 25 points" do
|
31
|
+
expect(TreasureTrove::TREASURES[1]).to eq(Treasure.new(:bottle, 25))
|
32
|
+
end
|
33
|
+
|
34
|
+
it "has a hammer worth 50 points" do
|
35
|
+
expect(TreasureTrove::TREASURES[2]).to eq(Treasure.new(:hammer, 50))
|
36
|
+
end
|
37
|
+
|
38
|
+
it "has a skillet worth 100 points" do
|
39
|
+
expect(TreasureTrove::TREASURES[3]).to eq(Treasure.new(:skillet, 100))
|
40
|
+
end
|
41
|
+
|
42
|
+
it "has a broomstick worth 200 points" do
|
43
|
+
expect(TreasureTrove::TREASURES[4]).to eq(Treasure.new(:broomstick, 200))
|
44
|
+
end
|
45
|
+
|
46
|
+
it "has a crowbar worth 400 points" do
|
47
|
+
expect(TreasureTrove::TREASURES[5]).to eq(Treasure.new(:crowbar, 400))
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns a random treasure" do
|
51
|
+
treasure = TreasureTrove.random
|
52
|
+
|
53
|
+
expect(TreasureTrove::TREASURES).to include(treasure)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: studio_game_tr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Todd Ruhlen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-15 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
|
+
A simple text-based game that you can run from the command line. Runs a list of
|
29
|
+
players (either those provided, or that you can provide via a '.csv' file that
|
30
|
+
you then name as a command line argument when loading the program), through a
|
31
|
+
series of 'rounds', the number of which you can choose as the game runs. Each
|
32
|
+
player is 'w00ted', 'blammed', or 'skipped' and receives a treasure. High-scores
|
33
|
+
are printed out to the console upon exiting the game. They are also saved to a
|
34
|
+
file ('high_scores.txt') in the top-level folder of the application.
|
35
|
+
|
36
|
+
Created while doing the Ruby Programming course from Pragmatic Studio.
|
37
|
+
email: rhythmtome@yahoo.com
|
38
|
+
executables:
|
39
|
+
- studio_game
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- LICENSE
|
44
|
+
- README
|
45
|
+
- bin/knuckleheads.csv
|
46
|
+
- bin/pool_pros.csv
|
47
|
+
- bin/studio_game
|
48
|
+
- lib/studio_game/auditable.rb
|
49
|
+
- lib/studio_game/berserk_player.rb
|
50
|
+
- lib/studio_game/clumsy_player.rb
|
51
|
+
- lib/studio_game/die.rb
|
52
|
+
- lib/studio_game/game.rb
|
53
|
+
- lib/studio_game/game_turn.rb
|
54
|
+
- lib/studio_game/loaded_die.rb
|
55
|
+
- lib/studio_game/playable.rb
|
56
|
+
- lib/studio_game/player.rb
|
57
|
+
- lib/studio_game/treasure_trove.rb
|
58
|
+
- spec/studio_game/berserk_player_spec.rb
|
59
|
+
- spec/studio_game/clumsy_player_spec.rb
|
60
|
+
- spec/studio_game/game_spec.rb
|
61
|
+
- spec/studio_game/player_spec.rb
|
62
|
+
- spec/studio_game/treasure_trove_spec.rb
|
63
|
+
homepage:
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.9'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.4.8
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Simple text-based game created during Pragmatic Studio's Ruby Programming
|
87
|
+
course.
|
88
|
+
test_files:
|
89
|
+
- spec/studio_game/player_spec.rb
|
90
|
+
- spec/studio_game/berserk_player_spec.rb
|
91
|
+
- spec/studio_game/game_spec.rb
|
92
|
+
- spec/studio_game/clumsy_player_spec.rb
|
93
|
+
- spec/studio_game/treasure_trove_spec.rb
|