studio_game_106 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: fd108f09ff48488d785554c488e0461be4c5845f55856b44235dd69fb5ef8a6e
4
+ data.tar.gz: e2bfa6f08b6ccf6194e6d889c4baa1e7576a5fa5c304a6aa8b228e8d0a010ffd
5
+ SHA512:
6
+ metadata.gz: 34f8a49eb81e260fa8c30961ce5b3244fdb0f65a374033def55aedc3adbc9b7783c7e29775dad899cb5109b88b3dabb04511377ff5ea6fe84f3c28413c584872
7
+ data.tar.gz: 9e83e959171774438616e266590fde14607b2e466f6e65a9e19cd98a7066ee89b2f3dd67d33c3b1a2edb6dc30fc1b9e9118a35ecd6f434e9937a8e4364ae4501
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) <year> <copyright holders>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ Text based game on finding treasures.
2
+
3
+ Enter number of rounds.
4
+
5
+ Type quit to exit.
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,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative("../lib/studio_game/game")
4
+ require_relative("../lib/studio_game/clumsy_player")
5
+ require_relative("../lib/studio_game/berserk_player")
6
+
7
+
8
+ puts "\nLet's play a game!\n\n\t3\n\t2\n\t1\n\n"
9
+ puts "🚀" * 25
10
+ puts "\n"
11
+
12
+ players_file = File.join(__dir__, "players.csv")
13
+
14
+ game = StudioGame::Game.new("Guardians")
15
+ game.load_players(ARGV.shift || players_file)
16
+ clumsy = StudioGame::ClumsyPlayer.new("klutz", 105, 3)
17
+ game.add_player(clumsy)
18
+ berserk = StudioGame::BerserkPlayer.new("berserker", 50)
19
+ game.add_player(berserk)
20
+
21
+ loop do
22
+ print "How 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
+ puts "Please enter a number or 'quit'"
33
+ end
34
+ end
35
+
36
+ game.save_high_scores
37
+
38
+
39
+
@@ -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,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
+
31
+ if __FILE__ == $0
32
+ berserker = BerserkPlayer.new("berserker", 50)
33
+ 6.times { berserker.boost }
34
+ 2.times { berserker.drain }
35
+ puts berserker.health
36
+ end
37
+ end
@@ -0,0 +1,36 @@
1
+ require_relative "player"
2
+
3
+ module StudioGame
4
+ class ClumsyPlayer < Player
5
+ attr_reader :boost_factor
6
+
7
+ def initialize(name, health = 100, boost_factor = 1)
8
+ super(name, health)
9
+ @boost_factor = boost_factor
10
+ end
11
+
12
+ def found_treasure(name, points)
13
+ points = points / 2.0
14
+ super(name, points)
15
+ end
16
+
17
+ def boost
18
+ @boost_factor.times { super }
19
+ end
20
+
21
+ end
22
+
23
+ if __FILE__ == $0
24
+ clumsy = ClumsyPlayer.new("klutz")
25
+
26
+ clumsy.found_treasure("flute", 50)
27
+ clumsy.found_treasure("flute", 50)
28
+ clumsy.found_treasure("flute", 50)
29
+ clumsy.found_treasure("star", 100)
30
+
31
+ clumsy.found_treasures.each do |name, points|
32
+ puts "#{name}: #{points} points"
33
+ end
34
+ puts "#{clumsy.points} total points"
35
+ end
36
+ end
@@ -0,0 +1,112 @@
1
+ require_relative "treasure_trove"
2
+ require_relative "player"
3
+ require_relative "auditable"
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 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 sorted_players
27
+ @players.sort_by { |player| player.score }.reverse
28
+ end
29
+
30
+ def print_high_scores
31
+ puts "\n#{@title} High Scores:"
32
+ sorted_players.each do |player|
33
+ puts "\n#{player.name}" + "." * (20 - player.name.length) + " #{player.score}"
34
+ end
35
+ end
36
+
37
+ def print_stats
38
+ puts "\n #{title} Game Stats:"
39
+ puts "-" * 30
40
+
41
+ puts sorted_players
42
+
43
+ @players.each do |player|
44
+ puts "\n#{player.name}'s treasure point totals: "
45
+ player.found_treasures.each do |name, points|
46
+ puts "#{name}: #{points}"
47
+ end
48
+ puts "total: #{player.points}"
49
+ end
50
+
51
+ print_high_scores
52
+ end
53
+
54
+ def play(rounds = 1)
55
+ puts "\nLet's play #{@title}!"
56
+ puts "\nThe treasures:"
57
+ puts TreasureTrove.treasure_items
58
+
59
+ puts "\nBefore playing:"
60
+ puts @players
61
+
62
+ 1.upto(rounds) do |round|
63
+ puts "\nRound: #{round}\n"
64
+
65
+ @players.each do |player|
66
+ number_rolled = roll_die
67
+
68
+ case number_rolled
69
+ when 1..2
70
+ player.drain
71
+ puts "#{player.name} got drained 😩"
72
+ when 3..4
73
+ puts "#{player.name} got skipped"
74
+ else
75
+ player.boost
76
+ puts "#{player.name} got boosted 😁"
77
+ end
78
+
79
+ treasure = TreasureTrove.random_treasure
80
+ player.found_treasure(treasure.name, treasure.points)
81
+ puts "#{player.name} found a #{treasure.name} worth #{treasure.points}"
82
+ end
83
+
84
+ puts "\nAfter playing:"
85
+ puts @players
86
+
87
+ end
88
+
89
+ end
90
+
91
+ def load_players(from_file)
92
+ if File.exist?(from_file)
93
+ File.readlines(from_file, chomp: true).each do |line|
94
+ player = Player.from_csv(line)
95
+ add_player(player)
96
+ end
97
+
98
+ else
99
+ puts "\nError: #{from_file} not found."
100
+ exit 1
101
+ end
102
+
103
+ end
104
+
105
+ def save_high_scores(to_file = "high_scores.txt")
106
+ File.open(to_file, "w") do |file|
107
+ file.puts print_high_scores
108
+ end
109
+ end
110
+
111
+ end
112
+ 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,53 @@
1
+ require_relative "playable"
2
+
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+
7
+ attr_accessor :name, :health
8
+ attr_reader :found_treasures
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 name=(new_name)
17
+ @name = new_name.capitalize
18
+ end
19
+
20
+ def to_s = "I'm #{@name} with health: #{@health}, points: #{points}, and score: #{score}"
21
+
22
+ def score
23
+ @health + points
24
+ end
25
+
26
+ def found_treasure(name, points)
27
+ @found_treasures[name] += points
28
+ end
29
+
30
+ def points
31
+ @found_treasures.values.sum
32
+ end
33
+
34
+ def self.from_csv(line)
35
+ name, health = line.split(',')
36
+ player = Player.new(name, Integer(health))
37
+ rescue ArgumentError
38
+ puts "Invalid health: #{health}"
39
+ Player.new(name)
40
+ end
41
+
42
+ end
43
+ end
44
+
45
+ if __FILE__ == $0
46
+ player = Player.new("jase")
47
+ puts player.name
48
+ puts player.health
49
+ player.boost
50
+ puts player.health
51
+ player.drain
52
+ puts player.health
53
+ end
@@ -0,0 +1,24 @@
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
+
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: studio_game_106
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Vishwa Dharmasena
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: vishravd@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://offthegrid.lk
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.14
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Find the treasures and win!
54
+ test_files: []