my-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 +0 -0
- data/README +0 -0
- data/bin/high score file.txt +4 -0
- data/bin/high_score_file.txt +6 -0
- data/bin/high_scores.txt +1 -0
- data/bin/my_favorite_players.csv +3 -0
- data/bin/players.csv +3 -0
- data/bin/studio_game +59 -0
- data/bin/studio_game.rb +57 -0
- data/lib/studio_game/auditable.rb +9 -0
- data/lib/studio_game/berserk_player.rb +39 -0
- data/lib/studio_game/clumsy_player.rb +44 -0
- data/lib/studio_game/die.rb +19 -0
- data/lib/studio_game/game.rb +117 -0
- data/lib/studio_game/game_turn.rb +27 -0
- data/lib/studio_game/iterators.rb +43 -0
- data/lib/studio_game/loaded_die.rb +16 -0
- data/lib/studio_game/playable.rb +25 -0
- data/lib/studio_game/player.rb +91 -0
- data/lib/studio_game/treasure_trove.rb +19 -0
- data/spec/studio_game/berserk_player_spec.rb +39 -0
- data/spec/studio_game/clumsy_player_spec.rb +55 -0
- data/spec/studio_game/game_spec.rb +66 -0
- data/spec/studio_game/player_spec.rb +123 -0
- data/spec/studio_game/treasure_trove_spec.rb +58 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: eb9edcb638b2f86aab9644df87f9bf157d1c72dd
|
|
4
|
+
data.tar.gz: 2bb6c76faded8ccddb9255fbd03d652df71fe19c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6c94afd19fb9b862dffade037755690f44451b770410315acdc5a253e9077238ad6f9d4dc77b1dea9a43b82aefed70ee81bb0a0c7ba701474fb85f783883aa01
|
|
7
|
+
data.tar.gz: e7b70a7fedb36923014eda2a1dadc1e684d778568a7fdcc1b7487ca37b077081145c1d3cd1d8775e820b90660aa591ae63a13297071e188f577a03595f887b45
|
data/LICENSE
ADDED
|
File without changes
|
data/README
ADDED
|
File without changes
|
data/bin/high_scores.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Knuckleheads high scores :
|
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
# require 'studio_game/player'
|
|
4
|
+
# require 'studio_game/game'
|
|
5
|
+
# require 'studio_game/clumsy_player'
|
|
6
|
+
# require 'studio_game/berserk_player'
|
|
7
|
+
|
|
8
|
+
require_relative '../lib/studio_game/player'
|
|
9
|
+
require_relative '../lib/studio_game/clumsy_player'
|
|
10
|
+
require_relative '../lib/studio_game/berserk_player'
|
|
11
|
+
require_relative '../lib/studio_game/game'
|
|
12
|
+
|
|
13
|
+
# player1 = Player.new("moe")
|
|
14
|
+
# player2 = Player.new("larry", 60)
|
|
15
|
+
# player3 = Player.new("curly", 125)
|
|
16
|
+
# player4 = Player.new("jack",110)
|
|
17
|
+
# player5 = Player.new("dave", 65)
|
|
18
|
+
# player6 = Player.new("eddy", 70)
|
|
19
|
+
|
|
20
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
|
21
|
+
|
|
22
|
+
# knuckleheads.add_player(player1)
|
|
23
|
+
# knuckleheads.add_player(player2)
|
|
24
|
+
# knuckleheads.add_player(player3)
|
|
25
|
+
# knuckleheads.add_player(player4)
|
|
26
|
+
# knuckleheads.add_player(player5)
|
|
27
|
+
# knuckleheads.add_player(player6)
|
|
28
|
+
# knuckleheads.play(100) do
|
|
29
|
+
# knuckleheads.total_points >= 2000
|
|
30
|
+
# end
|
|
31
|
+
|
|
32
|
+
# knuckleheads.print_stats
|
|
33
|
+
|
|
34
|
+
#knuckleheads.load_players(ARGV.shift || 'players.csv')
|
|
35
|
+
|
|
36
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
|
37
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
|
38
|
+
|
|
39
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
|
|
40
|
+
knuckleheads.add_player(klutz)
|
|
41
|
+
|
|
42
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
|
43
|
+
knuckleheads.add_player(berserker)
|
|
44
|
+
|
|
45
|
+
loop do
|
|
46
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
|
47
|
+
answer = gets.chomp.downcase
|
|
48
|
+
case answer
|
|
49
|
+
when /^\d+$/
|
|
50
|
+
knuckleheads.play(answer.to_i)
|
|
51
|
+
when 'quit', 'exit'
|
|
52
|
+
knuckleheads.print_stats
|
|
53
|
+
break
|
|
54
|
+
else
|
|
55
|
+
puts "Please enter a valid number or 'quit'"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
knuckleheads.save_high_scores('high_score_file.txt')
|
data/bin/studio_game.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# require 'studio_game/player'
|
|
2
|
+
# require 'studio_game/game'
|
|
3
|
+
# require 'studio_game/clumsy_player'
|
|
4
|
+
# require 'studio_game/berserk_player'
|
|
5
|
+
|
|
6
|
+
require_relative '../lib/studio_game/player'
|
|
7
|
+
require_relative '../lib/studio_game/clumsy_player'
|
|
8
|
+
require_relative '../lib/studio_game/berserk_player'
|
|
9
|
+
require_relative '../lib/studio_game/game'
|
|
10
|
+
|
|
11
|
+
# player1 = Player.new("moe")
|
|
12
|
+
# player2 = Player.new("larry", 60)
|
|
13
|
+
# player3 = Player.new("curly", 125)
|
|
14
|
+
# player4 = Player.new("jack",110)
|
|
15
|
+
# player5 = Player.new("dave", 65)
|
|
16
|
+
# player6 = Player.new("eddy", 70)
|
|
17
|
+
|
|
18
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
|
19
|
+
|
|
20
|
+
# knuckleheads.add_player(player1)
|
|
21
|
+
# knuckleheads.add_player(player2)
|
|
22
|
+
# knuckleheads.add_player(player3)
|
|
23
|
+
# knuckleheads.add_player(player4)
|
|
24
|
+
# knuckleheads.add_player(player5)
|
|
25
|
+
# knuckleheads.add_player(player6)
|
|
26
|
+
# knuckleheads.play(100) do
|
|
27
|
+
# knuckleheads.total_points >= 2000
|
|
28
|
+
# end
|
|
29
|
+
|
|
30
|
+
# knuckleheads.print_stats
|
|
31
|
+
|
|
32
|
+
#knuckleheads.load_players(ARGV.shift || 'players.csv')
|
|
33
|
+
|
|
34
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
|
35
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
|
36
|
+
|
|
37
|
+
klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
|
|
38
|
+
knuckleheads.add_player(klutz)
|
|
39
|
+
|
|
40
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
|
41
|
+
knuckleheads.add_player(berserker)
|
|
42
|
+
|
|
43
|
+
loop do
|
|
44
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
|
45
|
+
answer = gets.chomp.downcase
|
|
46
|
+
case answer
|
|
47
|
+
when /^\d+$/
|
|
48
|
+
knuckleheads.play(answer.to_i)
|
|
49
|
+
when 'quit', 'exit'
|
|
50
|
+
knuckleheads.print_stats
|
|
51
|
+
break
|
|
52
|
+
else
|
|
53
|
+
puts "Please enter a valid number or 'quit'"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
knuckleheads.save_high_scores('high_score_file.txt')
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'studio_game/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
|
+
|
|
19
|
+
puts "#{@name} is berserk!" if berserk?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def blam
|
|
23
|
+
if berserk?
|
|
24
|
+
w00t
|
|
25
|
+
else
|
|
26
|
+
super
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# or simply # berserk? ? w00t : super
|
|
30
|
+
end
|
|
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
|
|
39
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'studio_game/player'
|
|
2
|
+
|
|
3
|
+
module StudioGame
|
|
4
|
+
class ClumsyPlayer < Player
|
|
5
|
+
|
|
6
|
+
attr_reader :boost_factor
|
|
7
|
+
def initialize(name, health=100, boost_factor=1)
|
|
8
|
+
super(name, health)
|
|
9
|
+
@boost_factor = boost_factor
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def found_treasure(treasure)
|
|
13
|
+
# points = treasure.points / 2
|
|
14
|
+
# @found_treasures[treasure.name] += points
|
|
15
|
+
# puts "#{@name} found a #{treasure.name} worth #{points} points."
|
|
16
|
+
|
|
17
|
+
# To avoid duplication in code of this method and the one in the super class
|
|
18
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2.0)
|
|
19
|
+
super(damaged_treasure)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def w00t
|
|
23
|
+
@boost_factor.times { super }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
if __FILE__ == $0
|
|
29
|
+
clumsy = ClumsyPlayer.new("klutz")
|
|
30
|
+
|
|
31
|
+
hammer = Treasure.new(:hammer, 50)
|
|
32
|
+
clumsy.found_treasure(hammer)
|
|
33
|
+
clumsy.found_treasure(hammer)
|
|
34
|
+
clumsy.found_treasure(hammer)
|
|
35
|
+
|
|
36
|
+
crowbar = Treasure.new(:crowbar, 400)
|
|
37
|
+
clumsy.found_treasure(crowbar)
|
|
38
|
+
|
|
39
|
+
clumsy.each_found_treasure do |treasure|
|
|
40
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
41
|
+
end
|
|
42
|
+
puts "#{clumsy.points} grand total points"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'studio_game/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 # this will call the method from the module auditab
|
|
16
|
+
return @number
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
require 'studio_game/player'
|
|
2
|
+
#require_relative 'die'
|
|
3
|
+
require 'studio_game/game_turn'
|
|
4
|
+
require 'studio_game/treasure_trove'
|
|
5
|
+
require 'csv'
|
|
6
|
+
|
|
7
|
+
module StudioGame
|
|
8
|
+
class Game
|
|
9
|
+
attr_reader :title
|
|
10
|
+
def initialize(title)
|
|
11
|
+
@title = title.capitalize
|
|
12
|
+
@players = []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def add_player(player)
|
|
16
|
+
@players << player
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def high_score_entry(player)
|
|
20
|
+
formatted_name = player.name.ljust(30, '.')
|
|
21
|
+
"#{formatted_name}#{player.score}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def load_players(from_file)
|
|
25
|
+
# File.readlines(from_file).each do |line|
|
|
26
|
+
# #name, health = line.split(',')
|
|
27
|
+
# #player = Player.new(name, Integer(health))
|
|
28
|
+
# #add_player(player)
|
|
29
|
+
# add_player(Player.from_csv(line))
|
|
30
|
+
# end
|
|
31
|
+
|
|
32
|
+
# Using the CSV library
|
|
33
|
+
CSV.foreach(from_file) do |line|
|
|
34
|
+
player = Player.new(line[0], Integer(line[1]))
|
|
35
|
+
add_player(player)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def save_high_scores(to_file='high_scores.txt')
|
|
40
|
+
File.open(to_file, 'w') do |file|
|
|
41
|
+
file.puts "#{@title} High Scores :"
|
|
42
|
+
@players.sort.each do |player|
|
|
43
|
+
# file.puts "#{player.name.ljust(30, '.')}#{player.score}"
|
|
44
|
+
file.puts high_score_entry(player)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def total_points
|
|
50
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def play(rounds)
|
|
54
|
+
puts "There are #{@players.size} in #{@title} :"
|
|
55
|
+
@players.each do |player|
|
|
56
|
+
puts player
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
treasures = TreasureTrove::TREASURES
|
|
60
|
+
puts "\nThere are #{treasures.size} treasures :"
|
|
61
|
+
treasures.each { |treasure| puts "#{treasure.name} worths #{treasure.points} points"}
|
|
62
|
+
|
|
63
|
+
1.upto(rounds) do |round|
|
|
64
|
+
if block_given?
|
|
65
|
+
break if yield
|
|
66
|
+
end
|
|
67
|
+
puts "\nRound #{round}:"
|
|
68
|
+
@players.each do |player|
|
|
69
|
+
GameTurn.take_turn(player)
|
|
70
|
+
puts player
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def print_stats
|
|
76
|
+
strong_players, wimpy_players = @players.partition { |player| player.strong?}
|
|
77
|
+
puts "\n#{@title} Statistics:"
|
|
78
|
+
puts "\n#{strong_players.length} Strong Players:"
|
|
79
|
+
# puts strong_players
|
|
80
|
+
strong_players.each {|player| print_name_and_health(player)}
|
|
81
|
+
puts "\n#{wimpy_players.length} Wimpy Players:"
|
|
82
|
+
# puts wimpy_players
|
|
83
|
+
wimpy_players.each {|player| print_name_and_health(player)}
|
|
84
|
+
# sorted_players = @players.sort { |a, b| b.score <=> a.score }
|
|
85
|
+
puts "\n#{@title} Top Scores:"
|
|
86
|
+
#sorted_players.each { |player| puts "#{player.name.ljust(30, '.')}#{player.score}" }
|
|
87
|
+
@players.sort.each { |player| puts high_score_entry(player) }
|
|
88
|
+
|
|
89
|
+
puts "\nThe players have collected a total of #{total_points} points from treasures"
|
|
90
|
+
@players.each do |player|
|
|
91
|
+
puts "\n#{player.name}'s point totals:"
|
|
92
|
+
|
|
93
|
+
player.each_found_treasure do |treasure|
|
|
94
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
puts "#{player.points} grand total points"
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def print_name_and_health(player)
|
|
103
|
+
puts "#{player.name.ljust(30, '.')}#{player.health}"
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
if __FILE__ == $PROGRAM_NAME
|
|
110
|
+
player1 = Player.new('player1')
|
|
111
|
+
player2 = Player.new('player2', 70)
|
|
112
|
+
|
|
113
|
+
mygame = Game.new('mygame')
|
|
114
|
+
mygame.add_player(player1)
|
|
115
|
+
mygame.add_player(player2)
|
|
116
|
+
mygame.play
|
|
117
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'studio_game/player'
|
|
2
|
+
require 'studio_game/die'
|
|
3
|
+
require 'studio_game/treasure_trove'
|
|
4
|
+
# require_relative 'loaded_die'
|
|
5
|
+
|
|
6
|
+
module StudioGame
|
|
7
|
+
module GameTurn
|
|
8
|
+
|
|
9
|
+
def self.take_turn(player)
|
|
10
|
+
# Change Die with LoadedDie : rigged die
|
|
11
|
+
# die = LoadedDie.new
|
|
12
|
+
die = Die.new
|
|
13
|
+
number_rolled = die.roll
|
|
14
|
+
if number_rolled < 3
|
|
15
|
+
player.blam
|
|
16
|
+
elsif number_rolled < 5
|
|
17
|
+
puts "#{player.name} was skipped."
|
|
18
|
+
else
|
|
19
|
+
player.w00t
|
|
20
|
+
end
|
|
21
|
+
treasure_found = TreasureTrove.random
|
|
22
|
+
player.found_treasure(treasure_found)
|
|
23
|
+
#puts "#{player.name} founds a #{treasure_found.name} worth #{treasure_found.points}"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
def conversation
|
|
2
|
+
puts 'Hello'
|
|
3
|
+
yield
|
|
4
|
+
puts 'Goodbye'
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
conversation { puts "Good to meet you!" }
|
|
8
|
+
|
|
9
|
+
# def five_times
|
|
10
|
+
# yield 1
|
|
11
|
+
# yield 2
|
|
12
|
+
# yield 3
|
|
13
|
+
# yield 4
|
|
14
|
+
# yield 5
|
|
15
|
+
# end
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# def five_times
|
|
20
|
+
# 1.upto(5) do |i|
|
|
21
|
+
# yield i
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
|
|
25
|
+
# five_times do |n|
|
|
26
|
+
# puts "#{n} situps"
|
|
27
|
+
# puts "#{n} pushups"
|
|
28
|
+
# puts "#{n} chinups"
|
|
29
|
+
# end
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def n_times(number)
|
|
34
|
+
1.upto(number) do |i|
|
|
35
|
+
yield i
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
n_times(5) do |n|
|
|
40
|
+
puts "#{n} situps"
|
|
41
|
+
puts "#{n} pushups"
|
|
42
|
+
puts "#{n} chinups"
|
|
43
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module StudioGame
|
|
2
|
+
module Playable
|
|
3
|
+
|
|
4
|
+
def blam
|
|
5
|
+
@health -= 10
|
|
6
|
+
# puts "#{@name} <<< BLAMMED (-10)" # it is better to use self.name. And self is not required here, it is implicit
|
|
7
|
+
# because we are just reading the value of name and not modifying it here
|
|
8
|
+
# if we want to modify it and it is without self, ruby will consider it as a local variable in Playable
|
|
9
|
+
puts "#{name} <<< BLAMMED (-10)"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def w00t
|
|
13
|
+
# @health += 15
|
|
14
|
+
self.health += 15 # here we need self, because we are modifying health, if we leave it just health, ruby will consider it as a local variable in Playable and wont be modified in player
|
|
15
|
+
# puts "#{@name} +++ W00TED +++ (+15)"
|
|
16
|
+
puts "#{name} +++ W00TED +++ (+15)"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def strong?
|
|
20
|
+
# @health > 100
|
|
21
|
+
health > 100 # here we dont need self because we are not modifying health
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require_relative 'treasure_trove'
|
|
2
|
+
require_relative 'playable'
|
|
3
|
+
|
|
4
|
+
module StudioGame
|
|
5
|
+
class Player
|
|
6
|
+
|
|
7
|
+
include Playable
|
|
8
|
+
|
|
9
|
+
attr_accessor :name
|
|
10
|
+
attr_accessor :health # now health can be changed from outside he class: from Playable module
|
|
11
|
+
# attr_reader :health
|
|
12
|
+
|
|
13
|
+
def initialize(name, health = 100)
|
|
14
|
+
@name = name.capitalize
|
|
15
|
+
@health = health
|
|
16
|
+
@found_treasures = Hash.new(0)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.from_csv(line)
|
|
20
|
+
name, health = line.split(',')
|
|
21
|
+
Player.new(name, Integer(health))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def found_treasure(treasure)
|
|
25
|
+
@found_treasures[treasure.name] += treasure.points
|
|
26
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points"
|
|
27
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def points
|
|
31
|
+
@found_treasures.values.reduce(0, :+)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Moved to playable.rb as mixins
|
|
35
|
+
# def blam
|
|
36
|
+
# @health -= 10
|
|
37
|
+
# puts "#{@name} <<< BLAMMED (-10)"
|
|
38
|
+
# end
|
|
39
|
+
# def w00t
|
|
40
|
+
# @health += 15
|
|
41
|
+
# puts "#{@name} +++ W00TED +++ (+15)"
|
|
42
|
+
# end
|
|
43
|
+
# def strong?
|
|
44
|
+
# @health > 100
|
|
45
|
+
# end
|
|
46
|
+
|
|
47
|
+
def score
|
|
48
|
+
#@name.length + @health
|
|
49
|
+
@health + points
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def name=(new_name)
|
|
53
|
+
@name = new_name
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def name
|
|
57
|
+
@name
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def say_hello
|
|
61
|
+
"I'm #{@name} with a health of #{@health}."
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def to_s
|
|
65
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def <=>(other_player)
|
|
69
|
+
other_player.score <=> score
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def each_found_treasure
|
|
73
|
+
@found_treasures.each do |name, points|
|
|
74
|
+
yield Treasure.new(name, points)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# We can replace $PROGRAM_NAME with $0
|
|
82
|
+
|
|
83
|
+
if __FILE__ == $PROGRAM_NAME
|
|
84
|
+
player = Player.new("moe")
|
|
85
|
+
puts player.name
|
|
86
|
+
puts player.health
|
|
87
|
+
player.w00t
|
|
88
|
+
puts player.health
|
|
89
|
+
player.blam
|
|
90
|
+
puts player.health
|
|
91
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module StudioGame
|
|
2
|
+
Treasure = Struct.new(:name, :points)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
module TreasureTrove
|
|
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,39 @@
|
|
|
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
|
+
@player.berserk?.should be_false
|
|
15
|
+
|
|
16
|
+
# or if using Rspec 3.0:
|
|
17
|
+
# @player.berserk?.should be_falsey
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "goes berserk when w00ted more than 5 times" do
|
|
21
|
+
1.upto(6) { @player.w00t }
|
|
22
|
+
|
|
23
|
+
@player.berserk?.should be_true
|
|
24
|
+
|
|
25
|
+
# or if using Rspec 3.0:
|
|
26
|
+
# @player.berserk?.should be_truthy
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
|
30
|
+
1.upto(6) { @player.w00t }
|
|
31
|
+
1.upto(2) { @player.blam }
|
|
32
|
+
|
|
33
|
+
@player.health.should == @initial_health + (8 * 15)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require 'studio_game/clumsy_player'
|
|
2
|
+
|
|
3
|
+
module StudioGame
|
|
4
|
+
describe ClumsyPlayer do
|
|
5
|
+
|
|
6
|
+
before do
|
|
7
|
+
@player = ClumsyPlayer.new("klutz")
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it 'gets half of the treasures value' do
|
|
11
|
+
|
|
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
|
+
|
|
23
|
+
@player.found_treasure(crowbar)
|
|
24
|
+
|
|
25
|
+
@player.points.should == 275
|
|
26
|
+
|
|
27
|
+
treasures_found = []
|
|
28
|
+
|
|
29
|
+
@player.each_found_treasure do |treasure|
|
|
30
|
+
treasures_found << treasure
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
treasures_found.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
context "with a boost factor" do
|
|
38
|
+
before do
|
|
39
|
+
@initial_health = 100
|
|
40
|
+
@boost_factor = 5
|
|
41
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "has a boost factor" do
|
|
45
|
+
@player.boost_factor.should == 5
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "gets boost factor number of w00ts when w00ted" do
|
|
49
|
+
@player.w00t
|
|
50
|
+
@player.health.should == @initial_health + (15 * @boost_factor)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
require 'studio_game/game'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
module StudioGame
|
|
6
|
+
describe Game do
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
@game = Game.new("Knuckleheads")
|
|
10
|
+
|
|
11
|
+
@initial_health = 100
|
|
12
|
+
@player = Player.new("moe", @initial_health)
|
|
13
|
+
|
|
14
|
+
@game.add_player(@player)
|
|
15
|
+
$stdout = StringIO.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'w00ts the player if a high number is rolled' do
|
|
19
|
+
Die.any_instance.stub(:roll).and_return(5)
|
|
20
|
+
@game.play(2)
|
|
21
|
+
@player.health.should == @initial_health + ( 15 * 2 )
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'skips the player if a medium number is rolled' do
|
|
25
|
+
Die.any_instance.stub(:roll).and_return(3)
|
|
26
|
+
@game.play(2)
|
|
27
|
+
@player.health.should == @initial_health
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'blam the player if a low number is rolled' do
|
|
31
|
+
Die.any_instance.stub(:roll).and_return(1)
|
|
32
|
+
@game.play(2)
|
|
33
|
+
@player.health.should == @initial_health - ( 10 * 2 )
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "assigns a treasure for points during a player's turn" do
|
|
37
|
+
game = Game.new("Knuckleheads")
|
|
38
|
+
player = Player.new("moe")
|
|
39
|
+
|
|
40
|
+
game.add_player(player)
|
|
41
|
+
|
|
42
|
+
game.play(1)
|
|
43
|
+
|
|
44
|
+
player.points.should_not be_zero
|
|
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
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
require 'studio_game/player'
|
|
2
|
+
require 'studio_game/treasure_trove'
|
|
3
|
+
|
|
4
|
+
module StudioGame
|
|
5
|
+
describe Player do
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
@initial_health = 150
|
|
9
|
+
@player = Player.new("larry", @initial_health)
|
|
10
|
+
$stdout = StringIO.new #changed gloabal variable stdout to a new one in this script, to avoit puts commands
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'has a capitalized name' do
|
|
14
|
+
@player.name.should == 'Larry'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'has an initial health' do
|
|
18
|
+
@player.health.should == 150
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'has a string representation' do
|
|
22
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
23
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
24
|
+
|
|
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.score.should == 150 + 5
|
|
31
|
+
#end
|
|
32
|
+
|
|
33
|
+
it 'increases health by 15 when w00ted' do
|
|
34
|
+
@player.w00t
|
|
35
|
+
@player.health.should == @initial_health + 15
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'decreases health by 10 when blammed' do
|
|
39
|
+
@player.blam
|
|
40
|
+
@player.health.should == @initial_health - 10
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
context 'has initial health of 150' do
|
|
44
|
+
before do
|
|
45
|
+
@player == Player.new('Larry', 150)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'is strong' do
|
|
49
|
+
@player.should be_strong
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
context 'has initial health of 50' do
|
|
54
|
+
before do
|
|
55
|
+
@player = Player.new('Larry', 50)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it 'is not strong' do
|
|
59
|
+
@player.should_not be_strong
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context "in a collection of players" do
|
|
64
|
+
before do
|
|
65
|
+
@player1 = Player.new("moe", 100)
|
|
66
|
+
@player2 = Player.new("larry", 200)
|
|
67
|
+
@player3 = Player.new("curly", 300)
|
|
68
|
+
|
|
69
|
+
@players = [@player1, @player2, @player3]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "is sorted by decreasing score" do
|
|
73
|
+
@players.sort.should == [@player3, @player2, @player1]
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "computes points as the sum of all treasure points" do
|
|
78
|
+
@player.points.should == 0
|
|
79
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
80
|
+
@player.points.should == 50
|
|
81
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
|
82
|
+
@player.points.should == 450
|
|
83
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
84
|
+
@player.points.should == 500
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "computes a score as the sum of its health and points" do
|
|
88
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
89
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
90
|
+
|
|
91
|
+
@player.score.should == 250
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "yields each found treasure and its total points" do
|
|
95
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
96
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
|
97
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
|
98
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
99
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
100
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
101
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
102
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
|
103
|
+
|
|
104
|
+
yielded = []
|
|
105
|
+
@player.each_found_treasure do |treasure|
|
|
106
|
+
yielded << treasure
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
yielded.should == [
|
|
110
|
+
Treasure.new(:skillet, 200),
|
|
111
|
+
Treasure.new(:hammer, 50),
|
|
112
|
+
Treasure.new(:bottle, 25)
|
|
113
|
+
]
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it "can be created from a CSV string" do
|
|
117
|
+
player = Player.from_csv("larry,150")
|
|
118
|
+
|
|
119
|
+
player.name.should == "Larry"
|
|
120
|
+
player.health.should == 150
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
@treasure.name.should == :hammer
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "has a points attribute" do
|
|
15
|
+
@treasure.points.should == 50
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe TreasureTrove do
|
|
21
|
+
|
|
22
|
+
it "has six treasures" do
|
|
23
|
+
TreasureTrove::TREASURES.size.should == 6
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "has a pie worth 5 points" do
|
|
27
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "has a bottle worth 25 points" do
|
|
31
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "has a hammer worth 50 points" do
|
|
35
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "has a skillet worth 100 points" do
|
|
39
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "has a broomstick worth 200 points" do
|
|
43
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "has a crowbar worth 400 points" do
|
|
47
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "returns a random treasure" do
|
|
51
|
+
treasure = TreasureTrove.random
|
|
52
|
+
TreasureTrove::TREASURES.should include(treasure)
|
|
53
|
+
# or use alternate expectation syntax:
|
|
54
|
+
# expect(TreasureTrove::TREASURES).to include(treasure)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: my-studio_game
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Aymen Chetoui
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-04-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
|
+
email: aymen.chetoui@gmail.com
|
|
29
|
+
executables:
|
|
30
|
+
- studio_game
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- bin/high score file.txt
|
|
35
|
+
- bin/high_scores.txt
|
|
36
|
+
- bin/high_score_file.txt
|
|
37
|
+
- bin/my_favorite_players.csv
|
|
38
|
+
- bin/players.csv
|
|
39
|
+
- bin/studio_game
|
|
40
|
+
- bin/studio_game.rb
|
|
41
|
+
- lib/studio_game/auditable.rb
|
|
42
|
+
- lib/studio_game/berserk_player.rb
|
|
43
|
+
- lib/studio_game/clumsy_player.rb
|
|
44
|
+
- lib/studio_game/die.rb
|
|
45
|
+
- lib/studio_game/game.rb
|
|
46
|
+
- lib/studio_game/game_turn.rb
|
|
47
|
+
- lib/studio_game/iterators.rb
|
|
48
|
+
- lib/studio_game/loaded_die.rb
|
|
49
|
+
- lib/studio_game/playable.rb
|
|
50
|
+
- lib/studio_game/player.rb
|
|
51
|
+
- lib/studio_game/treasure_trove.rb
|
|
52
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
53
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
54
|
+
- spec/studio_game/game_spec.rb
|
|
55
|
+
- spec/studio_game/player_spec.rb
|
|
56
|
+
- spec/studio_game/treasure_trove_spec.rb
|
|
57
|
+
- LICENSE
|
|
58
|
+
- README
|
|
59
|
+
homepage: ''
|
|
60
|
+
licenses:
|
|
61
|
+
- MIT
|
|
62
|
+
metadata: {}
|
|
63
|
+
post_install_message:
|
|
64
|
+
rdoc_options: []
|
|
65
|
+
require_paths:
|
|
66
|
+
- lib
|
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - '>='
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '1.9'
|
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - '>='
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
requirements: []
|
|
78
|
+
rubyforge_project:
|
|
79
|
+
rubygems_version: 2.0.14
|
|
80
|
+
signing_key:
|
|
81
|
+
specification_version: 4
|
|
82
|
+
summary: ''
|
|
83
|
+
test_files:
|
|
84
|
+
- spec/studio_game/berserk_player_spec.rb
|
|
85
|
+
- spec/studio_game/clumsy_player_spec.rb
|
|
86
|
+
- spec/studio_game/game_spec.rb
|
|
87
|
+
- spec/studio_game/player_spec.rb
|
|
88
|
+
- spec/studio_game/treasure_trove_spec.rb
|