pragmatic_studio_game_tut 0.1.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: 0a7eb253bb4455f74238a166a9835b84d47d65ef61ed32516e83bedd78f42ae5
4
+ data.tar.gz: '0817a9c94395575cea13fa1859cea0c9ddc0504da127fcc4bca002e58c568138'
5
+ SHA512:
6
+ metadata.gz: c1dc7d5255b2902b7f02bb80a646445a6a37c272ea8000eebebfcc2851bc551d6b031b18c1f28062b445cc8f8ddc9acfde5a5e5d7f994565e5057ec31808a731
7
+ data.tar.gz: 2fa39bb1c2eb4d0a66efbd41b18005493a9033ce869771fa536e53cae4dff68aeda94ea3f4106fe12d424e6cebae1bb749f2c4e1d11585dbe7f12174b3a664ef
data/LICENSE.txt ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2025 Tamara Nawrocki
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
+ software and associated documentation files (the "Software"), to deal in the Software
5
+ without restriction, including without limitation the rights to use, copy, modify,
6
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7
+ permit persons to whom the Software is furnished to do so, subject to the following
8
+ conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies
11
+ or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
15
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
17
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
18
+ OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ StudioGame
2
+ StudioGame is a Ruby gem that allows you to create and manage a game with players, including special types of players like ClumsyPlayer and BerserkPlayer. The game keeps track of player scores, treasures found, and allows for multiple rounds of play.
3
+
4
+ Features
5
+ Player Management: Add, remove, and manage players in the game.
6
+ Special Players: Includes special player types like ClumsyPlayer and BerserkPlayer with unique behaviors.
7
+ Score Tracking: Automatically calculates and tracks player scores based on health and treasures found.
8
+ CSV Integration: Load player data from a CSV file.
9
+ Game Rounds: Play multiple rounds of the game, with user input to control the number of rounds.
10
+ Statistics: Print detailed statistics of the game, including player scores and treasures found.
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,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/studio_game/player.rb"
4
+ require_relative "../lib/studio_game/game.rb"
5
+ require_relative "../lib/studio_game/clumsy_player.rb"
6
+ require_relative "../lib/studio_game/berserk_player.rb"
7
+
8
+ game = StudioGame::Game.new("Guardians of the Galaxy")
9
+
10
+ players_file = File.join(__dir__, "players.csv")
11
+ game.load_players( ARGV.shift || players_file)
12
+
13
+ clumsy = StudioGame::ClumsyPlayer.new("klutz", 105)
14
+ game.add_player(clumsy)
15
+
16
+ berserk = StudioGame::BerserkPlayer.new("berserker", 50)
17
+ game.add_player(berserk)
18
+
19
+ loop do
20
+ print "\nHow many game rounds? ('quit' to exit) "
21
+ answer = gets.chomp.downcase
22
+
23
+ case answer
24
+ when /^\d+$/
25
+ game.play(answer.to_i)
26
+ when "quit", "exit"
27
+ game.print_stats
28
+ break
29
+ else
30
+ puts "Please enter a number or 'quit'"
31
+ end
32
+ end
33
+
34
+ game.save_high_scores
@@ -0,0 +1,8 @@
1
+
2
+ module StudioGame
3
+ module Auditable
4
+ def audit(number)
5
+ puts "Audit: Rolled a #{number}"
6
+ end
7
+ end
8
+ 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 "Berserker #{@name} has gone berserk!" if berserk?
19
+ end
20
+
21
+ def drain
22
+ berserk? ? boost : super
23
+ end
24
+
25
+ end
26
+
27
+ if __FILE__ == $0
28
+ berserker = BerserkPlayer.new("berserker", 50)
29
+ 6.times { berserker.boost }
30
+ 2.times { berserker.drain }
31
+ puts berserker.health
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class ClumsyPlayer < Player
5
+ def initialize(name, health = 100, boost_count = 2)
6
+ super(name, health)
7
+ @boost_count = boost_count
8
+ end
9
+ def found_treasure(name, points)
10
+ points /= 2
11
+ super(name, points)
12
+ end
13
+ def boost
14
+ @boost_count.times { super}
15
+ end
16
+ end
17
+
18
+ if __FILE__ == $0
19
+ clumsy = ClumsyPlayer.new("klutz")
20
+
21
+ clumsy.found_treasure("flute", 50)
22
+ clumsy.found_treasure("flute", 50)
23
+ clumsy.found_treasure("flute", 50)
24
+ clumsy.found_treasure("star", 100)
25
+
26
+ clumsy.found_treasures.each do |name, points|
27
+ puts "#{name}: #{points} points"
28
+ end
29
+ puts "#{clumsy.points} total points"
30
+
31
+ clumsy.boost
32
+ puts clumsy.health
33
+ clumsy.boost
34
+ puts clumsy.health
35
+ clumsy.boost
36
+ puts clumsy.health
37
+ clumsy.boost
38
+ puts clumsy.health
39
+ end
40
+ end
@@ -0,0 +1,111 @@
1
+ require_relative "player.rb"
2
+ require_relative "treasure_trove.rb"
3
+ require_relative "auditable.rb"
4
+
5
+ module StudioGame
6
+ class Game
7
+ include Auditable
8
+
9
+ attr_reader :title, :players
10
+ def initialize(title)
11
+ @title = title
12
+ @players = []
13
+ end
14
+
15
+ def save_high_scores(file_name = "high_scores.csv")
16
+ File.open(file_name, "w") do |file|
17
+ file.puts "#{@title} High Scores:"
18
+ sorted_players.each do |player|
19
+ file.puts high_score_entry(player)
20
+ end
21
+ end
22
+ end
23
+
24
+ def high_score_entry(player)
25
+ name = player.name.ljust(20, ".")
26
+ points = player.score.round.to_s.rjust(5)
27
+ "#{name}#{points}"
28
+ end
29
+
30
+ def sorted_players
31
+ @players.sort_by { |player| player.score }.reverse
32
+ end
33
+
34
+ def load_players(file)
35
+ if File.exist?(file)
36
+ File.readlines(file, chomp: true).each do |line|
37
+ player = Player.from_file(line)
38
+ add_player(player)
39
+ end
40
+ else
41
+ puts "Whoops, #{file} not found!"
42
+ exit 1
43
+ end
44
+ end
45
+
46
+ def print_stats
47
+ puts "\n#@title Stats:"
48
+ puts "-"*30
49
+ puts sorted_players
50
+
51
+ @players.each do |player|
52
+ puts "\n#{player.name}'s treasure point totals"
53
+ player.found_treasures.each do |name, points|
54
+ puts "#{name}: $#{points}"
55
+ end
56
+ puts "Total: $#{player.points}"
57
+ end
58
+
59
+ puts "\nHigh Scores:"
60
+ sorted_players.each do |player|
61
+ puts high_score_entry(player)
62
+ end
63
+
64
+ end
65
+
66
+ def add_player (player)
67
+ @players << player
68
+ end
69
+
70
+ def roll_die
71
+ number = rand(1..6)
72
+ audit(number)
73
+ number
74
+ end
75
+
76
+ def play (rounds = 1)
77
+ puts "Let's play #{@title}\n"
78
+
79
+ puts "\nThe following treasures can be found:"
80
+ puts TreasureTrove.treasure_items
81
+
82
+ puts "\nBefore playing:\n"
83
+ puts @players
84
+
85
+ 1.upto(rounds) do |round|
86
+ puts "\nROUND #{round}"
87
+
88
+ @players.each do |player|
89
+ number_rolled = roll_die
90
+ treasure = TreasureTrove.random_treasure
91
+ case number_rolled
92
+ when 1..2
93
+ player.drain
94
+ puts "#{player.name} got drained 😩"
95
+ when 3..4
96
+ puts "#{player.name} got skipped 🙄"
97
+ else
98
+ player.boost
99
+ puts "#{player.name} got boosted 😁"
100
+ end
101
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
102
+ player.found_treasure(treasure.name, treasure.points)
103
+ end
104
+ end
105
+
106
+ puts "\nAfter playing:\n"
107
+ puts @players
108
+
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,11 @@
1
+ module StudioGame
2
+ module Playable
3
+ def boost
4
+ self.health += 15
5
+ end
6
+
7
+ def drain
8
+ self.health -= 10
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ require_relative "playable.rb"
2
+
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+
7
+ attr_reader :health, :name, :score, :found_treasures
8
+ attr_accessor :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 self.from_file(line)
17
+ name, health = line.split(',')
18
+ Player.new(name, Integer(health))
19
+ rescue ArgumentError
20
+ puts "Ignored invalid health: #{health}"
21
+ Player.new(name)
22
+ end
23
+
24
+ def score
25
+ @health + points
26
+ end
27
+
28
+ def points
29
+ @found_treasures.values.sum
30
+ end
31
+
32
+ def found_treasure(name, points)
33
+ @found_treasures[name] += points
34
+ end
35
+
36
+ def name=(new_name)
37
+ @name = new_name.capitalize
38
+ end
39
+
40
+ def to_s
41
+ if @found_treasures.empty?
42
+ "I'm #{@name} with a health of #{@health}"
43
+ else
44
+ "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}"
45
+ end
46
+ end
47
+ end
48
+
49
+ if __FILE__ == $0
50
+ player = Player.new("jase")
51
+ puts player.name
52
+ puts player.health
53
+ player.boost
54
+ puts player.health
55
+ player.drain
56
+ puts player.health
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ module StudioGame
2
+ module TreasureTrove
3
+ Treasure = Data.define(:name, :points)
4
+ TREASURES = [
5
+ Treasure.new("pie", 10),
6
+ Treasure.new("coin", 25),
7
+ Treasure.new("flute", 50),
8
+ Treasure.new("compass", 65),
9
+ Treasure.new("key", 80),
10
+ Treasure.new("crown", 90),
11
+ Treasure.new("star", 100)
12
+ ]
13
+
14
+ def self.treasure_items
15
+ TREASURES.map { |treasure| "A #{treasure.name} is worth #{treasure.points} points" }
16
+ end
17
+
18
+ def self.random_treasure
19
+ TREASURES.sample
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pragmatic_studio_game_tut
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tamara Nawrocki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-01-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: tamara@sztanski.com
15
+ executables:
16
+ - studio_game
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - bin/players.csv
23
+ - bin/studio_game
24
+ - lib/studio_game/auditable.rb
25
+ - lib/studio_game/berserk_player.rb
26
+ - lib/studio_game/clumsy_player.rb
27
+ - lib/studio_game/game.rb
28
+ - lib/studio_game/playable.rb
29
+ - lib/studio_game/player.rb
30
+ - lib/studio_game/treasure_trove.rb
31
+ homepage: https://pragmaticstudio.com
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.2.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.22
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: A command line game
54
+ test_files: []