studio_game_3 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +7 -0
- data/README +1 -0
- data/bin/players.csv +3 -3
- data/bin/studio_game +42 -33
- data/lib/studio_game/auditable.rb +7 -7
- data/lib/studio_game/berserk_player.rb +37 -38
- data/lib/studio_game/clumsy_player.rb +29 -30
- data/lib/studio_game/die.rb +23 -26
- data/lib/studio_game/game.rb +92 -98
- data/lib/studio_game/game_turn.rb +26 -28
- data/lib/studio_game/loaded_die.rb +18 -23
- data/lib/studio_game/playable.rb +16 -33
- data/lib/studio_game/player.rb +79 -59
- data/lib/studio_game/treasure_trove.rb +19 -18
- data/spec/studio_game/berserk_player_spec.rb +33 -0
- data/spec/studio_game/clumsy_player_spec.rb +34 -32
- data/spec/studio_game/game_spec.rb +63 -69
- data/spec/studio_game/player_spec.rb +124 -133
- data/spec/studio_game/treasure_trove_spec.rb +67 -53
- metadata +10 -7
- data/spec/studio_game/beserk_player_spec.rb +0 -29
@@ -1,28 +1,26 @@
|
|
1
|
-
require_relative '
|
2
|
-
require_relative '
|
3
|
-
require_relative '
|
4
|
-
require_relative '
|
5
|
-
|
6
|
-
module StudioGame
|
7
|
-
module GameTurn
|
8
|
-
def self.take_turn(player)
|
9
|
-
|
10
|
-
case
|
11
|
-
when 1..2
|
12
|
-
player.blam
|
13
|
-
when 3..4
|
14
|
-
puts "#{player.name} was skipped."
|
15
|
-
else
|
16
|
-
player.
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
StudioGame::GameTurn.take_turn(player)
|
28
|
-
end
|
1
|
+
require_relative 'die'
|
2
|
+
require_relative 'player'
|
3
|
+
require_relative 'treasure_trove'
|
4
|
+
require_relative 'loaded_die'
|
5
|
+
|
6
|
+
module StudioGame
|
7
|
+
module GameTurn
|
8
|
+
def self.take_turn(player)
|
9
|
+
d = Die.new
|
10
|
+
case d.roll
|
11
|
+
when 1..2
|
12
|
+
player.blam
|
13
|
+
when 3..4
|
14
|
+
puts "#{player.name} was skipped."
|
15
|
+
else
|
16
|
+
player.woot
|
17
|
+
end
|
18
|
+
treasure = TreasureTrove.random
|
19
|
+
player.found_treasure(treasure)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
@@ -1,23 +1,18 @@
|
|
1
|
-
require_relative 'auditable'
|
2
|
-
|
3
|
-
module StudioGame
|
4
|
-
class LoadedDie
|
5
|
-
include Auditable
|
6
|
-
|
7
|
-
attr_reader :number
|
8
|
-
|
9
|
-
def roll
|
10
|
-
numbers = [1,
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
die = StudioGame::LoadedDie.new
|
20
|
-
puts die.roll
|
21
|
-
puts die.roll
|
22
|
-
puts die.roll
|
23
|
-
end
|
1
|
+
require_relative 'auditable'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
class LoadedDie
|
5
|
+
include Auditable
|
6
|
+
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def roll
|
10
|
+
numbers = [1,1,2,2,5,6]
|
11
|
+
@number = numbers.sample
|
12
|
+
audit
|
13
|
+
@number
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
data/lib/studio_game/playable.rb
CHANGED
@@ -1,33 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
module StudioGame
|
18
|
-
module Playable
|
19
|
-
def w00t
|
20
|
-
self.health += 15
|
21
|
-
puts "#{name} got w00ted!"
|
22
|
-
end
|
23
|
-
|
24
|
-
def blam
|
25
|
-
self.health -= 10
|
26
|
-
puts "#{name} got blammed!"
|
27
|
-
end
|
28
|
-
|
29
|
-
def strong?
|
30
|
-
self.health > 100
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
1
|
+
module Playable
|
2
|
+
def blam
|
3
|
+
puts "#{name} got blammed."
|
4
|
+
self.health -= 10
|
5
|
+
end
|
6
|
+
|
7
|
+
def woot
|
8
|
+
puts "#{name} got wooted."
|
9
|
+
self.health += 15
|
10
|
+
end
|
11
|
+
|
12
|
+
def strong?
|
13
|
+
health > 100
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
data/lib/studio_game/player.rb
CHANGED
@@ -1,59 +1,79 @@
|
|
1
|
-
require_relative 'treasure_trove'
|
2
|
-
require_relative 'playable'
|
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
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
def
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.from_csv(string)
|
45
|
-
name, health = string.split(',')
|
46
|
-
new(name, Integer(health))
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
1
|
+
require_relative 'treasure_trove'
|
2
|
+
require_relative 'playable'
|
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 found_treasure(t)
|
17
|
+
@found_treasures[t.name] += t.points
|
18
|
+
puts "#{@name} found a #{t.name} worth #{t.points} points."
|
19
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def points
|
23
|
+
@found_treasures.values.reduce(0, :+)
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"I'm #{@name} with health = #{@health}, points = #{points} and score = #{score}."
|
28
|
+
end
|
29
|
+
|
30
|
+
def score
|
31
|
+
@health + points
|
32
|
+
end
|
33
|
+
|
34
|
+
def <=> other_player
|
35
|
+
other_player.score <=> score
|
36
|
+
end
|
37
|
+
|
38
|
+
def each_found_treasure
|
39
|
+
@found_treasures.each do |name, points|
|
40
|
+
yield(Treasure.new(name, points))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.from_csv(string)
|
45
|
+
name, health = string.split(',')
|
46
|
+
Player.new(name, Integer(health))
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
def self.from_csv(string)
|
52
|
+
name, health = string.split(',')
|
53
|
+
Player.new(name, Integer(health))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
if __FILE__ ==$0
|
59
|
+
player = StudioGame::Player.new('moe')
|
60
|
+
puts player.name
|
61
|
+
puts player.health
|
62
|
+
player.woot
|
63
|
+
puts player.health
|
64
|
+
player.blam
|
65
|
+
puts player.health
|
66
|
+
puts player
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
@@ -1,18 +1,19 @@
|
|
1
|
-
module StudioGame
|
2
|
-
Treasure = Struct.new(:name, :points)
|
3
|
-
|
4
|
-
module TreasureTrove
|
5
|
-
TREASURES = [
|
6
|
-
Treasure.new(:pie,
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def self.random
|
15
|
-
TREASURES.sample
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
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
|
19
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'studio_game\berserk_player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe BerserkPlayer do
|
5
|
+
|
6
|
+
before do
|
7
|
+
$stdout = StringIO.new
|
8
|
+
@initial_health = 50
|
9
|
+
@player = BerserkPlayer.new("berserker", @initial_health)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "does not go berserk when wooted up to 5 times" do
|
13
|
+
1.upto(5) { @player.woot }
|
14
|
+
|
15
|
+
@player.should_not be_berserk
|
16
|
+
end
|
17
|
+
|
18
|
+
it "goes berserk when wooted more than 5 times" do
|
19
|
+
1.upto(6) { @player.woot }
|
20
|
+
|
21
|
+
@player.should be_berserk
|
22
|
+
end
|
23
|
+
|
24
|
+
it "gets wooted instead of blammed when it's gone berserk" do
|
25
|
+
1.upto(6) { @player.woot }
|
26
|
+
1.upto(2) { @player.blam }
|
27
|
+
|
28
|
+
@player.health.should == @initial_health + (8 * 15)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -1,32 +1,34 @@
|
|
1
|
-
require 'studio_game
|
2
|
-
|
3
|
-
module StudioGame
|
4
|
-
describe ClumsyPlayer do
|
5
|
-
before do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@player.found_treasure(hammer)
|
15
|
-
@player.found_treasure(hammer)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
1
|
+
require 'studio_game\clumsy_player'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe ClumsyPlayer do
|
5
|
+
before do
|
6
|
+
$stdout = StringIO.new
|
7
|
+
@player = ClumsyPlayer.new("klutz")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "only gets half the point value for each treasure" do
|
11
|
+
@player.points.should == 0
|
12
|
+
|
13
|
+
hammer = Treasure.new(:hammer, 50)
|
14
|
+
@player.found_treasure(hammer)
|
15
|
+
@player.found_treasure(hammer)
|
16
|
+
@player.found_treasure(hammer)
|
17
|
+
|
18
|
+
@player.points.should == 75
|
19
|
+
|
20
|
+
crowbar = Treasure.new(:crowbar, 400)
|
21
|
+
@player.found_treasure(crowbar)
|
22
|
+
|
23
|
+
@player.points.should == 275
|
24
|
+
|
25
|
+
yielded = []
|
26
|
+
@player.each_found_treasure do |treasure|
|
27
|
+
yielded << treasure
|
28
|
+
end
|
29
|
+
|
30
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -1,69 +1,63 @@
|
|
1
|
-
require 'studio_game
|
2
|
-
|
3
|
-
module StudioGame
|
4
|
-
describe Game do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@
|
9
|
-
|
10
|
-
|
11
|
-
@
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
player2.found_treasure(Treasure.new(:crowbar, 400))
|
65
|
-
|
66
|
-
game.total_points.should == 500
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
1
|
+
require 'studio_game\game'
|
2
|
+
|
3
|
+
module StudioGame
|
4
|
+
describe Game do
|
5
|
+
|
6
|
+
before do
|
7
|
+
$stdout = StringIO.new
|
8
|
+
@game = Game.new("Knuckleheads")
|
9
|
+
@rounds = 3
|
10
|
+
|
11
|
+
@initial_health = 100
|
12
|
+
@player = Player.new("moe", @initial_health)
|
13
|
+
|
14
|
+
@game.add_player(@player)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "woots a player when a high number is rolled" do
|
18
|
+
Die.any_instance.stub(:roll).and_return(5)
|
19
|
+
|
20
|
+
@game.play(@rounds)
|
21
|
+
|
22
|
+
@player.health.should == @initial_health + (15 * @rounds)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "blams a player when a low number is rolled" do
|
26
|
+
Die.any_instance.stub(:roll).and_return(2)
|
27
|
+
|
28
|
+
@game.play(@rounds)
|
29
|
+
|
30
|
+
@player.health.should == @initial_health - (10 * @rounds)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "skips a player when a medium number is rolled" do
|
34
|
+
Die.any_instance.stub(:roll).and_return(4)
|
35
|
+
|
36
|
+
@game.play(@rounds)
|
37
|
+
|
38
|
+
@player.health.should == @initial_health
|
39
|
+
end
|
40
|
+
|
41
|
+
it "assigns a treasure for points during a player's turn" do
|
42
|
+
game = Game.new("Knuckleheads")
|
43
|
+
player = Player.new("moe")
|
44
|
+
|
45
|
+
game.add_player(player)
|
46
|
+
|
47
|
+
game.play(1)
|
48
|
+
|
49
|
+
player.points.should_not be_zero
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|