bagodonuts_studio_game 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2013 James P. Berrettini
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,4 @@
1
+ Bagodonuts's Studio Game is a simple game that users can play by running the command
2
+ $ studio_game [filename]
3
+
4
+ studio_game takes a filename as an optional argument that has a comma-separated file describing initial values for players' names and their health, e.g., "Barney,150".
@@ -0,0 +1,3 @@
1
+ Alvin,100
2
+ Simon,60
3
+ Theo,125
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/studio_game/player'
3
+ require_relative '../lib/studio_game/game'
4
+ require_relative '../lib/studio_game/berserk_player'
5
+
6
+
7
+ knuckleheads = StudioGame::Game.new("Knuckleheads")
8
+
9
+ default_filename = File.join(File.dirname(__FILE__), 'players.csv')
10
+ knuckleheads.load_players(ARGV.shift || default_filename)
11
+ berserker = StudioGame::BerserkPlayer.new("berserker", 50)
12
+ knuckleheads.add_player(berserker)
13
+ loop do
14
+ puts "How many game rounds? ('quit' to exit)"
15
+ response = gets.chomp.downcase
16
+ case response
17
+ when 'quit', 'exit'
18
+ knuckleheads.print_stats
19
+ break
20
+ when /^\d+$/
21
+ knuckleheads.play(response.to_i)
22
+ else
23
+ puts "Please enter a number or 'quit'"
24
+ end
25
+ end
26
+
27
+ knuckleheads.save_high_scores
28
+
29
+
30
+
@@ -0,0 +1,29 @@
1
+ require_relative '../lib/studio_game/player'
2
+ require_relative '../lib/studio_game/game'
3
+ require_relative '../lib/studio_game/berserk_player'
4
+
5
+
6
+ knuckleheads = StudioGame::Game.new("Knuckleheads")
7
+
8
+ default_filename = File.join(File.dirname(__FILE__), 'players.csv')
9
+ knuckleheads.load_players(ARGV.shift || default_filename)
10
+ berserker = StudioGame::BerserkPlayer.new("berserker", 50)
11
+ knuckleheads.add_player(berserker)
12
+ loop do
13
+ puts "How many game rounds? ('quit' to exit)"
14
+ response = gets.chomp.downcase
15
+ case response
16
+ when 'quit', 'exit'
17
+ knuckleheads.print_stats
18
+ break
19
+ when /^\d+$/
20
+ knuckleheads.play(response.to_i)
21
+ else
22
+ puts "Please enter a number or 'quit'"
23
+ end
24
+ end
25
+
26
+ knuckleheads.save_high_scores
27
+
28
+
29
+
@@ -0,0 +1,7 @@
1
+ module StudioGame
2
+ module Auditable
3
+ def audit
4
+ puts "Rolled a #{@number} (#{self.class})"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'player'
2
+
3
+ module StudioGame
4
+ class BerserkPlayer < Player
5
+ def initialize(name, initial_health)
6
+ super(name, initial_health)
7
+ @w00t_count = 0
8
+ end
9
+ def w00t
10
+ super
11
+ @w00t_count += 1
12
+ if berserk? then
13
+ puts "#{name} is berserk!"
14
+ end
15
+ end
16
+ def berserk?
17
+ @w00t_count > 5
18
+ end
19
+ def blam
20
+ berserk? ? w00t : super
21
+ end
22
+ end
23
+
24
+ if __FILE__ == $0
25
+ berserker = BerserkPlayer.new("berserker", 50)
26
+ 6.times { berserker.w00t }
27
+ 2.times { berserker.blam }
28
+ puts berserker.health
29
+ end
30
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'player'
2
+ require_relative 'treasure_trove'
3
+
4
+ module StudioGame
5
+ class ClumsyPlayer < Player
6
+
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
+ super(Treasure.new(treasure.name, treasure.points / 2.0))
14
+ end
15
+
16
+ def w00t
17
+ @boost_factor.times { super }
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ if __FILE__ == $0
24
+ clumsy = ClumsyPlayer.new("klutz")
25
+
26
+ hammer = Treasure.new(:hammer, 50)
27
+ clumsy.found_treasure(hammer)
28
+ clumsy.found_treasure(hammer)
29
+ clumsy.found_treasure(hammer)
30
+
31
+ crowbar = Treasure.new(:crowbar, 400)
32
+ clumsy.found_treasure(crowbar)
33
+
34
+ clumsy.each_found_treasure do |treasure|
35
+ puts "#{treasure.points} total #{treasure.name} points"
36
+ end
37
+ puts "#{clumsy.points} grand total points"
38
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'auditable'
2
+
3
+ module StudioGame
4
+ class Die
5
+ include Auditable
6
+ attr_reader :number
7
+
8
+ def initialize
9
+ roll
10
+ end
11
+
12
+ def roll
13
+ @number = rand(1..6)
14
+ audit
15
+ @number
16
+ end
17
+ end
18
+ end
19
+
20
+ if __FILE__ == $0
21
+ die = Die.new
22
+ puts die.roll
23
+ puts die.roll
24
+ puts die.roll
25
+ end
@@ -0,0 +1,114 @@
1
+ require_relative 'player'
2
+ require_relative 'game_turn'
3
+ require_relative 'treasure_trove'
4
+ require 'csv'
5
+
6
+ module StudioGame
7
+ class Game
8
+
9
+ attr_accessor :title
10
+
11
+ def initialize(title)
12
+ @title = title
13
+ @players = []
14
+ end
15
+
16
+ def add_player(a_player)
17
+ @players.push(a_player)
18
+ end
19
+
20
+ def play(rounds)
21
+ puts "There are #{@players.size} players in #{@title}: "
22
+
23
+ @players.each do |player|
24
+ puts player
25
+ end
26
+
27
+ treasures = TreasureTrove::TREASURES
28
+ puts "\nThere are #{treasures.size} treasures to be found:"
29
+
30
+ treasures.each do |treasure|
31
+ puts "A #{treasure.name} is worth #{treasure.points}"
32
+ end
33
+
34
+ 1.upto(rounds) do |round|
35
+ if block_given? then
36
+ if yield then
37
+ puts "Game over, dude"
38
+ break
39
+ end
40
+ end
41
+ puts "\nRound #{round}:"
42
+ @players.each do |player|
43
+ GameTurn.take_turn(player)
44
+ puts player
45
+ end
46
+ end
47
+ end
48
+
49
+ def print_name_and_health(player)
50
+ puts "#{player.name} (#{player.health})"
51
+ end
52
+
53
+ def print_stats
54
+ puts "\n#{@title} Statistics:"
55
+
56
+ strong_players, wimpy_players = @players.partition { |player| player.strong? }
57
+
58
+ puts "\n#{strong_players.size} strong players:"
59
+ strong_players.each do |player|
60
+ print_name_and_health(player)
61
+ end
62
+
63
+ puts "\n#{wimpy_players.size} wimpy players:"
64
+ wimpy_players.each do |player|
65
+ print_name_and_health(player)
66
+ end
67
+
68
+ high_scores do |line|
69
+ puts line
70
+ end
71
+
72
+ @players.sort.each do |player|
73
+ puts "\n#{player.name}'s total points:"
74
+
75
+ player.each_found_treasure do |treasure|
76
+ puts "#{treasure.points} total #{treasure.name} points"
77
+ end
78
+
79
+ puts "#{player.points} grand total points"
80
+ end
81
+ puts "\n#{total_points} total points from treasures found"
82
+ end
83
+
84
+ def total_points
85
+ @players.reduce(0) {|sum, player| sum + player.points}
86
+ end
87
+
88
+ def load_players(filename)
89
+ CSV.foreach(filename) do |player_data|
90
+ add_player(Player.new(player_data[0], player_data[1].to_i))
91
+ end
92
+ end
93
+
94
+ def save_high_scores(filename = 'high_scores.txt')
95
+ File.open(filename, 'w') do |file|
96
+ high_scores do |line|
97
+ file.puts(line)
98
+ end
99
+ end
100
+ end
101
+
102
+ def high_scores
103
+ yield "#{@title} High Scores:"
104
+ @players.sort.each do |player|
105
+ yield high_score_entry(player)
106
+ end
107
+ end
108
+
109
+ def high_score_entry(player)
110
+ formatted_name = player.name.ljust(20, '.')
111
+ "#{formatted_name} #{player.score}"
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,30 @@
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
+ treasure = find_treasure
18
+ player.found_treasure(treasure)
19
+ end
20
+
21
+ def self.find_treasure
22
+ TreasureTrove.random
23
+ end
24
+ end
25
+ end
26
+
27
+ if __FILE__ == $0
28
+ player = Player.new("curly", 125)
29
+ GameTurn.take_turn(player)
30
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'auditable'
2
+
3
+ module StudioGame
4
+ class LoadedDie
5
+ include Auditable
6
+ attr_reader :number
7
+
8
+ def roll
9
+ numbers = [1, 1, 2, 5, 6, 6]
10
+ @number = numbers.sample
11
+ audit
12
+ @number
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module StudioGame
2
+ module Playable
3
+ def blam
4
+ self.health -= 10
5
+ puts "#{name} got blammed!"
6
+ end
7
+
8
+ def w00t
9
+ self.health += 15
10
+ puts "#{name} got w00ted!"
11
+ end
12
+
13
+ def strong?
14
+ health > 100
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,56 @@
1
+ require_relative 'playable'
2
+
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+ attr_accessor :name
7
+ attr_accessor :health
8
+
9
+ def initialize(name, health=100)
10
+ @name = name.capitalize
11
+ @health = health
12
+ @found_treasures = Hash.new(0)
13
+ end
14
+
15
+ def to_s
16
+ "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
17
+ end
18
+
19
+ def score
20
+ @health + points
21
+ end
22
+
23
+ def found_treasure(treasure)
24
+ @found_treasures[treasure.name] += treasure.points
25
+ end
26
+
27
+ def <=>(other)
28
+ other.score <=> score
29
+ end
30
+
31
+ def points
32
+ @found_treasures.values.reduce(0,:+)
33
+ end
34
+
35
+ def each_found_treasure
36
+ @found_treasures.each do |key, value|
37
+ treasure = Treasure.new(key, value)
38
+ yield treasure
39
+ end
40
+ end
41
+ def self.from_csv(csv_line)
42
+ name, initial_health = csv_line.split(',')
43
+ initial_health = Integer(initial_health)
44
+ Player.new(name, initial_health)
45
+ end
46
+ end
47
+ end
48
+ if __FILE__ == $0
49
+ player = Player.new("moe")
50
+ puts player.name
51
+ puts player.health
52
+ player.w00t
53
+ puts player.health
54
+ player.blam
55
+ puts player.health
56
+ end
@@ -0,0 +1,20 @@
1
+ module StudioGame
2
+ Treasure = Struct.new(:name, :points)
3
+
4
+ module TreasureTrove
5
+
6
+ TREASURES = [
7
+ Treasure.new(:pie, 5),
8
+ Treasure.new(:bottle, 25),
9
+ Treasure.new(:hammer, 50),
10
+ Treasure.new(:skillet, 100),
11
+ Treasure.new(:broomstick, 200),
12
+ Treasure.new(:crowbar, 400)
13
+ ]
14
+
15
+ def self.random
16
+ TREASURES.sample
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ require 'studio_game/berserk_player'
2
+
3
+ module StudioGame
4
+ describe BerserkPlayer do
5
+ before 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
+
13
+ @player.berserk?.should be_false
14
+ end
15
+
16
+ it "goes berserk when w00ted more than 5 times" do
17
+ 1.upto(6) { @player.w00t }
18
+
19
+ @player.berserk?.should be_true
20
+ end
21
+
22
+ it "gets w00ted instead of blammed when it's gone berserk" do
23
+ 1.upto(6) { @player.w00t }
24
+ 1.upto(2) { @player.blam }
25
+
26
+ @player.health.should == @initial_health + (8 * 15)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'berserk_player'
2
+
3
+ module StudioGame
4
+ describe BerserkPlayer do
5
+ before 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
+
13
+ @player.berserk?.should be_false
14
+ end
15
+
16
+ it "goes berserk when w00ted more than 5 times" do
17
+ 1.upto(6) { @player.w00t }
18
+
19
+ @player.berserk?.should be_true
20
+ end
21
+
22
+ it "gets w00ted instead of blammed when it's gone berserk" do
23
+ 1.upto(6) { @player.w00t }
24
+ 1.upto(2) { @player.blam }
25
+
26
+ @player.health.should == @initial_health + (8 * 15)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,38 @@
1
+ require 'studio_game/clumsy_player'
2
+
3
+ module StudioGame
4
+ describe ClumsyPlayer do
5
+ before do
6
+ @initial_health = 100
7
+ @boost_factor = 2
8
+ @player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
9
+ end
10
+
11
+ it "damages every treasure it has so that all treasures are worth half their point value" 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 == (50 * 3) / 2
20
+
21
+ crowbar = Treasure.new(:crowbar, 400)
22
+ @player.found_treasure(crowbar)
23
+
24
+ @player.points.should == ((50 * 3) + 400) / 2
25
+
26
+ yielded = []
27
+ @player.each_found_treasure do |treasure|
28
+ yielded << treasure
29
+ end
30
+ yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
31
+ end
32
+
33
+ it "multiplies every w00t by the boost factor" do
34
+ @player.w00t
35
+ @player.health.should == @initial_health + (15 * @boost_factor)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'clumsy_player'
2
+
3
+ module StudioGame
4
+ describe ClumsyPlayer do
5
+ before do
6
+ @initial_health = 100
7
+ @boost_factor = 2
8
+ @player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
9
+ end
10
+
11
+ it "damages every treasure it has so that all treasures are worth half their point value" 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 == (50 * 3) / 2
20
+
21
+ crowbar = Treasure.new(:crowbar, 400)
22
+ @player.found_treasure(crowbar)
23
+
24
+ @player.points.should == ((50 * 3) + 400) / 2
25
+
26
+ yielded = []
27
+ @player.each_found_treasure do |treasure|
28
+ yielded << treasure
29
+ end
30
+ yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
31
+ end
32
+
33
+ it "multiplies every w00t by the boost factor" do
34
+ @player.w00t
35
+ @player.health.should == @initial_health + (15 * @boost_factor)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,91 @@
1
+ require 'studio_game/game'
2
+ require 'csv'
3
+
4
+ module StudioGame
5
+ describe Game do
6
+
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 "has a title" do
17
+ @game.title.should == "Knuckleheads"
18
+ end
19
+
20
+ it "w00ts the player if a high number is rolled" do
21
+ Die.any_instance.stub(:roll).and_return(5)
22
+
23
+ @game.play(2)
24
+
25
+ @player.health.should == @initial_health + (15 * 2)
26
+ end
27
+
28
+ it "skips the player if a medium number is rolled" do
29
+ Die.any_instance.stub(:roll).and_return(3)
30
+
31
+ @game.play(2)
32
+
33
+ @player.health.should == @initial_health
34
+ end
35
+
36
+ it "blams the player if a low number is rolled" do
37
+ Die.any_instance.stub(:roll).and_return(1)
38
+
39
+ @game.play(2)
40
+
41
+ @player.health.should == @initial_health - (10 * 2)
42
+ end
43
+
44
+ it "assigns a treasure for points during a player's turn" do
45
+ #game = Game.new("Knuckleheads")
46
+ #player = Player.new("moe")
47
+
48
+ #game.add_player(player)
49
+
50
+ @game.play(1)
51
+
52
+ @player.points.should_not be_zero
53
+ end
54
+
55
+ it "computes total points as the sum of all player points" do
56
+ game = Game.new("Knuckleheads")
57
+
58
+ player1 = Player.new("moe")
59
+ player2 = Player.new("larry")
60
+
61
+ game.add_player(player1)
62
+ game.add_player(player2)
63
+
64
+ player1.found_treasure(Treasure.new(:hammer, 50))
65
+ player1.found_treasure(Treasure.new(:hammer, 50))
66
+ player2.found_treasure(Treasure.new(:crowbar, 400))
67
+
68
+ game.total_points.should == 500
69
+ player1.found_treasure(Treasure.new(:broomstick, 200))
70
+ game.total_points.should == 700
71
+ end
72
+ it "preempt playing a round of the game when the condition in a supplied block returns true" do
73
+ @game.play(1) {true}
74
+ @player.points.should be_zero
75
+ end
76
+
77
+ it "continuing playing a round of the game when the condition in a supplied block returns false" do
78
+ @game.play(1) {false}
79
+ @player.points.should_not be_zero
80
+ end
81
+
82
+ let(:data) { "Mr. CSV,50\nMadame CSV,250"}
83
+ #let(:result) {[["Mr. CSV, 50"]] }
84
+
85
+ it "loads player data from a csv file" do
86
+ # game = Game.new("CSV Test Game")
87
+ # File.stub(:open).with("filename",{:universal_newline=>false}) { StringIO.new(data) }
88
+ # puts game.players
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,91 @@
1
+ require_relative 'game'
2
+ require 'csv'
3
+
4
+ module StudioGame
5
+ describe Game do
6
+
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 "has a title" do
17
+ @game.title.should == "Knuckleheads"
18
+ end
19
+
20
+ it "w00ts the player if a high number is rolled" do
21
+ Die.any_instance.stub(:roll).and_return(5)
22
+
23
+ @game.play(2)
24
+
25
+ @player.health.should == @initial_health + (15 * 2)
26
+ end
27
+
28
+ it "skips the player if a medium number is rolled" do
29
+ Die.any_instance.stub(:roll).and_return(3)
30
+
31
+ @game.play(2)
32
+
33
+ @player.health.should == @initial_health
34
+ end
35
+
36
+ it "blams the player if a low number is rolled" do
37
+ Die.any_instance.stub(:roll).and_return(1)
38
+
39
+ @game.play(2)
40
+
41
+ @player.health.should == @initial_health - (10 * 2)
42
+ end
43
+
44
+ it "assigns a treasure for points during a player's turn" do
45
+ #game = Game.new("Knuckleheads")
46
+ #player = Player.new("moe")
47
+
48
+ #game.add_player(player)
49
+
50
+ @game.play(1)
51
+
52
+ @player.points.should_not be_zero
53
+ end
54
+
55
+ it "computes total points as the sum of all player points" do
56
+ game = Game.new("Knuckleheads")
57
+
58
+ player1 = Player.new("moe")
59
+ player2 = Player.new("larry")
60
+
61
+ game.add_player(player1)
62
+ game.add_player(player2)
63
+
64
+ player1.found_treasure(Treasure.new(:hammer, 50))
65
+ player1.found_treasure(Treasure.new(:hammer, 50))
66
+ player2.found_treasure(Treasure.new(:crowbar, 400))
67
+
68
+ game.total_points.should == 500
69
+ player1.found_treasure(Treasure.new(:broomstick, 200))
70
+ game.total_points.should == 700
71
+ end
72
+ it "preempt playing a round of the game when the condition in a supplied block returns true" do
73
+ @game.play(1) {true}
74
+ @player.points.should be_zero
75
+ end
76
+
77
+ it "continuing playing a round of the game when the condition in a supplied block returns false" do
78
+ @game.play(1) {false}
79
+ @player.points.should_not be_zero
80
+ end
81
+
82
+ let(:data) { "Mr. CSV,50\nMadame CSV,250"}
83
+ #let(:result) {[["Mr. CSV, 50"]] }
84
+
85
+ it "loads player data from a csv file" do
86
+ # game = Game.new("CSV Test Game")
87
+ # File.stub(:open).with("filename",{:universal_newline=>false}) { StringIO.new(data) }
88
+ # puts game.players
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,138 @@
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
+ 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.found_treasure(Treasure.new(:hammer, 50))
22
+ @player.found_treasure(Treasure.new(:hammer, 50))
23
+
24
+
25
+ @player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
26
+ end
27
+
28
+ it "computes a score as the sum of its health and points" do
29
+ @player.found_treasure(Treasure.new(:hammer, 50))
30
+ @player.found_treasure(Treasure.new(:hammer, 50))
31
+
32
+ @player.score.should == 250
33
+ end
34
+
35
+ it "increases health by 15 when w00ted" do
36
+ @player.w00t
37
+
38
+ @player.health.should == @initial_health + 15
39
+ end
40
+
41
+ it "decreases health by 10 when blammed" do
42
+ @player.blam
43
+
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
+
86
+ it "creates a player from a csv formatted string" do
87
+ player = Player.from_csv("Fred,200")
88
+ player.name.should == "Fred"
89
+ player.health.should == 200
90
+ end
91
+
92
+ context "created with a default health" do
93
+ before do
94
+ @player = Player.new("larry")
95
+ end
96
+
97
+ it "has a health of 100" do
98
+ @player.health.should == 100
99
+ end
100
+ end
101
+
102
+ context "with a health greater than 100" do
103
+ before do
104
+ @player = Player.new("larry", 150)
105
+ end
106
+
107
+ it "is strong" do
108
+ @player.should be_strong
109
+ end
110
+ end
111
+
112
+ context "with a health of 100 or less" do
113
+ before do
114
+ @player = Player.new("larry", 100)
115
+ end
116
+
117
+ it "is wimpy" do
118
+ @player.should_not be_strong
119
+ end
120
+ end
121
+
122
+ context "in a collection of players" do
123
+ before do
124
+ @player1 = Player.new("moe", 100)
125
+ @player2 = Player.new("larry", 200)
126
+ @player3 = Player.new("curly", 300)
127
+
128
+ @players = [@player1, @player2, @player3]
129
+ end
130
+
131
+ it "is sorted by decreasing score" do
132
+ @players.sort.should == [@player3, @player2, @player1]
133
+ end
134
+
135
+ end
136
+
137
+ end
138
+ end
@@ -0,0 +1,138 @@
1
+ require_relative 'player'
2
+ require_relative '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
+ 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.found_treasure(Treasure.new(:hammer, 50))
22
+ @player.found_treasure(Treasure.new(:hammer, 50))
23
+
24
+
25
+ @player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
26
+ end
27
+
28
+ it "computes a score as the sum of its health and points" do
29
+ @player.found_treasure(Treasure.new(:hammer, 50))
30
+ @player.found_treasure(Treasure.new(:hammer, 50))
31
+
32
+ @player.score.should == 250
33
+ end
34
+
35
+ it "increases health by 15 when w00ted" do
36
+ @player.w00t
37
+
38
+ @player.health.should == @initial_health + 15
39
+ end
40
+
41
+ it "decreases health by 10 when blammed" do
42
+ @player.blam
43
+
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
+
86
+ it "creates a player from a csv formatted string" do
87
+ player = Player.from_csv("Fred,200")
88
+ player.name.should == "Fred"
89
+ player.health.should == 200
90
+ end
91
+
92
+ context "created with a default health" do
93
+ before do
94
+ @player = Player.new("larry")
95
+ end
96
+
97
+ it "has a health of 100" do
98
+ @player.health.should == 100
99
+ end
100
+ end
101
+
102
+ context "with a health greater than 100" do
103
+ before do
104
+ @player = Player.new("larry", 150)
105
+ end
106
+
107
+ it "is strong" do
108
+ @player.should be_strong
109
+ end
110
+ end
111
+
112
+ context "with a health of 100 or less" do
113
+ before do
114
+ @player = Player.new("larry", 100)
115
+ end
116
+
117
+ it "is wimpy" do
118
+ @player.should_not be_strong
119
+ end
120
+ end
121
+
122
+ context "in a collection of players" do
123
+ before do
124
+ @player1 = Player.new("moe", 100)
125
+ @player2 = Player.new("larry", 200)
126
+ @player3 = Player.new("curly", 300)
127
+
128
+ @players = [@player1, @player2, @player3]
129
+ end
130
+
131
+ it "is sorted by decreasing score" do
132
+ @players.sort.should == [@player3, @player2, @player1]
133
+ end
134
+
135
+ end
136
+
137
+ end
138
+ end
@@ -0,0 +1,57 @@
1
+ require 'studio_game/treasure_trove'
2
+
3
+ module StudioGame
4
+ describe Treasure do
5
+
6
+ before do
7
+ @treasure = Treasure.new(:hammer, 50)
8
+ end
9
+
10
+ it "has a name attribute" do
11
+ @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
+
53
+ TreasureTrove::TREASURES.should include(treasure)
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,57 @@
1
+ require_relative '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
+
53
+ TreasureTrove::TREASURES.should include(treasure)
54
+ end
55
+
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bagodonuts_studio_game
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jim Berrettini
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! "Bagodonuts's Studio Game is a simple game that users can play by running
31
+ the command \n$ studio_game [filename]\n\nstudio_game takes a filename as an optional
32
+ argument that has a comma-separated file describing initial values for players'
33
+ names and their health, e.g., \"Barney,150\"."
34
+ email: jpb@alum.mit.edu
35
+ executables:
36
+ - studio_game
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - bin/players.csv
41
+ - bin/studio_game
42
+ - bin/studio_game.rb~
43
+ - lib/studio_game/auditable.rb
44
+ - lib/studio_game/berserk_player.rb
45
+ - lib/studio_game/clumsy_player.rb
46
+ - lib/studio_game/die.rb
47
+ - lib/studio_game/game.rb
48
+ - lib/studio_game/game_turn.rb
49
+ - lib/studio_game/loaded_die.rb
50
+ - lib/studio_game/playable.rb
51
+ - lib/studio_game/player.rb
52
+ - lib/studio_game/treasure_trove.rb
53
+ - spec/studio_game/berserk_player_spec.rb
54
+ - spec/studio_game/berserk_player_spec.rb~
55
+ - spec/studio_game/clumsy_player_spec.rb
56
+ - spec/studio_game/clumsy_player_spec.rb~
57
+ - spec/studio_game/game_spec.rb
58
+ - spec/studio_game/game_spec.rb~
59
+ - spec/studio_game/player_spec.rb
60
+ - spec/studio_game/player_spec.rb~
61
+ - spec/studio_game/treasure_trove_spec.rb
62
+ - spec/studio_game/treasure_trove_spec.rb~
63
+ - LICENSE
64
+ - README
65
+ homepage: http://www.timeinc.com
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '1.9'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.25
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Studio Game gem for Pragmatic Studio's Ruby course
89
+ test_files:
90
+ - spec/studio_game/berserk_player_spec.rb
91
+ - spec/studio_game/berserk_player_spec.rb~
92
+ - spec/studio_game/clumsy_player_spec.rb
93
+ - spec/studio_game/clumsy_player_spec.rb~
94
+ - spec/studio_game/game_spec.rb
95
+ - spec/studio_game/game_spec.rb~
96
+ - spec/studio_game/player_spec.rb
97
+ - spec/studio_game/player_spec.rb~
98
+ - spec/studio_game/treasure_trove_spec.rb
99
+ - spec/studio_game/treasure_trove_spec.rb~