studio_game_aanonymoususeroninternet 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be7975f26dab494c8e0387e10ee526553058befbc5c185d346ea8d5a233d3086
4
+ data.tar.gz: fb2afb5ff76c9d3466f9e174772962b2b1fed4730c6f30897e3e9c1297662f04
5
+ SHA512:
6
+ metadata.gz: 709847925d14ec4bb0bd3a15eaf00c6e389c9123ce93b563974660d33eb6349a6bb60c58b638e53989ff28773066aa36ecaa2200d6ff2b766b3f050fe0903c96
7
+ data.tar.gz: f5ec42ddea42fa0ff1be2fe6216520bd37646ce30028d41764a8d96334c0f7bc7b0fecc40de41e65c756255a908b5bbddeb423b63b9fdaf093b261e1251d74ad
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2024 Ramesh
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 Studio Game gem created as part of 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
data/bin/studio_game ADDED
@@ -0,0 +1,32 @@
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
+ game = StudioGame::Game.new("Winner Take All")
9
+ filename = File.join(__dir__, 'players.csv')
10
+ game.load_players(ARGV.shift || filename)
11
+ player_1 = StudioGame::ClumsyPlayer.new("klutz", 105)
12
+ game.add_player(player_1)
13
+
14
+ player_2 = StudioGame::BerserkPlayer.new("berserker", 50)
15
+ game.add_player(player_2)
16
+
17
+ loop do
18
+ print "How many game rounds? ('quit' to exit)"
19
+ answer = gets.chomp.downcase
20
+ case answer
21
+ when /^\d+$/
22
+ game.play(answer.to_i)
23
+ when "quit", "exit"
24
+ game.print_stats
25
+ break
26
+ else
27
+ puts "Please enter a valid numer or quit to exit:"
28
+ end
29
+ end
30
+
31
+ game.save_high_scores
32
+
@@ -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,32 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class BerserkPlayer < Player
5
+ def initialize(name, health)
6
+ super(name, health)
7
+ @boost_count = 0
8
+ end
9
+
10
+ def boost
11
+ super
12
+ @boost_count += 1
13
+ puts "#{name} is berserk!" if berserk?
14
+ end
15
+
16
+ def drain
17
+ berserk? ? boost : super
18
+ end
19
+
20
+ def berserk?
21
+ @boost_count > 5
22
+ end
23
+ end
24
+ end
25
+
26
+ if __FILE__ == $0
27
+ berserker = BerserkPlayer.new("berserker", 50)
28
+ 6.times { berserker.boost }
29
+ 2.times { berserker.drain }
30
+ puts berserker.health
31
+ end
32
+
@@ -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
+ end
11
+
12
+ if __FILE__ == $0
13
+ clumsy = ClumsyPlayer.new("klutz")
14
+
15
+ clumsy.found_treasure("flute", 50)
16
+ clumsy.found_treasure("flute", 50)
17
+ clumsy.found_treasure("flute", 50)
18
+ clumsy.found_treasure("star", 100)
19
+
20
+ clumsy.found_treasures.each do |name, points|
21
+ puts "#{name}: #{points} points"
22
+ end
23
+ puts "#{clumsy.points} total points"
24
+ end
@@ -0,0 +1,115 @@
1
+ require "csv"
2
+ require_relative "treasure_trove"
3
+ require_relative "player"
4
+ require_relative "auditable"
5
+
6
+ include StudioGame::Auditable
7
+ module StudioGame
8
+ class Game
9
+ attr_accessor :players
10
+
11
+ def initialize(name)
12
+ @name = name
13
+ @players = []
14
+ end
15
+
16
+ def load_players(filename)
17
+ CSV.foreach(filename) do |row|
18
+ player = Player.from_csv(row)
19
+ add_player(player)
20
+ end
21
+ rescue Errno::ENOENT => e
22
+ puts "Invalid file name: #{filename}"
23
+ exit 1
24
+ end
25
+
26
+ def save_high_scores(filename = "high_scores.txt")
27
+ File.open(filename, "w") do |file|
28
+ file.puts "Winner Takes All High Scores:"
29
+ high_score_players.each do |player|
30
+ file.puts player.to_csv
31
+ end
32
+ end
33
+ end
34
+
35
+ def high_score_entry(player)
36
+ "#{player.name}#{'.'*10} #{player.score}"
37
+ end
38
+
39
+
40
+ def add_player(player)
41
+ players << player
42
+ end
43
+
44
+ def roll_die
45
+ number = rand(1..6)
46
+ audit(number)
47
+ number
48
+ end
49
+
50
+ def print_stats
51
+ puts "\Winner Takes All Game Stats:\n"
52
+ puts "-" * 20
53
+ puts sorted_players
54
+ players.each do |player|
55
+ puts "\n#{player.name}'s treasure point totals:"
56
+ puts player_stats(player)
57
+ puts "total: #{player.found_treasures.values.sum}"
58
+ end
59
+ puts "\nHight Scores:"
60
+ puts high_score_players_formatted
61
+ end
62
+
63
+ def high_score_players
64
+ players.sort_by {|player| player.score }.reverse
65
+ end
66
+
67
+ def high_score_players_formatted
68
+ high_score_players.map {|player| high_score_entry(player) }
69
+ end
70
+
71
+ def player_stats(player)
72
+ player.found_treasures.map {|key, value| "#{key}: #{value}"}
73
+ end
74
+
75
+ def sorted_players
76
+ players.sort_by { |player| player.score }
77
+ end
78
+
79
+ def play(rounds = 1)
80
+ 1.upto(rounds) do |round|
81
+ puts "Round #{round}:\n"
82
+ puts "Lets play #{@name}!\n"
83
+ puts TreasureTrove.treasure_items
84
+ puts "Before playing:"
85
+ puts players
86
+
87
+ players.each do |player|
88
+ number_rolled = roll_die
89
+ case number_rolled
90
+ when 1..2
91
+ player.drain
92
+ puts "#{player.name} got drained 😩"
93
+ when 3..4
94
+ puts "#{player.name} got skipped"
95
+ else
96
+ player.boost
97
+ puts "#{player.name} got boosted 😁"
98
+ end
99
+
100
+ treasure = TreasureTrove.random_treasure
101
+ player.found_treasure(treasure.name, treasure.points)
102
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
103
+ end
104
+
105
+ puts "\nAfter playing:"
106
+ puts players
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ if __FILE__ == $0
113
+ game = Game.new("Hell yeah!")
114
+ puts "********** Hell yeah"
115
+ 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,49 @@
1
+ require "csv"
2
+ require_relative "playable"
3
+
4
+ include StudioGame::Playable
5
+
6
+ module StudioGame
7
+ class Player
8
+ attr_reader :found_treasures
9
+ attr_accessor :name, :health
10
+ def initialize(name, health = 100)
11
+ @name = name.split(" ").map { |word| word.capitalize }.join(" ")
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 score
21
+ health + points
22
+ end
23
+
24
+ def points
25
+ @found_treasures.values.sum
26
+ end
27
+
28
+ def name=(new_name)
29
+ @name = new_name.capitalize
30
+ end
31
+
32
+ def to_s
33
+ "I'm #{name} with health = #{health}, points = #{points}, and score = #{score}"
34
+ end
35
+
36
+ def to_csv
37
+ CSV.generate_line([name, score])
38
+ end
39
+
40
+ def self.from_csv(row)
41
+ name = row[0]
42
+ health = Integer(row[1])
43
+ Player.new(name, health)
44
+ rescue
45
+ puts "Invalid health value: #{row[1]}"
46
+ Player.new(name)
47
+ end
48
+ end
49
+ 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: studio_game_aanonymoususeroninternet
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Doe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: aanonymoususeroninternet.efrvg@aleeas.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://www.example.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: This is a gem created as part of Pragmatic studio Ruby course
54
+ test_files: []