studio_game_ex_PS 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6f0af8b1af323c9f2c8209c97a589753cff79cd954790939365941d9e405b3e3
4
+ data.tar.gz: 425b0e018b3071a33cf1e416f74ac3d7963f7e0383feb92ac3f6754631989b52
5
+ SHA512:
6
+ metadata.gz: c5b68fdf8a51a1af9ad3b01b96c614c0929d26b7de22b44842b99d0300dd75ad784b269fa4a9efe777b841c11883646a83354cb7c626d198f16ee2310bb590d7
7
+ data.tar.gz: f6ad833afd630bd5ef88c2548e1b9768db62c4bf0ce676e0a77453823642f001b91b0286180fcd4a45d77f623b6d95701ac8922623b462f0a21512df40f89b00
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 The Pragmatic Studio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ - You may not use this Software in other training contexts.
13
+
14
+ - The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # Studio Game
2
+
3
+ This is an example application used in The Pragmatic Studio's
4
+ [Ruby video course](https://pragmaticstudio.com/courses/ruby).
5
+
6
+ ## Usage
7
+
8
+ To run the example main program file, use:
9
+
10
+ ```sh
11
+ studio_game
12
+ ```
13
+
14
+ You can also specify a CSV file with movie titles and ranks:
15
+
16
+ ```sh
17
+ studio_game my_players.csv
18
+ ```
19
+
20
+ ## License
21
+
22
+ This code is available as open source under the terms of the LICENSE.txt file.
data/bin/players.csv ADDED
@@ -0,0 +1,5 @@
1
+ Starlord,50
2
+ Gamora,75
3
+ Rocket,100
4
+ Drax, 125
5
+ Groot,80
data/bin/studio_game ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/studio_game/clumsy_player"
4
+ require_relative "../lib/studio_game/berserk_player"
5
+ require_relative "../lib/studio_game/game"
6
+
7
+ game = StudioGame::Game.new("Guardians")
8
+
9
+ clumsy = StudioGame::ClumsyPlayer.new("klutz", 105, 3)
10
+ game.add_player(clumsy)
11
+
12
+ berserker = StudioGame::BerserkPlayer.new("berserker", 50)
13
+ game.add_player(berserker)
14
+
15
+ players_file = File.join(__dir__, "players.csv")
16
+ game.load_players(ARGV.shift || players_file)
17
+
18
+ loop do
19
+
20
+ print "\nHow many game rounds? ('quit' to exit) "
21
+
22
+ answer = gets.chomp.downcase
23
+
24
+ case answer
25
+ when /^\d+$/
26
+ game.play(answer.to_i)
27
+ when "quit", "exit"
28
+ game.print_stats
29
+ break
30
+ else
31
+ puts "Please enter a number or 'quit'"
32
+ end
33
+ end
34
+
35
+ game.save_high_scores
@@ -0,0 +1,7 @@
1
+ module StudioGame
2
+ module Auditable
3
+ def audit(number)
4
+ puts "Audit: Rolled a #{number}"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class BerserkPlayer < Player
5
+
6
+ def initialize(name, health = 100)
7
+ super(name, health)
8
+ @boost_count = 0
9
+ end
10
+
11
+ def berserk?
12
+ @boost_count > 5
13
+ end
14
+
15
+ def boost
16
+ super
17
+ @boost_count += 1
18
+ puts "#{@name} is berserk!" if berserk?
19
+ end
20
+
21
+ def drain
22
+ berserk? ? boost : super
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+ if __FILE__ == $0
29
+ berserker = BerserkPlayer.new("berserker", 50)
30
+ 6.times { berserker.boost }
31
+ 2.times { berserker.drain }
32
+ puts berserker.health
33
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class ClumsyPlayer < Player
5
+
6
+ def initialize(name, health = 100, boost_factor = 1)
7
+ super(name, health)
8
+ @boost_factor = boost_factor
9
+ end
10
+
11
+ def boost
12
+ @boost_factor.times { super }
13
+ end
14
+
15
+ def found_treasure(name, points)
16
+ points = points / 2.0
17
+ super(name, points)
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ if __FILE__ == $0
24
+ clumsy = ClumsyPlayer.new("klutz")
25
+
26
+ clumsy.found_treasure("flute", 50)
27
+ clumsy.found_treasure("flute", 50)
28
+ clumsy.found_treasure("flute", 50)
29
+ clumsy.found_treasure("star", 100)
30
+
31
+ clumsy.found_treasures.each do |name, points|
32
+ puts "#{name}: #{points} points"
33
+ end
34
+ puts "#{clumsy.points} total points"
35
+ end
@@ -0,0 +1,107 @@
1
+ require_relative "treasure_trove"
2
+ require_relative "auditable"
3
+
4
+ module StudioGame
5
+ class Game
6
+ include Auditable
7
+
8
+ attr_reader :title, :players
9
+
10
+ def initialize(title)
11
+ @title = title
12
+ @players = []
13
+ end
14
+
15
+ def load_players(from_file)
16
+ File.readlines(from_file, chomp:true).each do |line|
17
+ player = Player.from_csv(line)
18
+ add_player(player)
19
+ end
20
+ rescue Errno::ENOENT
21
+ puts "Whoops, #{from_file} not found!"
22
+ exit 1
23
+ end
24
+
25
+ def add_player(player)
26
+ @players << player
27
+ end
28
+
29
+ def roll_die
30
+ number = rand(1..6)
31
+ audit(number)
32
+ number
33
+ end
34
+
35
+ def play(rounds = 1)
36
+ puts "\nLet's play #{@title}!"
37
+
38
+ puts "\nThe following treasures can be found:"
39
+ puts TreasureTrove.treasure_items
40
+
41
+ puts "\nBefore playing:"
42
+ puts @players
43
+
44
+ 1.upto(rounds) do |round|
45
+ puts "\nRound #{round}:"
46
+ @players.each do |player|
47
+ number_rolled = roll_die
48
+ case number_rolled
49
+ when 1..2
50
+ player.drain
51
+ puts "#{player.name} got drained 😩"
52
+ when 3..4
53
+ puts "#{player.name} got skipped"
54
+ else
55
+ player.boost
56
+ puts "#{player.name} got boosted 😁"
57
+ end
58
+
59
+ treasure = TreasureTrove.random_treasure
60
+ player.found_treasure(treasure.name, treasure.points)
61
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
62
+ end
63
+ end
64
+
65
+ puts "\nAfter playing:"
66
+ puts @players
67
+ end
68
+
69
+ def sorted_players
70
+ @players.sort_by { |player| player.score }.reverse
71
+ end
72
+
73
+ def high_score_entry (player)
74
+ name = player.name.ljust(20, ".")
75
+ score = player.score.round.to_s.rjust(5)
76
+ "#{name}#{score}"
77
+ end
78
+
79
+ def print_stats
80
+ puts "\n Game Stats:"
81
+ puts "-" * 30
82
+ puts sorted_players
83
+
84
+ @players.each do |player|
85
+ puts "\n#{player.name}'s treasure point totals:"
86
+ player.found_treasures.each do |name, points|
87
+ puts "#{name}: #{points}"
88
+ end
89
+ puts "total: #{player.points}"
90
+ end
91
+
92
+ puts "\nHigh Scores:"
93
+ sorted_players.each do |player|
94
+ puts high_score_entry(player)
95
+ end
96
+ end
97
+
98
+ def save_high_scores (to_file = "high_scores.txt")
99
+ File.open(to_file, "w") do |file|
100
+ file.puts "#{@title} High Scores:"
101
+ sorted_players.each do |player|
102
+ file.puts high_score_entry(player)
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,13 @@
1
+ module StudioGame
2
+ module Playable
3
+
4
+ def drain
5
+ self.health -= 10
6
+ end
7
+
8
+ def boost
9
+ self.health += 15
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,53 @@
1
+ require_relative "playable"
2
+
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+
7
+ attr_accessor :name, :health
8
+ attr_reader :found_treasures
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(line)
17
+ name, health = line.split(',')
18
+ Player.new(name, Integer(health))
19
+ rescue ArgumentError
20
+ puts "Ignored imvalid health: #{health}"
21
+ Player.new(name)
22
+ end
23
+
24
+ def found_treasure(name, points)
25
+ @found_treasures[name] += points
26
+ end
27
+
28
+ def name=(new_name)
29
+ @name = new_name.capitalize
30
+ end
31
+
32
+ def points
33
+ @found_treasures.values.sum
34
+ end
35
+
36
+ def score
37
+ @health + points
38
+ end
39
+
40
+ def to_s = "I'm #{@name} with a health = #{@health}, points = #{points} and score = #{score}"
41
+
42
+ end
43
+ end
44
+
45
+ if __FILE__ == $0
46
+ player = Player.new("jase")
47
+ puts player.name
48
+ puts player.health
49
+ player.boost
50
+ puts player.health
51
+ player.drain
52
+ puts player.health
53
+ end
@@ -0,0 +1,23 @@
1
+ module StudioGame
2
+ module TreasureTrove
3
+ Treasure = Data.define(:name, :points)
4
+
5
+ TREASURES = [
6
+ Treasure.new("pie", 10),
7
+ Treasure.new("coin", 25),
8
+ Treasure.new("flute", 50),
9
+ Treasure.new("compass", 65),
10
+ Treasure.new("key", 80),
11
+ Treasure.new("crown", 90),
12
+ Treasure.new("star", 100)
13
+ ]
14
+
15
+ def self.random_treasure
16
+ TREASURES.sample
17
+ end
18
+
19
+ def self.treasure_items
20
+ TREASURES.map {|treasure| "A #{treasure.name} is worth #{treasure.points} points"}
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: studio_game_ex_PS
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tania Ramirez
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email: tregina7018@gmail.com
13
+ executables:
14
+ - studio_game
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - LICENSE.txt
19
+ - README.md
20
+ - bin/players.csv
21
+ - bin/studio_game
22
+ - lib/studio_game/auditable.rb
23
+ - lib/studio_game/berserk_player.rb
24
+ - lib/studio_game/clumsy_player.rb
25
+ - lib/studio_game/game.rb
26
+ - lib/studio_game/playable.rb
27
+ - lib/studio_game/player.rb
28
+ - lib/studio_game/treasure_trove.rb
29
+ homepage: https://pragmaticstudio.com
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.6.7
48
+ specification_version: 4
49
+ summary: Studio game exercise from The Pragmatic Studio
50
+ test_files: []