zsolo_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 +7 -0
- data/LICENSE.txt +19 -0
- data/README.md +1 -0
- data/bin/players.csv +5 -0
- data/bin/zsolo_game +43 -0
- data/lib/zsolo_game/auditable.rb +7 -0
- data/lib/zsolo_game/berserk_player.rb +33 -0
- data/lib/zsolo_game/clumsy_player.rb +39 -0
- data/lib/zsolo_game/game.rb +108 -0
- data/lib/zsolo_game/playable.rb +11 -0
- data/lib/zsolo_game/player.rb +56 -0
- data/lib/zsolo_game/treasure_trove.rb +23 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: da511a6c03595930270330ca3887a9c3439c313e60d47b8db9327d0c2d453b35
|
|
4
|
+
data.tar.gz: f0afaf492963d4b3daf535f061ef7a8e775991ee2630a9d9c1f99758ec961157
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fad774fba310f2f95c5675c2381b04c71046b13a0ef44600e23e523cd747fe90bb2e9189505e31a7142098d712dab76b2864e0033026448016130649d0214363
|
|
7
|
+
data.tar.gz: b98a06efeb8549903eabc5d1deddc74e3603333c68257c10cb1df28b0c81051566ecc3f597969e854d92c5a2e194d6d7475ce93b45f25f92a99e39518e3cff97
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2024 Zsolt Szalma
|
|
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 very simple command line game which was built as a project of the [Pragmatic Studio's](https://pragmaticstudio.com) Ruby course.
|
data/bin/players.csv
ADDED
data/bin/zsolo_game
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require_relative "../lib/zsolo_game/clumsy_player.rb"
|
|
4
|
+
require_relative "../lib/zsolo_game/berserk_player.rb"
|
|
5
|
+
require_relative "../lib/zsolo_game/game.rb"
|
|
6
|
+
|
|
7
|
+
game = ZsoloGame::Game.new("The Winner Takes All")
|
|
8
|
+
file_path = File.join(__dir__, 'players.csv')
|
|
9
|
+
game.load_players(ARGV.shift || file_path)
|
|
10
|
+
berserker = ZsoloGame::BerserkPlayer.new("berserker", 50)
|
|
11
|
+
klutz = ZsoloGame::ClumsyPlayer.new("klutz", 120)
|
|
12
|
+
game.add_player(berserker)
|
|
13
|
+
game.add_player(klutz)
|
|
14
|
+
|
|
15
|
+
# this method could be incapsulated in the Game class!?
|
|
16
|
+
def run_game(game)
|
|
17
|
+
loop_runs = 0
|
|
18
|
+
|
|
19
|
+
loop do
|
|
20
|
+
print "\nHow many rounds? ('quit' to exit) "
|
|
21
|
+
answer = gets.chomp.downcase
|
|
22
|
+
|
|
23
|
+
case answer
|
|
24
|
+
when /^\d+$/
|
|
25
|
+
game.play(answer.to_i)
|
|
26
|
+
when "quit"
|
|
27
|
+
if loop_runs > 0
|
|
28
|
+
game.print_stats
|
|
29
|
+
else
|
|
30
|
+
puts "Bye-bye!"
|
|
31
|
+
end
|
|
32
|
+
break
|
|
33
|
+
else
|
|
34
|
+
puts "Please type only numbers or 'quit'"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
loop_runs += 1
|
|
38
|
+
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
run_game(game)
|
|
43
|
+
game.save_high_scores
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require_relative "./player.rb"
|
|
2
|
+
|
|
3
|
+
module ZsoloGame
|
|
4
|
+
class BerserkPlayer < Player
|
|
5
|
+
|
|
6
|
+
def initialize(name, health=100)
|
|
7
|
+
super(name, health)
|
|
8
|
+
@times_boosted = 0
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def berserk?
|
|
12
|
+
@times_boosted > 5
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def boost
|
|
16
|
+
super
|
|
17
|
+
@times_boosted += 1
|
|
18
|
+
puts "#{name} has gone berserk!!" if berserk?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def drain
|
|
22
|
+
berserk? ? boost : super
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
if __FILE__ == $0
|
|
28
|
+
berserker = BerserkPlayer.new("berserker", 50)
|
|
29
|
+
6.times {berserker.boost}
|
|
30
|
+
2.times {berserker.drain}
|
|
31
|
+
puts berserker.health
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require_relative './player.rb'
|
|
2
|
+
|
|
3
|
+
module ZsoloGame
|
|
4
|
+
class ClumsyPlayer < Player
|
|
5
|
+
|
|
6
|
+
attr_reader :boost_factor
|
|
7
|
+
|
|
8
|
+
def initialize(name, health=100, boost_factor=1)
|
|
9
|
+
super(name, health)
|
|
10
|
+
@boost_factor = boost_factor
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add_treasure(name, value)
|
|
14
|
+
value = (value / 2.0).round
|
|
15
|
+
super(name, value)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def boost
|
|
19
|
+
@boost_factor.times { super }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if __FILE__ == $0
|
|
25
|
+
clumsy = ClumsyPlayer.new("klutz", 3)
|
|
26
|
+
clumsy.add_treasure('flute', 50)
|
|
27
|
+
clumsy.add_treasure('flute', 50)
|
|
28
|
+
clumsy.add_treasure('flute', 50)
|
|
29
|
+
clumsy.add_treasure('star', 100)
|
|
30
|
+
|
|
31
|
+
puts clumsy.treasures
|
|
32
|
+
|
|
33
|
+
clumsy.treasures.each do |treasure, points|
|
|
34
|
+
puts "#{treasure}: #{points} points"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
puts "#{clumsy.points} total points"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
require_relative './treasure_trove.rb'
|
|
2
|
+
require_relative './player.rb'
|
|
3
|
+
require_relative './auditable.rb'
|
|
4
|
+
|
|
5
|
+
module ZsoloGame
|
|
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 load_players(from_file)
|
|
17
|
+
begin
|
|
18
|
+
File.readlines(from_file, chomp: true).each do |line|
|
|
19
|
+
add_player(Player.from_csv(line))
|
|
20
|
+
end
|
|
21
|
+
rescue Errno::ENOENT => exception
|
|
22
|
+
puts "Whoops, #{from_file} not found!"
|
|
23
|
+
# puts exception.message
|
|
24
|
+
exit 1 # exit program with code 1 (error happened)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def add_player(player)
|
|
29
|
+
@players.append(player)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def roll_die
|
|
33
|
+
# number = [1, 1, 2, 5, 6, 6].sample
|
|
34
|
+
number = rand(1..6)
|
|
35
|
+
puts audit(number)
|
|
36
|
+
number
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def audit(number)
|
|
40
|
+
"Audit: rolled a #{number}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def sorted_players
|
|
44
|
+
@players.sort_by { |p| p.score }.reverse
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def high_scores
|
|
48
|
+
sorted_players.map do |player|
|
|
49
|
+
"#{player.name.ljust(20, '.')} #{player.score}"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def save_high_scores(to_file="high_scores.txt")
|
|
54
|
+
File.open(to_file, "w") do |file|
|
|
55
|
+
file.puts "The #{@title} High Scores:"
|
|
56
|
+
file.puts high_scores
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def player_treasures
|
|
61
|
+
@players.each do |player|
|
|
62
|
+
puts "\n#{player.name}'s treasue point totals:\n"
|
|
63
|
+
player.treasures.each do |name, value|
|
|
64
|
+
puts "#{name}: #{value}"
|
|
65
|
+
end
|
|
66
|
+
puts "total: $#{player.points}\n\n"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def print_stats
|
|
71
|
+
puts "\n#{title} Game Stats:"
|
|
72
|
+
puts '-' * 30
|
|
73
|
+
puts sorted_players
|
|
74
|
+
player_treasures
|
|
75
|
+
puts "High Scores:"
|
|
76
|
+
puts high_scores
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def play(rounds)
|
|
80
|
+
puts self, "\n"
|
|
81
|
+
puts "\nAvailable treasures:"
|
|
82
|
+
puts TreasureTrove.treasure_items
|
|
83
|
+
puts "\nBefore playing:"
|
|
84
|
+
puts @players, "\n"
|
|
85
|
+
|
|
86
|
+
1.upto(rounds) do |round|
|
|
87
|
+
puts "\nRound #{round}.\n"
|
|
88
|
+
@players.each do |player|
|
|
89
|
+
case roll_die
|
|
90
|
+
when 1..2
|
|
91
|
+
player.drain
|
|
92
|
+
puts "#{player.name}'s health was drained"
|
|
93
|
+
when 3..4
|
|
94
|
+
puts "#{player.name} was skipped"
|
|
95
|
+
else
|
|
96
|
+
player.boost
|
|
97
|
+
puts "#{player.name}'s health was boosted"
|
|
98
|
+
end
|
|
99
|
+
treasure_found = TreasureTrove.random_treasure
|
|
100
|
+
player.add_treasure(treasure_found.name, treasure_found.points)
|
|
101
|
+
puts "#{player.name} found a #{treasure_found.name} worth of #{treasure_found.points} points"
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def to_s = "Now playing '#{@title}'"
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require_relative "playable"
|
|
2
|
+
|
|
3
|
+
module ZsoloGame
|
|
4
|
+
class Player
|
|
5
|
+
include Playable
|
|
6
|
+
|
|
7
|
+
# An attribute is actually a setter or getter method
|
|
8
|
+
attr_reader :treasures
|
|
9
|
+
attr_accessor :name, :health
|
|
10
|
+
|
|
11
|
+
def initialize(name, health=100)
|
|
12
|
+
@name = name.capitalize
|
|
13
|
+
@health = health
|
|
14
|
+
@treasures = Hash.new(0)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.from_csv(line)
|
|
18
|
+
name, health = line.split(',')
|
|
19
|
+
# health = Integer(health) rescue 100
|
|
20
|
+
Player.new(name, Integer(health))
|
|
21
|
+
rescue ArgumentError => e
|
|
22
|
+
puts e.message
|
|
23
|
+
puts "Using 100 as default for player's health"
|
|
24
|
+
Player.new(name)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_csv(player)
|
|
28
|
+
"#{player.name},#{player.score}"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def name=(new_name)
|
|
32
|
+
@name = new_name.capitalize
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def add_treasure(name, value)
|
|
36
|
+
@treasures[name] += value
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def score
|
|
40
|
+
@health + points
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def points
|
|
44
|
+
@treasures.values.sum
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# A one-liner (or endless) method
|
|
48
|
+
def to_s = "I'm #{@name} with a health of #{@health}, points of #{points} and a score of #{score}."
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
if __FILE__ == $0
|
|
53
|
+
player = Player.new("bob", 70)
|
|
54
|
+
puts player
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module ZsoloGame
|
|
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 {|t| "A #{t.name} is worth #{t.points} points"}
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: zsolo_game
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Zsolt Szalma
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2024-02-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email: zsolt.szalma@gmail.com
|
|
15
|
+
executables:
|
|
16
|
+
- zsolo_game
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- LICENSE.txt
|
|
21
|
+
- README.md
|
|
22
|
+
- bin/players.csv
|
|
23
|
+
- bin/zsolo_game
|
|
24
|
+
- lib/zsolo_game/auditable.rb
|
|
25
|
+
- lib/zsolo_game/berserk_player.rb
|
|
26
|
+
- lib/zsolo_game/clumsy_player.rb
|
|
27
|
+
- lib/zsolo_game/game.rb
|
|
28
|
+
- lib/zsolo_game/playable.rb
|
|
29
|
+
- lib/zsolo_game/player.rb
|
|
30
|
+
- lib/zsolo_game/treasure_trove.rb
|
|
31
|
+
homepage: https://github.com/szalmazsolt/zsolo_game
|
|
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.19
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 4
|
|
53
|
+
summary: A very, very simple command line game
|
|
54
|
+
test_files: []
|