jamies_studio_game_speedrun 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: 860f308bc6f8867fbd641ff8529b4a18b5add5e58e4a48e37966e2d5e54dcb03
4
+ data.tar.gz: f290a1d056fc8d08c85ab4cd42123f4e92342ffd259f4d4de2076e21ac2a5669
5
+ SHA512:
6
+ metadata.gz: 39e3e2bbfaa0e9ccb67334745e2c321c29775bc167fadfcf0384117ea5487a223b6b41f7031175dbd53d56ffeb08edc243e4b7b33c3c7c5ef55dcebb1e138a37
7
+ data.tar.gz: 38637ad06361155c2791625bb508017c2a9ec683ea073a7eb57b6ff99b62d3af96e59fafc1ff165484bf59839542524010a139cf045231c8c2655946b4658226
data/LICENSE.txt ADDED
File without changes
data/README.txt ADDED
File without changes
data/bin/players.csv ADDED
@@ -0,0 +1,4 @@
1
+ larry,60
2
+ curly,125
3
+ moe,100
4
+ shemp, 90
data/bin/studio_game ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # --------------------------------display options for name and health: -------------------------------------
3
+ # puts "\n#{name1}'s initial health is #{health1}."
4
+ # puts "#{name2.upcase} has a health of #{health2}."
5
+ # puts "#{name3} has a health of #{health3}.".center(50, '*')
6
+ # puts "#{name4.ljust(30, '.')} #{health4} health"
7
+ # puts "Players:\n#{name1}\n#{name2}\n#{name3}\n#{name4}"
8
+
9
+ # require_relative 'player'
10
+ # require_relative 'game'
11
+ require_relative '../lib/studio_game/player'
12
+ require_relative '../lib/studio_game/game'
13
+
14
+ # creating new players
15
+ # player1 = Player.new("larry", 60)
16
+ # player2 = Player.new("curly", 125)
17
+ # # moe takes default health value
18
+ # player3 = Player.new("moe")
19
+ # player4 = Player.new("shemp", 90)
20
+ # create new game
21
+ knuckleheads = StudioGame::Game.new("Knuckleheads")
22
+ # takes cmd line argument given or if none given use a default file
23
+ default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
24
+ knuckleheads.load_players(ARGV.shift || default_player_file)
25
+ # add spme players to game
26
+
27
+ loop do
28
+ puts "\nHow many game rounds? ('quit' to exit)"
29
+ answer = gets.chomp.downcase
30
+ case answer
31
+ when /^\d+$/
32
+ # run the game, converts input to integer (always +)
33
+ knuckleheads.play(answer.to_i)
34
+ when 'quit', 'exit', 'q'
35
+ knuckleheads.print_stats
36
+ break
37
+ else
38
+ puts 'Please enter a number or type quit'
39
+ end
40
+ end
41
+ knuckleheads.save_high_scores
42
+
43
+
@@ -0,0 +1,15 @@
1
+ module StudioGame
2
+ class Die
3
+ # we may want our die to always have a valid
4
+ # number between 1 and 6 even if it hasn't been rolled yet.
5
+ # Think of it as the die rolling itself when it's created.
6
+ # To do that, write an initialize method that calls roll, which sets @number.
7
+ attr_reader :number
8
+ def initialize
9
+ roll
10
+ end
11
+ def roll
12
+ @number = rand(1..6)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,103 @@
1
+ require_relative 'player'
2
+ require_relative 'die'
3
+ require_relative 'gameturn'
4
+ require_relative 'treasure_trove'
5
+ require 'csv'
6
+ module StudioGame
7
+ class Game
8
+ attr_reader :title
9
+ def initialize(title)
10
+ @title = title
11
+ @players = []
12
+ end
13
+ def add_player(p)
14
+ @players.push(p)
15
+ end
16
+ #setting default rounds played to one
17
+ def play(rounds=1)
18
+ # an example of using the last returned value from one function (format time) as input to another function
19
+ puts "The game has started on #{Time.now.strftime("%A %-m/%d/%Y at %-I:%M%p")}."
20
+ puts "There are #{@players.length} players in #{@title}:"
21
+ @players.each do |p|
22
+ puts p
23
+ end
24
+ #playing rounds 1 to whatever is specified
25
+ 1.upto(rounds) do |round|
26
+ puts "\nRound #{round}:"
27
+ @players.each do |p|
28
+ #references gameturn.rb
29
+ GameTurn.take_turn(p)
30
+ end
31
+ end
32
+ #treasure stuff
33
+ treasures = TreasureTrove::TREASURES
34
+ treasures.each do |t|
35
+ puts "A #{t.name} is worth #{t.points} points."
36
+ end
37
+
38
+ end
39
+ def load_players(from_file)
40
+ CSV.foreach(from_file) do |row|
41
+ player = Player.new(row[0], row[1].to_i)
42
+ add_player(player)
43
+ end
44
+ end
45
+ def high_score_entry(player)
46
+ formatted_name = player.name.ljust(20, '.')
47
+ "#{formatted_name} #{player.score}"
48
+ end
49
+ def save_high_scores(to_file="high_scores.txt")
50
+ File.open(to_file, "w") do |file|
51
+ file.puts "#{@title} High Scores:"
52
+ sorted_players = @players.sort_by {|p| p.score}.reverse
53
+ sorted_players.each do |player|
54
+ file.puts high_score_entry(player)
55
+ end
56
+ end
57
+ end
58
+ def print_stats
59
+ puts "\n#{@title} Statistics:"
60
+ strong_players, wimpy_players = @players.partition {|p| p.strong?}
61
+ puts "\n#{strong_players.size} strong players:"
62
+ strong_players.each do |p|
63
+ puts "#{p.name} (#{p.health})"
64
+ end
65
+ puts "\n#{wimpy_players.size} wimpy players:"
66
+ wimpy_players.each do |p|
67
+ puts "#{p.name} (#{p.health})"
68
+ end
69
+
70
+ sorted_players = @players.sort_by {|p| p.score}.reverse
71
+ puts "\n#{@title} High Scores:"
72
+ sorted_players.each do |p|
73
+ puts high_score_entry(p)
74
+ end
75
+ # TIP: you can replace @players below with the sorted players if you want but it doesn't matter too much
76
+ @players.each do |p|
77
+ puts "\n#{p.name}'s point totals:"
78
+ # NOTE: the value of |treasure| is being yielded (inserted)
79
+ # into this block below and is being called from line 55 of player.rb (yield Treasure.new(name, points)
80
+ # so every Treasure.new gets passed in as |treasure|
81
+ p.each_found_treasure do |treasure|
82
+ puts "#{treasure.points} total #{treasure.name} points"
83
+ end
84
+ puts "#{p.points} grand total points"
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ if __FILE__ == $0
91
+ # creating new players
92
+ player5 = Player.new("alvin", 60)
93
+ player6 = Player.new("simon", 70)
94
+ player7 = Player.new("theodore", 80)
95
+ # create new game
96
+ chipmunks = Game.new("Chipmunks")
97
+ # add spme players to game
98
+ chipmunks.add_player(player5)
99
+ chipmunks.add_player(player6)
100
+ chipmunks.add_player(player7)
101
+ # run the game
102
+ chipmunks.play
103
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'die'
2
+ require_relative 'player'
3
+ require_relative 'treasure_trove'
4
+ module StudioGame
5
+ module GameTurn
6
+ def self.take_turn(p)
7
+ # moved from play method in game class
8
+ die = Die.new
9
+ number_rolled = die.roll
10
+ if number_rolled < 3
11
+ p.blam
12
+ elsif number_rolled < 5
13
+ puts "#{p.name} was skipped."
14
+ else
15
+ p.woot
16
+ end
17
+ # p.blam
18
+ # p.woot
19
+ # p.woot
20
+ puts p
21
+
22
+ treasure = TreasureTrove.random
23
+ # assigns the variable treasure to the players treasure/point hash using the .found_treasure method!
24
+ p.found_treasure(treasure)
25
+ # puts "#{p.name} found a #{treasure.name} worth #{treasure.points} points."
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module StudioGame
2
+ module Playable
3
+ def woot()
4
+ @health += 15
5
+ puts "Player #{@name} got wooted!"
6
+ end
7
+
8
+ def blam()
9
+ @health -= 10
10
+ puts "Player #{@name} got blammed!"
11
+ end
12
+
13
+ def strong?
14
+ @health > 100
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,65 @@
1
+ require_relative 'treasure_trove'
2
+ require_relative 'playable'
3
+ module StudioGame
4
+ class Player
5
+ include Playable
6
+ # allows you to READ and WRITE TO (change) the object values of name and health from the outside / top level.
7
+ # NOTE: these variables are grabbed directly from the names of the instance variables
8
+ attr_accessor :name, :health
9
+ def initialize(name, health=100)
10
+ @name = name.capitalize
11
+ @health = health
12
+ # puts "New player #{@name} with health of #{@health} created."
13
+ #create a new hash for every player (every player has their own hash for treasure accumulation)
14
+ @found_treasures = Hash.new(0)
15
+ end
16
+
17
+ def self.from_csv(string)
18
+ # assign name to left csv column and health to right csv column
19
+ name, health = string.split(',')
20
+ # safely convert the health value from a string to an integer.
21
+ player = Player.new(name, Integer(health))
22
+ end
23
+
24
+ def found_treasure(treasure)
25
+ # accumulate points based on the treasure's name in the hash (@found_treasures[treasure.name] fetches the points associated with that treasures name)
26
+ @found_treasures[treasure.name] += treasure.points
27
+ puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
28
+ puts "#{@name}'s treasures: #{@found_treasures}"
29
+ end
30
+
31
+ def points
32
+ @found_treasures.values.reduce(0, :+)
33
+ end
34
+
35
+ # if you rename a player without the name capitalized, it will not auto-update unless this method is specified inside this class.
36
+ def name=(new_name)
37
+ @name = new_name.capitalize
38
+ end
39
+
40
+ def score
41
+ @health + points
42
+ end
43
+
44
+ def to_s
45
+ # "I'm #{@name} with a health of #{@health} and a score of #{score}."
46
+ "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."
47
+ end
48
+
49
+ def each_found_treasure
50
+ @found_treasures.each do |name, points|
51
+ yield Treasure.new(name, points)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ # dummy sample code to test (only will run if you run this current file)
57
+ if __FILE__ == $0
58
+ player = Player.new("moe")
59
+ puts player.name
60
+ puts player.health
61
+ player.woot
62
+ puts player.health
63
+ player.blam
64
+ puts player.health
65
+ end
@@ -0,0 +1,8 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |c|
3
+ c.syntax = [:should, :expect]
4
+ end
5
+ config.mock_with :rspec do |c|
6
+ c.syntax = [:should, :expect]
7
+ end
8
+ end
@@ -0,0 +1,25 @@
1
+ # A pie is worth 5 points
2
+ # A bottle is worth 25 points
3
+ # A hammer is worth 50 points
4
+ # A skillet is worth 100 points
5
+ # A broomstick is worth 200 points
6
+ # A crowbar is worth 400 points
7
+ module StudioGame
8
+ Treasure = Struct.new(:name, :points)
9
+
10
+ module TreasureTrove
11
+ #const defined with all caps
12
+ TREASURES = [
13
+ Treasure.new(:pie, 5),
14
+ Treasure.new(:bottle, 25),
15
+ Treasure.new(:hammer, 50),
16
+ Treasure.new(:skillet, 100),
17
+ Treasure.new(:broomstick, 200),
18
+ Treasure.new(:crowbar, 400)
19
+ ]
20
+
21
+ def self.random
22
+ TREASURES.sample
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ # require_relative 'game'
2
+ require 'studio_game/game'
3
+ module StudioGame
4
+ describe Game do
5
+
6
+ before do
7
+ @game = Game.new("Knuckleheads")
8
+
9
+ @initial_health = 100
10
+ @player = Player.new("moe", @initial_health)
11
+ @game.add_player(@player)
12
+ end
13
+
14
+ it "w00ts the player if a high number is rolled" do
15
+ Die.any_instance.stub(:roll).and_return(5)
16
+
17
+ @game.play(1)
18
+
19
+ @player.health.should == @initial_health + 15
20
+ end
21
+
22
+ it "skips the player if a medium number is rolled" do
23
+ Die.any_instance.stub(:roll).and_return(3)
24
+
25
+ @game.play(1)
26
+
27
+ @player.health.should == @initial_health
28
+ end
29
+
30
+ it "blams the player if a low number is rolled" do
31
+ Die.any_instance.stub(:roll).and_return(1)
32
+
33
+ @game.play(1)
34
+
35
+ @player.health.should == @initial_health - 10
36
+ end
37
+ # hash lesson ------------------------------------
38
+ it "assigns a treasure for points during a player's turn" do
39
+ game = Game.new("Knuckleheads")
40
+ player = Player.new("moe")
41
+
42
+ game.add_player(player)
43
+
44
+ game.play(1)
45
+
46
+ player.points.should_not be_zero
47
+
48
+ # or use alternate expectation syntax:
49
+ # expect(player.points).not_to be_zero
50
+ end
51
+ # ------------------------------------
52
+
53
+
54
+ end
55
+ end
@@ -0,0 +1,125 @@
1
+ # require_relative 'player'
2
+ # require_relative 'spec_helper'
3
+ # require_relative 'gameturn'
4
+ require 'studio_game/player'
5
+ require 'studio_game/spec_helper'
6
+ require 'studio_game/gameturn'
7
+
8
+ module StudioGame
9
+
10
+ describe Player do
11
+ # code here
12
+ before do
13
+ @player = Player.new("larry", 150)
14
+ @initial_health = @player.health
15
+ end
16
+
17
+ it "has a capitalized name" do
18
+ @player.name.should == "Larry"
19
+ end
20
+
21
+ it "has a initial health of 150" do
22
+ # @player.health.should == 150
23
+ @initial_health.should == 150
24
+ end
25
+
26
+ it "has a string representation" do
27
+ @player.found_treasure(Treasure.new(:hammer, 50))
28
+ @player.found_treasure(Treasure.new(:hammer, 50))
29
+ # score is health + points so 150 health + 50 + 50 points = 250
30
+ # @player.to_s.should == "I'm Larry with a health of 150 and a score of 250."
31
+ @player.to_s.should == "I'm Larry with health = 150, points = 100, and score = 250."
32
+ end
33
+
34
+ it "computes a score as the sum of its health and points" do
35
+ # score is health + points so 150 health + 50 + 50 points = 250
36
+ @player.found_treasure(Treasure.new(:hammer, 50))
37
+ @player.found_treasure(Treasure.new(:hammer, 50))
38
+ @player.score.should == 250
39
+ end
40
+
41
+ it "increases health by 15 when w00ted" do
42
+ @player.woot
43
+ @player.health.should == @initial_health + 15
44
+ end
45
+
46
+ it "decreases health by 10 when blammed" do
47
+ @player.blam
48
+ @player.health.should == @initial_health - 10
49
+ end
50
+ # new code to check hash -------------------------
51
+ it "computes points as the sum of all treasure points" do
52
+ @player.points.should == 0
53
+
54
+ @player.found_treasure(Treasure.new(:hammer, 50))
55
+
56
+ @player.points.should == 50
57
+
58
+ @player.found_treasure(Treasure.new(:crowbar, 400))
59
+
60
+ @player.points.should == 450
61
+
62
+ @player.found_treasure(Treasure.new(:hammer, 50))
63
+
64
+ @player.points.should == 500
65
+ end
66
+
67
+ it "computes a score as the sum of its health and points" do
68
+ @player.found_treasure(Treasure.new(:hammer, 50))
69
+ @player.found_treasure(Treasure.new(:hammer, 50))
70
+
71
+ @player.score.should == 250
72
+ end
73
+ # -----------------------------------------------------
74
+ # add custom iterator
75
+ it "yields each found treasure and its total points" do
76
+ @player.found_treasure(Treasure.new(:skillet, 100))
77
+ @player.found_treasure(Treasure.new(:skillet, 100))
78
+ @player.found_treasure(Treasure.new(:hammer, 50))
79
+ @player.found_treasure(Treasure.new(:bottle, 5))
80
+ @player.found_treasure(Treasure.new(:bottle, 5))
81
+ @player.found_treasure(Treasure.new(:bottle, 5))
82
+ @player.found_treasure(Treasure.new(:bottle, 5))
83
+ @player.found_treasure(Treasure.new(:bottle, 5))
84
+
85
+ yielded = []
86
+ @player.each_found_treasure do |treasure|
87
+ yielded << treasure
88
+ end
89
+
90
+ yielded.should == [
91
+ Treasure.new(:skillet, 200),
92
+ Treasure.new(:hammer, 50),
93
+ Treasure.new(:bottle, 25)
94
+ ]
95
+ end
96
+ # -----------------------------------------------------
97
+ context "strong player" do
98
+ before do
99
+ @player = Player.new("larry", 150)
100
+ @initial_health = @player.health
101
+ end
102
+ it "is a strong player" do
103
+ @player.should be_strong
104
+ end
105
+ end
106
+
107
+ context "wimpy player" do
108
+ before do
109
+ @player = Player.new("larry", 100)
110
+ @initial_health = @player.health
111
+ end
112
+ it "is a wimpy player" do
113
+ @player.should_not be_strong
114
+ end
115
+ end
116
+ it "can be created from a CSV string" do
117
+ player = Player.from_csv("larry,150")
118
+
119
+ player.name.should == "Larry"
120
+ player.health.should == 150
121
+ end
122
+ end
123
+ end
124
+
125
+
@@ -0,0 +1,60 @@
1
+ # require_relative 'treasure_trove'
2
+ require 'studio_game/treasure_trove'
3
+ module StudioGame
4
+ describe Treasure do
5
+
6
+ before do
7
+ @treasure = Treasure.new(:hammer, 50)
8
+ end
9
+
10
+ it "has a name attribute" do
11
+ @treasure.name.should == :hammer
12
+ end
13
+
14
+ it "has a points attribute" do
15
+ @treasure.points.should == 50
16
+ end
17
+
18
+ end
19
+
20
+ describe TreasureTrove do
21
+
22
+ it "has six treasures" do
23
+ TreasureTrove::TREASURES.size.should == 6
24
+ end
25
+
26
+ it "has a pie worth 5 points" do
27
+ TreasureTrove::TREASURES[0].should == Treasure.new(:pie, 5)
28
+ end
29
+
30
+ it "has a bottle worth 25 points" do
31
+ TreasureTrove::TREASURES[1].should == Treasure.new(:bottle, 25)
32
+ end
33
+
34
+ it "has a hammer worth 50 points" do
35
+ TreasureTrove::TREASURES[2].should == Treasure.new(:hammer, 50)
36
+ end
37
+
38
+ it "has a skillet worth 100 points" do
39
+ TreasureTrove::TREASURES[3].should == Treasure.new(:skillet, 100)
40
+ end
41
+
42
+ it "has a broomstick worth 200 points" do
43
+ TreasureTrove::TREASURES[4].should == Treasure.new(:broomstick, 200)
44
+ end
45
+
46
+ it "has a crowbar worth 400 points" do
47
+ TreasureTrove::TREASURES[5].should == Treasure.new(:crowbar, 400)
48
+ end
49
+
50
+ it "returns a random treasure" do
51
+ treasure = TreasureTrove.random
52
+
53
+ TreasureTrove::TREASURES.should include(treasure)
54
+
55
+ # or use alternate expectation syntax:
56
+ # expect(TreasureTrove::TREASURES).to include(treasure)
57
+ end
58
+
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jamies_studio_game_speedrun
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamie Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-21 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: '2.8'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.8.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.8'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ description: ''
34
+ email: clarkcjamie@gmail.com
35
+ executables:
36
+ - studio_game
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - LICENSE.txt
41
+ - README.txt
42
+ - bin/players.csv
43
+ - bin/studio_game
44
+ - lib/studio_game/die.rb
45
+ - lib/studio_game/game.rb
46
+ - lib/studio_game/gameturn.rb
47
+ - lib/studio_game/playable.rb
48
+ - lib/studio_game/player.rb
49
+ - lib/studio_game/spec_helper.rb
50
+ - lib/studio_game/treasure_trove.rb
51
+ - spec/studio_game/game_spec.rb
52
+ - spec/studio_game/player_spec.rb
53
+ - spec/studio_game/treasure_trove_spec.rb
54
+ homepage: http://pragmaticstudio.com
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: '1.9'
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.6
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Text based game with rounds and treasures
77
+ test_files:
78
+ - spec/studio_game/game_spec.rb
79
+ - spec/studio_game/player_spec.rb
80
+ - spec/studio_game/treasure_trove_spec.rb