studio_game_edu 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: 3d23aae1820699455ebf4c3555a47cfab3fc0dfdeb38742c4518c2f1674a46c1
4
+ data.tar.gz: cdb764161c2acdc805ce8710f5654de4ccb92e896c79bf2fd6aeb68df9ef8002
5
+ SHA512:
6
+ metadata.gz: af843d75fdc5a3bc80c0faa9220160b4d443f9594a98a0be296dd51145e65b6b3c241bff9e074a0a91ac09033e4037c7eb62a641009c6d588c9f3c68522053df
7
+ data.tar.gz: dde10d0cd79ad2a1c3c9a02f5d64560565860ccf96696be85d2248ead1be19ad91246a16b4055bd94d11675f219a8988734ebd3d40dd9936da07027fbef05f28
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2024 Eduardo C. Braga
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
+ # Game created in Pragmatic Studio Ruby Class
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
+ clumsy = StudioGame::ClumsyPlayer.new("klutz", 105)
11
+ # clumsy = ClumsyPlayer.new("klutz", 105, 3)
12
+ berserker = StudioGame::BerserkPlayer.new("berserker", 50)
13
+ game = StudioGame::Game.new('Winner Takes All')
14
+ game.load_players(ARGV.shift || players_file)
15
+ game.add_player(clumsy)
16
+ game.add_player(berserker)
17
+
18
+ loop do
19
+ print "How many game rounds? ('quit' to exit) "
20
+ answer = gets.chomp.downcase
21
+
22
+ case answer
23
+ when /^\d+$/
24
+ game.play(answer.to_i)
25
+ when 'quit', 'exit'
26
+ game.print_stats
27
+ break
28
+ else
29
+ puts "Please enter a number or 'quit'"
30
+ break
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,35 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class BerserkPlayer < Player
5
+ def initialize(name, health = 100)
6
+ super(name, health)
7
+ @boost_count = 0
8
+ end
9
+
10
+ def berserk?
11
+ @boost_count > 5
12
+ end
13
+
14
+ def boost
15
+ super
16
+ @boost_count += 1
17
+ puts "#{@name} is berserk!" if berserk?
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 = 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,116 @@
1
+ require_relative './treasure_trove'
2
+ require_relative './auditable'
3
+ require 'csv'
4
+
5
+ module StudioGame
6
+
7
+ class Game
8
+ include Auditable
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 << player
18
+ end
19
+
20
+ def load_players(from_file)
21
+ File.readlines(from_file, chomp: true).each do |line|
22
+ player = Player.from_csv(line)
23
+ add_player(player)
24
+ end
25
+ rescue Errno::ENOENT
26
+ puts "Whoops, #{from_file} not found!"
27
+ exit 1
28
+ end
29
+
30
+ def high_score_entry(player)
31
+ name = player.name.ljust(20, '.')
32
+ points = player.score.round.to_s.rjust(5)
33
+ "#{name}#{points}"
34
+ end
35
+
36
+ def save_high_scores(to_file = 'high_scores.txt')
37
+ File.open(to_file, 'w') do |file|
38
+ file.puts "#{@title} High Scores:"
39
+
40
+ sorted_players.each do |player|
41
+ file.puts high_score_entry(player)
42
+ end
43
+ end
44
+ end
45
+
46
+ def sorted_players
47
+ @players.sort_by { |player| player.score }.reverse
48
+ end
49
+
50
+ def roll_die
51
+ # loaded die
52
+ # number = [1, 1, 2, 5, 6, 6].sample
53
+ # non-loaded die
54
+ number = rand(1..6)
55
+ audit(number)
56
+ number
57
+
58
+ end
59
+
60
+ def play(rounds = 1)
61
+ puts "\nLet's play #{@title}!"
62
+
63
+ puts TreasureTrove.treasure_items
64
+
65
+ puts "\nBefore playing:"
66
+ puts @players
67
+
68
+ 1.upto(rounds) do |round|
69
+ puts "\nRound #{round}:"
70
+
71
+ @players.each do |player|
72
+ number_rolled = roll_die
73
+
74
+ case number_rolled
75
+ when 1..2
76
+ player.drain
77
+ puts "#{player.name} got drained 😩"
78
+ when 3..5
79
+ puts "#{player.name} got skipped"
80
+ else
81
+ player.boost
82
+ puts "#{player.name} got boosted 😁"
83
+ end
84
+
85
+ treasure = TreasureTrove.random_treasure
86
+
87
+ player.found_treasure(treasure.name, treasure.points)
88
+
89
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
90
+ end
91
+ end
92
+
93
+ def print_stats
94
+ puts "\n Game Stats:"
95
+ puts '-' * 30
96
+ puts sorted_players
97
+
98
+ @players.each do |player|
99
+ puts "\n#{player.name}'s treasure point totals:"
100
+ player.found_treasures.each do |name, points|
101
+ puts "#{name}: #{points}"
102
+ end
103
+ puts "total: #{player.points}"
104
+ end
105
+
106
+ puts "\nHigh Scores:"
107
+ sorted_players.each do |player|
108
+ puts high_score_entry(player)
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 drain
4
+ self.health -= 10
5
+ end
6
+
7
+ def boost
8
+ self.health += 15
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'playable'
2
+
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+ attr_accessor :health
7
+ attr_reader :name, :found_treasures
8
+
9
+ def initialize(name, health = 100)
10
+ # @name = name.split(' ').map { |word| word.capitalize }.join(' ')
11
+ # w é para letras e números (alfanumérico). d é para números
12
+ # /[a-zA-Z]+/ é para apenas letras
13
+ @name = name.gsub(/\w+/) { |word| word.capitalize }
14
+ @health = health
15
+ @found_treasures = Hash.new(0)
16
+ end
17
+
18
+ def name=(new_name)
19
+ @name = new_name.capitalize
20
+ end
21
+
22
+ def to_s
23
+ "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}"
24
+ end
25
+
26
+ def score
27
+ @health + points
28
+ end
29
+
30
+ def points
31
+ @found_treasures.values.sum
32
+ end
33
+
34
+ def found_treasure(name, points)
35
+ @found_treasures[name] += points
36
+ end
37
+
38
+ def self.from_csv(line)
39
+ name, health = line.split(',')
40
+ Player.new(name, Integer(health))
41
+ rescue ArgumentError
42
+ puts "Ignored invalid health: #{health}"
43
+ Player.new(name)
44
+ end
45
+
46
+ end
47
+
48
+ if __FILE__ == $0
49
+ player = Player.new('jase')
50
+ puts player.name
51
+ puts player.health
52
+ player.boost
53
+ puts player.health
54
+ player.drain
55
+ puts player.health
56
+ end
57
+ end
@@ -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.treasure_items
16
+ TREASURES.map { |treasure| "A #{treasure.name} is worth #{treasure.points} points" }
17
+ end
18
+
19
+ def self.random_treasure
20
+ TREASURES.sample
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: studio_game_edu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo Braga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: eduardocbraga@hotmail.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:
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: Game created in Pragmatic Studio Ruby Class
54
+ test_files: []