jkims_game 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +19 -0
- data/README +1 -0
- data/bin/jkims_game +32 -0
- data/bin/players.csv +3 -0
- data/lib/jkims_game/auditable.rb +7 -0
- data/lib/jkims_game/berserk_player.rb +34 -0
- data/lib/jkims_game/clumsy_player.rb +28 -0
- data/lib/jkims_game/die.rb +15 -0
- data/lib/jkims_game/game.rb +80 -0
- data/lib/jkims_game/game_turn.rb +21 -0
- data/lib/jkims_game/loaded_die.rb +13 -0
- data/lib/jkims_game/playable.rb +17 -0
- data/lib/jkims_game/player.rb +59 -0
- data/lib/jkims_game/treasure_trove.rb +17 -0
- data/spec/jkims_game/berserk_player_spec.rb +35 -0
- data/spec/jkims_game/clumsy_player_spec.rb +32 -0
- data/spec/jkims_game/game_spec.rb +66 -0
- data/spec/jkims_game/player_spec.rb +121 -0
- data/spec/jkims_game/treasure_trove_spec.rb +58 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b88030a87509cbb29b53bb10bc01d79e58de5579
|
4
|
+
data.tar.gz: 89daa93b20f7d5a34950fcfb7a3613876697263d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25189ca70aeb6dfd3245d4e0fbda14afaf9c82f18f5d8b6cd72ab8009526755c5f1573746892275a587bb5bb8eb6ab12c0fa17985abfd88772b7aa256db1cc34
|
7
|
+
data.tar.gz: 5d2cfd77e0b4e6837830f4009c862ecaa55196ac5e112a0fc3a8f54022e347790d444e8f5069ecb0095a713a69337eeca1ecff4cead003de5f5704e9e987ae77
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2018 Jongmin Kim
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
This is a game created as an exercise of The Pragmatic Studio's Ruby Programming course.
|
data/bin/jkims_game
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require_relative "../lib/jkims_game/game"
|
3
|
+
# player1 = Player.new("moe")
|
4
|
+
# player2 = Player.new("larry", 60)
|
5
|
+
# player3 = Player.new("curly", 125)
|
6
|
+
|
7
|
+
knuckleheads = JkimsGame::Game.new("Knuckleheads")
|
8
|
+
# knuckleheads.add_player(player1)
|
9
|
+
# knuckleheads.add_player(player2)
|
10
|
+
# knuckleheads.add_player(player3)
|
11
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
12
|
+
knuckleheads.loop_players(ARGV.shift || default_player_file)
|
13
|
+
klutz = JkimsGame::ClumsyPlayer.new("klutz", 105)
|
14
|
+
knuckleheads.add_player(klutz)
|
15
|
+
berserker = JkimsGame::BerserkPlayer.new("berserker", 50)
|
16
|
+
knuckleheads.add_player(berserker)
|
17
|
+
loop do
|
18
|
+
puts "How many game rounds? ('quit' to exit)"
|
19
|
+
answer = gets.chomp.downcase
|
20
|
+
case answer
|
21
|
+
when /^\d+$/
|
22
|
+
knuckleheads.play(answer.to_i)
|
23
|
+
when 'quit', 'exit'
|
24
|
+
knuckleheads.print_stats
|
25
|
+
break
|
26
|
+
else
|
27
|
+
puts "Please enter a number or quit"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
knuckleheads.save_high_scores
|
data/bin/players.csv
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
module JkimsGame
|
3
|
+
class BerserkPlayer < Player
|
4
|
+
def initialize(name, health=100)
|
5
|
+
super(name, health)
|
6
|
+
@w00t_count = 0
|
7
|
+
end
|
8
|
+
|
9
|
+
def w00t
|
10
|
+
super
|
11
|
+
@w00t_count += 1
|
12
|
+
puts "#{@name} is berserk!" if berserk?
|
13
|
+
end
|
14
|
+
|
15
|
+
def blam
|
16
|
+
if berserk?
|
17
|
+
w00t
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def berserk?
|
24
|
+
@w00t_count >= 6
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
if __FILE__ == $0
|
30
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
31
|
+
6.times { berserker.w00t }
|
32
|
+
2.times { berserker.blam }
|
33
|
+
puts berserker.health
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
module JkimsGame
|
3
|
+
class ClumsyPlayer < Player
|
4
|
+
def found_treasure(treasure)
|
5
|
+
points = treasure.points / 2.0
|
6
|
+
@found_treasures[treasure.name] += points
|
7
|
+
puts "#{@name} found a #{treasure.name} worth #{points} points."
|
8
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
if __FILE__ == $0
|
14
|
+
clumsy = ClumsyPlayer.new("klutz")
|
15
|
+
|
16
|
+
hammer = Treasure.new(:hammer, 50)
|
17
|
+
clumsy.found_treasure(hammer)
|
18
|
+
clumsy.found_treasure(hammer)
|
19
|
+
clumsy.found_treasure(hammer)
|
20
|
+
|
21
|
+
crowbar = Treasure.new(:crowbar, 400)
|
22
|
+
clumsy.found_treasure(crowbar)
|
23
|
+
|
24
|
+
clumsy.each_found_treasure do |treasure|
|
25
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
26
|
+
end
|
27
|
+
puts "#{clumsy.points} grand total points"
|
28
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require_relative "player"
|
2
|
+
require_relative "die"
|
3
|
+
require_relative "game_turn"
|
4
|
+
require_relative "treasure_trove"
|
5
|
+
require_relative "clumsy_player"
|
6
|
+
require_relative "berserk_player"
|
7
|
+
module JkimsGame
|
8
|
+
class Game
|
9
|
+
attr_reader :title
|
10
|
+
def initialize(title)
|
11
|
+
@title = title
|
12
|
+
@players = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def loop_players(from_file)
|
16
|
+
File.readlines(from_file).each do |line|
|
17
|
+
add_player (Player.from_csv(line))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def save_high_scores(filename = "high_scores.txt")
|
22
|
+
File.open(filename, "w") do |file|
|
23
|
+
file.puts "#{@title} High Scores:"
|
24
|
+
@players.sort.each do |player|
|
25
|
+
file.puts "#{player.name.ljust(20, ".")}#{player.score.to_s.rjust(3)}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_player(player)
|
32
|
+
@players << player
|
33
|
+
end
|
34
|
+
|
35
|
+
def play(rounds)
|
36
|
+
puts "There are #{@players.length} players in #{@title}:"
|
37
|
+
@players.each do |player|
|
38
|
+
puts player
|
39
|
+
end
|
40
|
+
treasures = TreasureTrove::TREASURES
|
41
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
42
|
+
treasures.each do |treasure|
|
43
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
44
|
+
end
|
45
|
+
1.upto(rounds) do |round|
|
46
|
+
puts "\nRound: #{round}:"
|
47
|
+
@players.each do |player|
|
48
|
+
GameTurn.take_turn(player)
|
49
|
+
puts player
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def print_name_and_health(player)
|
55
|
+
"#{player.name} (#{player.health})"
|
56
|
+
end
|
57
|
+
|
58
|
+
def total_points
|
59
|
+
@players.reduce(0) {|sum, player| sum + player.points}
|
60
|
+
end
|
61
|
+
|
62
|
+
def print_stats
|
63
|
+
@players.sort.each do |player|
|
64
|
+
puts "#{player.name.ljust(20, ".")}#{player.score.to_s.rjust(3)}"
|
65
|
+
end
|
66
|
+
puts "\n#{@title} Statistics:"
|
67
|
+
strong, wimpy = @players.partition { |player| player.strong? }
|
68
|
+
puts "\n#{strong.length} strong players:"
|
69
|
+
strong.each { |s| puts print_name_and_health(s)}
|
70
|
+
puts "\n#{wimpy.length} wimpy players:"
|
71
|
+
wimpy.each { |w| puts print_name_and_health(w)}
|
72
|
+
@players.each do |player|
|
73
|
+
puts "#{player.name}'s total points:"
|
74
|
+
player.each_found_treasure { |treasure| puts "#{treasure.points} total #{treasure.name} points"}
|
75
|
+
puts "#{player.points} grand total points"
|
76
|
+
end
|
77
|
+
puts "#{total_points} total points from treasures found"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative "die"
|
2
|
+
require_relative "player"
|
3
|
+
require_relative "treasure_trove"
|
4
|
+
require_relative 'loaded_die'
|
5
|
+
|
6
|
+
module JkimsGame
|
7
|
+
module GameTurn
|
8
|
+
def self.take_turn(player)
|
9
|
+
case Die.new.roll
|
10
|
+
when 5..6
|
11
|
+
player.w00t
|
12
|
+
when 3..4
|
13
|
+
puts "#{player.name} is skipped"
|
14
|
+
else
|
15
|
+
player.blam
|
16
|
+
end
|
17
|
+
treasure = TreasureTrove.random
|
18
|
+
player.found_treasure(treasure)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require_relative 'treasure_trove'
|
2
|
+
require_relative 'playable'
|
3
|
+
module JkimsGame
|
4
|
+
class Player
|
5
|
+
attr_accessor :health
|
6
|
+
attr_accessor :name
|
7
|
+
|
8
|
+
include Playable
|
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(csv)
|
17
|
+
name, health = csv.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 + self.points
|
27
|
+
end
|
28
|
+
|
29
|
+
def <=>(other)
|
30
|
+
other.score <=> self.score
|
31
|
+
end
|
32
|
+
|
33
|
+
def points
|
34
|
+
@found_treasures.values.reduce(0, :+)
|
35
|
+
end
|
36
|
+
|
37
|
+
def each_found_treasure
|
38
|
+
@found_treasures.each do |name, points|
|
39
|
+
yield Treasure.new(name, points)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
def found_treasure(treasure)
|
45
|
+
@found_treasures[treasure.name] += treasure.points
|
46
|
+
puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
|
47
|
+
puts "#{@name}'s treasures: #{@found_treasures}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
if __FILE__ == $0
|
52
|
+
player = Player.new("moe")
|
53
|
+
puts player.name
|
54
|
+
puts player.health
|
55
|
+
player.w00t
|
56
|
+
puts player.health
|
57
|
+
player.blam
|
58
|
+
puts player.health
|
59
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module JkimsGame
|
2
|
+
Treasure = Struct.new(:name, :points)
|
3
|
+
module TreasureTrove
|
4
|
+
TREASURES = [
|
5
|
+
Treasure.new(:pie, 5),
|
6
|
+
Treasure.new(:bottle, 25),
|
7
|
+
Treasure.new(:hammer, 50),
|
8
|
+
Treasure.new(:skillet, 100),
|
9
|
+
Treasure.new(:broomstick, 200),
|
10
|
+
Treasure.new(:crowbar, 400)
|
11
|
+
]
|
12
|
+
|
13
|
+
def self.random
|
14
|
+
self::TREASURES.sample
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'jkims_game/berserk_player'
|
2
|
+
|
3
|
+
module JkimsGame
|
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
|
+
|
15
|
+
# or if using Rspec 3.0:
|
16
|
+
# @player.berserk?.should be_falsey
|
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
|
+
|
24
|
+
# or if using Rspec 3.0:
|
25
|
+
# @player.berserk?.should be_truthy
|
26
|
+
end
|
27
|
+
|
28
|
+
it "gets w00ted instead of blammed when it's gone berserk" do
|
29
|
+
1.upto(6) { @player.w00t }
|
30
|
+
1.upto(2) { @player.blam }
|
31
|
+
|
32
|
+
@player.health.should == @initial_health + (8 * 15)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'jkims_game/clumsy_player'
|
2
|
+
|
3
|
+
module JkimsGame
|
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
|
+
@player.points.should == 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
|
+
@player.points.should == 75
|
18
|
+
|
19
|
+
crowbar = Treasure.new(:crowbar, 400)
|
20
|
+
@player.found_treasure(crowbar)
|
21
|
+
|
22
|
+
@player.points.should == 275
|
23
|
+
|
24
|
+
yielded = []
|
25
|
+
@player.each_found_treasure do |treasure|
|
26
|
+
yielded << treasure
|
27
|
+
end
|
28
|
+
|
29
|
+
yielded.should == [Treasure.new(:hammer, 75), Treasure.new(:crowbar, 200)]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "jkims_game/game"
|
2
|
+
|
3
|
+
module JkimsGame
|
4
|
+
describe Game do
|
5
|
+
before do
|
6
|
+
@game = Game.new("Knuckleheads")
|
7
|
+
@initial_health = 100
|
8
|
+
@player = Player.new("moe", @initial_health)
|
9
|
+
@game.add_player(@player)
|
10
|
+
end
|
11
|
+
|
12
|
+
context "when a high number is rolled" do
|
13
|
+
it "w00ts the player" do
|
14
|
+
Die.any_instance.stub(:roll).and_return(5)
|
15
|
+
@game.play(2)
|
16
|
+
@player.health.should == @initial_health + (15 * 2)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when a medium number is rolled" do
|
21
|
+
it "skips the player" do
|
22
|
+
Die.any_instance.stub(:roll).and_return(4)
|
23
|
+
@game.play(2)
|
24
|
+
@player.health.should == @initial_health
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when a low number is rolled" do
|
29
|
+
it "blams the player" do
|
30
|
+
Die.any_instance.stub(:roll).and_return(1)
|
31
|
+
@game.play(2)
|
32
|
+
@player.health.should == @initial_health - (10 * 2)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "assigns a treasure for points during a player's turn" do
|
37
|
+
game = Game.new("Knuckleheads")
|
38
|
+
player = Player.new("moe")
|
39
|
+
|
40
|
+
game.add_player(player)
|
41
|
+
|
42
|
+
game.play(1)
|
43
|
+
|
44
|
+
player.points.should_not be_zero
|
45
|
+
|
46
|
+
# or use alternate expectation syntax:
|
47
|
+
# expect(player.points).not_to be_zero
|
48
|
+
end
|
49
|
+
|
50
|
+
it "computes total points as the sum of all player points" do
|
51
|
+
game = Game.new("Knuckleheads")
|
52
|
+
|
53
|
+
player1 = Player.new("moe")
|
54
|
+
player2 = Player.new("larry")
|
55
|
+
|
56
|
+
game.add_player(player1)
|
57
|
+
game.add_player(player2)
|
58
|
+
|
59
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
60
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
61
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
62
|
+
|
63
|
+
game.total_points.should == 500
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'jkims_game/player'
|
2
|
+
require 'jkims_game/treasure_trove'
|
3
|
+
|
4
|
+
module JkimsGame
|
5
|
+
describe Player do
|
6
|
+
before do
|
7
|
+
$stdout = StringIO.new
|
8
|
+
@initial_health = 150
|
9
|
+
@player = Player.new("larry", @initial_health)
|
10
|
+
end
|
11
|
+
it "has a capitalized name" do
|
12
|
+
@player.name.should == "Larry"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "has an initial health" do
|
16
|
+
@player.health.should == 150
|
17
|
+
end
|
18
|
+
|
19
|
+
it "has a string representation" do
|
20
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
21
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
22
|
+
|
23
|
+
@player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
|
24
|
+
end
|
25
|
+
|
26
|
+
it "computes a score as the sum of its health and points" do
|
27
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
28
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
29
|
+
|
30
|
+
@player.score.should == 250
|
31
|
+
end
|
32
|
+
|
33
|
+
it "increases health by 15 when w00ted" do
|
34
|
+
@player.w00t
|
35
|
+
@player.health == @initial_health + 15
|
36
|
+
end
|
37
|
+
|
38
|
+
it "decreases health by 10 when blammed" do
|
39
|
+
@player.blam
|
40
|
+
@player.health == @initial_health - 10
|
41
|
+
end
|
42
|
+
|
43
|
+
context "with a health greater than 100" do
|
44
|
+
before do
|
45
|
+
@player = Player.new("larry", 150)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "is strong" do
|
49
|
+
@player.should be_strong
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "with a health less than or equal to 100" do
|
54
|
+
before do
|
55
|
+
@player = Player.new("larry", 100)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "is not 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
|
+
it "can be created from a CSV string" do
|
115
|
+
player = Player.from_csv("larry,150")
|
116
|
+
|
117
|
+
player.name.should == "Larry"
|
118
|
+
player.health.should == 150
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'jkims_game/treasure_trove'
|
2
|
+
|
3
|
+
module JkimsGame
|
4
|
+
describe Treasure do
|
5
|
+
before do
|
6
|
+
@treasure = Treasure.new(:hammer, 50)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has a name attribute" do
|
10
|
+
@treasure.name.should == :hammer
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has a points attribute" do
|
14
|
+
@treasure.points.should == 50
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
describe TreasureTrove do
|
20
|
+
|
21
|
+
it "has six treasures" do
|
22
|
+
TreasureTrove::TREASURES.size.should == 6
|
23
|
+
end
|
24
|
+
|
25
|
+
it "has a pie worth 5 points" do
|
26
|
+
TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "has a bottle worth 25 points" do
|
30
|
+
TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "has a hammer worth 50 points" do
|
34
|
+
TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "has a skillet worth 100 points" do
|
38
|
+
TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "has a broomstick worth 200 points" do
|
42
|
+
TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "has a crowbar worth 400 points" do
|
46
|
+
TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns a random treasure" do
|
50
|
+
treasure = TreasureTrove.random
|
51
|
+
|
52
|
+
TreasureTrove::TREASURES.should include(treasure)
|
53
|
+
|
54
|
+
# or use alternate expectation syntax:
|
55
|
+
# expect(TreasureTrove::TREASURES).to include(treasure)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jkims_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jongmin Kim
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-21 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: 'This is a game created as an exercise of The Pragmatic Studio''s Ruby
|
28
|
+
Programming course.
|
29
|
+
|
30
|
+
'
|
31
|
+
email: jkim@firstclass.codes
|
32
|
+
executables:
|
33
|
+
- jkims_game
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- LICENSE
|
38
|
+
- README
|
39
|
+
- bin/jkims_game
|
40
|
+
- bin/players.csv
|
41
|
+
- lib/jkims_game/auditable.rb
|
42
|
+
- lib/jkims_game/berserk_player.rb
|
43
|
+
- lib/jkims_game/clumsy_player.rb
|
44
|
+
- lib/jkims_game/die.rb
|
45
|
+
- lib/jkims_game/game.rb
|
46
|
+
- lib/jkims_game/game_turn.rb
|
47
|
+
- lib/jkims_game/loaded_die.rb
|
48
|
+
- lib/jkims_game/playable.rb
|
49
|
+
- lib/jkims_game/player.rb
|
50
|
+
- lib/jkims_game/treasure_trove.rb
|
51
|
+
- spec/jkims_game/berserk_player_spec.rb
|
52
|
+
- spec/jkims_game/clumsy_player_spec.rb
|
53
|
+
- spec/jkims_game/game_spec.rb
|
54
|
+
- spec/jkims_game/player_spec.rb
|
55
|
+
- spec/jkims_game/treasure_trove_spec.rb
|
56
|
+
homepage: ''
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.9'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.5.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: This is a game created as an exercise of The Pragmatic Studio's Ruby Programming
|
80
|
+
course.
|
81
|
+
test_files:
|
82
|
+
- spec/jkims_game/clumsy_player_spec.rb
|
83
|
+
- spec/jkims_game/treasure_trove_spec.rb
|
84
|
+
- spec/jkims_game/berserk_player_spec.rb
|
85
|
+
- spec/jkims_game/player_spec.rb
|
86
|
+
- spec/jkims_game/game_spec.rb
|