x1c-studio-game 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
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
+ # 1.upto(5) do
11
+ # |n| yield n
12
+ # end
13
+ # end
14
+
15
+ # five_times do |n|
16
+ # puts "#{n} situps"
17
+ # puts "#{n} pushups"
18
+ # puts "#{n} chinups"
19
+ # end
20
+ # module StudioGame
21
+ # def n_times(n)
22
+ # 1.upto(n) do |b|
23
+ # yield b
24
+ # end
25
+ # end
26
+
27
+ # n_times(5) do |n|
28
+ # puts "#{n} situps"
29
+ # puts "#{n} pushups"
30
+ # puts "#{n} chinups"
31
+ # end
32
+ # end
@@ -0,0 +1,16 @@
1
+ require_relative 'auditable'
2
+
3
+ module StudioGame
4
+ class LoadedDie
5
+ include StudioGame::Auditable
6
+
7
+ attr_reader :number
8
+
9
+ def roll
10
+ numbers = [1, 1, 2, 5, 6, 6]
11
+ @number = numbers.sample
12
+ audit
13
+ @number
14
+ end
15
+ end
16
+ 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,65 @@
1
+ require_relative 'treasure_trove'
2
+ require_relative 'playable.rb'
3
+
4
+ module StudioGame
5
+ class Player
6
+ include Playable
7
+
8
+ attr_accessor :name, :health
9
+
10
+ def initialize(name, health=100)
11
+ @name = name.capitalize
12
+ @health = health
13
+ @found_treasures = Hash.new(0)
14
+ end
15
+
16
+ def self.from_csv(string)
17
+ name, health = string.split(',')
18
+ Player.new(name, Integer(health))
19
+ end
20
+
21
+ def to_s
22
+ "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
23
+ end
24
+
25
+ def score
26
+ # @health + @name.length
27
+ @health + points
28
+ end
29
+
30
+ def <=>(player)
31
+ player.score <=> self.score
32
+ end
33
+
34
+ def found_treasure(treasure)
35
+ @found_treasures[treasure.name] += treasure.points
36
+
37
+ puts "#{@name} found a #{treasure.name}" +
38
+ " worth #{treasure.points} points."
39
+ puts "#{@name}'s treasures: #{@found_treasures}"
40
+
41
+ end
42
+
43
+ def points
44
+ @found_treasures.values.reduce(0, :+)
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
+ end
53
+
54
+ if __FILE__ == $0
55
+ player = Player.new("moe")
56
+ puts player.name
57
+ puts player.health
58
+
59
+ player.w00t
60
+ puts player.health
61
+
62
+ player.blam
63
+ puts player.health
64
+ end
65
+ end
@@ -0,0 +1,18 @@
1
+ module StudioGame
2
+ Treasure = Struct.new(:name, :points)
3
+
4
+ module TreasureTrove
5
+ TREASURES = [
6
+ Treasure.new(:pie, 5),
7
+ Treasure.new(:bottle, 25),
8
+ Treasure.new(:hammer, 50),
9
+ Treasure.new(:skillet, 100),
10
+ Treasure.new(:broomstick, 200),
11
+ Treasure.new(:crowbar, 400)
12
+ ]
13
+
14
+ def self.random
15
+ TREASURES.sample
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
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
+ expect(@player.berserk?).to be_falsey
15
+ end
16
+
17
+ it "goes berserk when w00ted more than 5 times" do
18
+ 1.upto(6) { @player.w00t }
19
+
20
+ expect(@player.berserk?).to be_truthy
21
+ end
22
+
23
+ it "gets w00ted instead of blammed when it's gone berserk" do
24
+ 1.upto(6) { @player.w00t }
25
+ 1.upto(2) { @player.blam }
26
+
27
+ expect(@player.health).to eq(@initial_health + (8 * 15))
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,49 @@
1
+ require 'studio_game/clumsy_player'
2
+
3
+ module StudioGame
4
+ describe ClumsyPlayer do
5
+ before do
6
+ @player = ClumsyPlayer.new("klutz")
7
+ end
8
+
9
+ it "only gets half the point value for each treasure" do
10
+ expect(@player.points).to eq(0)
11
+
12
+ hammer = Treasure.new(:hammer, 50)
13
+ @player.found_treasure(hammer)
14
+ @player.found_treasure(hammer)
15
+ @player.found_treasure(hammer)
16
+
17
+ expect(@player.points).to eq(75)
18
+
19
+ crowbar = Treasure.new(:crowbar, 400)
20
+ @player.found_treasure(crowbar)
21
+
22
+ expect(@player.points).to eq(275)
23
+
24
+ yielded = []
25
+ @player.each_found_treasure do |treasure|
26
+ yielded << treasure
27
+ end
28
+
29
+ expect(yielded).to eq([Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)])
30
+ end
31
+
32
+ context "with a boost factor" do
33
+ before do
34
+ @initial_health = 100
35
+ @boost_factor = 5
36
+ @player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
37
+ end
38
+
39
+ it "has a boost factor" do
40
+ expect(@player.boost_factor).to eq(5)
41
+ end
42
+
43
+ it "gets boost factor number of w00ts when w00ted" do
44
+ @player.w00t
45
+ expect(@player.health).to eq(@initial_health + (15 * @boost_factor))
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,86 @@
1
+ require 'studio_game/game'
2
+
3
+ module StudioGame
4
+ describe Game do
5
+ before do
6
+ @game = Game.new("Knuckleheads")
7
+
8
+ @initial_health = 100
9
+ @player = Player.new("moe", @initial_health)
10
+ @game.add_player(@player)
11
+ end
12
+
13
+ it "w00ts a player if high number is rolled" do
14
+ allow_any_instance_of(Die).to receive(:roll).and_return(5)
15
+
16
+ @game.play(2)
17
+
18
+ expect(@player.health).to eq(@initial_health + 2 * 15)
19
+ end
20
+
21
+ it "skips the player if a medium number is rolled" do
22
+ allow_any_instance_of(Die).to receive(:roll).and_return(3)
23
+
24
+ @game.play(2)
25
+
26
+ expect(@player.health).to eq(@initial_health)
27
+ end
28
+
29
+ it "blams the player if a low number is rolled" do
30
+ allow_any_instance_of(Die).to receive(:roll).and_return(1)
31
+
32
+ @game.play(2)
33
+
34
+ expect(@player.health).to eq(@initial_health - 2 * 10)
35
+ end
36
+
37
+ it "assigns a treasure for points during a player's turn" do
38
+ game = Game.new("Knuckleheads")
39
+ player = Player.new("moe")
40
+
41
+ game.add_player(player)
42
+
43
+ game.play(1)
44
+
45
+ expect(player.points).not_to be_zero
46
+ end
47
+
48
+ it "computes total points as the sum of all player points" do
49
+ game = Game.new("Knuckleheads")
50
+
51
+ player1 = Player.new("moe")
52
+ player2 = Player.new("larry")
53
+
54
+ game.add_player(player1)
55
+ game.add_player(player2)
56
+
57
+ player1.found_treasure(Treasure.new(:hammer, 50))
58
+ player1.found_treasure(Treasure.new(:hammer, 50))
59
+ player2.found_treasure(Treasure.new(:crowbar, 400))
60
+
61
+ expect(game.total_points).to eq(500)
62
+ end
63
+
64
+ it "yields each found treasure and its total points" do
65
+ @player.found_treasure(Treasure.new(:skillet, 100))
66
+ @player.found_treasure(Treasure.new(:skillet, 100))
67
+ @player.found_treasure(Treasure.new(:hammer, 50))
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
+ @player.found_treasure(Treasure.new(:bottle, 5))
73
+
74
+ yielded = []
75
+ @player.each_found_treasure do |treasure|
76
+ yielded << treasure
77
+ end
78
+
79
+ expect(yielded).to eq([
80
+ Treasure.new(:skillet, 200),
81
+ Treasure.new(:hammer, 50),
82
+ Treasure.new(:bottle, 25)
83
+ ])
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,103 @@
1
+ require 'studio_game/player'
2
+ require 'studio_game/spec_helper'
3
+ require 'studio_game/treasure_trove'
4
+
5
+ module StudioGame
6
+ describe Player do
7
+ before do
8
+ @initial_health = 150
9
+ @player = Player.new("larry", @initial_health)
10
+
11
+ $stdout = StringIO.new
12
+ end
13
+
14
+ it "has a capitalized name" do
15
+ expect(@player.name).to eq("Larry")
16
+ end
17
+
18
+ it "has an initial health" do
19
+ expect(@player.health).to eq(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
+ expect(@player.to_s).to eq("I'm Larry with health = 150, points = 100, and score = 250.")
26
+ end
27
+
28
+ it "computes a score as the num of its health and points" do
29
+ # expect(@player.score).to eq(150 + 5)
30
+ @player.found_treasure(Treasure.new(:hammer, 50))
31
+ @player.found_treasure(Treasure.new(:hammer, 50))
32
+ expect(@player.score).to eq(250)
33
+
34
+ end
35
+
36
+ it "increases health by 15 when w00ted" do
37
+ @player.w00t
38
+ expect(@player.health).to eq(@initial_health + 15)
39
+ end
40
+
41
+ it "decreases health by 10 when blammed" do
42
+ @player.blam
43
+ expect(@player.health).to eq(@initial_health - 10)
44
+ end
45
+
46
+ context "with a health greater than 100" do
47
+ before do
48
+ @player = Player.new("larry", 150)
49
+ end
50
+
51
+ it "is strong" do
52
+ expect(@player).to be_strong
53
+ end
54
+ end
55
+
56
+ context "with a health of 100 or less" do
57
+ before do
58
+ @player = Player.new("larry", 100)
59
+ end
60
+
61
+ it "is wimpy" do
62
+ expect(@player).not_to be_strong
63
+ end
64
+ end
65
+
66
+ context "in a collection of players" do
67
+ before do
68
+ @player1 = Player.new("moe", 100)
69
+ @player2 = Player.new("larry", 200)
70
+ @player3 = Player.new("curly", 300)
71
+
72
+ @players = [@player1, @player2, @player3]
73
+ end
74
+
75
+ it "is sorted by decreasing score" do
76
+ @players.sort.should == [@player3, @player2, @player1]
77
+ end
78
+ end
79
+
80
+ it "computes points as the sum of all treasure points" do
81
+ expect(@player.points).to eq(0)
82
+
83
+ @player.found_treasure(Treasure.new(:hammer, 50))
84
+
85
+ expect(@player.points).to eq(50)
86
+
87
+ @player.found_treasure(Treasure.new(:crowbar, 400))
88
+
89
+ expect(@player.points).to eq(450)
90
+
91
+ @player.found_treasure(Treasure.new(:hammer, 50))
92
+
93
+ expect(@player.points).to eq(500)
94
+ end
95
+
96
+ it "can be created from a CSV string" do
97
+ player = Player.from_csv("larry,150")
98
+
99
+ expect(player.name).to eq("Larry")
100
+ expect(player.health).to eq(150)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,8 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |c|
3
+ c.syntax = [:should, :expect]
4
+ end
5
+ config.mock_with :rspec do |c|
6
+ c.syntax = [:should, :expect]
7
+ end
8
+ 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
+ expect(@treasure.name).to eq(:hammer)
12
+ end
13
+
14
+ it "has a points attribute" do
15
+ expect(@treasure.points).to eq(50)
16
+ end
17
+
18
+ end
19
+
20
+ describe TreasureTrove do
21
+
22
+ it "has six treasures" do
23
+ expect(TreasureTrove::TREASURES.size).to eq(6)
24
+ end
25
+
26
+ it "has a pie worth 5 points" do
27
+ expect(TreasureTrove::TREASURES[0]).to eq(Treasure.new(:pie, 5))
28
+ end
29
+
30
+ it "has a bottle worth 25 points" do
31
+ expect(TreasureTrove::TREASURES[1]).to eq(Treasure.new(:bottle, 25))
32
+ end
33
+
34
+ it "has a hammer worth 50 points" do
35
+ expect(TreasureTrove::TREASURES[2]).to eq(Treasure.new(:hammer, 50))
36
+ end
37
+
38
+ it "has a skillet worth 100 points" do
39
+ expect(TreasureTrove::TREASURES[3]).to eq(Treasure.new(:skillet, 100))
40
+ end
41
+
42
+ it "has a broomstick worth 200 points" do
43
+ expect(TreasureTrove::TREASURES[4]).to eq(Treasure.new(:broomstick, 200))
44
+ end
45
+
46
+ it "has a crowbar worth 400 points" do
47
+ expect(TreasureTrove::TREASURES[5]).to eq(Treasure.new(:crowbar, 400))
48
+ end
49
+
50
+ it "returns a random treasure" do
51
+ treasure = TreasureTrove.random
52
+
53
+ expect(TreasureTrove::TREASURES).to include(treasure)
54
+ end
55
+
56
+ end
57
+ end