treasure_game 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +7 -0
- data/README.md +41 -0
- data/bin/players.csv +3 -0
- data/bin/treasure_game +34 -0
- data/lib/treasure_game/auditable.rb +7 -0
- data/lib/treasure_game/berserk_player.rb +35 -0
- data/lib/treasure_game/clumsy_player.rb +38 -0
- data/lib/treasure_game/die.rb +15 -0
- data/lib/treasure_game/game.rb +81 -0
- data/lib/treasure_game/game_turn.rb +26 -0
- data/lib/treasure_game/loaded_die.rb +16 -0
- data/lib/treasure_game/playable.rb +19 -0
- data/lib/treasure_game/player.rb +58 -0
- data/lib/treasure_game/treasure_hunt.rb +18 -0
- data/spec/treasure_game/berserk_player_spec.rb +28 -0
- data/spec/treasure_game/clumsy_player_spec.rb +50 -0
- data/spec/treasure_game/game_spec.rb +50 -0
- data/spec/treasure_game/player_spec.rb +110 -0
- data/spec/treasure_game/treasure_hunt_spec.rb +52 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 41d57ddefd27c203be589209daa8a49be9d3bc89
|
4
|
+
data.tar.gz: 4de95689dd0120fc06cdfc17efee0a5441875126
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90c82ffac3e6614190fb1ea21a05fbc58f76ef86fd557a8009e7642cc44060ab3f7adb03ae0070ed5dc9d986a64a2c90a595a6c7f3b7f8afba2a040898447d43
|
7
|
+
data.tar.gz: b9dd49ebc6d701a1de7c504501f376269b0d0269f9a9eee309609dc2c840cf3f58de46179d33c6f33f6122e754b063c7f808ae32d402f0634ea6367b01b7cd6b
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2015 Mikael Salmela
|
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.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Project Name
|
2
|
+
|
3
|
+
Treasure Game
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
Find the Ruby in this old school text game.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
```
|
12
|
+
$ gem install treasure_game
|
13
|
+
```
|
14
|
+
https://rubygems.org/gems/treasure_game
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
```
|
19
|
+
$ treasure_game
|
20
|
+
```
|
21
|
+
Optional:
|
22
|
+
```
|
23
|
+
$ treasure_game (custom csv file with player names line by line)
|
24
|
+
```
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it!
|
29
|
+
2. Create your feature branch: `git checkout -b my-new-feature`
|
30
|
+
3. Commit your changes: `git commit -am 'Add some feature'`
|
31
|
+
4. Push to the branch: `git push origin my-new-feature`
|
32
|
+
5. Submit a pull request :D
|
33
|
+
|
34
|
+
## Credits
|
35
|
+
|
36
|
+
- Pickaxe: https://pragprog.com/book/ruby4/programming-ruby-1-9-2-0
|
37
|
+
- The Pragmatic Studio: https://pragmaticstudio.com/
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
MIT
|
data/bin/players.csv
ADDED
data/bin/treasure_game
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
module TreasureHunt
|
4
|
+
require_relative '../lib/treasure_game/game'
|
5
|
+
require_relative '../lib/treasure_game/player'
|
6
|
+
require_relative '../lib/treasure_game/clumsy_player'
|
7
|
+
require_relative '../lib/treasure_game/berserk_player'
|
8
|
+
|
9
|
+
game = Game.new('game')
|
10
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
11
|
+
game.load_players(ARGV.shift || default_player_file)
|
12
|
+
|
13
|
+
sms_boy = ClumsyPlayer.new('sms boy')
|
14
|
+
game.add_player(sms_boy)
|
15
|
+
|
16
|
+
hulk = BerserkPlayer.new("hulk")
|
17
|
+
game.add_player(hulk)
|
18
|
+
|
19
|
+
loop do
|
20
|
+
puts "\nHow many rounds do you want to play? ('quit' to exit)"
|
21
|
+
selection = gets.chomp.downcase
|
22
|
+
case selection
|
23
|
+
when /^\d+$/
|
24
|
+
game.play(selection.to_i)
|
25
|
+
when 'quit', 'exit'
|
26
|
+
game.print_stats
|
27
|
+
break
|
28
|
+
else
|
29
|
+
puts "\nPlease enter a number or 'quit'"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
game.save_high_scores
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require_relative 'player'
|
3
|
+
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
|
6
|
+
def initialize(name, health=100)
|
7
|
+
super(name, health)
|
8
|
+
@heal_count = 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def berserk?
|
12
|
+
@heal_count > 5
|
13
|
+
end
|
14
|
+
|
15
|
+
def heal
|
16
|
+
super
|
17
|
+
@heal_count += 1
|
18
|
+
puts "#{@name} has gone berserk!" if berserk?
|
19
|
+
end
|
20
|
+
|
21
|
+
def hit
|
22
|
+
berserk? ? heal : super
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
if __FILE__ == $0
|
28
|
+
berserker = BerserkPlayer.new('hulk', 50)
|
29
|
+
puts berserker
|
30
|
+
6.times { berserker.heal }
|
31
|
+
puts berserker
|
32
|
+
2.times { berserker.hit }
|
33
|
+
puts berserker
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require_relative 'player'
|
3
|
+
|
4
|
+
class ClumsyPlayer < Player
|
5
|
+
attr_reader :boost
|
6
|
+
|
7
|
+
def initialize(name, health=100, boost=1)
|
8
|
+
super(name, health)
|
9
|
+
@boost = boost
|
10
|
+
end
|
11
|
+
|
12
|
+
def heal
|
13
|
+
@boost.times { super }
|
14
|
+
end
|
15
|
+
|
16
|
+
def found_treasure(treasure)
|
17
|
+
super((Treasure.new(treasure.name, treasure.points / Integer(2.0))))
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
if __FILE__ == $0
|
23
|
+
sms_boy = ClumsyPlayer.new('sms boy')
|
24
|
+
|
25
|
+
guitar = Treasure.new(:guitar, 50)
|
26
|
+
sms_boy.found_treasure(guitar)
|
27
|
+
sms_boy.found_treasure(guitar)
|
28
|
+
sms_boy.found_treasure(guitar)
|
29
|
+
|
30
|
+
ruby = Treasure.new(:ruby, 400)
|
31
|
+
sms_boy.found_treasure(ruby)
|
32
|
+
|
33
|
+
sms_boy.each_found_treasure do |treasure|
|
34
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
35
|
+
end
|
36
|
+
puts "#{sms_boy.points} grand total points"
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require_relative 'player'
|
3
|
+
require_relative 'die'
|
4
|
+
require_relative 'game_turn'
|
5
|
+
require_relative 'treasure_hunt'
|
6
|
+
|
7
|
+
class Game
|
8
|
+
attr_reader :title
|
9
|
+
|
10
|
+
def initialize(title)
|
11
|
+
@title = title.capitalize
|
12
|
+
@players = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def high_score_entry(player)
|
16
|
+
formatted_name = player.name.ljust(20, '.')
|
17
|
+
"#{formatted_name} #{player.score}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def save_high_scores(to_file='highscores.txt')
|
21
|
+
File.open('highscores.txt', 'w') do |file|
|
22
|
+
file.puts "#{@title} High Scores:"
|
23
|
+
@players.sort.each { |p| file.puts high_score_entry(p) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def load_players(from_file)
|
28
|
+
File.foreach(from_file) { |line| add_player(Player.new(line.chomp)) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_player(p)
|
32
|
+
@players << p
|
33
|
+
end
|
34
|
+
|
35
|
+
def total_points
|
36
|
+
@players.reduce(0) { |memo, object| memo += object.points }
|
37
|
+
end
|
38
|
+
|
39
|
+
def print_stats
|
40
|
+
puts "\n#{@title} statistics:"
|
41
|
+
|
42
|
+
@players.sort.each do |p|
|
43
|
+
puts "\n#{p.name}'s points totals:"
|
44
|
+
p.each_found_treasure do |t|
|
45
|
+
puts "#{t.points} total #{t.name} points"
|
46
|
+
end
|
47
|
+
puts "#{p.points} grand total points"
|
48
|
+
end
|
49
|
+
|
50
|
+
puts "\n#{total_points} total points from treasures found"
|
51
|
+
|
52
|
+
strong_players, weak_players = @players.partition { |p| p.strong? }
|
53
|
+
|
54
|
+
puts "\n#{strong_players.size} strong players:"
|
55
|
+
strong_players.sort.each { |p| puts p }
|
56
|
+
|
57
|
+
puts "\n#{weak_players.size} weak players:"
|
58
|
+
weak_players.sort.each { |p| puts p }
|
59
|
+
|
60
|
+
puts "\n#{@title} High Scores:"
|
61
|
+
@players.sort.each { |p| puts high_score_entry(p) }
|
62
|
+
end
|
63
|
+
|
64
|
+
def play(rounds)
|
65
|
+
treasures = TreasureHunt::TREASURES
|
66
|
+
|
67
|
+
puts "\nThere are #{treasures.length} treasures to be found:"
|
68
|
+
treasures.each { |t| puts "A #{t.name} is worth #{t.points} points" }
|
69
|
+
|
70
|
+
puts "\nThere are #{@players.length} players in the game."
|
71
|
+
|
72
|
+
1.upto(rounds) do |r|
|
73
|
+
# break if yield if block_given?
|
74
|
+
puts "\nRound #{r}:"
|
75
|
+
@players.each do |p|
|
76
|
+
GameTurn.take_turn(p)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require_relative 'die'
|
3
|
+
require_relative 'loaded_die'
|
4
|
+
require_relative 'player'
|
5
|
+
require_relative 'treasure_hunt'
|
6
|
+
|
7
|
+
module GameTurn
|
8
|
+
def self.take_turn(p)
|
9
|
+
treasure = TreasureHunt.random
|
10
|
+
die = Die.new
|
11
|
+
number_rolled = die.roll
|
12
|
+
case number_rolled
|
13
|
+
when 1..2
|
14
|
+
p.hit
|
15
|
+
puts "#{p} got hit."
|
16
|
+
p.found_treasure(treasure)
|
17
|
+
when 3..4
|
18
|
+
puts "(#{p.name}) was skipped"
|
19
|
+
else
|
20
|
+
p.heal
|
21
|
+
puts "#{p} drank a potion."
|
22
|
+
p.found_treasure(treasure)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require_relative 'treasure_hunt'
|
3
|
+
require_relative 'playable'
|
4
|
+
|
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 each_found_treasure
|
17
|
+
@found_treasures.each do |name, points|
|
18
|
+
yield Treasure.new(name, Integer(points))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def found_treasure(treasure)
|
23
|
+
@found_treasures[treasure.name] += treasure.points
|
24
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
25
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def points
|
29
|
+
@found_treasures.values.reduce(0, :+)
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
%Q{#{name} #{health}hp}
|
34
|
+
end
|
35
|
+
|
36
|
+
def name=(player)
|
37
|
+
@name = player.capitalize
|
38
|
+
end
|
39
|
+
|
40
|
+
def <=>(other)
|
41
|
+
other.score <=> score
|
42
|
+
end
|
43
|
+
|
44
|
+
def score
|
45
|
+
@health + points
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
if __FILE__ == $0
|
51
|
+
player = Player.new('keke')
|
52
|
+
puts player
|
53
|
+
player.heal
|
54
|
+
puts player
|
55
|
+
player.hit
|
56
|
+
puts player
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
Treasure = Struct.new(:name, :points)
|
3
|
+
|
4
|
+
module TreasureHunt
|
5
|
+
TREASURES = [
|
6
|
+
Treasure.new(:grass, 5),
|
7
|
+
Treasure.new(:bottle, 25),
|
8
|
+
Treasure.new(:guitar, 50),
|
9
|
+
Treasure.new(:cash, 100),
|
10
|
+
Treasure.new(:painting, 200),
|
11
|
+
Treasure.new(:ruby, 400)
|
12
|
+
]
|
13
|
+
|
14
|
+
def self.random
|
15
|
+
TREASURES.sample
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require 'treasure_game/berserk_player'
|
3
|
+
|
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 healed up to 5 times' do
|
12
|
+
1.upto(5) { @player.heal }
|
13
|
+
expect(@player.berserk?).to be_falsey
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'goes berserk when healed more than 5 times' do
|
17
|
+
1.upto(6) { @player.heal }
|
18
|
+
expect(@player.berserk?).to be_truthy
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'gets healed instead of hit when it\'s gone berserk' do
|
22
|
+
1.upto(6) { @player.heal }
|
23
|
+
1.upto(2) { @player.hit }
|
24
|
+
expect(@player.health).to eq(@initial_health + (8 * 15))
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require 'treasure_game/clumsy_player'
|
3
|
+
|
4
|
+
describe ClumsyPlayer do
|
5
|
+
before do
|
6
|
+
@player = ClumsyPlayer.new('sms boy')
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'only gets half the point value for each treasure' do
|
10
|
+
expect(@player.points).to eq(0)
|
11
|
+
|
12
|
+
guitar = Treasure.new(:guitar, 50)
|
13
|
+
@player.found_treasure(guitar)
|
14
|
+
@player.found_treasure(guitar)
|
15
|
+
@player.found_treasure(guitar)
|
16
|
+
|
17
|
+
expect(@player.points).to eq(75)
|
18
|
+
|
19
|
+
ruby = Treasure.new(:ruby, 400)
|
20
|
+
@player.found_treasure(ruby)
|
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(:guitar, 75), Treasure.new(:ruby, 200)])
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with a boost' do
|
33
|
+
before do
|
34
|
+
@initial_health = 100
|
35
|
+
@boost = 5
|
36
|
+
@player = ClumsyPlayer.new('sms boy', @initial_health, @boost)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'has been boosted' do
|
40
|
+
expect(@player.boost).to eq(5)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'gets boost times the number of heal\'s when healed' do
|
44
|
+
@player.heal
|
45
|
+
expect(@player.health).to eq(@initial_health + (15 * @boost))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require 'rspec'
|
3
|
+
require 'treasure_game/game'
|
4
|
+
|
5
|
+
RSpec.describe Game do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@game = Game.new('Blokes')
|
9
|
+
@initial_health = 100
|
10
|
+
@player = Player.new('moe', @initial_health)
|
11
|
+
@game.add_player(@player)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'heals the player if a high number is rolled' do
|
15
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
16
|
+
@game.play(2)
|
17
|
+
expect(@player.health).to eq(@initial_health + (15 * 2))
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should do nothing if a medium number is rolled' do
|
21
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(3)
|
22
|
+
@game.play(2)
|
23
|
+
expect(@player.health).to eq(@initial_health)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'hits the player if a low number is rolled' do
|
27
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(2)
|
28
|
+
@game.play(2)
|
29
|
+
expect(@player.health).to eq(@initial_health - (10 * 2))
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'tallying up all points to display total points' do
|
33
|
+
before do
|
34
|
+
@game = Game.new("Knuckleheads")
|
35
|
+
@player1 = Player.new("moe")
|
36
|
+
@player2 = Player.new("larry")
|
37
|
+
@game.add_player(@player1)
|
38
|
+
@game.add_player(@player2)
|
39
|
+
@player1.found_treasure(Treasure.new(:guitar, 50))
|
40
|
+
@player1.found_treasure(Treasure.new(:guitar, 50))
|
41
|
+
@player2.found_treasure(Treasure.new(:ruby, 400))
|
42
|
+
end
|
43
|
+
|
44
|
+
it "computes total points as the sum of all player points" do
|
45
|
+
expect(@game.total_points).to eq(500)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require 'rspec'
|
3
|
+
require 'treasure_game/player'
|
4
|
+
require 'treasure_game/treasure_hunt'
|
5
|
+
|
6
|
+
RSpec.describe Player do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@player = Player.new('mike', 150)
|
10
|
+
@h = @player.health
|
11
|
+
$stdout = StringIO.new
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'has a capitalized name' do
|
15
|
+
expect(@player.name).to eq('Mike')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'has an initial health' do
|
19
|
+
expect(@h).to eq(150)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'has a string representation' do
|
23
|
+
expect(@player.to_s).to eq('Mike 150hp')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'computes a score as the sum of its health and points' do
|
27
|
+
@player.found_treasure(Treasure.new(:guitar, 50))
|
28
|
+
@player.found_treasure(Treasure.new(:guitar, 50))
|
29
|
+
expect(@player.score).to eq(250)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
it 'increases health by 15 when healed' do
|
34
|
+
@player.heal
|
35
|
+
expect(@player.health).to eq(@h + 15)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'decreases health by 10 when hit' do
|
39
|
+
@player.hit
|
40
|
+
expect(@player.health).to eq(@h - 10)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'computes points as the sum of all treasure points' do
|
44
|
+
expect(@player.points).to eq(0)
|
45
|
+
@player.found_treasure(Treasure.new(:guitar, 50))
|
46
|
+
expect(@player.points).to eq(50)
|
47
|
+
@player.found_treasure(Treasure.new(:ruby, 400))
|
48
|
+
expect(@player.points).to eq(450)
|
49
|
+
@player.found_treasure(Treasure.new(:guitar, 50))
|
50
|
+
expect(@player.points).to eq(500)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'yields each found treasure and its total points' do
|
54
|
+
@player.found_treasure(Treasure.new(:cash, 100))
|
55
|
+
@player.found_treasure(Treasure.new(:cash, 100))
|
56
|
+
@player.found_treasure(Treasure.new(:guitar, 50))
|
57
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
58
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
59
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
60
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
61
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
62
|
+
|
63
|
+
yielded = []
|
64
|
+
@player.each_found_treasure do |treasure|
|
65
|
+
yielded << treasure
|
66
|
+
end
|
67
|
+
|
68
|
+
expect(yielded).to eq([
|
69
|
+
Treasure.new(:cash, 200),
|
70
|
+
Treasure.new(:guitar, 50),
|
71
|
+
Treasure.new(:bottle, 25)
|
72
|
+
])
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'with a health greater than 100' do
|
76
|
+
before do
|
77
|
+
@player = Player.new('mike', 150)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'is strong' do
|
81
|
+
expect(@player.strong?).to eq(true)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'with a health less than 100' do
|
86
|
+
before do
|
87
|
+
@player = Player.new('mike', 50)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'is weak' do
|
91
|
+
expect(@player.strong?).to eq(false)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'in a collection of players' do
|
96
|
+
before do
|
97
|
+
@player1 = Player.new('mike', 100)
|
98
|
+
@player2 = Player.new('seb', 200)
|
99
|
+
@player3 = Player.new('fina', 300)
|
100
|
+
|
101
|
+
@players = [@player1, @player2, @player3]
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'is sorted by decreasing score' do
|
105
|
+
expect(@players.sort).to eq([@player3, @player2, @player1])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module TreasureHunt
|
2
|
+
require 'treasure_game/treasure_hunt'
|
3
|
+
|
4
|
+
describe Treasure do
|
5
|
+
before do
|
6
|
+
@treasure = Treasure.new(:guitar, 50)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'has a name attribute' do
|
10
|
+
expect(@treasure.name).to eq(:guitar)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'has a points attribute' do
|
14
|
+
expect(@treasure.points).to eq(50)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe TreasureHunt do
|
19
|
+
it 'has six treasures' do
|
20
|
+
expect(TreasureHunt::TREASURES.size).to eq(6)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'has grass worth 5 points' do
|
24
|
+
expect(TreasureHunt::TREASURES[0]).to eq(Treasure.new(:grass, 5))
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has a bottle worth 25 points' do
|
28
|
+
expect(TreasureHunt::TREASURES[1]).to eq(Treasure.new(:bottle, 25))
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'has a guitar worth 50 points' do
|
32
|
+
expect(TreasureHunt::TREASURES[2]).to eq(Treasure.new(:guitar, 50))
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'has cash worth 100 points' do
|
36
|
+
expect(TreasureHunt::TREASURES[3]).to eq(Treasure.new(:cash, 100))
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'has a painting worth 200 points' do
|
40
|
+
expect(TreasureHunt::TREASURES[4]).to eq(Treasure.new(:painting, 200))
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'has a ruby worth 400 points' do
|
44
|
+
expect(TreasureHunt::TREASURES[5]).to eq(Treasure.new(:ruby, 400))
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns a random treasure' do
|
48
|
+
treasure = TreasureHunt.random
|
49
|
+
expect(TreasureHunt::TREASURES).to include(treasure)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: treasure_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mikael Salmela
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-05 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: Find the Ruby in this old school text game.
|
28
|
+
email: mikael.salmela@live.com
|
29
|
+
executables:
|
30
|
+
- treasure_game
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- bin/players.csv
|
37
|
+
- bin/treasure_game
|
38
|
+
- lib/treasure_game/auditable.rb
|
39
|
+
- lib/treasure_game/berserk_player.rb
|
40
|
+
- lib/treasure_game/clumsy_player.rb
|
41
|
+
- lib/treasure_game/die.rb
|
42
|
+
- lib/treasure_game/game.rb
|
43
|
+
- lib/treasure_game/game_turn.rb
|
44
|
+
- lib/treasure_game/loaded_die.rb
|
45
|
+
- lib/treasure_game/playable.rb
|
46
|
+
- lib/treasure_game/player.rb
|
47
|
+
- lib/treasure_game/treasure_hunt.rb
|
48
|
+
- spec/treasure_game/berserk_player_spec.rb
|
49
|
+
- spec/treasure_game/clumsy_player_spec.rb
|
50
|
+
- spec/treasure_game/game_spec.rb
|
51
|
+
- spec/treasure_game/player_spec.rb
|
52
|
+
- spec/treasure_game/treasure_hunt_spec.rb
|
53
|
+
homepage: https://github.com/salmino/treasure_game
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '1.9'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.4.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Completely random old school text game.
|
77
|
+
test_files:
|
78
|
+
- spec/treasure_game/berserk_player_spec.rb
|
79
|
+
- spec/treasure_game/clumsy_player_spec.rb
|
80
|
+
- spec/treasure_game/game_spec.rb
|
81
|
+
- spec/treasure_game/player_spec.rb
|
82
|
+
- spec/treasure_game/treasure_hunt_spec.rb
|