studio_game_practice_gem 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: 0035a825f1715a29bcdcf8a840b94b4900182226beadf0238b782d20757605ba
4
+ data.tar.gz: 9a30bcf7544bb2f8cf29b1a97288702f71e91f05ec26f207b63ff2ba803cf6e3
5
+ SHA512:
6
+ metadata.gz: 9d0878d9835fa41390bf11fac9ef9396151b00df0e46f076ce6c909c8f72b38496f8d6aea3f353461efb3c886906b155d019e0791b0aa935a981a97a7bc4fe61
7
+ data.tar.gz: 504d398816616d1c2a49d49ee5ec7d6f9985b7551191b1fd8e3c3f346267a6f52d56fdcc8973b6c15fa55262efbcebe836fa462665d131da644d7606406912e2
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) <year> <copyright holders>
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.md ADDED
@@ -0,0 +1 @@
1
+ This is a console app that will read in movies with a title and ranking from a csv file then randomly rank them and output a file with the movie rankings.
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/game"
4
+ require_relative "../lib/studio_game/player"
5
+ require_relative "../lib/studio_game/clumsy_player"
6
+ require_relative "../lib/studio_game/berserk_player"
7
+
8
+ players_file = File.join(__dir__, "players.csv")
9
+
10
+ game = StudioGame::Game.new("Guardians")
11
+ game.load_players(ARGV.shift || players_file)
12
+
13
+ clumsy = StudioGame::ClumsyPlayer.new("klutz", 105, 3)
14
+ game.add_player(clumsy)
15
+
16
+ berserker = StudioGame::BerserkPlayer.new("berserker", 50)
17
+ game.add_player(berserker)
18
+
19
+ loop do
20
+ print "\nHow many game rounds? (or 'quit' to exit): "
21
+ answer = gets.chomp.downcase
22
+
23
+ case answer
24
+ when /^\d+$/
25
+ game.play(answer.to_i)
26
+ when "quit"
27
+ game.print_stats
28
+ break
29
+ else
30
+ puts "Please enter a number of round or 'quit' to exit the game."
31
+ end
32
+ end
33
+
34
+ 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
+
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,36 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
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 found_treasure(name, points)
13
+ points = points / 2.0
14
+ super(name, points)
15
+ end
16
+
17
+ def boost
18
+ @boost_factor.times { super }
19
+ end
20
+
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
36
+ end
@@ -0,0 +1,100 @@
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
+ attr_accessor :health
10
+
11
+ def initialize(title)
12
+ @title = title
13
+ @players = []
14
+ end
15
+
16
+ def load_players(file_name)
17
+ File.readlines(file_name, chomp: true).each do |line|
18
+ player = Player.from_csv(line)
19
+ add_player(player)
20
+ end
21
+ rescue Errno::ENOENT
22
+ puts "No file named '#{file_name}' found!"
23
+ exit 1
24
+ end
25
+
26
+ def save_high_scores(to_file = "high_scores.txt")
27
+ File.open(to_file, "w") do |file|
28
+ file.puts "#{@title} High Scores:"
29
+ sorted_players.each do |player|
30
+ file.print "#{player.name}".ljust(15, ".")
31
+ file.puts " #{player.score}"
32
+ end
33
+ end
34
+ end
35
+
36
+ def add_player(player)
37
+ @players << player
38
+ end
39
+
40
+ def roll_die
41
+ number = rand(1..6)
42
+ audit(number)
43
+ number
44
+ end
45
+
46
+ def play(rounds = 1)
47
+ puts "Let's play #{@title}!\n"
48
+
49
+ puts "\nPossible treasures to collect:"
50
+ puts TreasureTrove.treasure_items
51
+
52
+ puts "\nBefore playing:"
53
+ puts @players
54
+
55
+ 1.upto(rounds) do |round|
56
+ puts "\nRound #{round}:"
57
+ @players.each do |player|
58
+ number_rolled = roll_die
59
+
60
+ if number_rolled < 3
61
+ player.drain
62
+ puts "#{player.name} got drained 😩"
63
+ elsif number_rolled < 5
64
+ puts "#{player.name} got skipped"
65
+ else
66
+ player.boost
67
+ puts "#{player.name} got boosted 😁"
68
+ end
69
+
70
+ treasure = TreasureTrove.random_treasure
71
+ player.found_treasure(treasure.name, treasure.points)
72
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
73
+ end
74
+ end
75
+ end
76
+
77
+ def sorted_players
78
+ @players.sort_by { |player| player.score}.reverse
79
+ end
80
+
81
+ def print_stats
82
+ puts "\nWinner Takes All Game Stats:"
83
+ puts "-" * 30
84
+ puts sorted_players
85
+ print "\n"
86
+ @players.each do |player|
87
+ puts "#{player.name}'s treasure point totals:"
88
+ player.found_treasures.each do |name, points|
89
+ puts "#{name}: #{points}"
90
+ end
91
+ puts "Total: #{player.points}\n\n"
92
+ end
93
+ puts "High scores:"
94
+ sorted_players.each do |player|
95
+ print "#{player.name}".ljust(15, ".")
96
+ puts " #{player.score}"
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,12 @@
1
+ module StudioGame
2
+ module Playable
3
+
4
+ def boost
5
+ self.health += 15
6
+ end
7
+
8
+ def drain
9
+ self.health -= 10
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,50 @@
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 to_s
17
+ "#{@name} has a health of #{@health}, has accumulated #{points} points, and has a score of #{score}"
18
+ end
19
+
20
+ def self.from_csv(line)
21
+ name, health = line.split(",")
22
+ player = Player.new(name, Integer(health))
23
+ rescue ArgumentError
24
+ puts "Ignored invalid health: #{health}"
25
+ Player.new(name)
26
+ end
27
+
28
+ def found_treasure(name, points)
29
+ @found_treasures[name] += points
30
+ end
31
+
32
+ def points
33
+ @found_treasures.values.sum
34
+ end
35
+
36
+ def score
37
+ @health + points
38
+ end
39
+ end
40
+ end
41
+
42
+ if __FILE__ == $0
43
+ player = Player.new("jase")
44
+ puts player.name
45
+ puts player.health
46
+ player.boost
47
+ puts player.health
48
+ player.drain
49
+ puts player.health
50
+ 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("flue", 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.random_treasure
15
+ TREASURES.sample
16
+ end
17
+
18
+ def self.treasure_items
19
+ TREASURES.map {|treasure| "#{treasure.name.upcase} is worth #{treasure.points} points"}
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: studio_game_practice_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Victor Kinsella
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: vkinsella@infotech.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.4.10
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Random movie rating console app
54
+ test_files: []