fysh-game 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +7 -0
- data/README +3 -0
- data/bin/high_scores.txt +9 -0
- data/bin/players.csv +6 -0
- data/bin/studio_game.rb +29 -0
- data/lib/studio_game/auditable.rb +9 -0
- data/lib/studio_game/berserk_player.rb +37 -0
- data/lib/studio_game/clumsy_player.rb +41 -0
- data/lib/studio_game/die.rb +21 -0
- data/lib/studio_game/game.rb +103 -0
- data/lib/studio_game/game_turn.rb +22 -0
- data/lib/studio_game/loaded_die.rb +18 -0
- data/lib/studio_game/playable.rb +19 -0
- data/lib/studio_game/player.rb +63 -0
- data/lib/studio_game/treasure_trove.rb +20 -0
- data/spec/studio_game/berserk_player_spec.rb +32 -0
- data/spec/studio_game/clumsy_player_spec.rb +47 -0
- data/spec/studio_game/game_spec.rb +67 -0
- data/spec/studio_game/player_spec.rb +122 -0
- data/spec/studio_game/treasure_trove_spec.rb +58 -0
- metadata +85 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (C) 2012 Brian Wherry
|
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
data/bin/high_scores.txt
ADDED
data/bin/players.csv
ADDED
data/bin/studio_game.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# studio_game.rb
|
3
|
+
|
4
|
+
require_relative '../lib/studio_game/game'
|
5
|
+
|
6
|
+
knuckleheads = StudioGame::Game.new("Knuckleheads")
|
7
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
8
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
9
|
+
clumsy = StudioGame::ClumsyPlayer.new('klutz', 105)
|
10
|
+
knuckleheads.add_player(clumsy)
|
11
|
+
|
12
|
+
rawr = StudioGame::BerserkPlayer.new('rawr', 75)
|
13
|
+
knuckleheads.add_player(rawr)
|
14
|
+
|
15
|
+
loop do
|
16
|
+
puts "\nHow many game rounds? ('quit' to exit)"
|
17
|
+
answer = gets.chomp.downcase
|
18
|
+
case answer
|
19
|
+
when /^\d+$/
|
20
|
+
knuckleheads.play(5)
|
21
|
+
when 'quit', 'exit'
|
22
|
+
knuckleheads.print_stats
|
23
|
+
break
|
24
|
+
else
|
25
|
+
puts 'Please enter a number or quit.'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
knuckleheads.save_high_scores
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# berserk_player.rb
|
2
|
+
|
3
|
+
require_relative 'player'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
class BerserkPlayer < Player
|
7
|
+
def initialize(name, health)
|
8
|
+
super(name, health)
|
9
|
+
@times_w00ted = 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def berserk?
|
13
|
+
@times_w00ted > 5
|
14
|
+
end
|
15
|
+
|
16
|
+
def w00t
|
17
|
+
super
|
18
|
+
@times_w00ted += 1
|
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
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if __FILE__ == $0
|
32
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
33
|
+
6.times { berserker.w00t }
|
34
|
+
2.times { berserker.blam }
|
35
|
+
puts berserker.health
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# clumsy_player.rb
|
2
|
+
|
3
|
+
require_relative 'player'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
class ClumsyPlayer < Player
|
7
|
+
|
8
|
+
attr_reader :w00t_bonus_factor
|
9
|
+
|
10
|
+
def initialize(name, health)
|
11
|
+
super(name, health)
|
12
|
+
@w00t_bonus_factor = 5
|
13
|
+
end
|
14
|
+
|
15
|
+
def found_treasure(treasure)
|
16
|
+
damaged_treasure = Treasure.new(treasure.name, treasure.points / 2)
|
17
|
+
super(damaged_treasure)
|
18
|
+
end
|
19
|
+
|
20
|
+
def w00t
|
21
|
+
@w00t_bonus_factor.times { super }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if __FILE__ == $0
|
26
|
+
clumsy = ClumsyPlayer.new("klutz", 100)
|
27
|
+
|
28
|
+
hammer = Treasure.new(:hammer, 50)
|
29
|
+
clumsy.found_treasure(hammer)
|
30
|
+
clumsy.found_treasure(hammer)
|
31
|
+
clumsy.found_treasure(hammer)
|
32
|
+
|
33
|
+
crowbar = Treasure.new(:crowbar, 400)
|
34
|
+
clumsy.found_treasure(crowbar)
|
35
|
+
|
36
|
+
clumsy.each_found_treasure do |treasure|
|
37
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
38
|
+
end
|
39
|
+
puts "#{clumsy.points} grand total points"
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# die.rb
|
2
|
+
|
3
|
+
require_relative 'auditable'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
class Die
|
7
|
+
include Auditable
|
8
|
+
|
9
|
+
attr_reader :number
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
roll
|
13
|
+
end
|
14
|
+
|
15
|
+
def roll
|
16
|
+
@number = rand(1..6)
|
17
|
+
audit
|
18
|
+
return @number
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# game.rb
|
2
|
+
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'game_turn'
|
5
|
+
require_relative 'treasure_trove'
|
6
|
+
require_relative 'clumsy_player'
|
7
|
+
require_relative 'berserk_player'
|
8
|
+
require 'csv'
|
9
|
+
|
10
|
+
module StudioGame
|
11
|
+
class Game
|
12
|
+
attr_reader :title
|
13
|
+
|
14
|
+
def initialize(title)
|
15
|
+
@title = title
|
16
|
+
@players = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_player(player)
|
20
|
+
@players << player
|
21
|
+
end
|
22
|
+
|
23
|
+
def load_players(filename)
|
24
|
+
CSV.foreach(filename) do |line|
|
25
|
+
player = Player.new(line[0], line[1].to_i)
|
26
|
+
add_player(player)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def save_high_scores(filename='high_scores.txt')
|
31
|
+
File.open(filename, 'w') do |file|
|
32
|
+
file.puts "Knuckleheads High Scores:"
|
33
|
+
@players.sort.each do |p|
|
34
|
+
file.puts high_score_format(p)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def print_name_and_health(player)
|
40
|
+
puts "#{player.name} (#{player.health})"
|
41
|
+
end
|
42
|
+
|
43
|
+
def total_points
|
44
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
45
|
+
end
|
46
|
+
|
47
|
+
def high_score_format(player)
|
48
|
+
formatted_name = player.name.ljust(20, '.')
|
49
|
+
"#{formatted_name} #{player.score}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def print_stats
|
53
|
+
puts "\n#{@title} Statistics:"
|
54
|
+
strong, wimpy = @players.partition { |p| p.strong? }
|
55
|
+
puts "\n#{strong.size} strong players:"
|
56
|
+
strong.each { |s| print_name_and_health(s) }
|
57
|
+
puts "\n#{wimpy.size} wimpy players:"
|
58
|
+
wimpy.each { |w| print_name_and_health(w) }
|
59
|
+
puts "\n#{title} High Scores:"
|
60
|
+
@players.sort.each do |p|
|
61
|
+
puts high_score_format(p)
|
62
|
+
end
|
63
|
+
|
64
|
+
@players.each do |player|
|
65
|
+
puts "\n#{player.name}'s point totals:"
|
66
|
+
|
67
|
+
player.each_found_treasure do |treasure|
|
68
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
69
|
+
end
|
70
|
+
|
71
|
+
puts "#{player.points} grand total points"
|
72
|
+
end
|
73
|
+
|
74
|
+
puts "\nTotal game points: #{total_points}"
|
75
|
+
end
|
76
|
+
|
77
|
+
def sorted_players
|
78
|
+
@players.sort { |a, b| b <=> a }
|
79
|
+
end
|
80
|
+
|
81
|
+
def play(rounds)
|
82
|
+
puts "There are #{@players.size} players in #{title}:"
|
83
|
+
@players.each { |player| puts player }
|
84
|
+
|
85
|
+
treasures = TreasureTrove::TREASURES
|
86
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
87
|
+
treasures.each do |treasure|
|
88
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
89
|
+
end
|
90
|
+
|
91
|
+
1.upto(rounds) do |round|
|
92
|
+
if block_given?
|
93
|
+
break if yield
|
94
|
+
end
|
95
|
+
puts "\nRound #{round}:"
|
96
|
+
@players.each do |player|
|
97
|
+
GameTurn.take_turn(player)
|
98
|
+
puts player
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# game_turn.rb
|
2
|
+
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'die'
|
5
|
+
require_relative 'loaded_die'
|
6
|
+
|
7
|
+
module StudioGame
|
8
|
+
module GameTurn
|
9
|
+
def self.take_turn(player)
|
10
|
+
die = Die.new
|
11
|
+
case die.roll
|
12
|
+
when 1..2
|
13
|
+
player.blam
|
14
|
+
when 3..4
|
15
|
+
puts "#{player.name} was skipped."
|
16
|
+
else
|
17
|
+
player.w00t
|
18
|
+
end
|
19
|
+
player.found_treasure(TreasureTrove.random)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# loaded_die.rb
|
2
|
+
|
3
|
+
require_relative 'auditable'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
class LoadedDie
|
7
|
+
include Auditable
|
8
|
+
|
9
|
+
attr_reader :number
|
10
|
+
|
11
|
+
def roll
|
12
|
+
numbers = [1, 1, 2, 5, 6, 6]
|
13
|
+
@number = numbers.sample
|
14
|
+
audit
|
15
|
+
@number
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# playable.rb
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
module Playable
|
5
|
+
def blam
|
6
|
+
self.health -= 10
|
7
|
+
puts "#{name} got blammed!"
|
8
|
+
end
|
9
|
+
|
10
|
+
def w00t
|
11
|
+
self.health += 15
|
12
|
+
puts "#{name} got w00ted!"
|
13
|
+
end
|
14
|
+
|
15
|
+
def strong?
|
16
|
+
self.health > 100
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# player.rb
|
2
|
+
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
require_relative 'playable'
|
5
|
+
|
6
|
+
module StudioGame
|
7
|
+
class Player
|
8
|
+
include Playable
|
9
|
+
|
10
|
+
attr_accessor :health
|
11
|
+
attr_accessor :name
|
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 to_s
|
20
|
+
"I'm #{@name} with health = #{@health}, points = #{points} and score = #{score}."
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.from_csv(line)
|
24
|
+
name, health = line.split(',')
|
25
|
+
new(name, Integer(health))
|
26
|
+
end
|
27
|
+
|
28
|
+
def <=>(other_player)
|
29
|
+
other_player.score <=> score
|
30
|
+
end
|
31
|
+
|
32
|
+
def score
|
33
|
+
@health + points
|
34
|
+
end
|
35
|
+
|
36
|
+
def found_treasure(treasure)
|
37
|
+
@found_treasures[treasure.name] += treasure.points
|
38
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
39
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def each_found_treasure
|
43
|
+
@found_treasures.each do |name, points|
|
44
|
+
treasure = Treasure.new(name, points)
|
45
|
+
yield treasure
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def points
|
50
|
+
@found_treasures.values.reduce(0, :+)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
if __FILE__ == $0
|
55
|
+
player = Player.new("moe")
|
56
|
+
puts player.name
|
57
|
+
puts player.health
|
58
|
+
player.w00t
|
59
|
+
puts player.health
|
60
|
+
player.blam
|
61
|
+
puts player.health
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# treasure_trove.rb
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
Treasure = Struct.new(:name, :points)
|
5
|
+
|
6
|
+
module TreasureTrove
|
7
|
+
TREASURES = [
|
8
|
+
Treasure.new(:pie, 5),
|
9
|
+
Treasure.new(:bottle, 25),
|
10
|
+
Treasure.new(:hammer, 50),
|
11
|
+
Treasure.new(:skillet, 100),
|
12
|
+
Treasure.new(:broomstick, 200),
|
13
|
+
Treasure.new(:crowbar, 400)
|
14
|
+
]
|
15
|
+
|
16
|
+
def self.random
|
17
|
+
TREASURES.sample
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# berserk_player_spec.rb
|
2
|
+
|
3
|
+
require 'studio_game/berserk_player'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe BerserkPlayer do
|
7
|
+
|
8
|
+
before do
|
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_false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "goes berserk when w00ted more than 5 times" do
|
20
|
+
1.upto(6) { @player.w00t }
|
21
|
+
|
22
|
+
@player.berserk?.should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
26
|
+
1.upto(6) { @player.w00t }
|
27
|
+
1.upto(2) { @player.blam }
|
28
|
+
|
29
|
+
@player.health.should == @initial_health + (8 * 15)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# clumsy_player_spec.rb
|
2
|
+
|
3
|
+
require 'studio_game/clumsy_player'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe ClumsyPlayer do
|
7
|
+
before do
|
8
|
+
@initial_health = 100
|
9
|
+
@player = ClumsyPlayer.new("klutz", @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "only gets half the point value for each treasure" do
|
13
|
+
@player.points.should == 0
|
14
|
+
|
15
|
+
hammer = Treasure.new(:hammer, 50)
|
16
|
+
@player.found_treasure(hammer)
|
17
|
+
@player.found_treasure(hammer)
|
18
|
+
@player.found_treasure(hammer)
|
19
|
+
|
20
|
+
@player.points.should == 75
|
21
|
+
|
22
|
+
crowbar = Treasure.new(:crowbar, 400)
|
23
|
+
@player.found_treasure(crowbar)
|
24
|
+
|
25
|
+
@player.points.should == 275
|
26
|
+
|
27
|
+
yielded = []
|
28
|
+
@player.each_found_treasure do |treasure|
|
29
|
+
yielded << treasure
|
30
|
+
end
|
31
|
+
|
32
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should have a woot bonus factor" do
|
36
|
+
@player.w00t_bonus_factor.should == 5
|
37
|
+
end
|
38
|
+
|
39
|
+
it "gets five times the benefits of w00ts" do
|
40
|
+
@player.health.should == 100
|
41
|
+
|
42
|
+
@player.w00t
|
43
|
+
|
44
|
+
@player.health.should == 175
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# game_spec.rb
|
2
|
+
|
3
|
+
require 'studio_game/game'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe Game do
|
7
|
+
before do
|
8
|
+
@game = Game.new("Knuckleheads")
|
9
|
+
|
10
|
+
@initial_health = 100
|
11
|
+
@player = Player.new("moe", @initial_health)
|
12
|
+
|
13
|
+
@game.add_player(@player)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "w00ts a player when a high number is rolled" do
|
17
|
+
Die.any_instance.stub(:roll).and_return(5)
|
18
|
+
|
19
|
+
@game.play(2)
|
20
|
+
|
21
|
+
@player.health.should == @initial_health + (15 * 2)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "skips a player when a medium number is rolled" do
|
25
|
+
Die.any_instance.stub(:roll).and_return(3)
|
26
|
+
|
27
|
+
@game.play(2)
|
28
|
+
|
29
|
+
@player.health.should == @initial_health
|
30
|
+
end
|
31
|
+
|
32
|
+
it "blams a player when a low number is rolled" do
|
33
|
+
Die.any_instance.stub(:roll).and_return(1)
|
34
|
+
|
35
|
+
@game.play(2)
|
36
|
+
|
37
|
+
@player.health.should == @initial_health - (10 * 2)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "assigns a treasure for points during a player's turn" do
|
41
|
+
game = Game.new("Knuckleheads")
|
42
|
+
player = Player.new("moe")
|
43
|
+
|
44
|
+
game.add_player(player)
|
45
|
+
|
46
|
+
game.play(1)
|
47
|
+
|
48
|
+
player.points.should_not be_zero
|
49
|
+
end
|
50
|
+
|
51
|
+
it "computes total points as the sum of all player points" do
|
52
|
+
game = Game.new("Knuckleheads")
|
53
|
+
|
54
|
+
player1 = Player.new("moe")
|
55
|
+
player2 = Player.new("larry")
|
56
|
+
|
57
|
+
game.add_player(player1)
|
58
|
+
game.add_player(player2)
|
59
|
+
|
60
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
61
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
62
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
63
|
+
|
64
|
+
game.total_points.should == 500
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# player_spec.rb
|
2
|
+
|
3
|
+
require 'studio_game/player'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe Player do
|
7
|
+
before do
|
8
|
+
@initial_health = 150
|
9
|
+
@player = Player.new('larry', @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "has a capitalized name" do
|
13
|
+
@player.name.should == "Larry"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has an initial health" do
|
17
|
+
@player.health.should == 150
|
18
|
+
end
|
19
|
+
|
20
|
+
it "has a string representation" do
|
21
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 0 and score = 150."
|
22
|
+
end
|
23
|
+
|
24
|
+
it "computes a score as the sum of its health and points" do
|
25
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
26
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
27
|
+
|
28
|
+
@player.score.should == 250
|
29
|
+
end
|
30
|
+
|
31
|
+
it "increases health by 15 when w00ted" do
|
32
|
+
@player.w00t
|
33
|
+
|
34
|
+
@player.health.should == @initial_health + 15
|
35
|
+
end
|
36
|
+
|
37
|
+
it "decreases health by 10 when blammed" do
|
38
|
+
@player.blam
|
39
|
+
|
40
|
+
@player.health.should == @initial_health - 10
|
41
|
+
end
|
42
|
+
|
43
|
+
context "has a health attribute of over 100" do
|
44
|
+
before do
|
45
|
+
@player = Player.new('larry', 150)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should be a 'strong' player" do
|
49
|
+
@player.should be_strong
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "has a health attribute of 100 or below" do
|
54
|
+
before do
|
55
|
+
@player = Player.new('larry', 100)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should not be 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
|
+
|
80
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
81
|
+
|
82
|
+
@player.points.should == 50
|
83
|
+
|
84
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
85
|
+
|
86
|
+
@player.points.should == 450
|
87
|
+
|
88
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
89
|
+
|
90
|
+
@player.points.should == 500
|
91
|
+
end
|
92
|
+
|
93
|
+
it "yields each found treasure and its total points" do
|
94
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
95
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
96
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
97
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
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
|
+
|
103
|
+
yielded = []
|
104
|
+
@player.each_found_treasure do |treasure|
|
105
|
+
yielded << treasure
|
106
|
+
end
|
107
|
+
|
108
|
+
yielded.should == [
|
109
|
+
Treasure.new(:skillet, 200),
|
110
|
+
Treasure.new(:hammer, 50),
|
111
|
+
Treasure.new(:bottle, 25)
|
112
|
+
]
|
113
|
+
end
|
114
|
+
|
115
|
+
it "can be created from csv" do
|
116
|
+
player = Player.from_csv('Joe,100')
|
117
|
+
|
118
|
+
player.name.should == 'Joe'
|
119
|
+
player.health.should == 100
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# treasure_trove_spec.rb
|
2
|
+
|
3
|
+
require 'studio_game/treasure_trove'
|
4
|
+
|
5
|
+
module StudioGame
|
6
|
+
describe Treasure do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@treasure = Treasure.new(:hammer, 50)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "has a name attribute" do
|
13
|
+
@treasure.name.should == :hammer
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has a points attribute" do
|
17
|
+
@treasure.points.should == 50
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
describe TreasureTrove do
|
23
|
+
|
24
|
+
it "has six treasures" do
|
25
|
+
TreasureTrove::TREASURES.size.should == 6
|
26
|
+
end
|
27
|
+
|
28
|
+
it "has a pie worth 5 points" do
|
29
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "has a bottle worth 25 points" do
|
33
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "has a hammer worth 50 points" do
|
37
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "has a skillet worth 100 points" do
|
41
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "has a broomstick worth 200 points" do
|
45
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "has a crowbar worth 400 points" do
|
49
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns a random treasure" do
|
53
|
+
treasure = TreasureTrove.random
|
54
|
+
|
55
|
+
TreasureTrove::TREASURES.should include(treasure)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fysh-game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kryptyk Fysh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &19676856 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *19676856
|
25
|
+
description: ! 'A game package produced through the excellent Pragmatic Studio Ruby
|
26
|
+
Programming Course.
|
27
|
+
|
28
|
+
|
29
|
+
Use ''studio_game'' to run with an optional argument of a CSV file containing players.'
|
30
|
+
email: kryptykfysh@kryptykfysh.co.uk
|
31
|
+
executables:
|
32
|
+
- studio_game.rb
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- bin/high_scores.txt
|
37
|
+
- bin/players.csv
|
38
|
+
- bin/studio_game.rb
|
39
|
+
- lib/studio_game/auditable.rb
|
40
|
+
- lib/studio_game/berserk_player.rb
|
41
|
+
- lib/studio_game/clumsy_player.rb
|
42
|
+
- lib/studio_game/die.rb
|
43
|
+
- lib/studio_game/game.rb
|
44
|
+
- lib/studio_game/game_turn.rb
|
45
|
+
- lib/studio_game/loaded_die.rb
|
46
|
+
- lib/studio_game/playable.rb
|
47
|
+
- lib/studio_game/player.rb
|
48
|
+
- lib/studio_game/treasure_trove.rb
|
49
|
+
- spec/studio_game/berserk_player_spec.rb
|
50
|
+
- spec/studio_game/clumsy_player_spec.rb
|
51
|
+
- spec/studio_game/game_spec.rb
|
52
|
+
- spec/studio_game/player_spec.rb
|
53
|
+
- spec/studio_game/treasure_trove_spec.rb
|
54
|
+
- LICENSE
|
55
|
+
- README
|
56
|
+
homepage:
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.9'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.16
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: A tutorial game in Ruby
|
80
|
+
test_files:
|
81
|
+
- spec/studio_game/berserk_player_spec.rb
|
82
|
+
- spec/studio_game/clumsy_player_spec.rb
|
83
|
+
- spec/studio_game/game_spec.rb
|
84
|
+
- spec/studio_game/player_spec.rb
|
85
|
+
- spec/studio_game/treasure_trove_spec.rb
|