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