yard_game 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +19 -0
- data/README +29 -0
- data/bin/my_favorite_players.csv +3 -0
- data/bin/players.csv +3 -0
- data/bin/yard_game +40 -0
- data/lib/yard_game/auditable.rb +8 -0
- data/lib/yard_game/berserk_player.rb +38 -0
- data/lib/yard_game/clumsy_player.rb +43 -0
- data/lib/yard_game/die.rb +19 -0
- data/lib/yard_game/game.rb +114 -0
- data/lib/yard_game/game_turn.rb +30 -0
- data/lib/yard_game/loaded_die.rb +16 -0
- data/lib/yard_game/playable.rb +18 -0
- data/lib/yard_game/player.rb +65 -0
- data/lib/yard_game/treasure_trove.rb +19 -0
- data/spec/yard_game/berserk_player_spec.rb +31 -0
- data/spec/yard_game/clumsy_player_spec.rb +51 -0
- data/spec/yard_game/game_spec.rb +85 -0
- data/spec/yard_game/player_spec.rb +129 -0
- data/spec/yard_game/treasure_trove_spec.rb +57 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 69219f511fba5afffafec9840a7484b9c54556a5a3df1fc39af384c7a8b0102b
|
4
|
+
data.tar.gz: cbec1186f0eb86e38e8063691375c69eaaa909d220dce1f0cc4505672d003f09
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3af1faa7d54f8e96081bda60b20fbe4753e720365b4519704cbbf193a6087203209e72ffba63b87f43e366b591371e9d61edbd3b85a28b3b5914bedfc232dbea
|
7
|
+
data.tar.gz: 9aa1df3f86e426157ed6b69483ed30b3e1201f4c26d12b31f39df74cb146315602a0b3c4a8e0baffd57a55e35c83fc75dc6cbac03925d6f9db0084be08eab8b7
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2022 Paulette Erijo
|
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,29 @@
|
|
1
|
+
## YardGame
|
2
|
+
A simple game app created with ruby.
|
3
|
+
|
4
|
+
## Table of Contents
|
5
|
+
* [General info](#general-info)
|
6
|
+
* [Technologies](#technologies)
|
7
|
+
* [Setup](#setup)
|
8
|
+
* [Sources](#sources)
|
9
|
+
|
10
|
+
## Introduction
|
11
|
+
YardGame was while taking course by Nicole and Mike, the Pragmatic duo. Its a
|
12
|
+
game that allow player earn points on treasures found in the yard. Points are
|
13
|
+
added to the player's health to gain higher scores. It involves a random die roll.
|
14
|
+
|
15
|
+
## Technologies
|
16
|
+
* Ruby 3
|
17
|
+
* Rspec 3
|
18
|
+
|
19
|
+
## Setup
|
20
|
+
To run this project, install it locally.
|
21
|
+
|
22
|
+
$ gem install yard_game_1.0.0.gem
|
23
|
+
|
24
|
+
You can load players from the command line in a CSV file. You can also run the
|
25
|
+
game without specifying a player file. A default players.csv file is packaged
|
26
|
+
in the gem.
|
27
|
+
|
28
|
+
## Sources
|
29
|
+
This app was inspired by Mike and Nicole Clark of The Pragmatic Studio.
|
data/bin/players.csv
ADDED
data/bin/yard_game
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/yard_game/player'
|
4
|
+
require_relative '../lib/yard_game/game'
|
5
|
+
require_relative '../lib/yard_game/clumsy_player'
|
6
|
+
require_relative '../lib/yard_game/berserk_player'
|
7
|
+
|
8
|
+
|
9
|
+
knuckleheads = YardGame::Game.new("knuckleheads")
|
10
|
+
default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
|
11
|
+
knuckleheads.load_players(ARGV.shift || default_player_file)
|
12
|
+
clumsy = YardGame::ClumsyPlayer.new("klutz", 105)
|
13
|
+
knuckleheads.add_player(clumsy)
|
14
|
+
berserker = YardGame::BerserkPlayer.new("berserker", 50)
|
15
|
+
knuckleheads.add_player(berserker)
|
16
|
+
|
17
|
+
loop do
|
18
|
+
puts "\nHow 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 puts "Please enter a number or quit"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
knuckleheads.save_high_scores
|
30
|
+
|
31
|
+
#chipmunks = Game.new("chipmunks")
|
32
|
+
#chipmunks.add_player(player4)
|
33
|
+
#chipmunks.add_player(player5)
|
34
|
+
#chipmunks.add_player(player6)
|
35
|
+
#puts "Welcome to Chipmunks Game"
|
36
|
+
#puts "\nHow many game rounds? ('quit' to exit)"
|
37
|
+
#answer = gets.chomp()
|
38
|
+
#chipmunks.play(answer.to_i)
|
39
|
+
#puts "Enjoy your game"
|
40
|
+
#chipmunks.print_stats
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module YardGame
|
4
|
+
class BerserkPlayer < Player
|
5
|
+
|
6
|
+
def initialize(name, health=100)
|
7
|
+
super(name, health)
|
8
|
+
@w00t_count = 0
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
def berserk?
|
13
|
+
@w00t_count > 5
|
14
|
+
end
|
15
|
+
|
16
|
+
def w00t
|
17
|
+
super
|
18
|
+
@w00t_count += 1
|
19
|
+
puts "#{@name} is berserk!" if berserk?
|
20
|
+
end
|
21
|
+
|
22
|
+
def blam
|
23
|
+
if berserk?
|
24
|
+
w00t
|
25
|
+
else super
|
26
|
+
end
|
27
|
+
# Using the ternary operator instead if ? berserk? w00t else : super
|
28
|
+
# berserk? ? w00t : super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
if __FILE__ == $0
|
34
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
35
|
+
6.times { berserker.w00t }
|
36
|
+
2.times { berserker.blam }
|
37
|
+
puts berserker.health
|
38
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative 'player'
|
2
|
+
|
3
|
+
module YardGame
|
4
|
+
class ClumsyPlayer < Player
|
5
|
+
attr_reader :boost_factor
|
6
|
+
|
7
|
+
def initialize(name, health=100, boost_factor=1)
|
8
|
+
super(name, health)
|
9
|
+
@boost_factor = boost_factor
|
10
|
+
end
|
11
|
+
|
12
|
+
def w00t
|
13
|
+
@boost_factor.times { super }
|
14
|
+
# @health += @boost_factor
|
15
|
+
end
|
16
|
+
|
17
|
+
def found_treasure(treasure)
|
18
|
+
damanged_treasure = Treasure.new(treasure.name, treasure.points / 2.0)
|
19
|
+
super(damanged_treasure)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if __FILE__ == $0
|
25
|
+
clumsy = ClumsyPlayer.new("klutz", 50, 10)
|
26
|
+
|
27
|
+
hammer = Treasure.new(:hammer, 50)
|
28
|
+
clumsy.found_treasure(hammer)
|
29
|
+
clumsy.found_treasure(hammer)
|
30
|
+
clumsy.found_treasure(hammer)
|
31
|
+
|
32
|
+
crowbar = Treasure.new(:crowbar, 400)
|
33
|
+
clumsy.found_treasure(crowbar)
|
34
|
+
|
35
|
+
clumsy.each_found_treasure do |treasure|
|
36
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
37
|
+
end
|
38
|
+
puts "#{clumsy.points} grand total points"
|
39
|
+
|
40
|
+
|
41
|
+
3.times { clumsy.w00t }
|
42
|
+
puts "#{clumsy.name} has #{clumsy.health}"
|
43
|
+
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 YardGame
|
7
|
+
class Game
|
8
|
+
attr_reader :title
|
9
|
+
|
10
|
+
def initialize(title)
|
11
|
+
@g_title = title.capitalize
|
12
|
+
@players = []
|
13
|
+
end
|
14
|
+
|
15
|
+
# Using File class method
|
16
|
+
# def load_players(from_file)
|
17
|
+
# File.readlines(from_file).each do |line|
|
18
|
+
# add_player(Player.from_csv(line))
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
|
22
|
+
# Using CSV class method
|
23
|
+
def load_players(from_file)
|
24
|
+
CSV.foreach(from_file) do |row|
|
25
|
+
player = Player.new(row[0], row[1].to_i)
|
26
|
+
add_player(player)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_player(a_player)
|
31
|
+
@players.push(a_player) # @players << a_player
|
32
|
+
end
|
33
|
+
|
34
|
+
def play(rounds)
|
35
|
+
puts "*" * 40
|
36
|
+
puts "There are #{@players.size} players in #{@g_title}:"
|
37
|
+
@players.each do |player|
|
38
|
+
puts player
|
39
|
+
end
|
40
|
+
|
41
|
+
treasures = TreasureTrove::TREASURES
|
42
|
+
puts "\nThere are #{treasures.size} treasures to be found:"
|
43
|
+
treasures.each do |treasure|
|
44
|
+
puts "A #{treasure.name} is worth #{treasure.points} points"
|
45
|
+
end
|
46
|
+
|
47
|
+
1.upto(rounds) do |round|
|
48
|
+
puts "\nRound #{round}:"
|
49
|
+
@players.each do |player|
|
50
|
+
GameTurn.take_turn(player)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def print_name_and_health(player)
|
56
|
+
puts "#{player.name} : (#{player.health})"
|
57
|
+
end
|
58
|
+
|
59
|
+
def print_stats
|
60
|
+
strong_players, wimpy_players = @players.partition { |player| player.strong? }
|
61
|
+
|
62
|
+
puts "\n#{@g_title} Statistics:"
|
63
|
+
|
64
|
+
puts "\n#{strong_players.count} strong players:"
|
65
|
+
strong_players.each do |player|
|
66
|
+
print_name_and_health(player)
|
67
|
+
end
|
68
|
+
|
69
|
+
puts "\n#{wimpy_players.count} wimpy players:"
|
70
|
+
wimpy_players.each do |player|
|
71
|
+
print_name_and_health(player)
|
72
|
+
end
|
73
|
+
|
74
|
+
puts "#{@g_title} High Scores:"
|
75
|
+
@players.each do |player|
|
76
|
+
puts high_scores_entry(player)
|
77
|
+
end
|
78
|
+
|
79
|
+
puts "\nPoints:"
|
80
|
+
@players.each do |player|
|
81
|
+
puts "#{player.name}'s point totals:"
|
82
|
+
puts "#{player.points} grand total points"
|
83
|
+
end
|
84
|
+
puts "#{total_points} total points from treasures found"
|
85
|
+
|
86
|
+
@players.sort.each do |player|
|
87
|
+
puts "\n#{player.name}'s point totals:"
|
88
|
+
player.each_found_treasure do |treasure|
|
89
|
+
puts "#{treasure.points} total #{treasure.name} points"
|
90
|
+
end
|
91
|
+
player_total_points = "#{player.points} grand total points"
|
92
|
+
puts player_total_points.upcase
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def high_scores_entry(player)
|
97
|
+
formatted_name = player.name.ljust(20, '.')
|
98
|
+
"#{formatted_name} #{player.score}"
|
99
|
+
end
|
100
|
+
|
101
|
+
def total_points
|
102
|
+
@players.reduce(0) { |sum, player| sum + player.points }
|
103
|
+
end
|
104
|
+
|
105
|
+
def save_high_scores(score_to_file= "high_scores.txt")
|
106
|
+
File.open(score_to_file, 'w') do |file|
|
107
|
+
file.puts "#{@g_title} High Scores:"
|
108
|
+
@players.sort.each do |player|
|
109
|
+
file.puts high_scores_entry(player)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'die'
|
2
|
+
#require_relative 'loaded_die'
|
3
|
+
require_relative 'player'
|
4
|
+
require_relative 'treasure_trove'
|
5
|
+
|
6
|
+
module YardGame
|
7
|
+
module GameTurn
|
8
|
+
# this is referred as a module method. Its defined on the module itself
|
9
|
+
# self refers to the module beight defined. ie. GameTurn. So we need to
|
10
|
+
# define the method with the form self.xxx.
|
11
|
+
def self.take_turn(player)
|
12
|
+
rolled_die = Die.new
|
13
|
+
#rolled_die = LoadedDie.new
|
14
|
+
number_rolled = rolled_die.roll
|
15
|
+
|
16
|
+
case number_rolled
|
17
|
+
when 1..2
|
18
|
+
player.blam
|
19
|
+
when 3..4
|
20
|
+
puts "#{player.name} was skipped."
|
21
|
+
else
|
22
|
+
player.w00t
|
23
|
+
end
|
24
|
+
|
25
|
+
treasure = TreasureTrove.random
|
26
|
+
player.found_treasure(treasure)
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require_relative 'treasure_trove'
|
2
|
+
require_relative 'playable'
|
3
|
+
|
4
|
+
module YardGame
|
5
|
+
class Player
|
6
|
+
include Playable
|
7
|
+
attr_accessor :name, :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 each_found_treasure
|
16
|
+
@found_treasures.each do |name, points|
|
17
|
+
yield Treasure.new(name, points)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def points
|
23
|
+
@found_treasures.values.reduce(0, :+)
|
24
|
+
end
|
25
|
+
|
26
|
+
def found_treasure(treasure)
|
27
|
+
@found_treasures[treasure.name ] += treasure.points
|
28
|
+
puts "#{name} found a #{treasure.name} worth #{treasure.points} points."
|
29
|
+
puts "#{name}'s treasures: #{@found_treasures}"#{treasure.name}, #{treasure.points}.
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s # changed say_hello method to to_s to allow ruby implement to_s method to render a string
|
33
|
+
"I'm #{@name} with a health = #{@health}, points = #{points}, and score = #{score}."
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def score # This is a virtual accessor it returns the sum of the player's health and length of the player's name
|
38
|
+
@health + points
|
39
|
+
end
|
40
|
+
|
41
|
+
def name=(new_name)
|
42
|
+
@name = new_name.capitalize
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def <=> (other)
|
47
|
+
other.score <=> score
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.from_csv(string)
|
51
|
+
name, health = string.split(',')
|
52
|
+
new(name, Integer(health))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
if __FILE__ == $0 # used to enclose and hide sample code from other files requiring player.rb file
|
58
|
+
player = Player.new("moe")
|
59
|
+
puts player.name
|
60
|
+
puts player.health
|
61
|
+
player.w00t
|
62
|
+
puts player.health
|
63
|
+
player.blam
|
64
|
+
puts player.health
|
65
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module YardGame
|
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
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'yard_game/berserk_player'
|
2
|
+
|
3
|
+
module YardGame
|
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 w00ted up to 5 times" do
|
13
|
+
1.upto(5) { @player.w00t }
|
14
|
+
|
15
|
+
expect(@player.berserk?).to be_falsey
|
16
|
+
end
|
17
|
+
|
18
|
+
it "goes berserk when w00ted more than 5 times" do
|
19
|
+
1.upto(6) { @player.w00t }
|
20
|
+
|
21
|
+
expect(@player.berserk?).to be_truthy
|
22
|
+
end
|
23
|
+
|
24
|
+
it "get's w00ted instead of blammed when it's gone berserk" do
|
25
|
+
1.upto(6) { @player.w00t }
|
26
|
+
1.upto(2) { @player.blam }
|
27
|
+
|
28
|
+
expect(@player.health).to eq(@initial_health + (8 * 15))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'yard_game/clumsy_player'
|
2
|
+
|
3
|
+
module YardGame
|
4
|
+
describe ClumsyPlayer do
|
5
|
+
before do
|
6
|
+
@player = ClumsyPlayer.new("klutz")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "only gets half the point 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 "boost a clumsy player's health" do
|
33
|
+
before do
|
34
|
+
@initial_health = 50
|
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 a boost everytime its w00ted" do
|
44
|
+
@player = ClumsyPlayer.new("klutz", @initial_health, @boost_factor)
|
45
|
+
|
46
|
+
1.upto(3) { @player.w00t }
|
47
|
+
expect(@player.health).to eq(@initial_health + (15 * 15))
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'yard_game/game'
|
2
|
+
|
3
|
+
module YardGame
|
4
|
+
describe Game do
|
5
|
+
|
6
|
+
before do
|
7
|
+
$stdout = StringIO.new
|
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 "increases the player's health when a high number is rolled" do
|
17
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(5)
|
18
|
+
|
19
|
+
@game.play(2)
|
20
|
+
|
21
|
+
expect(@player.health).to eq(@initial_health + (15 * 2))
|
22
|
+
end
|
23
|
+
|
24
|
+
it "skips the player's health when a medium number is rolled" do
|
25
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(3)
|
26
|
+
|
27
|
+
@game.play(2)
|
28
|
+
|
29
|
+
expect(@player.health).to eq(@initial_health)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "decreases the player's health when a low number is rolled" do
|
33
|
+
allow_any_instance_of(Die).to receive(:roll).and_return(1)
|
34
|
+
|
35
|
+
@game.play(2)
|
36
|
+
|
37
|
+
expect(@player.health).to eq(@initial_health - (10 * 2))
|
38
|
+
end
|
39
|
+
|
40
|
+
context "players that are strong from their statistics" do
|
41
|
+
@game = Game.new("Knuckleheads")
|
42
|
+
|
43
|
+
@initial_health = 100
|
44
|
+
@player = Player.new("moe", @initial_health)
|
45
|
+
|
46
|
+
@game.add_player(@player)
|
47
|
+
|
48
|
+
# it "shows a strong player" do
|
49
|
+
# @game.print_stats
|
50
|
+
|
51
|
+
# expect(@player).to eq( "Moe : #{@initial_health}" )
|
52
|
+
# end
|
53
|
+
|
54
|
+
# it "shows a winpy player"
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
it "assigns a treasure for points during a player's turn" do
|
59
|
+
game = Game.new("Knuckleheads")
|
60
|
+
player = Player.new("moe")
|
61
|
+
|
62
|
+
game.add_player(player)
|
63
|
+
|
64
|
+
game.play(1)
|
65
|
+
|
66
|
+
expect(player.points).not_to eq(0)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "computes total points as the sum of all player points" do
|
70
|
+
@game = Game.new("Knuckleheads")
|
71
|
+
|
72
|
+
player1 = Player.new("moe")
|
73
|
+
player2 = Player.new("larry")
|
74
|
+
|
75
|
+
@game.add_player(player1)
|
76
|
+
@game.add_player(player2)
|
77
|
+
|
78
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
79
|
+
player1.found_treasure(Treasure.new(:hammer, 50))
|
80
|
+
player2.found_treasure(Treasure.new(:crowbar, 400))
|
81
|
+
|
82
|
+
expect(@game.total_points).to eq(500)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'yard_game/player'
|
2
|
+
require 'yard_game/treasure_trove'
|
3
|
+
|
4
|
+
module YardGame
|
5
|
+
describe Player do
|
6
|
+
before do
|
7
|
+
$stdout = StringIO.new # writes standard output to string instead of console
|
8
|
+
@initial_health = 150
|
9
|
+
@player = Player.new("larry", @initial_health)
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
it "has a capitalized name" do
|
14
|
+
expect(@player.name).to eq("Larry")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "has an initial health" do
|
18
|
+
expect(@player.health).to eq(150)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has a string representation" do
|
22
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
23
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
24
|
+
|
25
|
+
expect(@player.to_s).to eq("I'm Larry with a health = 150, points = 100, and score = 250.")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "computes a score as the sum of its health and length of name" do
|
29
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
30
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
31
|
+
|
32
|
+
expect(@player.score).to eq(250)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "increases health by 15 when w00ted" do
|
36
|
+
@player.w00t
|
37
|
+
|
38
|
+
expect(@player.health).to eq(@initial_health + 15 )
|
39
|
+
end
|
40
|
+
|
41
|
+
it "decreasrs health by 10 when blammed" do
|
42
|
+
@player.blam
|
43
|
+
|
44
|
+
expect(@player.health).to eq(@initial_health - 10 )
|
45
|
+
end
|
46
|
+
|
47
|
+
context "player's initial health greater than 100" do
|
48
|
+
before do
|
49
|
+
@player = Player.new("larry", 150)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "is strong" do
|
53
|
+
expect(@player).to be_strong
|
54
|
+
#@player.strong?.should be_truthy
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "player's initial health is 100" do
|
59
|
+
before do
|
60
|
+
@player = Player.new("larry", 100)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "is wimpy" do
|
64
|
+
expect(@player).not_to be_strong
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "in a collection of players" do
|
69
|
+
before do
|
70
|
+
@player1 = Player.new("moe", 100)
|
71
|
+
@player2 = Player.new("larry", 200)
|
72
|
+
@player3 = Player.new("curly", 300)
|
73
|
+
|
74
|
+
@players = [@player1, @player2, @player3]
|
75
|
+
end
|
76
|
+
|
77
|
+
it "is sorted by decreasing score" do
|
78
|
+
expect(@players.sort).to eq([@player3, @player2, @player1])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "computes points as the sum of all treasure points" do
|
83
|
+
expect(@player.points).to eq(0)
|
84
|
+
|
85
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
86
|
+
|
87
|
+
expect(@player.points).to eq(50)
|
88
|
+
|
89
|
+
@player.found_treasure(Treasure.new(:crowbar, 400))
|
90
|
+
|
91
|
+
expect(@player.points).to eq(450)
|
92
|
+
|
93
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
94
|
+
|
95
|
+
expect(@player.points).to eq(500)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "yields each found treasure and its total points" do
|
99
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
100
|
+
@player.found_treasure(Treasure.new(:skillet, 100))
|
101
|
+
@player.found_treasure(Treasure.new(:hammer, 50))
|
102
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
103
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
104
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
105
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
106
|
+
@player.found_treasure(Treasure.new(:bottle, 5))
|
107
|
+
|
108
|
+
yielded = []
|
109
|
+
|
110
|
+
@player.each_found_treasure do |treasure|
|
111
|
+
yielded << treasure
|
112
|
+
end
|
113
|
+
|
114
|
+
expect(yielded).to eq([
|
115
|
+
Treasure.new(:skillet, 200),
|
116
|
+
Treasure.new(:hammer, 50),
|
117
|
+
Treasure.new(:bottle, 25)
|
118
|
+
])
|
119
|
+
end
|
120
|
+
|
121
|
+
it "has a feature that read csv files" do
|
122
|
+
player = Player.from_csv("Alvin, 100")
|
123
|
+
|
124
|
+
expect(player.name).to eq("Alvin")
|
125
|
+
expect(player.health).to eq(100)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'yard_game/treasure_trove'
|
2
|
+
|
3
|
+
module YardGame
|
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 skillset 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
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yard_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paulette Erijo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-22 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: '3.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.0.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
description: |
|
34
|
+
## YardGame
|
35
|
+
A simple game app created with ruby.
|
36
|
+
|
37
|
+
## Table of Contents
|
38
|
+
* [General info](#general-info)
|
39
|
+
* [Technologies](#technologies)
|
40
|
+
* [Setup](#setup)
|
41
|
+
* [Sources](#sources)
|
42
|
+
|
43
|
+
## Introduction
|
44
|
+
YardGame was while taking course by Nicole and Mike, the Pragmatic duo. Its a
|
45
|
+
game that allow player earn points on treasures found in the yard. Points are
|
46
|
+
added to the player's health to gain higher scores. It involves a random die roll.
|
47
|
+
|
48
|
+
## Technologies
|
49
|
+
* Ruby 3
|
50
|
+
* Rspec 3
|
51
|
+
|
52
|
+
## Setup
|
53
|
+
To run this project, install it locally.
|
54
|
+
|
55
|
+
$ gem install yard_game_1.0.0.gem
|
56
|
+
|
57
|
+
You can load players from the command line in a CSV file. You can also run the
|
58
|
+
game without specifying a player file. A default players.csv file is packaged
|
59
|
+
in the gem.
|
60
|
+
|
61
|
+
## Sources
|
62
|
+
This app was inspired by Mike and Nicole Clark of The Pragmatic Studio.
|
63
|
+
email: paulette@letche.net
|
64
|
+
executables:
|
65
|
+
- yard_game
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- LICENSE
|
70
|
+
- README
|
71
|
+
- bin/my_favorite_players.csv
|
72
|
+
- bin/players.csv
|
73
|
+
- bin/yard_game
|
74
|
+
- lib/yard_game/auditable.rb
|
75
|
+
- lib/yard_game/berserk_player.rb
|
76
|
+
- lib/yard_game/clumsy_player.rb
|
77
|
+
- lib/yard_game/die.rb
|
78
|
+
- lib/yard_game/game.rb
|
79
|
+
- lib/yard_game/game_turn.rb
|
80
|
+
- lib/yard_game/loaded_die.rb
|
81
|
+
- lib/yard_game/playable.rb
|
82
|
+
- lib/yard_game/player.rb
|
83
|
+
- lib/yard_game/treasure_trove.rb
|
84
|
+
- spec/yard_game/berserk_player_spec.rb
|
85
|
+
- spec/yard_game/clumsy_player_spec.rb
|
86
|
+
- spec/yard_game/game_spec.rb
|
87
|
+
- spec/yard_game/player_spec.rb
|
88
|
+
- spec/yard_game/treasure_trove_spec.rb
|
89
|
+
homepage: https://rubygems.org
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '1.9'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubygems_version: 3.3.4
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A ruby game where players search for treasures in a yard and earn points.
|
112
|
+
test_files:
|
113
|
+
- spec/yard_game/berserk_player_spec.rb
|
114
|
+
- spec/yard_game/clumsy_player_spec.rb
|
115
|
+
- spec/yard_game/game_spec.rb
|
116
|
+
- spec/yard_game/player_spec.rb
|
117
|
+
- spec/yard_game/treasure_trove_spec.rb
|