studio_game_bswift 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: ef1d9e36dc128c4d057722b4702a5272bfebc29079ef0f266e5aa3cc546d44c3
4
+ data.tar.gz: e3deb8f4f3f7a2f2c672d5dabc8612a7583ae1f32b493d5ab7e881c162567bd4
5
+ SHA512:
6
+ metadata.gz: 7a5769a48056c756c18ff1f2f7b147e8aebb4a1f2778db22ce57a6b780666226b8dbfb614c6468f0a806fd0ea048c02cea96c3f278c278613325b412fdc6d459
7
+ data.tar.gz: 9e80ec8ed690c482173fc4a03ccd949c25aa8711e5bebfce0a96687e1d1ce8b508ea9792d259cd826a4d24d62b1b73a5e3a2c8c0d7f13e8e52dda8c7910290f4
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) <2024> <Ben Swift>
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 game gem from the Pragmatic Studio Ruby Course
data/bin/players.csv ADDED
@@ -0,0 +1,5 @@
1
+ Starlord,50
2
+ Gamora,75
3
+ Rocket,100
4
+ Drax,125
5
+ Groot,80
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/studio_game_bswift/game'
4
+ require_relative '../lib/studio_game_bswift/ClumsyPlayer'
5
+ require_relative '../lib/studio_game_bswift/BerserkPlayer'
6
+
7
+ # Start Game here!
8
+
9
+ players_file = File.join(__dir__, "players.csv")
10
+ game_1 = StudioGame::Game.new("Guardians")
11
+ game_1.load_players(ARGV.shift || players_file)
12
+
13
+ loop do
14
+ print "\nHow many game rounds? ('quit' to exit) "
15
+ answer = gets.chomp.downcase
16
+
17
+ case answer
18
+ when /^\d+$/
19
+ game_1.play(answer.to_i)
20
+ when "quit", "exit"
21
+ game_1.print_stats
22
+ break
23
+ else
24
+ puts "Please enter a number or 'quit'"
25
+ end
26
+ end
27
+
28
+ game_1.save_high_score
@@ -0,0 +1,35 @@
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
34
+
35
+
@@ -0,0 +1,26 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class ClumsyPlayer < Player
5
+ def found_treasure(name, points)
6
+ points = 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
25
+
26
+
@@ -0,0 +1,114 @@
1
+ require_relative "treasuretrove"
2
+ require_relative "auditable"
3
+ require "csv"
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 load_players(from_file)
17
+ CSV.foreach(from_file) do |row|
18
+ player = Player.from_csv(row)
19
+ add_player(player)
20
+ end
21
+ rescue Errno::ENOENT
22
+ puts "Whoops, #{from_file} doesn't exist!"
23
+ exit 1
24
+ end
25
+
26
+ def save_high_score(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.puts high_score_entity(player)
31
+ end
32
+ end
33
+ end
34
+
35
+ def add_player(player)
36
+ @players << player
37
+ end
38
+
39
+ def roll_die
40
+ num = rand(1..6)
41
+ audit(num)
42
+ num
43
+ end
44
+
45
+ def play(rounds = 1)
46
+ puts @title
47
+
48
+ puts "Before playing:"
49
+ puts @players
50
+
51
+ 1.upto(rounds) do |round|
52
+ puts "\nStarting Round #{round} of #{rounds}#{"." * round}\n"
53
+ puts "You can find the following treasures"
54
+ puts TreasureTrove.treasure_items
55
+
56
+ puts "\n"
57
+
58
+ @players.each do |player|
59
+ number_rolled = roll_die
60
+
61
+ case number_rolled
62
+ when 1..2
63
+ player.drain
64
+ puts "#{player.name} got drained 😩"
65
+ when 3..4
66
+ puts "#{player.name} got skipped"
67
+ else
68
+ player.boost
69
+ puts "#{player.name} got boosted 😄"
70
+ end
71
+
72
+ treasure = TreasureTrove.random_treasure
73
+ player.found_treasure(treasure.name, treasure.points)
74
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
75
+ end
76
+ end
77
+
78
+ puts "\nAfter playing:"
79
+ puts @players
80
+ end
81
+
82
+ def sorted_players
83
+ @players.sort_by { |p| p.score }.reverse
84
+ end
85
+
86
+ def high_score_entity(player)
87
+ "#{player.name}............. #{player.score.round}"
88
+ end
89
+
90
+ def print_stats
91
+ puts "\n #{@title} Game Stats\n"
92
+ puts "-" * 30
93
+
94
+ puts sorted_players
95
+
96
+ @players.each do |player|
97
+ puts "\n#{player.name}'s treasure point total:"
98
+ player.found_treasures.each do |name, points|
99
+ puts "#{name}: $#{points}"
100
+ end
101
+ puts "total: $#{player.points}"
102
+ end
103
+
104
+ puts "\nHigh Scores:\n"
105
+ sorted_players.each do |player|
106
+ puts high_score_entity(player)
107
+ end
108
+ end
109
+ end
110
+
111
+ if __FILE__ == $0
112
+ puts "Test code here for Game"
113
+ end
114
+ end
@@ -0,0 +1,54 @@
1
+ require_relative "playable"
2
+
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+
7
+ attr_reader :found_treasures
8
+ attr_accessor :name, :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 found_treasure(name, points)
17
+ @found_treasures[name] += points
18
+ end
19
+
20
+ def points
21
+ @found_treasures.values.sum
22
+ end
23
+
24
+ def score
25
+ @health + points
26
+ end
27
+
28
+ def name=(new_name)
29
+ @name = new_name.capitalize
30
+ end
31
+
32
+ def to_s = "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}"
33
+
34
+ def self.from_csv(row)
35
+ Player.new(row[0], Integer(row[1]))
36
+ rescue ArgumentError
37
+ puts "Ignored invalid health: #{row[1]}"
38
+ Player.new(row[0])
39
+ end
40
+ end
41
+
42
+ if __FILE__ == $0
43
+ puts "Test code for Player goes here"
44
+ player = Player.new("jase")
45
+ puts player.name
46
+ puts player.health
47
+ player.boost
48
+ puts player.health
49
+ player.drain
50
+ puts player.health
51
+ end
52
+ end
53
+
54
+
@@ -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
@@ -0,0 +1,7 @@
1
+ module StudioGame
2
+ module Auditable
3
+ def audit(num)
4
+ puts "Audit: Rolled a #{num}"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module StudioGame
2
+ module Playable
3
+ def drain
4
+ self.health -= 10
5
+ end
6
+
7
+ def boost
8
+ self.health += 15
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: studio_game_bswift
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Swift
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: benswift404@gmail.com
15
+ executables:
16
+ - studio_game_bswift
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - bin/players.csv
23
+ - bin/studio_game_bswift
24
+ - lib/studio_game_bswift/BerserkPlayer.rb
25
+ - lib/studio_game_bswift/ClumsyPlayer.rb
26
+ - lib/studio_game_bswift/Game.rb
27
+ - lib/studio_game_bswift/Player.rb
28
+ - lib/studio_game_bswift/TreasureTrove.rb
29
+ - lib/studio_game_bswift/auditable.rb
30
+ - lib/studio_game_bswift/playable.rb
31
+ homepage: https://github.com/benswift404
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.3.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.3
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: This is a game gem from the Pragmatic Studio Ruby Course
54
+ test_files: []