wsgame 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: 55ec29aef284ee944a51a238064080b97d3b2c667226f358664244690cbea8d4
4
+ data.tar.gz: 5ce58558da21a4ebf445694f032c8eddba0ec8fe542403a0c69cbaa6cb13990b
5
+ SHA512:
6
+ metadata.gz: 000ea9411ebf32c5aae673e179efcedc161cea7b5152f720d9be82ea39675e06ee352f4e23dfbd66ecbe128ed7b2cd76dc8264c12455657cdae2bf72a3315303
7
+ data.tar.gz: 90617c3d9f47468d4b54d9b453b0c2850998299ae5e795ba7d0626b0f3b3e478afa7a2822887c52aa5105c122762d2e6c0f3f32c84e8ce3a144e2d614a667043
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2022 Wesen
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
+ - You may not use this Software in other training contexts.
11
+
12
+ - The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ This is an application used in The Pragmatic Studio's home work
2
+ Ruby Programming course, as described at
3
+
4
+ http://pragmaticstudio.com
5
+
6
+ This code is Copyright 2022 Wesen. See the LICENSE file.
data/bin/players.csv ADDED
@@ -0,0 +1,4 @@
1
+ ann,50
2
+ ily,90
3
+ samanta,40
4
+ lilly,120
data/bin/wsgame ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/wsgame/class_game'
4
+
5
+ knuckleheads = WSGame::Game.new("Knuckleheads")
6
+
7
+ default_movie_file = File.join(File.dirname(__FILE__), 'players.csv')
8
+ knuckleheads.load(ARGV.shift || default_movie_file)
9
+
10
+ clumsy = WSGame::ClumsyPlayer.new('clumsy',90)
11
+ knuckleheads.add_player(clumsy)
12
+
13
+ berserk = WSGame::BerserkPlayer.new('berserk',90)
14
+ knuckleheads.add_player(berserk)
15
+
16
+
17
+ loop do
18
+ puts "\nHow many rounds? ('quit' to exit)"
19
+ answer = gets.chomp.downcase
20
+ case answer
21
+ when /^\d+$/
22
+ knuckleheads.play(answer.to_i)
23
+ when 'quit', 'exit','e','q'
24
+ knuckleheads.statistic
25
+ knuckleheads.hight_score
26
+ break
27
+ else
28
+ puts "Please enter a number or 'quit'"
29
+ end
30
+ end
31
+
32
+
33
+ knuckleheads.save
34
+
35
+
36
+
37
+
38
+
@@ -0,0 +1,109 @@
1
+ require_relative 'class_player'
2
+ require_relative 'class_player_clumpsy'
3
+ require_relative 'class_player_berserk'
4
+ require_relative 'gt'
5
+ require_relative 'treasure_trove'
6
+
7
+
8
+ module WSGame
9
+
10
+ class Game
11
+
12
+ def initialize(name="Some list")
13
+ @name=name.capitalize
14
+ @players=[]
15
+ end
16
+
17
+ def add_player(player)
18
+ @players << player
19
+ end
20
+
21
+
22
+ def play(rounds=1)
23
+ greet = "Welcome!"
24
+ gtime=Time.new
25
+ puts "\nThe game started on #{gtime.strftime('%A %d/%m/%Y at %H:%M%p')}\n\n"
26
+ puts "There are #{@players.length} players in the game #{@name}:"
27
+ puts @players
28
+
29
+ treasures= TreasureTrove::TREASURES
30
+ puts "\nThere are #{treasures.size} treasures in the game:"
31
+ treasures.each do |tr|
32
+ puts "#{tr.name} (#{tr.points})"
33
+ end
34
+
35
+
36
+ 1.upto(rounds) do |cnt|
37
+ puts "\n Round #{cnt}:"
38
+ @players.each do |p|
39
+ GameTurn.take_turn(p)
40
+ tr = TreasureTrove.random
41
+ p. found_treasure(tr)
42
+ #puts p
43
+ end
44
+ end
45
+ end
46
+
47
+ def print_statistic(players)
48
+ players.each do |p|
49
+ puts "#{p.pname} (#{p.phealth})"
50
+ end
51
+
52
+ end
53
+
54
+ def total_treasures
55
+ @players.reduce(0) do |sum, m|
56
+ sum + m.all_treasures
57
+ end
58
+ end
59
+
60
+
61
+ def statistic
62
+ puts "\n#{@name} Statistics:"
63
+
64
+
65
+ @players.sort.each do |m|
66
+ puts "\n#{m.pname}'s treasure points: "
67
+
68
+ m.each_treasure do |treasure|
69
+ puts "-- #{treasure.points} total #{treasure.name} points"
70
+ end
71
+ puts " Total: #{m.all_treasures} points"
72
+ end
73
+
74
+ puts "Total treasures piont in the Game: #{total_treasures}"
75
+
76
+ strong,wimpy= @players.partition {|p| p.strong?}
77
+ puts "\n #{strong.length} Strong:"
78
+ print_statistic(strong)
79
+ puts "\n #{wimpy.length} Wimpy:"
80
+ print_statistic(wimpy)
81
+
82
+ end
83
+
84
+
85
+ def hight_score
86
+ puts "\n#{@name} Scores:"
87
+ @players.sort.each do |p|
88
+ puts "#{p.pname.ljust(20,'.')} #{p.score}"
89
+ end
90
+ end
91
+
92
+ def load(from_file)
93
+ File.readlines(from_file).each do |line|
94
+ add_player(Player.from_csv(line))
95
+ end
96
+ end
97
+
98
+ def save(to_file="my_favorite_players.csv")
99
+ File.open(to_file, "w") do |file|
100
+ file.puts @players.sort.map { |p| p.to_csv }
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+
108
+
109
+
@@ -0,0 +1,70 @@
1
+ require_relative 'module_playable'
2
+
3
+ module WSGame
4
+
5
+ class Player
6
+
7
+ attr_accessor :pname, :phealth
8
+ include Playable
9
+
10
+ def initialize(pname = "guest", phealth = 10)
11
+ @pname = pname.capitalize
12
+ @phealth = phealth
13
+ @treasure_hash = Hash.new(0)
14
+ end
15
+
16
+ def to_s
17
+ "I'm #{@pname} with a health of #{@phealth} and a score of #{score}."
18
+ end
19
+
20
+
21
+ def found_treasure(tr)
22
+ @treasure_hash[tr.name] += tr.points
23
+ puts "#{@pname} found #{tr.name} with #{tr.points} points"
24
+ puts "#{@pname} treasures: #{@treasure_hash}"
25
+ end
26
+
27
+ def all_treasures
28
+ @treasure_hash.values.reduce(0,:+)
29
+ end
30
+
31
+ def each_treasure
32
+ @treasure_hash.each do |name, points|
33
+ tr = Treasure.new(name,points)
34
+ yield tr
35
+ end
36
+
37
+ end
38
+
39
+ def to_csv
40
+ "#{@pname},#{@phealth},#{score}"
41
+ end
42
+
43
+ def self.from_csv(string)
44
+ pname, phealth = string.split(',')
45
+ new(pname, Integer(phealth))
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ # ��� ������
52
+
53
+ if __FILE__ == $0
54
+
55
+ user_1=Player.new("larry",60)
56
+ user_2=Player.new("curly",125)
57
+ user_3=Player.new("moe",100)
58
+
59
+
60
+ puts user_1
61
+ user_1.blamm
62
+ puts user_1
63
+ puts user_2
64
+ user_2.w00t
65
+ puts user_2
66
+ puts user_3
67
+ user_3.blamm
68
+ puts user_3
69
+
70
+ end
@@ -0,0 +1,54 @@
1
+ require_relative 'class_player'
2
+ require_relative 'treasure_trove'
3
+
4
+ module WSGame
5
+
6
+ class BerserkPlayer < Player
7
+
8
+ def initialize(pname,phealth)
9
+ super(pname,phealth)
10
+
11
+ @cnt_w00t = 0
12
+ end
13
+
14
+
15
+ def blamm
16
+ if @cnt_w00t > 3
17
+ self.w00t
18
+ puts "#{@pname} is BERSERK!!!!"
19
+ else
20
+ super
21
+ end
22
+
23
+ #@cnt_w00t > 3 ? self.w00t : super
24
+ end
25
+
26
+ def w00t
27
+ @cnt_w00t +=1
28
+ super
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ if __FILE__ == $0
36
+
37
+ berserk = BerserkPlayer.new('berserk',90)
38
+ puts berserk
39
+ berserk.w00t
40
+ puts berserk
41
+ berserk.w00t
42
+ puts berserk
43
+ berserk.w00t
44
+ puts berserk
45
+ berserk.w00t
46
+ puts berserk
47
+ berserk.w00t
48
+ puts berserk
49
+ berserk.blamm
50
+ puts berserk
51
+ berserk.blamm
52
+ puts berserk
53
+
54
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'class_player'
2
+ require_relative 'treasure_trove'
3
+
4
+ module WSGame
5
+
6
+ class ClumsyPlayer < Player
7
+
8
+
9
+ def initialize(pname,phealth, boost=1)
10
+ super(pname,phealth)
11
+
12
+ @boost=boost
13
+ end
14
+
15
+ def w00t
16
+ @boost.times {super}
17
+
18
+ end
19
+
20
+
21
+ def found_treasure(tr)
22
+ tr.points=tr.points/2
23
+ super
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+
31
+ if __FILE__ == $0
32
+
33
+ clumsy = ClumsyPlayer.new('clumsy',90,3)
34
+ puts clumsy
35
+ clumsy.w00t
36
+ puts clumsy
37
+
38
+ end
data/lib/wsgame/gt.rb ADDED
@@ -0,0 +1,25 @@
1
+ module WSGame
2
+
3
+ module GameTurn
4
+
5
+ def self.roll_die
6
+ rand(1..6)
7
+ end
8
+
9
+ def self.take_turn(p)
10
+
11
+ rolled=roll_die
12
+
13
+ case rolled
14
+ when 1..2
15
+ p.blamm
16
+ when 3..4
17
+ puts "#{p.pname} was skipped"
18
+ else
19
+ p.w00t
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,40 @@
1
+ module WSGame
2
+
3
+ module Playable
4
+
5
+
6
+ def blamm
7
+ puts "#{@pname} got blammed!"
8
+ self.phealth -= 10
9
+ end
10
+
11
+ def w00t
12
+ puts "#{@pname} got w00ted!"
13
+ self.phealth += 15
14
+ end
15
+
16
+ def score
17
+ self.phealth + all_treasures
18
+ end
19
+
20
+ def strong?
21
+ self.phealth > 100
22
+ end
23
+
24
+ def <=>(other_player)
25
+ other_player.score <=> self.score
26
+ end
27
+
28
+
29
+ end
30
+
31
+ end
32
+
33
+ # ��� ������
34
+
35
+ if __FILE__ == $0
36
+
37
+
38
+
39
+
40
+ end
@@ -0,0 +1,30 @@
1
+
2
+ module WSGame
3
+
4
+ Treasure = Struct.new(:name, :points)
5
+
6
+ module TreasureTrove
7
+
8
+ TREASURES = [
9
+ Treasure.new(:pie, 5),
10
+ Treasure.new(:bottle, 25),
11
+ Treasure.new(:hammer, 50),
12
+ Treasure.new(:skillet, 100),
13
+ Treasure.new(:broomstick, 200),
14
+ Treasure.new(:crowbar , 400)
15
+ ]
16
+
17
+ def self.random
18
+ TREASURES.sample
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ if __FILE__ == $0
26
+ tr = TreasureTrove.random
27
+ puts "Enjoy #{tr.name}"
28
+
29
+ end
30
+
@@ -0,0 +1,46 @@
1
+ require 'rspec'
2
+
3
+ require_relative 'class_game'
4
+
5
+ module WSGame
6
+
7
+ describe Game do
8
+
9
+ before do
10
+ @game = Game.new("Knuckleheads")
11
+
12
+
13
+ end
14
+
15
+ context "being played wiyh one player" do
16
+
17
+ before do
18
+ @initial_health = 60
19
+ @player = Player.new("larry",@initial_health)
20
+ @game.add_player(@player)
21
+ end
22
+
23
+ it "it gives the player a blamm if the low number is rolled" do
24
+ allow(GameTurn).to receive(:roll_die) {1}
25
+ @game.play
26
+ expect(@player.phealth).to eq (@initial_health - 10)
27
+ end
28
+
29
+
30
+ it "skip the player if the medium number is rolled" do
31
+ allow(GameTurn).to receive(:roll_die) {4}
32
+ @game.play
33
+ expect(@player.phealth).to eq (@initial_health)
34
+ end
35
+
36
+ it "it gives the player a w00t if the high number is rolled" do
37
+ allow(GameTurn).to receive(:roll_die) {5}
38
+ @game.play
39
+ expect(@player.phealth).to eq (@initial_health + 15)
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,66 @@
1
+ require 'rspec'
2
+
3
+ require_relative 'class_player'
4
+
5
+ module WSGame
6
+
7
+ describe Player do
8
+
9
+ before do
10
+ @initial_health = 60
11
+ @player = Player.new("larry",@initial_health)
12
+ end
13
+
14
+ it "has a capitalized name" do
15
+ expect(@player.pname).to eq "Larry"
16
+ end
17
+
18
+ it "has an initial health" do
19
+ expect(@player.phealth).to eq @initial_health
20
+ end
21
+
22
+ it "has a string representation" do
23
+ expect(@player.to_s).to eq "I'm Larry with a health of 60 and a score of 60."
24
+
25
+ end
26
+
27
+ it "computes a score as the sum of its health and length of name" do
28
+ expect(@player.score).to eq (@player.phealth)
29
+ end
30
+
31
+ it "increases health by 15 when w00ted" do
32
+ expect(@player.w00t).to eq (@initial_health + 15)
33
+ end
34
+
35
+ it "decreases health by 10 when blammed"do
36
+ expect(@player.blamm).to eq (@initial_health - 10)
37
+ end
38
+
39
+
40
+ context "players health 150" do
41
+ before do
42
+ @player = Player.new("larry",150)
43
+ end
44
+
45
+ it "is strong" do
46
+ expect(@player).to be_strong
47
+ end
48
+
49
+ end
50
+
51
+
52
+ context "players health 100" do
53
+ before do
54
+ @player = Player.new("larry",100)
55
+ end
56
+
57
+ it "is wimpy" do
58
+ expect(@player).to_not be_strong
59
+ end
60
+
61
+ end
62
+
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,98 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # This allows you to limit a spec run to individual examples or groups
51
+ # you care about by tagging them with `:focus` metadata. When nothing
52
+ # is tagged with `:focus`, all examples get run. RSpec also provides
53
+ # aliases for `it`, `describe`, and `context` that include `:focus`
54
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55
+ config.filter_run_when_matching :focus
56
+
57
+ # Allows RSpec to persist some state between runs in order to support
58
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
59
+ # you configure your source control system to ignore this file.
60
+ config.example_status_persistence_file_path = "spec/examples.txt"
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is
63
+ # recommended. For more details, see:
64
+ # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = "doc"
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ =end
98
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wsgame
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Wesen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.11'
27
+ description: |-
28
+ This is an application used in The Pragmatic Studio's home work
29
+ Ruby Programming course, as described at
30
+
31
+ http://pragmaticstudio.com
32
+
33
+ This code is Copyright 2022 Wesen. See the LICENSE file.
34
+ email: t.wesen@yandex.ru
35
+ executables:
36
+ - wsgame
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - LICENSE
41
+ - README
42
+ - bin/players.csv
43
+ - bin/wsgame
44
+ - lib/wsgame/class_game.rb
45
+ - lib/wsgame/class_player.rb
46
+ - lib/wsgame/class_player_berserk.rb
47
+ - lib/wsgame/class_player_clumpsy.rb
48
+ - lib/wsgame/gt.rb
49
+ - lib/wsgame/module_playable.rb
50
+ - lib/wsgame/treasure_trove.rb
51
+ - spec/wegame/game_rspec.rb
52
+ - spec/wegame/player_rspec.rb
53
+ - spec/wegame/spec_helper.rb
54
+ homepage: http://wesenart.ru
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '3.1'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.3.7
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Play game
77
+ test_files:
78
+ - spec/wegame/game_rspec.rb
79
+ - spec/wegame/player_rspec.rb
80
+ - spec/wegame/spec_helper.rb