studio_game_pragmatic_course 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 +7 -0
- data/LICENSE.txt +7 -0
- data/README.md +1 -0
- data/bin/players.csv +5 -0
- data/bin/studio_game +32 -0
- data/lib/studio_game/auditable.rb +7 -0
- data/lib/studio_game/berserk_player.rb +28 -0
- data/lib/studio_game/clumsy_player.rb +10 -0
- data/lib/studio_game/game.rb +106 -0
- data/lib/studio_game/playable.rb +6 -0
- data/lib/studio_game/player.rb +44 -0
- data/lib/studio_game/treasure_trove.rb +22 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 47cfa3de569833dddb1f91dc98a5e50f996a6e0e7d081fabfa200af51c534bde
|
|
4
|
+
data.tar.gz: 42791e01d7f2f9bb562b8577bb49d6e073b17517a68b603fdadc6fb0ae615a7f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 84567ec48827eadd44ae67489b2d5ef3f38a5049b2bae5afe9aa46c78f62bda2c360cc005751b383f0a55c6445bfd641589fc22d8161ec043b28aec9c60edd12
|
|
7
|
+
data.tar.gz: ea3f3b05211a305c14a96be5e5a309d1326535f52510a3b8e44b9e3bed9224487872f5278fd93d93de0d006889885fbfb0943012ba187d33bc8e3bae661a5baa
|
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 @@
|
|
|
1
|
+
Ruby gem created for Pragmatic Studio course
|
data/bin/players.csv
ADDED
data/bin/studio_game
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
game = StudioGame::Game.new("winner takes All")
|
|
9
|
+
players_file = File.join(__dir__, "players.csv")
|
|
10
|
+
game.load_players(ARGV.shift || players_file)
|
|
11
|
+
clumsy = StudioGame::ClumsyPlayer.new("klutz", 105)
|
|
12
|
+
game.add_player(clumsy)
|
|
13
|
+
berserker = StudioGame::BerserkPlayer.new("berserker", 50)
|
|
14
|
+
game.add_player(berserker)
|
|
15
|
+
|
|
16
|
+
loop do
|
|
17
|
+
print "\nHow many rounds? "
|
|
18
|
+
|
|
19
|
+
answer = gets.chomp.downcase
|
|
20
|
+
|
|
21
|
+
case answer
|
|
22
|
+
when /^\d+$/
|
|
23
|
+
game.play(answer.to_i)
|
|
24
|
+
when "quit", "exit"
|
|
25
|
+
game.print_stats
|
|
26
|
+
break
|
|
27
|
+
else
|
|
28
|
+
puts "Please enter a number or quit"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
game.save_high_scores
|
|
@@ -0,0 +1,28 @@
|
|
|
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 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
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require_relative "treasure_trove"
|
|
2
|
+
require_relative "auditable"
|
|
3
|
+
|
|
4
|
+
module StudioGame
|
|
5
|
+
class Game
|
|
6
|
+
include Auditable
|
|
7
|
+
|
|
8
|
+
attr_reader :title, :players
|
|
9
|
+
|
|
10
|
+
def initialize(title)
|
|
11
|
+
@title = title.split(" ").map { |word| word.capitalize }.join(" ")
|
|
12
|
+
@players = []
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def load_players(filename)
|
|
16
|
+
File.readlines(filename, chomp: true).each do |line|
|
|
17
|
+
player = Player.from_csv(line)
|
|
18
|
+
add_player(player)
|
|
19
|
+
end
|
|
20
|
+
rescue Errno::ENOENT => e
|
|
21
|
+
puts "Error: #{e}"
|
|
22
|
+
exit 1
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def save_high_scores(filename = "high_scores.txt")
|
|
26
|
+
File.open(filename, "w") do |file|
|
|
27
|
+
file.puts "#{@title} High Scores:"
|
|
28
|
+
sorted_players.each do |player|
|
|
29
|
+
file.puts high_score_entry(player)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def high_score_entry(player)
|
|
35
|
+
name = player.name.ljust(20, ".")
|
|
36
|
+
score = player.score.round.to_s.rjust(5)
|
|
37
|
+
"#{name}#{score}"
|
|
38
|
+
end
|
|
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 sorted_players
|
|
51
|
+
@players.sort_by { |player| player.score }.reverse
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def print_stats
|
|
55
|
+
puts "\nGAME #{@title}"
|
|
56
|
+
puts "-" * 30
|
|
57
|
+
|
|
58
|
+
puts sorted_players
|
|
59
|
+
|
|
60
|
+
@players.each do |player|
|
|
61
|
+
puts "\n#{player.name} treasures"
|
|
62
|
+
player.found_treasures.each do |name, points|
|
|
63
|
+
puts "#{name}: #{points}"
|
|
64
|
+
end
|
|
65
|
+
puts "total: #{player.points}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
puts "\nHigh Scores:"
|
|
69
|
+
sorted_players.each do |player|
|
|
70
|
+
puts high_score_entry(player)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def play(rounds = 1)
|
|
75
|
+
puts @players
|
|
76
|
+
|
|
77
|
+
puts "\nThe following treasures can be found:"
|
|
78
|
+
puts TreasureTrove.treasure_items
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
1.upto(rounds) do |round|
|
|
82
|
+
puts "\nround #{round}"
|
|
83
|
+
@players.each do |player|
|
|
84
|
+
number_rolled = roll_die
|
|
85
|
+
|
|
86
|
+
case number_rolled
|
|
87
|
+
when 1..2
|
|
88
|
+
player.drain
|
|
89
|
+
puts "#{player.name} got drained"
|
|
90
|
+
when 3..4
|
|
91
|
+
puts "#{player.name} got skipped"
|
|
92
|
+
else
|
|
93
|
+
player.boost
|
|
94
|
+
puts "#{player.name} got boosted"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
treasure = TreasureTrove.random_treasure
|
|
98
|
+
puts "#{player.name} found a #{treasure.name} worth #{treasure.points} points"
|
|
99
|
+
|
|
100
|
+
player.found_treasure(treasure.name, treasure.points)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
puts @players
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require_relative "playable"
|
|
2
|
+
|
|
3
|
+
module StudioGame
|
|
4
|
+
class Player
|
|
5
|
+
include Playable
|
|
6
|
+
|
|
7
|
+
attr_reader :name, :found_treasures
|
|
8
|
+
attr_accessor :health
|
|
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 to_s
|
|
17
|
+
"I'm #{@name} with health = #{@health}, points = #{points}, score = #{score}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.from_csv(line)
|
|
21
|
+
name, health = line.split(',')
|
|
22
|
+
Player.new(name, Integer(health))
|
|
23
|
+
rescue ArgumentError
|
|
24
|
+
puts "#{health} is not a valid health"
|
|
25
|
+
Player.new(name)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def points
|
|
29
|
+
@found_treasures.values.sum
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def score
|
|
33
|
+
@health + points
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def name=(new_name)
|
|
37
|
+
@name = new_name.capitalize
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def found_treasure(name, points)
|
|
41
|
+
@found_treasures[name] += points
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module StudioGame
|
|
2
|
+
module TreasureTrove
|
|
3
|
+
Treasure = Data.define(:name, :points)
|
|
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
|
|
22
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: studio_game_pragmatic_course
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Lucas
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-05-24 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email: lucasjin.hh@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://www.lucasjin.ca/
|
|
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.18
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 4
|
|
53
|
+
summary: Can create text based game in Ruby.
|
|
54
|
+
test_files: []
|