studio_game_ksl 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 +11 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +34 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +38 -0
- data/lib/studio_game/clumsy_player.rb +45 -0
- data/lib/studio_game/die.rb +15 -0
- data/lib/studio_game/game.rb +115 -0
- data/lib/studio_game/game_turn.rb +24 -0
- data/lib/studio_game/playable.rb +18 -0
- data/lib/studio_game/player.rb +68 -0
- data/lib/studio_game/treasure_trove.rb +20 -0
- data/spec/studio_game/berserk_player_spec.rb +39 -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 +128 -0
- data/spec/studio_game/spec_helper.rb +9 -0
- data/spec/studio_game/treasure_trove_spec.rb +58 -0
- metadata +100 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c0d8c381a810f7363a728656743cf75059733f2c6422c5b9cfa3c5bf65443bb6
|
|
4
|
+
data.tar.gz: e16aed43ba92380db8fd5b205076f1ac5706e6a56b9a9c022a8f6d7d9a206d25
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e77e16386321754a5605d7d3fa87f63f1e8be3c926120e34487b858fbc8158591be6a5380cbcd56f4bcb8a7fccaeea76d0f82453cd4d29ab11604d8094ee4099
|
|
7
|
+
data.tar.gz: aa55d273ed9f1adf5223638c895092af1c87184c031234d6ad9022ac0b6fab57b930d6965afb899871db0c990ec4d1b1e7e9932550dae5ddf4cc723a2f2d0ce0
|
data/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2022 Kelly S Loyd
|
|
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 all
|
|
11
|
+
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 THE
|
|
19
|
+
SOFTWARE.
|
data/README
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Knuckleheads Game from Pragmatic Studio Ruby course.
|
|
2
|
+
|
|
3
|
+
type: studio_game to play.
|
|
4
|
+
A CSV filename can be passed to the studio_game to specifiy a collection of Players.
|
|
5
|
+
Format of CSV: playername,health
|
|
6
|
+
|
|
7
|
+
Players will be randomly 'w00ted' and 'blammed' and find random treasures on each turn.
|
|
8
|
+
|
|
9
|
+
after game play, high scores are written to the working directory.
|
|
10
|
+
|
|
11
|
+
|
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
module StudioKLGame
|
|
8
|
+
knuckleheads = StudioKLGame::Game.new("Knuckleheads")
|
|
9
|
+
|
|
10
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
|
11
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
|
12
|
+
shemp = ClumsyPlayer.new("shemp", 80, 5)
|
|
13
|
+
knuckleheads.add_player(shemp)
|
|
14
|
+
joe = BerserkPlayer.new("joe", 100)
|
|
15
|
+
knuckleheads.add_player(joe)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
loop do
|
|
19
|
+
puts "How many game rounds? ('quit' to exit)"
|
|
20
|
+
answer = gets.chomp.downcase
|
|
21
|
+
case answer
|
|
22
|
+
when /^\d+$/
|
|
23
|
+
knuckleheads.play(answer.to_i)
|
|
24
|
+
when 'quit' || 'exit'
|
|
25
|
+
knuckleheads.print_stats
|
|
26
|
+
break
|
|
27
|
+
else
|
|
28
|
+
puts "Enter a number or 'quit'"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
knuckleheads.save_high_scores
|
|
33
|
+
|
|
34
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
|
|
3
|
+
module StudioKLGame
|
|
4
|
+
class BerserkPlayer < Player
|
|
5
|
+
|
|
6
|
+
def initialize(name, health=100)
|
|
7
|
+
@w00tcount = 0
|
|
8
|
+
super(name, health)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def w00t
|
|
12
|
+
super
|
|
13
|
+
@w00tcount += 1
|
|
14
|
+
puts "#{@name} is berserk!" if berserk?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def blam
|
|
18
|
+
if berserk?
|
|
19
|
+
w00t
|
|
20
|
+
else
|
|
21
|
+
super
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def berserk?
|
|
26
|
+
@w00tcount > 5
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
if __FILE__ == $0
|
|
34
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
|
35
|
+
6.times { berserker.w00t }
|
|
36
|
+
2.times { berserker.blam }
|
|
37
|
+
puts berserker.health
|
|
38
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
|
|
3
|
+
module StudioKLGame
|
|
4
|
+
class ClumsyPlayer < Player
|
|
5
|
+
|
|
6
|
+
attr_reader :boost_factor
|
|
7
|
+
|
|
8
|
+
def initialize(name, health=100, boost=1)
|
|
9
|
+
super(name, health)
|
|
10
|
+
@boost_factor = boost
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def w00t
|
|
14
|
+
@boost_factor.times do
|
|
15
|
+
super
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def found_treasure(treasure)
|
|
20
|
+
new_treasure = Treasure.new(treasure.name, treasure.points / 2.0)
|
|
21
|
+
super new_treasure
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
if __FILE__ == $0
|
|
28
|
+
clumsy = ClumsyPlayer.new("klutz", 105, 3)
|
|
29
|
+
|
|
30
|
+
hammer = Treasure.new(:hammer, 50)
|
|
31
|
+
clumsy.found_treasure(hammer)
|
|
32
|
+
clumsy.found_treasure(hammer)
|
|
33
|
+
clumsy.found_treasure(hammer)
|
|
34
|
+
|
|
35
|
+
crowbar = Treasure.new(:crowbar, 400)
|
|
36
|
+
clumsy.found_treasure(crowbar)
|
|
37
|
+
|
|
38
|
+
puts "#{clumsy.name}'s boost factor: #{clumsy.boost_factor}"
|
|
39
|
+
clumsy.w00t
|
|
40
|
+
|
|
41
|
+
clumsy.each_found_treasure do |treasure|
|
|
42
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
43
|
+
end
|
|
44
|
+
puts "#{clumsy.points} grand total points"
|
|
45
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
require_relative 'player'
|
|
2
|
+
require_relative 'die'
|
|
3
|
+
require_relative 'game_turn'
|
|
4
|
+
require_relative 'treasure_trove'
|
|
5
|
+
|
|
6
|
+
module StudioKLGame
|
|
7
|
+
class Game
|
|
8
|
+
|
|
9
|
+
attr_reader :title
|
|
10
|
+
|
|
11
|
+
def initialize(title)
|
|
12
|
+
@title = title
|
|
13
|
+
@players = []
|
|
14
|
+
@game_start_time = Time.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def load_players(filename)
|
|
18
|
+
File.readlines(filename).each do |line|
|
|
19
|
+
add_player(Player.from_csv(line))
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def format_game_start_time
|
|
24
|
+
@game_start_time.strftime("%I:%M:%S")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def add_player(player)
|
|
28
|
+
@players.push(player)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def print_players()
|
|
32
|
+
puts "\nThere are #{@players.size} players in #{@title}."
|
|
33
|
+
@players.each do |player|
|
|
34
|
+
puts player
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def print_treasures()
|
|
39
|
+
treasures = TreasureTrove::TREASURES
|
|
40
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
|
41
|
+
treasures.each do |treasure|
|
|
42
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def health_report()
|
|
47
|
+
puts "Health Report".center(40, '-')
|
|
48
|
+
@players.each do |player|
|
|
49
|
+
puts player.health
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def play(rounds)
|
|
54
|
+
print_players
|
|
55
|
+
print_treasures
|
|
56
|
+
1.upto(rounds) do |round|
|
|
57
|
+
puts "\nRound #{round}:"
|
|
58
|
+
@players.each do |player|
|
|
59
|
+
GameTurn.take_turn(player)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def print_stats
|
|
65
|
+
strong, wimpy = @players.partition{ |player| player.strong?}
|
|
66
|
+
puts "\n#{strong.length} strong players:"
|
|
67
|
+
strong.each do |player|
|
|
68
|
+
print_name_and_health(player)
|
|
69
|
+
end
|
|
70
|
+
puts "\n#{wimpy.length} wimpy players:"
|
|
71
|
+
wimpy.each do |player|
|
|
72
|
+
print_name_and_health(player)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
puts "\n#{@title} High Scores:"
|
|
76
|
+
@players.sort.each do |player|
|
|
77
|
+
puts high_score_entry(player)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
puts "\nPoint Totals:"
|
|
81
|
+
@players.each do |player|
|
|
82
|
+
puts "\n#{player.name} point totals:"
|
|
83
|
+
player.each_found_treasure do |treasure|
|
|
84
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
85
|
+
end
|
|
86
|
+
puts "#{player.points} grand total points"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def save_high_scores(to_file="high_scores.txt")
|
|
92
|
+
File.open(to_file, "w") do |file|
|
|
93
|
+
file.puts("\n#{@title} High Scores:")
|
|
94
|
+
@players.sort.each do |player|
|
|
95
|
+
file.puts high_score_entry(player)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def high_score_entry(player)
|
|
101
|
+
formatted_name = player.name.ljust(20, '.')
|
|
102
|
+
"#{formatted_name} #{player.score}"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def total_points
|
|
106
|
+
@players.reduce(0) { |sum, player| sum += player.points}
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def print_name_and_health(player)
|
|
111
|
+
puts "#{player.name} (#{player.health})"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require_relative 'die'
|
|
2
|
+
require_relative 'player'
|
|
3
|
+
require_relative 'treasure_trove'
|
|
4
|
+
|
|
5
|
+
module StudioKLGame
|
|
6
|
+
module GameTurn
|
|
7
|
+
|
|
8
|
+
def self.take_turn(player)
|
|
9
|
+
die = Die.new()
|
|
10
|
+
case die.roll
|
|
11
|
+
when 1..2
|
|
12
|
+
player.blam
|
|
13
|
+
when 3..4
|
|
14
|
+
puts "#{player.name} was skipped"
|
|
15
|
+
when 5..6
|
|
16
|
+
player.w00t
|
|
17
|
+
end
|
|
18
|
+
puts player
|
|
19
|
+
treasure = TreasureTrove.random
|
|
20
|
+
player.found_treasure(treasure)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module StudioKLGame
|
|
2
|
+
module Playable
|
|
3
|
+
def w00t
|
|
4
|
+
self.health += 15
|
|
5
|
+
puts "#{self.name} got w00ted!"
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def blam
|
|
9
|
+
self.health -= 10
|
|
10
|
+
puts "#{self.name} got blammed!"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def strong?
|
|
14
|
+
self.health > 100
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require_relative 'treasure_trove'
|
|
2
|
+
require_relative 'playable'
|
|
3
|
+
|
|
4
|
+
module StudioKLGame
|
|
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
|
+
|
|
23
|
+
def name=(name)
|
|
24
|
+
@name = name.capitalize
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_s
|
|
28
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def score
|
|
33
|
+
@health + points
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def <=>(other_player)
|
|
38
|
+
other_player.score <=> self.score
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def found_treasure(treasure)
|
|
42
|
+
@found_treasures[treasure.name] += treasure.points
|
|
43
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
|
44
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def each_found_treasure
|
|
48
|
+
@found_treasures.each do |name, points|
|
|
49
|
+
yield Treasure.new(name, points)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def points
|
|
54
|
+
@found_treasures.values.reduce(0, :+)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
if __FILE__ == $0
|
|
61
|
+
player = Player.new("moe")
|
|
62
|
+
puts player.name
|
|
63
|
+
puts player.health
|
|
64
|
+
player.w00t
|
|
65
|
+
puts player.health
|
|
66
|
+
player.blam
|
|
67
|
+
puts player.health
|
|
68
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module StudioKLGame
|
|
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
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/berserk_player'
|
|
3
|
+
|
|
4
|
+
module StudioKLGame
|
|
5
|
+
describe BerserkPlayer do
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
$stdout = StringIO.new
|
|
9
|
+
@initial_health = 50
|
|
10
|
+
@player = BerserkPlayer.new("berserker", @initial_health)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "does not go berserk when w00ted up to 5 times" do
|
|
14
|
+
1.upto(5) { @player.w00t }
|
|
15
|
+
|
|
16
|
+
@player.berserk?.should be_falsey
|
|
17
|
+
|
|
18
|
+
# or if using Rspec 3.0:
|
|
19
|
+
# @player.berserk?.should be_falsey
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "goes berserk when w00ted more than 5 times" do
|
|
23
|
+
1.upto(6) { @player.w00t }
|
|
24
|
+
|
|
25
|
+
@player.berserk?.should be_truthy
|
|
26
|
+
|
|
27
|
+
# or if using Rspec 3.0:
|
|
28
|
+
# @player.berserk?.should be_truthy
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
|
32
|
+
1.upto(6) { @player.w00t }
|
|
33
|
+
1.upto(2) { @player.blam }
|
|
34
|
+
|
|
35
|
+
@player.health.should == @initial_health + (8 * 15)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/clumsy_player'
|
|
3
|
+
|
|
4
|
+
module StudioKLGame
|
|
5
|
+
describe ClumsyPlayer do
|
|
6
|
+
before do
|
|
7
|
+
$stdout = StringIO.new
|
|
8
|
+
@player = ClumsyPlayer.new("klutz")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "only gets half the point value for each treasure" do
|
|
12
|
+
@player.points.should == 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
|
+
@player.points.should == 75
|
|
20
|
+
|
|
21
|
+
crowbar = Treasure.new(:crowbar, 400)
|
|
22
|
+
@player.found_treasure(crowbar)
|
|
23
|
+
|
|
24
|
+
@player.points.should == 275
|
|
25
|
+
|
|
26
|
+
yielded = []
|
|
27
|
+
@player.each_found_treasure do |treasure|
|
|
28
|
+
yielded << treasure
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context "with a boost factor" do
|
|
35
|
+
before do
|
|
36
|
+
@initial_health = 100
|
|
37
|
+
@boost_factor = 5
|
|
38
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "has a boost factor" do
|
|
42
|
+
@player.boost_factor.should == 5
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "gets boosted by boost health per w00t" do
|
|
46
|
+
@player.w00t
|
|
47
|
+
@player.health.should == @initial_health + (15 * @boost_factor)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/game'
|
|
3
|
+
|
|
4
|
+
module StudioKLGame
|
|
5
|
+
describe Game do
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
$stdout = StringIO.new
|
|
9
|
+
@game = Game.new("Knuckleheads")
|
|
10
|
+
|
|
11
|
+
@initial_health = 100
|
|
12
|
+
@player = Player.new("moe", @initial_health)
|
|
13
|
+
|
|
14
|
+
@game.add_player(@player)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "w00ts the player when a high number is rolled" do
|
|
18
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
|
19
|
+
@game.play(2)
|
|
20
|
+
@player.health.should == @initial_health + (15 * 2)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "skips the player when a medium number is rolled" do
|
|
24
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(3)
|
|
25
|
+
@game.play(2)
|
|
26
|
+
@player.health.should == @initial_health
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "blams the player when a low number is rolled" do
|
|
30
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(1)
|
|
31
|
+
@game.play(2)
|
|
32
|
+
@player.health.should == @initial_health - (10 * 2)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "assigns a treasure for points during a player's turn" do
|
|
36
|
+
game = Game.new("Knuckleheads")
|
|
37
|
+
player = Player.new("moe")
|
|
38
|
+
|
|
39
|
+
game.add_player(player)
|
|
40
|
+
|
|
41
|
+
game.play(1)
|
|
42
|
+
|
|
43
|
+
player.points.should_not be_zero
|
|
44
|
+
|
|
45
|
+
# or use alternate expectation syntax:
|
|
46
|
+
# expect(player.points).not_to be_zero
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "computes total points as the sum of all player points" do
|
|
50
|
+
game = Game.new("Knuckleheads")
|
|
51
|
+
|
|
52
|
+
player1 = Player.new("moe")
|
|
53
|
+
player2 = Player.new("larry")
|
|
54
|
+
|
|
55
|
+
game.add_player(player1)
|
|
56
|
+
game.add_player(player2)
|
|
57
|
+
|
|
58
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
|
59
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
|
60
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
|
61
|
+
|
|
62
|
+
game.total_points.should == 500
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/player'
|
|
3
|
+
require 'studio_game/treasure_trove'
|
|
4
|
+
|
|
5
|
+
module StudioKLGame
|
|
6
|
+
describe Player do
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
$stdout = StringIO.new
|
|
10
|
+
@initial_health = 150
|
|
11
|
+
@player = Player.new("larry", @initial_health)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "has a capitalized name" do
|
|
15
|
+
@player.name.should == "Larry"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "has an initial health" do
|
|
19
|
+
@player.health.should == 150
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "has a string representation" do
|
|
23
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
24
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
25
|
+
|
|
26
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "computes a score as the sum of its health and length of name" do
|
|
30
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
31
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
32
|
+
|
|
33
|
+
@player.score.should == 250
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "increases health by 15 when w00ted" do
|
|
37
|
+
@player.w00t
|
|
38
|
+
@player.health.should == @initial_health + 15
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
it "decreases health by 10 when blammed" do
|
|
43
|
+
@player.blam
|
|
44
|
+
@player.health.should == (@initial_health - 10)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "computes points as the sum of all treasure points" do
|
|
48
|
+
@player.points.should == 0
|
|
49
|
+
|
|
50
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
51
|
+
|
|
52
|
+
@player.points.should == 50
|
|
53
|
+
|
|
54
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
|
55
|
+
|
|
56
|
+
@player.points.should == 450
|
|
57
|
+
|
|
58
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
59
|
+
|
|
60
|
+
@player.points.should == 500
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "yields each found treasure and its total points" do
|
|
64
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
65
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
66
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
67
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
68
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
69
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
70
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
71
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
72
|
+
|
|
73
|
+
yielded = []
|
|
74
|
+
@player.each_found_treasure do |treasure|
|
|
75
|
+
yielded << treasure
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
yielded.should == [
|
|
79
|
+
Treasure.new(:skillet, 200),
|
|
80
|
+
Treasure.new(:hammer, 50),
|
|
81
|
+
Treasure.new(:bottle, 25)
|
|
82
|
+
]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "creates a new player from a comma separated string" do
|
|
86
|
+
player = Player.from_csv("moe,100")
|
|
87
|
+
|
|
88
|
+
player.name.should == "Moe"
|
|
89
|
+
player.health.should == 100
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
context "with a health greater than 100" do
|
|
93
|
+
before do
|
|
94
|
+
@player = Player.new("larry", 150)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "is a strong player" do
|
|
98
|
+
@player.should be_strong
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
context "with a health of 100" do
|
|
103
|
+
before do
|
|
104
|
+
@player = Player.new("larry", 100)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it "is a wimpy player" do
|
|
108
|
+
@player.should_not be_strong
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
context "in a 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
|
+
@players.sort.should == [@player3, @player2, @player1]
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
end
|
|
128
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require_relative 'spec_helper'
|
|
2
|
+
require 'studio_game/treasure_trove'
|
|
3
|
+
|
|
4
|
+
module StudioKLGame
|
|
5
|
+
describe Treasure do
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
@treasure = Treasure.new(:hammer, 50)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "has a name attribute" do
|
|
12
|
+
@treasure.name.should == :hammer
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "has a points attribute" do
|
|
16
|
+
@treasure.points.should == 50
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe TreasureTrove do
|
|
22
|
+
|
|
23
|
+
it "has six treasures" do
|
|
24
|
+
TreasureTrove::TREASURES.size.should == 6
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "has a pie worth 5 points" do
|
|
28
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "has a bottle worth 25 points" do
|
|
32
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "has a hammer worth 50 points" do
|
|
36
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "has a skillet worth 100 points" do
|
|
40
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "has a broomstick worth 200 points" do
|
|
44
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "has a crowbar worth 400 points" do
|
|
48
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "returns a random treasure" do
|
|
52
|
+
treasure = TreasureTrove.random
|
|
53
|
+
|
|
54
|
+
TreasureTrove::TREASURES.should include(treasure)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: studio_game_ksl
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kelly Loyd
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-09-26 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'
|
|
20
|
+
- - ">="
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: 2.8.0
|
|
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'
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 2.8.0
|
|
33
|
+
description: |+
|
|
34
|
+
Knuckleheads Game from Pragmatic Studio Ruby course.
|
|
35
|
+
|
|
36
|
+
type: studio_game to play.
|
|
37
|
+
A CSV filename can be passed to the studio_game to specifiy a collection of Players.
|
|
38
|
+
Format of CSV: playername,health
|
|
39
|
+
|
|
40
|
+
Players will be randomly 'w00ted' and 'blammed' and find random treasures on each turn.
|
|
41
|
+
|
|
42
|
+
after game play, high scores are written to the working directory.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
email: kelly.loyd@icloud.com
|
|
46
|
+
executables:
|
|
47
|
+
- studio_game
|
|
48
|
+
extensions: []
|
|
49
|
+
extra_rdoc_files: []
|
|
50
|
+
files:
|
|
51
|
+
- LICENSE
|
|
52
|
+
- README
|
|
53
|
+
- bin/players.csv
|
|
54
|
+
- bin/studio_game
|
|
55
|
+
- lib/studio_game/auditable.rb
|
|
56
|
+
- lib/studio_game/berserk_player.rb
|
|
57
|
+
- lib/studio_game/clumsy_player.rb
|
|
58
|
+
- lib/studio_game/die.rb
|
|
59
|
+
- lib/studio_game/game.rb
|
|
60
|
+
- lib/studio_game/game_turn.rb
|
|
61
|
+
- lib/studio_game/playable.rb
|
|
62
|
+
- lib/studio_game/player.rb
|
|
63
|
+
- lib/studio_game/treasure_trove.rb
|
|
64
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
65
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
66
|
+
- spec/studio_game/game_spec.rb
|
|
67
|
+
- spec/studio_game/player_spec.rb
|
|
68
|
+
- spec/studio_game/spec_helper.rb
|
|
69
|
+
- spec/studio_game/treasure_trove_spec.rb
|
|
70
|
+
homepage: http://www.kansascitysubdivision.net
|
|
71
|
+
licenses:
|
|
72
|
+
- MIT
|
|
73
|
+
metadata: {}
|
|
74
|
+
post_install_message:
|
|
75
|
+
rdoc_options: []
|
|
76
|
+
require_paths:
|
|
77
|
+
- lib
|
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '1.9'
|
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - ">="
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '0'
|
|
88
|
+
requirements: []
|
|
89
|
+
rubygems_version: 3.2.33
|
|
90
|
+
signing_key:
|
|
91
|
+
specification_version: 4
|
|
92
|
+
summary: Knuckleheads Game
|
|
93
|
+
test_files:
|
|
94
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
95
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
96
|
+
- spec/studio_game/game_spec.rb
|
|
97
|
+
- spec/studio_game/player_spec.rb
|
|
98
|
+
- spec/studio_game/spec_helper.rb
|
|
99
|
+
- spec/studio_game/treasure_trove_spec.rb
|
|
100
|
+
...
|