ch_studio_game 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: 56266236ef1370f4a6bdf0c943abee4f90022083d9111b2c8f44aa257005965e
4
+ data.tar.gz: 1d23f138409620a4043697241e8df66e1c8be7ec39d649290fecec93faf579f9
5
+ SHA512:
6
+ metadata.gz: 04f77c85528a952c032f05ce3a89e3df2b8e73f5bb7057eab81e5bc68abb92bc259c283d562c605b6f53fca7af73d323f989a3ed35d2e4a341cba6a0721e2b1f
7
+ data.tar.gz: 4d1238d594d16fa1b0dd174b06a99b71945a37bcb2d1bfff9240d282648780c635aa81ea4993a6cd03f0f1f53794e337e5281044935a018a422f5e5ec53ad1c3
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) <2024> <Chris Harrison>
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,2 @@
1
+ # Studio Game
2
+ This is a simple game made following the ruby course on [PragmaticStudio.com](https://pragmaticstudio.com/courses/ruby)
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,40 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/studio_game/game'
3
+ require_relative '../lib/studio_game/player'
4
+ require_relative '../lib/studio_game/clumsy_player'
5
+ require_relative '../lib/studio_game/berserk_player'
6
+
7
+ player_1 = StudioGame::Player.new("broccoli rob", 60)
8
+ player_2 = StudioGame::Player.new("lucy", 90)
9
+ player_3 = StudioGame::Player.new("jase")
10
+ player_4 = StudioGame::Player.new("alex", 125)
11
+ klutz = StudioGame::ClumsyPlayer.new("klutz", 105, 3)
12
+ berserker = StudioGame::BerserkPlayer.new("berserker", 50)
13
+
14
+
15
+ game = StudioGame::Game.new("Winner Takes All")
16
+ game.add_player(player_1)
17
+ game.add_player(player_2)
18
+ game.add_player(player_3)
19
+ game.add_player(player_4)
20
+ game.add_player(klutz)
21
+ game.add_player(berserker)
22
+ game.play
23
+ # game = Game.new("Guardians of the Galaxy")
24
+ # players_file = File.join(__dir__, "players.csv")
25
+ # game.load_players(ARGV.shift || players_file)
26
+ # puts game.players
27
+ loop do
28
+ print "\nHow many game rounds? ('quit' to exit) "
29
+ answer = gets.chomp.downcase
30
+ case answer
31
+ when /^\d+$/
32
+ game.play(answer.to_i)
33
+ when 'quit', 'exit'
34
+ game.print_stats
35
+ break
36
+ else
37
+ "Please enter a number or type 'quit' or 'exit' to end"
38
+ end
39
+ end
40
+ # 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,28 @@
1
+ require_relative 'player'
2
+ module StudioGame
3
+ class BerserkPlayer < Player
4
+ def initialize(name, health)
5
+ super(name, health = 100)
6
+ @boost_count = 0
7
+ end
8
+ def berserk?
9
+ @boost_count > 5
10
+ end
11
+
12
+ def boost
13
+ super
14
+ @boost_count += 1
15
+ if berserk?
16
+ puts "#{@name} is berserk!"
17
+ end
18
+ end
19
+
20
+ def drain
21
+ berserk? ? boost : super
22
+ end
23
+ end
24
+ if __FILE__ == $0
25
+ berserker = BerserkPlayer.new("berserker", 50)
26
+ 8.times { berserker.boost }
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ require_relative 'player'
2
+ module StudioGame
3
+ class ClumsyPlayer < Player
4
+ def initialize(name, health = 100, boost_factor = 1)
5
+ super(name, health)
6
+ @boost_factor = boost_factor
7
+ end
8
+ def found_treasure(name, points)
9
+ points /= 2
10
+ super(name, points)
11
+ end
12
+ def boost
13
+ @boost_factor.times {super}
14
+ end
15
+ end
16
+
17
+
18
+
19
+ if __FILE__ == $0
20
+ clumsy = ClumsyPlayer.new("klutz")
21
+
22
+ clumsy.found_treasure("flute", 50)
23
+ clumsy.found_treasure("flute", 50)
24
+ clumsy.found_treasure("flute", 50)
25
+ clumsy.found_treasure("star", 100)
26
+
27
+ clumsy.found_treasures.each do |name, points|
28
+ puts "#{name}: #{points} points"
29
+ end
30
+ puts "#{clumsy.points} total points"
31
+ end
32
+ end
33
+
@@ -0,0 +1,108 @@
1
+ require_relative 'treasure_trove'
2
+ require_relative 'auditable'
3
+ require 'csv'
4
+ module StudioGame
5
+ class Game
6
+ include Auditable
7
+ attr_reader :title, :players
8
+ def initialize(title)
9
+ @title = title
10
+ @players = []
11
+ end
12
+
13
+ def add_player(player)
14
+ @players.append(player)
15
+ end
16
+
17
+ def roll_die
18
+ number = rand(1..6)
19
+ audit(number)
20
+ number
21
+ end
22
+
23
+ def play(rounds = 1)
24
+ puts "\n #{@title}"
25
+ puts "The following treasures can be found:"
26
+ puts TreasureTrove.treasure_items
27
+ puts "\n Before playing:"
28
+ puts @players
29
+ puts "-" * 10
30
+ 1.upto(rounds) do |round|
31
+ puts "Round #{round}"
32
+ @players.each do |player|
33
+ number_rolled = roll_die
34
+ case number_rolled
35
+ when 1..2
36
+ player.drain
37
+ puts("#{player.name} got drained 😩")
38
+ when 3..4
39
+ puts "#{player.name} got skipped"
40
+ else
41
+ player.boost
42
+ puts "#{player.name} got boosted 😁"
43
+ end
44
+ found_treasure = TreasureTrove.random_treasure
45
+ player.found_treasure(found_treasure.name, found_treasure.points)
46
+ puts "#{player.name} found a #{found_treasure.name} worth #{found_treasure.points} points"
47
+ end
48
+ puts "-" * 10
49
+ end
50
+ puts "After Playing: "
51
+ puts @players
52
+ end
53
+
54
+ def sorted_players
55
+ @players.sort_by {|player| player.score}.reverse
56
+ end
57
+
58
+ def print_treasure_total
59
+ @players.each do |player |
60
+ puts "\n#{player.name}'s treasure point totals:"
61
+ player.found_treasures.each do |treasure, value|
62
+ puts "#{treasure}: #{value}"
63
+ end
64
+ puts "total: #{player.points}"
65
+ end
66
+ end
67
+
68
+ def print_high_scores
69
+ sorted_players.each do |player|
70
+ puts "#{player.name}" + "." * 10 + " #{player.score}"
71
+ end
72
+ end
73
+
74
+ def print_stats
75
+ puts "\n Game Stats:"
76
+ puts "-" * 40
77
+ sorted_players.each do |player|
78
+ puts high_score_entry(player)
79
+ end
80
+ end
81
+
82
+
83
+ def high_score_entry(player)
84
+ name = player.name.ljust(20, ".")
85
+ score = player.score.round.to_s.rjust(5)
86
+ "#{name}#{score}"
87
+ end
88
+
89
+ def save_high_scores(file_name = "high_scores.txt")
90
+ File.open(file_name, 'w') do |file|
91
+ sorted_players.each do |player|
92
+ file.puts high_score_entry(player)
93
+ end
94
+ end
95
+ end
96
+
97
+ def load_players(from_file)
98
+ File.readlines(from_file, chomp: true).each do |line|
99
+ player = Player.from_csv(line)
100
+ add_player(player)
101
+ end
102
+ rescue Errno::ENOENT
103
+ puts "Whoops, #{from_file} not found!"
104
+ exit 1
105
+ end
106
+ end
107
+ end
108
+
@@ -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,51 @@
1
+ require_relative 'playable'
2
+ module StudioGame
3
+ class Player
4
+ include Playable
5
+ attr_accessor :name, :health
6
+ attr_reader :found_treasures
7
+ def initialize(name, health = 100)
8
+ @name = name.split(" ").map {|word| word.capitalize}.join(" ")
9
+ @health = health
10
+ @found_treasures = Hash.new(0)
11
+ end
12
+
13
+ def to_s
14
+ "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}"
15
+ end
16
+
17
+ def score
18
+ @health + points
19
+ end
20
+
21
+ def name=(new_name)
22
+ @name = new_name.capitalize
23
+ end
24
+
25
+ def found_treasure(name, points)
26
+ @found_treasures[name] += points
27
+ end
28
+
29
+ def points
30
+ @found_treasures.values.sum
31
+ end
32
+
33
+ def self.from_csv(line)
34
+ name, health = line.split(",")
35
+ player = Player.new(name, Integer(health))
36
+ rescue ArgumentError
37
+ puts "Ignored invalid health: #{health}"
38
+ player = Player.new(name)
39
+ end
40
+ end
41
+
42
+ if __FILE__ == $0
43
+ player = Player.new("jase")
44
+ puts player.name
45
+ puts player.health
46
+ player.boost
47
+ puts player.health
48
+ player.drain
49
+ puts player.health
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ module TreasureTrove
2
+ Treasure = Data.define(:name, :points)
3
+
4
+ TREASURES = [
5
+ Treasure.new("pie", 10),
6
+ Treasure.new("coin", 25),
7
+ Treasure.new("flute", 50),
8
+ Treasure.new("compass", 65),
9
+ Treasure.new("key", 80),
10
+ Treasure.new("crown", 90),
11
+ Treasure.new("star", 100)
12
+ ]
13
+
14
+ def self.random_treasure
15
+ TREASURES.sample
16
+ end
17
+
18
+ def self.treasure_items
19
+ TREASURES.map { |treasure| "A #{treasure.name} is worth #{treasure.points} points" }
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ch_studio_game
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Harrison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: chris@charrison.dev
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://charrison.dev
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: simple command line game
54
+ test_files: []