studio_game_cduck 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: b7a66cc8a7fd33547bab284e8861130a989e1ae22e538ff97e1794166d93674f
4
+ data.tar.gz: '0018f92f1feff669faf50c97fc4062976c82e5e71555d019cba7b228f64c434b'
5
+ SHA512:
6
+ metadata.gz: a6f6c0eee4742eb7eaf0dcf10afebb622fe9437c1dd214d330d07af47759d0e57f4fac31f392bba9db0d8f65b4ca3dc338a25c71731f3edff330822008f9ece5
7
+ data.tar.gz: 44cfd8f6ad93cbe58410faff3440480af15341950d0599516d15f99b26e1f8cfe0af8b95c8fc2e09b7e09f8c68b1b1f0bba8479d7525e5100c59c3602cea0974
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2025 Patricio Estrada Carreto
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,2 @@
1
+ <h1>Studio Game</h1>
2
+ My first ruby app, which is also a gem!
data/bin/misfits.csv ADDED
@@ -0,0 +1,3 @@
1
+ Lucky Luke,🤠
2
+ Calvin,Infinity
3
+ Scooby Doo,🐶
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/players_2.csv ADDED
@@ -0,0 +1,4 @@
1
+ finn, 60
2
+ lucy, 90
3
+ jase, 100
4
+ alex, 125
data/bin/studio_game ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/studio_game/game"
4
+ require_relative "../lib/studio_game/clumsy_player"
5
+ require_relative "../lib/studio_game/berserk_player"
6
+
7
+ game = StudioGame::Game.new("Winner Takes All!")
8
+
9
+ klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
10
+ game.add_player(klutz)
11
+
12
+ berserker = StudioGame::BerserkPlayer.new("berserker", 105)
13
+ game.add_player(berserker)
14
+
15
+ players_file = File.join(__dir__, "players_2.csv")
16
+
17
+ game.load_players(ARGV.shift || players_file)
18
+
19
+ loop do
20
+
21
+ print "How many game rounds? ('quit' to exit) "
22
+ answer = gets.chomp.downcase
23
+
24
+ case answer
25
+ when /^\d+$/
26
+ game.play_game(answer.to_i)
27
+
28
+ when "quit", "exit"
29
+ game.print_stats
30
+ break
31
+
32
+ else
33
+ puts "Please enter a number or 'quit'"
34
+ end
35
+ end
36
+
37
+ 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,35 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class BerserkPlayer < Player
5
+ def initialize(name, health = 100)
6
+ @times_boosted = 0
7
+ super(name, health)
8
+ end
9
+
10
+ def boost
11
+ @times_boosted += 1
12
+ super
13
+ puts "#{@name} is berserk!" if berserk?
14
+ end
15
+
16
+ def berserk?
17
+ @times_boosted > 5
18
+ end
19
+
20
+ def drain
21
+ if berserk?
22
+ boost
23
+ else
24
+ super
25
+ end
26
+ end
27
+ end
28
+
29
+ if __FILE__ == $0
30
+ berserker = BerserkPlayer.new("berserker", 50)
31
+ 6.times { berserker.boost }
32
+ 2.times { berserker.drain }
33
+ puts berserker.health
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class ClumsyPlayer < Player
5
+ def found_treasure(name, points)
6
+ points /= 2.0
7
+ super(name, points)
8
+ end
9
+ end
10
+
11
+ if __FILE__ == $0
12
+ clumsy = ClumsyPlayer.new("klutz")
13
+
14
+ clumsy.found_treasure("flute", 50)
15
+ clumsy.found_treasure("flute", 50)
16
+ clumsy.found_treasure("flute", 50)
17
+ clumsy.found_treasure("star", 100)
18
+
19
+ clumsy.found_treasures.each do |name, points|
20
+ puts "#{name}: #{points} points"
21
+ end
22
+ puts "#{clumsy.points} total points"
23
+ end
24
+ end
@@ -0,0 +1,109 @@
1
+
2
+ require_relative "treasure_trove"
3
+ require_relative "auditable"
4
+
5
+ module StudioGame
6
+ class Game
7
+ include Auditable
8
+
9
+ attr_reader :title, :players
10
+
11
+ def initialize(title)
12
+ @title = title
13
+ @players = []
14
+ end
15
+
16
+ def add_player(player)
17
+ @players.push(player)
18
+ end
19
+
20
+ def roll_die
21
+ number = rand(1..6)
22
+ audit(number)
23
+ number
24
+ end
25
+
26
+ def play_game(rounds = 1)
27
+ puts @title
28
+ puts "\nThe following treasures can be found:"
29
+ puts TreasureTrove.treasure_items
30
+ 1.upto(rounds) do |n|
31
+ puts "Round #{n}"
32
+ puts "\nBefore playing:"
33
+ puts @players
34
+ puts "\nLet's play!"
35
+
36
+ @players.each do |player|
37
+
38
+ case roll_die
39
+ when 1..2
40
+ player.drain
41
+ puts "#{player.name} got drained 😩"
42
+ when 3..4
43
+ puts "#{player.name} got skipped"
44
+ else
45
+ player.boost
46
+ puts "#{player.name} got boosted 😁"
47
+ end
48
+
49
+ treasure = TreasureTrove.random_treasure
50
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points."
51
+ player.found_treasure(treasure.name, treasure.points)
52
+
53
+ end
54
+
55
+ puts "\nAfter playing:"
56
+ puts @players
57
+ end
58
+ end
59
+
60
+ def sorted_players
61
+ @players.sort_by {|player| player.score}.reverse
62
+ end
63
+
64
+ def print_stats
65
+ puts "\n Game Stats:"
66
+ puts "-" * 30
67
+ sorted_players.each do |player|
68
+ puts "\n#{player.name}'s found treasures"
69
+ player.found_treasures.each do |name, points|
70
+ puts "#{name} #{points}"
71
+ end
72
+ puts "\nTotal points = #{player.score}"
73
+ end
74
+ final_score
75
+ end
76
+
77
+ def final_score
78
+ puts "\n Final Score"
79
+ puts "-" * 30
80
+ sorted_players.each do |player|
81
+ print "#{player.name}"
82
+ print "." * 21
83
+ puts " #{player.score}"
84
+ end
85
+ end
86
+
87
+ def load_players(file_name)
88
+ File.readlines(file_name, chomp: true).each do |line|
89
+ player = Player.from_csv(line)
90
+ add_player(player)
91
+ end
92
+ rescue Errno::ENOENT
93
+ puts "ERROR: File '#{file_name}' doesn't exist!"
94
+ exit 1
95
+ end
96
+
97
+ def save_high_scores(to_file = "high_scores.txt")
98
+ File.open(to_file, "w") do |file|
99
+ file.puts "#{@title} High Scores:"
100
+
101
+ sorted_players.each do |player|
102
+ file.print "#{player.name}"
103
+ file.print "." * 21
104
+ file.puts " #{player.score}"
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,11 @@
1
+ module StudioGame
2
+ module Playable
3
+ def boost
4
+ @health += 15
5
+ end
6
+
7
+ def drain
8
+ @health -= 10
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ require_relative "playable"
2
+
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+
7
+ attr_reader :health, :found_treasures
8
+ attr_accessor :name
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 name=(new_name)
17
+ @name = new_name.capitalize
18
+ end
19
+
20
+ def score
21
+ @health + points
22
+ end
23
+
24
+ def points
25
+ @found_treasures.values.sum
26
+ end
27
+
28
+ def to_s
29
+ "I'm #{@name} with a health of #{@health} and a score of #{score} : #{@found_treasures}"
30
+ end
31
+
32
+ def found_treasure(name, points)
33
+ @found_treasures[name] += points
34
+ end
35
+
36
+ def self.from_csv(csv_line)
37
+ name, health = csv_line.split(",")
38
+ Player.new(name, Integer(health))
39
+
40
+ rescue ArgumentError
41
+ puts "Error: Invalid health property value '#{health}'. Setting default health value"
42
+ Player.new(name)
43
+ end
44
+ end
45
+
46
+ if __FILE__ == $0
47
+ player = Player.new("jase")
48
+ puts player.name
49
+ puts player.health
50
+ player.boost
51
+ puts player.health
52
+ player.drain
53
+ puts player.health
54
+ end
55
+ end
56
+
@@ -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,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: studio_game_cduck
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Patricio Estrada Carreto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-10-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: ''
15
+ executables:
16
+ - studio_game
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - bin/misfits.csv
23
+ - bin/players.csv
24
+ - bin/players_2.csv
25
+ - bin/studio_game
26
+ - lib/studio_game/auditable.rb
27
+ - lib/studio_game/berserk_player.rb
28
+ - lib/studio_game/clumsy_player.rb
29
+ - lib/studio_game/game.rb
30
+ - lib/studio_game/playable.rb
31
+ - lib/studio_game/player.rb
32
+ - lib/studio_game/treasure_trove.rb
33
+ homepage: ''
34
+ licenses:
35
+ - MIT
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.4.19
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Little game
56
+ test_files: []