pragstudio_game_exercise 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: 49857dac3526cd76e417be0f1a4e095738678c65b8b16d261ad5f4d376b87f8a
4
+ data.tar.gz: 2d9d1f1738f86b65b2dd8e12cb871c86076ccde725f8db22e83aaaf4552d7001
5
+ SHA512:
6
+ metadata.gz: a9efe34d369f4121c9e17077574fd66265266523e83334b895e1999df940571e818e4565661a573c6991567354be5f7abfbc02b5a6f068845d90850b2bdc82d5
7
+ data.tar.gz: 27dbd888414ad2428cdc53ad57ed8ce071af130cd63c47eade3807d16fa5afde6b9bd49d716e7aa75979843839f26a17c491492a2b95dea7c03bbe6a8ba2736a
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2023
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,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,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/studio_game/player"
4
+ require_relative "../lib/studio_game/clumsy_player"
5
+ require_relative "../lib/studio_game/berserk_player"
6
+ require_relative "../lib/studio_game/game"
7
+
8
+ player_1 = StudioGame::Player.new("finn", 60)
9
+ player_2 = StudioGame::Player.new("lucy", 90)
10
+ player_3 = StudioGame::Player.new("jase")
11
+ player_4 = StudioGame::Player.new("alex", 125)
12
+
13
+ # Initialize a clumsy player and a berserk player
14
+ klutz = StudioGame::ClumsyPlayer.new("klutz", 105)
15
+ berserker = StudioGame::BerserkPlayer.new("berserker", 50)
16
+
17
+
18
+
19
+ # game = Game.new("Winner Takes All")
20
+ # game.add_player(player_1)
21
+ # game.add_player(player_2)
22
+ # game.add_player(player_3)
23
+ # game.add_player(player_4)
24
+
25
+
26
+ game = StudioGame::Game.new("Guardians")
27
+ players_file = File.join(__dir__, "players.csv")
28
+ game.load_players(ARGV.shift || players_file)
29
+
30
+ # Add klutz and berserker to game with Guardians
31
+ game.add_player(klutz)
32
+ game.add_player(berserker)
33
+
34
+ loop do
35
+ print "\nHow many game rounds? ('quit' to exit) "
36
+ answer = gets.chomp.downcase
37
+
38
+ case answer
39
+ when /^\d+$/
40
+ game.play(answer.to_i)
41
+ when "quit", "exit",
42
+ game.print_stats
43
+ break
44
+ else
45
+ "Please enter a number or 'quit'"
46
+ end
47
+
48
+ end
49
+
50
+ game.save_high_scores
51
+
52
+ # game.play(10)
53
+
54
+ # game.print_stats
55
+
56
+
57
+
58
+
59
+
@@ -0,0 +1,9 @@
1
+ module StudioGame
2
+ module Auditable
3
+
4
+ def audit(number)
5
+ puts "Audit: Rolled a #{number}"
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,34 @@
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 is berserk!" if berserk?
19
+ end
20
+
21
+ def drain
22
+ berserk? ? boost : super
23
+ end
24
+
25
+ end
26
+
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
34
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class ClumsyPlayer < Player
5
+
6
+ attr_reader :boost_factor
7
+
8
+ def initialize(name, health = 100, boost_factor = 2)
9
+ super(name, health)
10
+ @boost_factor = boost_factor
11
+ end
12
+
13
+ def boost
14
+ @boost_factor.times { super }
15
+ end
16
+
17
+ def found_treasure(name, points)
18
+ points = points / 2.0
19
+ super(name, points)
20
+ end
21
+
22
+ end
23
+
24
+ if __FILE__ == $0
25
+ clumsy = ClumsyPlayer.new("klutz")
26
+
27
+ clumsy.found_treasure("flute", 50)
28
+ clumsy.found_treasure("flute", 50)
29
+ clumsy.found_treasure("flute", 50)
30
+ clumsy.found_treasure("star", 100)
31
+
32
+ clumsy.found_treasures.each do |name, points|
33
+ puts "#{name}: #{points} points"
34
+ end
35
+ puts "#{clumsy.points} total points"
36
+ end
37
+ end
@@ -0,0 +1,116 @@
1
+ require_relative "auditable"
2
+
3
+ module StudioGame
4
+ class Game
5
+ include Auditable
6
+
7
+ require_relative "treasure_trove"
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
+ File.readlines(from_file, chomp: true).each do |line|
18
+ player = Player.from_csv(line)
19
+ add_player(player)
20
+ end
21
+ rescue Errno::ENOENT
22
+ puts "Whoops, #{from_file} not found!"
23
+ exit 1
24
+ end
25
+
26
+ def high_score_entry(player)
27
+ name = player.name.ljust(20,".")
28
+ score = player.score.round.to_s.rjust(5)
29
+ "#{name}#{score}"
30
+ end
31
+
32
+ def save_high_scores(to_file = "high_scores.txt")
33
+ File.open(to_file, "w") do |file|
34
+ file.puts "\n#{@title} High Scores:"
35
+ sorted_players.each do |player|
36
+ file.puts high_score_entry(player)
37
+ end
38
+ end
39
+ end
40
+
41
+ def sorted_players
42
+ @players.sort_by { |player| player.score }.reverse
43
+ end
44
+
45
+ def print_stats
46
+ puts "\n#{@title} Game Stats:"
47
+ puts "-" * 30
48
+
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
+ end
64
+
65
+ def add_player(player)
66
+ @players.push(player)
67
+ end
68
+
69
+ def roll_die
70
+ number = rand(1..6)
71
+ audit(number)
72
+ number
73
+ end
74
+
75
+ def play(rounds = 1)
76
+ puts "\nLet's play #{@title}!"
77
+
78
+ puts "\nThe following treasures can be found:"
79
+
80
+ # TreasureTrove::TREASURES.each do |treasure|
81
+ # puts "A #{treasure.name} is worth #{treasure.points} points"
82
+ # end
83
+
84
+ puts TreasureTrove.treasure_items
85
+
86
+ puts "\nBefore playing:"
87
+ puts @players
88
+
89
+ 1.upto(rounds) do |round|
90
+ puts "\nRound #{round}:"
91
+
92
+ @players.each do |player|
93
+ number_rolled = roll_die
94
+
95
+ case number_rolled
96
+ when 1..2
97
+ player.drain
98
+ puts "#{player.name} got drained 😩"
99
+ when 3..4
100
+ puts "#{player.name} got skipped"
101
+ else
102
+ player.boost
103
+ puts "#{player.name} got boosted 😁"
104
+ end
105
+
106
+ treasure = TreasureTrove.random_treasure
107
+ player.found_treasure(treasure.name, treasure.points)
108
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
109
+ end
110
+ end
111
+
112
+ puts "\nAfter playing:"
113
+ puts @players
114
+ end
115
+ end
116
+ 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"
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 points
17
+ @found_treasures.values.sum
18
+ end
19
+
20
+ def found_treasure(name, points)
21
+ @found_treasures[name] += points
22
+ end
23
+
24
+ def name=(new_name)
25
+ @name = new_name.capitalize
26
+ end
27
+
28
+ def score
29
+ @health + points
30
+ end
31
+
32
+
33
+
34
+ def self.from_csv(line)
35
+ name, health = line.split(",")
36
+ Player.new(name, Integer(health))
37
+ rescue ArgumentError
38
+ puts "Ignored invalid health: #{health}"
39
+ Player.new(name)
40
+ end
41
+
42
+ def to_s
43
+ "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}"
44
+ end
45
+ end
46
+
47
+
48
+ # Example of Player Class
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
+
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
+ 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: pragstudio_game_exercise
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Trudeau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: thekevintrudeau@gmail.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: A randomized game exercise by Pragmatic Studios in Ruby
54
+ test_files: []