pragma_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 +19 -0
- data/README +13 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +36 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +31 -0
- data/lib/studio_game/clumsy_player.rb +38 -0
- data/lib/studio_game/die.rb +26 -0
- data/lib/studio_game/game.rb +118 -0
- data/lib/studio_game/game_turn.rb +27 -0
- data/lib/studio_game/loaded_die.rb +16 -0
- data/lib/studio_game/playable.rb +17 -0
- data/lib/studio_game/player.rb +76 -0
- data/lib/studio_game/treasure_trove.rb +18 -0
- data/spec/studio_game/berserk_player_spec.rb +27 -0
- data/spec/studio_game/clumsy_player_spec.rb +45 -0
- data/spec/studio_game/game_spec.rb +57 -0
- data/spec/studio_game/player_spec.rb +129 -0
- data/spec/studio_game/treasure_trove_spec.rb +52 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d8afe177d44978781ad7f36ed5093306e6b7b38164b777e72f6a717890763ea6
|
4
|
+
data.tar.gz: 4999d7806844a34a65d22d2cf32f81f4525699dc2dfafc8173ab7ec8e2f1f8d6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c51bf832935d19c783fdc93f9642d477fef575400fc1f730b6f154e350660fed88f77fd5ca11c287668250b0db2fd081b0d7c80448f2927455304cee01caa5de
|
7
|
+
data.tar.gz: 82d6ae59a6f9593aa3718d316f8c1dba4b37882e2ab8472c7d0d4dcc7a3535b6903e7a20ed8c03da9ac62a610e57489c1253ca4b4931b167d12d795b40e1fb10
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Studio Game is a simple text-based, fully random game.
|
2
|
+
|
3
|
+
To install the gem, run `gem install pragma_studio_game-1.0.0.gem`.
|
4
|
+
|
5
|
+
To play, run `studio_game` and choose the number of rounds. The players will be loaded from the `bin/players.csv` file.
|
6
|
+
If you want to load your own players, pass the path as an argument to the `studio_game.
|
7
|
+
Players should be provided with their names and health value, comma separated.
|
8
|
+
|
9
|
+
At the end of the game, high scores will be sorted and written to the `high_scores.txt` file.
|
10
|
+
|
11
|
+
To quit any time, type in `quit` or `exit`.
|
12
|
+
|
13
|
+
To uninstall the gem, run `gem uninstall studio_game`.
|
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/studio_game/game'
|
4
|
+
require_relative '../lib/studio_game/clumsy_player'
|
5
|
+
require_relative '../lib/studio_game/berserk_player'
|
6
|
+
|
7
|
+
game = StudioGame::Game.new("Knuckleheads")
|
8
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
9
|
+
game.load_players(ARGV.shift || default_player_file)
|
10
|
+
# take the last argument of the command-line array of arguments provided by the user
|
11
|
+
# if no argument is provided to the 'load_players' method, then default the file to 'players.csv'
|
12
|
+
|
13
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
|
14
|
+
game.add_player(klutz)
|
15
|
+
|
16
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
17
|
+
game.add_player(berserker)
|
18
|
+
|
19
|
+
loop do
|
20
|
+
puts "\nHow many rounds would you like to play the game? ('quit' to exit)"
|
21
|
+
answer = gets.chomp.downcase
|
22
|
+
case answer
|
23
|
+
when /^\d+$/
|
24
|
+
game.play(answer.to_i) do
|
25
|
+
game.total_points >= 2000
|
26
|
+
end
|
27
|
+
when 'quit' || 'exit'
|
28
|
+
game.print_stats
|
29
|
+
break
|
30
|
+
else
|
31
|
+
puts "Please enter a number or 'quit'"
|
32
|
+
answer = gets.chomp.downcase
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
game.save_high_scores
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
def initialize(name, health=100)
|
6
|
+
super(name, health)
|
7
|
+
@w00t_count = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def berserk?
|
11
|
+
@w00t_count > 5
|
12
|
+
end
|
13
|
+
|
14
|
+
def w00t
|
15
|
+
super
|
16
|
+
@w00t_count += 1
|
17
|
+
puts "#{@name} is berserk!" if berserk?
|
18
|
+
end
|
19
|
+
|
20
|
+
def blam
|
21
|
+
berserk? ? w00t : super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
if __FILE__ == $0
|
27
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
28
|
+
6.times { berserker.w00t }
|
29
|
+
2.times { berserker.blam }
|
30
|
+
puts berserker.health
|
31
|
+
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,26 @@
|
|
1
|
+
require_relative 'auditable'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class Die
|
5
|
+
include Auditable
|
6
|
+
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
roll
|
11
|
+
end
|
12
|
+
|
13
|
+
def roll
|
14
|
+
@number = rand(1..6)
|
15
|
+
audit
|
16
|
+
@number
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
if __FILE__ == $0
|
22
|
+
die = Die.new
|
23
|
+
puts die.roll
|
24
|
+
puts die.roll
|
25
|
+
puts die.roll
|
26
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'game_turn'
|
5
|
+
require_relative 'treasure_trove'
|
6
|
+
|
7
|
+
module StudioGame
|
8
|
+
class Game
|
9
|
+
attr_reader :title, :players
|
10
|
+
|
11
|
+
def initialize(title)
|
12
|
+
@title = title
|
13
|
+
@players = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_players(from_file)
|
17
|
+
CSV.foreach(from_file) do |row|
|
18
|
+
player = Player.new(row[0], row[1].to_i)
|
19
|
+
add_player(player)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_player(player)
|
24
|
+
@players << player
|
25
|
+
end
|
26
|
+
|
27
|
+
def play(rounds)
|
28
|
+
puts "There are #{@players.size} players in #{@title}: "
|
29
|
+
|
30
|
+
@players.each do |player|
|
31
|
+
puts player
|
32
|
+
end
|
33
|
+
|
34
|
+
treasures = TreasureTrove::TREASURES
|
35
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
36
|
+
treasures.each do |treasure|
|
37
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
38
|
+
end
|
39
|
+
|
40
|
+
1.upto(rounds) do |round|
|
41
|
+
if block_given?
|
42
|
+
break if yield
|
43
|
+
end
|
44
|
+
puts "\nRound #{round}:"
|
45
|
+
@players.each do |player|
|
46
|
+
GameTurn.take_turn(player)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def print_name_and_health(player)
|
52
|
+
puts "#{player.name} (#{player.health})"
|
53
|
+
end
|
54
|
+
|
55
|
+
def total_points
|
56
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
57
|
+
end
|
58
|
+
|
59
|
+
def save_high_scores(to_file="high_scores.txt")
|
60
|
+
File.open(to_file, "w") do |file|
|
61
|
+
file.puts "#{@title} High Scores:"
|
62
|
+
@players.sort.each do |player|
|
63
|
+
formatted_name = player.name.ljust(20, '.')
|
64
|
+
file.puts "#{formatted_name} #{player.score}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def print_stats
|
70
|
+
puts "\n#{@title} Statistics:"
|
71
|
+
|
72
|
+
strong_players, wimpy_players = @players.partition { |player| player.strong? }
|
73
|
+
|
74
|
+
puts "\n#{strong_players.size} strong players:"
|
75
|
+
strong_players.each do |player|
|
76
|
+
print_name_and_health(player)
|
77
|
+
end
|
78
|
+
|
79
|
+
puts "\n#{wimpy_players.size} wimpy players:"
|
80
|
+
wimpy_players.each do |player|
|
81
|
+
print_name_and_health(player)
|
82
|
+
end
|
83
|
+
|
84
|
+
puts "\n#{total_points} total points from treasures found"
|
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
|
+
|
93
|
+
puts "\n#{@title} High Scores:"
|
94
|
+
@players.sort.each do |player|
|
95
|
+
puts high_score_entry(player)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def high_score_entry(player)
|
100
|
+
formatted_name = player.name.ljust(20, '.')
|
101
|
+
"#{formatted_name} #{player.score}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
if __FILE__ == $0
|
107
|
+
game = Game.new("Knuckleheads")
|
108
|
+
player1 = Player.new("anna")
|
109
|
+
player2 = Player.new("alex")
|
110
|
+
player3 = Player.new("mark")
|
111
|
+
game.add_player(player1)
|
112
|
+
game.add_player(player2)
|
113
|
+
game.add_player(player3)
|
114
|
+
|
115
|
+
game.play(2)
|
116
|
+
|
117
|
+
game.print_stats
|
118
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
module GameTurn
|
7
|
+
def self.take_turn(player)
|
8
|
+
die = Die.new
|
9
|
+
case die.roll
|
10
|
+
when 1..2
|
11
|
+
player.blam
|
12
|
+
when 3..4
|
13
|
+
puts "#{player.name} was skipped."
|
14
|
+
else
|
15
|
+
player.w00t
|
16
|
+
end
|
17
|
+
|
18
|
+
treasure = TreasureTrove.random
|
19
|
+
player.found_treasure(treasure)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if __FILE__ == $0
|
25
|
+
player = Player.new("curly", 125)
|
26
|
+
GameTurn.take_turn(player)
|
27
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative 'die'
|
2
|
+
require_relative 'treasure_trove'
|
3
|
+
require_relative 'playable'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
class Player
|
7
|
+
include Playable
|
8
|
+
|
9
|
+
attr_accessor :name, :health
|
10
|
+
|
11
|
+
def initialize(name, health=100)
|
12
|
+
@name = name.capitalize
|
13
|
+
@health = health
|
14
|
+
@found_treasures = Hash.new(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
def name=(new_name)
|
18
|
+
# override the default writer method to capitalize the name passed
|
19
|
+
@name = new_name.capitalize
|
20
|
+
end
|
21
|
+
|
22
|
+
def roll_the_dice
|
23
|
+
die = Die.new
|
24
|
+
die.roll
|
25
|
+
end
|
26
|
+
|
27
|
+
def <=>(other_player)
|
28
|
+
other_player.score <=> score
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
|
33
|
+
end
|
34
|
+
|
35
|
+
def found_treasure(treasure)
|
36
|
+
@found_treasures[treasure.name] += treasure.points
|
37
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
38
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def each_found_treasure
|
42
|
+
@found_treasures.each do |name, points|
|
43
|
+
treasure = Treasure.new(name, points)
|
44
|
+
yield(treasure)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def points
|
49
|
+
@found_treasures.values.reduce(0, :+)
|
50
|
+
end
|
51
|
+
|
52
|
+
def score
|
53
|
+
@health + points
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.from_csv(string)
|
57
|
+
name, health = string.split(',')
|
58
|
+
Player.new(name, Integer(health))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# only run the below code if this very file is being run
|
64
|
+
# and not another file that required this one
|
65
|
+
# __FILE__ -> variable that holds the filename of this file
|
66
|
+
# $0 -> the currently running programm
|
67
|
+
|
68
|
+
if __FILE__ == $0
|
69
|
+
player = Player.new("moe")
|
70
|
+
puts player.name
|
71
|
+
puts player.health
|
72
|
+
player.w00t
|
73
|
+
puts player.health
|
74
|
+
player.blam
|
75
|
+
puts player.health
|
76
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module StudioGame
|
2
|
+
Treasure = Struct.new(:name, :points)
|
3
|
+
|
4
|
+
module TreasureTrove
|
5
|
+
TREASURES = [
|
6
|
+
Treasure.new(:pie, 5),
|
7
|
+
Treasure.new(:bottle, 25),
|
8
|
+
Treasure.new(:hammer, 50),
|
9
|
+
Treasure.new(:skillet, 100),
|
10
|
+
Treasure.new(:broomstick, 200),
|
11
|
+
Treasure.new(:crowbar, 400)
|
12
|
+
]
|
13
|
+
|
14
|
+
def self.random
|
15
|
+
TREASURES.sample
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'studio_game/berserk_player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe BerserkPlayer do
|
5
|
+
before(:example) do
|
6
|
+
@initial_health = 50
|
7
|
+
@player = BerserkPlayer.new("berserker", @initial_health)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "does not go berserk when w00ted up to 5 times" do
|
11
|
+
1.upto(5) { @player.w00t }
|
12
|
+
expect(@player.berserk?).to be_falsey
|
13
|
+
end
|
14
|
+
|
15
|
+
it "goes berserk when w00ted more than 5 times" do
|
16
|
+
1.upto(6) { @player.w00t }
|
17
|
+
expect(@player.berserk?).to be_truthy
|
18
|
+
end
|
19
|
+
|
20
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
21
|
+
1.upto(6) { @player.w00t }
|
22
|
+
1.upto(2) { @player.blam }
|
23
|
+
|
24
|
+
expect(@player.health).to eq(@initial_health + (8 * 15))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'studio_game/clumsy_player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe ClumsyPlayer do
|
5
|
+
before(:example) do
|
6
|
+
$stdout = StringIO.new
|
7
|
+
|
8
|
+
@initial_health = 100
|
9
|
+
@boost_factor = 5
|
10
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "only gets half the point value for each treasure" do
|
14
|
+
expect(@player.points).to eq(0)
|
15
|
+
|
16
|
+
hammer = Treasure.new(:hammer, 50)
|
17
|
+
@player.found_treasure(hammer)
|
18
|
+
@player.found_treasure(hammer)
|
19
|
+
@player.found_treasure(hammer)
|
20
|
+
|
21
|
+
expect(@player.points).to eq(75)
|
22
|
+
|
23
|
+
crowbar = Treasure.new(:crowbar, 400)
|
24
|
+
@player.found_treasure(crowbar)
|
25
|
+
|
26
|
+
expect(@player.points).to eq(275)
|
27
|
+
|
28
|
+
yielded = []
|
29
|
+
@player.each_found_treasure do |treasure|
|
30
|
+
yielded << treasure
|
31
|
+
end
|
32
|
+
|
33
|
+
expect(yielded).to eq([Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "has a boost factor" do
|
37
|
+
expect(@player.boost_factor).to eq(5)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "gets w00ted a specified boost_factor of times" do
|
41
|
+
@player.w00t
|
42
|
+
expect(@player.health).to eq(@initial_health + 15 * @boost_factor)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'studio_game/game'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe 'Game' do
|
5
|
+
before(:example) do
|
6
|
+
$stdout = StringIO.new #prevent puts methods from logging to the console while running the tests
|
7
|
+
|
8
|
+
@game = Game.new("Knuckleheads")
|
9
|
+
@initial_health = 100
|
10
|
+
@player = Player.new("moe", @initial_health)
|
11
|
+
|
12
|
+
@game.add_player(@player)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "w00ts the player if a high number is rolled" do
|
16
|
+
Die.any_instance.stub(:roll).and_return(5)
|
17
|
+
@game.play(2)
|
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
|
+
Die.any_instance.stub(:roll).and_return(3)
|
23
|
+
@game.play(2)
|
24
|
+
expect(@player.health).to eq(@initial_health)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "blams the player if a low number is rolled" do
|
28
|
+
Die.any_instance.stub(:roll).and_return(1)
|
29
|
+
@game.play(2)
|
30
|
+
expect(@player.health).to eq(@initial_health - 10 * 2)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "assigns a treasure for points during a player's turn" do
|
34
|
+
game = Game.new("Knuckleheads")
|
35
|
+
player = Player.new("moe")
|
36
|
+
game.add_player(player)
|
37
|
+
game.play(1)
|
38
|
+
expect(player.points).not_to be_zero
|
39
|
+
end
|
40
|
+
|
41
|
+
it "computes total points as the sum of all player points" do
|
42
|
+
game = Game.new("Knuckleheads")
|
43
|
+
|
44
|
+
player1 = Player.new("moe")
|
45
|
+
player2 = Player.new("larry")
|
46
|
+
|
47
|
+
game.add_player(player1)
|
48
|
+
game.add_player(player2)
|
49
|
+
|
50
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
51
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
52
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
53
|
+
|
54
|
+
expect(game.total_points).to eq(500)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'studio_game/player'
|
2
|
+
require 'studio_game/treasure_trove'
|
3
|
+
|
4
|
+
module StudioGame
|
5
|
+
describe 'Player' do
|
6
|
+
before(:example) do
|
7
|
+
$stdout = StringIO.new #prevent puts methods from logging to the console while running the tests
|
8
|
+
@initial_health = 50
|
9
|
+
@player = Player.new("andrew", @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "capitalizes the player's name" do
|
13
|
+
expect(@player.name).to eq("Andrew")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has a string representation" do
|
17
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
18
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
19
|
+
expect(@player.to_s).to eq("I'm Andrew with health = 50, points = 100, and score = 150.")
|
20
|
+
end
|
21
|
+
|
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(150)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "decreases health by 10 when blammed" do
|
29
|
+
@player.blam
|
30
|
+
expect(@player.health).to eq(@initial_health - 10)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "increases health by 15 when w00ted" do
|
34
|
+
@player.w00t
|
35
|
+
expect(@player.health).to eq(@initial_health + 15)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "computes points as the sum of all treasure points" do
|
39
|
+
expect(@player.points).to eq(0)
|
40
|
+
|
41
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
42
|
+
expect(@player.points).to eq(50)
|
43
|
+
|
44
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
45
|
+
expect(@player.points).to eq(450)
|
46
|
+
|
47
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
48
|
+
expect(@player.points).to eq(500)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "yields each found treasure and its total points" do
|
52
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
53
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
54
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
55
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
56
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
57
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
58
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
59
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
60
|
+
|
61
|
+
yielded = []
|
62
|
+
@player.each_found_treasure do |treasure|
|
63
|
+
yielded << treasure
|
64
|
+
end
|
65
|
+
|
66
|
+
expect(yielded).to eq([ Treasure.new(:skillet, 200), Treasure.new(:hammer, 50), Treasure.new(:bottle, 25)])
|
67
|
+
end
|
68
|
+
|
69
|
+
it "creates a Player object from a CSV string" do
|
70
|
+
player = Player.from_csv("larry,150")
|
71
|
+
|
72
|
+
expect(player.name).to eq("Larry")
|
73
|
+
expect(player.health).to eq(150)
|
74
|
+
end
|
75
|
+
|
76
|
+
context "created with a default health" do
|
77
|
+
before(:example) do
|
78
|
+
@player = Player.new("andrew")
|
79
|
+
end
|
80
|
+
|
81
|
+
it "has a default health value of 100" do
|
82
|
+
expect(@player.health).to eq(100)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "player with a health greater than 100" do
|
87
|
+
before(:example) do
|
88
|
+
@player = Player.new("mark", 150)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "is strong" do
|
92
|
+
expect(@player).to be_strong
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "player with a health lesser than 100" do
|
97
|
+
before(:example) do
|
98
|
+
@player = Player.new("mike", 70)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "is not strong" do
|
102
|
+
expect(@player).not_to be_strong
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "in a collection of players" do
|
107
|
+
before(:example) do
|
108
|
+
@player1 = Player.new("moe", 100)
|
109
|
+
@player2 = Player.new("larry", 200)
|
110
|
+
@player3 = Player.new("curly", 300)
|
111
|
+
|
112
|
+
@players = [@player1, @player2, @player3]
|
113
|
+
end
|
114
|
+
|
115
|
+
it "is sorted by decreasing score" do
|
116
|
+
expect(@players.sort).to eq([@player3, @player2, @player1])
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'studio_game/treasure_trove'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe 'Treasure' do
|
5
|
+
before(:example) 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,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pragma_studio_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kamila Matla-Tomczyk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-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: 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: "Studio Game is a simple text-based, fully random game.\n\nTo install
|
34
|
+
the gem, run `gem install pragma_studio_game-1.0.0.gem`.\n\nTo play, run `studio_game`
|
35
|
+
and choose the number of rounds. The players will be loaded from the `bin/players.csv`
|
36
|
+
file.\nIf you want to load your own players, pass the path as an argument to the
|
37
|
+
`studio_game. \nPlayers should be provided with their names and health value, comma
|
38
|
+
separated.\n\nAt the end of the game, high scores will be sorted and written to
|
39
|
+
the `high_scores.txt` file.\n\nTo quit any time, type in `quit` or `exit`.\n\nTo
|
40
|
+
uninstall the gem, run `gem uninstall studio_game`."
|
41
|
+
email: ka1130@o2.pl
|
42
|
+
executables:
|
43
|
+
- studio_game
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE
|
48
|
+
- README
|
49
|
+
- bin/players.csv
|
50
|
+
- bin/studio_game
|
51
|
+
- lib/studio_game/auditable.rb
|
52
|
+
- lib/studio_game/berserk_player.rb
|
53
|
+
- lib/studio_game/clumsy_player.rb
|
54
|
+
- lib/studio_game/die.rb
|
55
|
+
- lib/studio_game/game.rb
|
56
|
+
- lib/studio_game/game_turn.rb
|
57
|
+
- lib/studio_game/loaded_die.rb
|
58
|
+
- lib/studio_game/playable.rb
|
59
|
+
- lib/studio_game/player.rb
|
60
|
+
- lib/studio_game/treasure_trove.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://kamilamatla.com
|
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
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.7.8
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: A fun, and entirely random, text-based game
|
90
|
+
test_files:
|
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
|