99_game 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c96962a69d0a4bfd4d474e5c6c3d2e1dffa71d07
4
- data.tar.gz: 71398ed0684f61ecf2a11a0c8fb3981efe15bc43
3
+ metadata.gz: 79c3b36f954a1926f4e319e3584ce62d397af750
4
+ data.tar.gz: 23d12f11a841e502c980cb64750c66ab5790d439
5
5
  SHA512:
6
- metadata.gz: fca2b490828686ae8384fb41906c2929a85365907fff34a1ae1afb07c8585c9b18bc6f55128d17cca3b239fd592efb5f0e29e9be29c8d793f35ee077a75b6e4c
7
- data.tar.gz: 41aec9e1313c39ad65f643d89a0fd4cb271a0ecb41aff93605168406a178ecbea2d81fb03f748894989967498294aa0cf24cfd1b9723a25f818c919d106a0495
6
+ metadata.gz: d563dec8728ab21af85908c7cea2520375ce326b4b9c410d21133781f0d8552f9b4fedf40177c264994eea75603ef7d49b9c2727d8fabf87e0bb20652a7a0966
7
+ data.tar.gz: d381e887c98a7825e64526ae42578057ea55e06b3b7a5d08ca4854fb73f4c0c7c7cce93b1648f284547f1b1d3085cffb529aa599dfa8f0d6b6e1a1da72313ed4
data/.rspec CHANGED
@@ -1,4 +1,4 @@
1
1
  --color
2
- --warnings
3
- --require spec_helper
2
+ --require spec_helper.rb
4
3
  --format doc
4
+ --warnings
data/README.md CHANGED
@@ -1,11 +1,14 @@
1
+
2
+ # 99_game
3
+
4
+ To install: `gem install 99_game`
5
+
6
+ For the rules and arguments: `99_game -h`
7
+
8
+ ## Badges
1
9
  [![Gem Version](https://badge.fury.io/rb/99_game.png)](http://badge.fury.io/rb/99_game)
2
10
  [![Coverage Status](https://img.shields.io/coveralls/Zrp200/99_game.svg)](https://coveralls.io/r/Zrp200/99_game)
3
11
  [![Code Climate](https://codeclimate.com/github/Zrp200/99_game.png)](https://codeclimate.com/github/Zrp200/99_game)
4
12
  [![Dependency Status](https://gemnasium.com/Zrp200/99_game.svg)](https://gemnasium.com/Zrp200/99_game)
5
13
  [![Inline docs](http://inch-ci.org/github/Zrp200/99_game.png?branch=master)](http://inch-ci.org/github/Zrp200/99_game)
6
- 99_game
7
- =======
8
-
9
- To install: `gem install 99_game`
10
-
11
- For the rules and arguments: `gem '99_game'`
14
+ [![Test Coverage](https://codeclimate.com/github/Zrp200/99_game/coverage.png)](https://codeclimate.com/github/Zrp200/99_game)
data/bin/99_game CHANGED
@@ -1,22 +1,23 @@
1
1
  #!/usr/bin/env ruby
2
2
  require '99_game'
3
+ deck = Deck.new
3
4
  BEGIN { # Looks at its arguements
4
5
  ARGV[0] = "-h" if ARGV[0] == "--help"
5
6
  ARGV[0] = "-v" if ARGV[0] == "--version"
6
- case ARGV[0]
7
- when "-v"
8
- puts "1.3.0"
7
+ case ARGV[0]
8
+ when "-v"
9
+ puts "2.0.0"
9
10
  exit
10
- when "-h"
11
- puts "\u00B7 Commands"
12
- puts "\t\u00b7 -v/--version - display version
13
- puts "\t\u00B7 -h/--help - shows this message\n"
14
- puts "\u00B7 Abbrevations"
15
- puts "\t\u00B7 J -> Jack"
16
- puts "\t\u00b7 Q -> Queen"
17
- puts "\t\u00b7 K -> King"
18
- puts "\t\u00b7 A -> Ace"
19
- puts "\t\u00b7 $ -> Joker"
11
+ when "-h"
12
+ puts "\u00B7 Commands"
13
+ puts "\t\u00b7 -v/--version - display version"
14
+ puts "\t\u00B7 -h/--help - shows this message\n"
15
+ puts "\u00B7 Abbrevations"
16
+ puts "\t\u00B7 J -> Jack"
17
+ puts "\t\u00b7 Q -> Queen"
18
+ puts "\t\u00b7 K -> King"
19
+ puts "\t\u00b7 A -> Ace"
20
+ puts "\t\u00b7 $ -> Joker"
20
21
  puts "\u00B7 Gameplay"
21
22
  puts "\t\u00B7 Your goal is to get your opponent to bring the value over 99 by playing 1 of your 3 cards."
22
23
  puts "\t\u00B7 A card will usually increase the value by itself, but there are a few exceptions:"
@@ -34,35 +35,27 @@ END { # Thanks for playing
34
35
  puts "\n\tThanks for playing 99!"
35
36
  sleep(3)
36
37
  }
37
- $value, value1, value2, value3, dealer, user = 0,0,0,0, Hand.new, Hand.new
38
+ $value, value1, value2, value3, dealer, user = 0,0,0,0, Hand.new(deck), Hand.new(deck)
38
39
  loop do
39
- $deck.shuffle!
40
40
  puts
41
41
  puts "\tIt is the dealer's turn!"
42
- def test(card, value) # argument 'card' needs to be an instance of Card
43
- if card.num == "King"; value = 99
44
- elsif card.num == "Joker"; value = -1
45
- else; value = $value + card.value
46
- end
47
- value = -100 if value > 99
48
- end
49
42
  i = 1
50
43
  for card in dealer.hand
51
44
  case i
52
- when 1 then test(card, value1)
53
- when 2 then test(card, value2)
54
- when 3 then test(card, value3)
45
+ when 1 then test card, $value, value1
46
+ when 2 then test card, $value, value2
47
+ when 3 then test card, $value, value3
55
48
  end
56
49
  i += 1
57
50
  end
58
51
  if value1 >= value2 and value1 >= value3
59
- $card = dealer.hand[0].num
52
+ $card = dealer.hand[0].num
60
53
  dealer.play(dealer.hand[0])
61
54
  elsif value2 >= value3
62
- $card = dealer.hand[1].num
55
+ $card = dealer.hand[1].num
63
56
  dealer.play(dealer.hand[1])
64
57
  else
65
- $card = dealer.hand[2].num
58
+ $card = dealer.hand[2].num
66
59
  dealer.play(dealer.hand[2])
67
60
  end
68
61
  pause(2)
data/lib/99_game.rb CHANGED
@@ -1,101 +1,136 @@
1
+ # Used by the CPU to determine which card to play. Parameter card needs to be an instance of Card.
2
+ def test(card, actual_value, test_value)
3
+ if card.num == "King"
4
+ test_value = 99
5
+ elsif card.num == "Joker"
6
+ test_value = 0
7
+ else; test_value = actual_value + card.value
8
+ end
9
+ test_value = -100 if test_value > 99
10
+ end
1
11
  # Tests if obj is not nil.
2
- def not_nil?(obj)
3
- if obj.nil?
4
- return false
5
- else
6
- return true
12
+ def not_nil?(obj)
13
+ if obj.nil?
14
+ return false
15
+ else
16
+ return true
17
+ end
7
18
  end
8
- end
9
- # Converts _input_ to an integer if String#capitalize does something. If _input_ is an abbreviation, _input_ is converted to what it stands for. Otherwise, it simply returns a capitalized version of _input_. If _input_ is nil or an emtpy string, raises a CardError
10
- def converter(input)
11
- abbrev = {"$".to_sym => "Joker", K: "King", J: "Jack", Q: "Queen", A: "Ace"}
12
- if input == input.capitalize
13
- return input.to_i
14
- elsif not_nil? abbrev[input.capitalize.to_sym]
15
- return abbrev[input.capitalize.to_sym]
16
- elsif input.nil? || input == String.new
17
- raise CardError, "Input not allowed"
18
- else
19
- return input.capitalize
19
+ # Converts input to an integer if String#capitalize does something. If parameter input is an abbreviation, _input_ is converted to what it stands for. Otherwise, it simply returns a capitalized version of _input_. If _input_ is nil or an emtpy string, raises a CardError
20
+ def converter(input)
21
+ abbrev = {"$" => "Joker", "K" => "King", "J" => "Jack", "Q" => "Queen", "A" => "Ace"}
22
+ raise(CardError, "Input cannot be blank") if input == String.new
23
+ if input.to_i == 0
24
+ case input.capitalize
25
+ when "$" then "Joker"
26
+ when "K" then "King"
27
+ when "J" then "Jack"
28
+ when "Q" then "Queen"
29
+ when "A" then "Ace"
30
+ end
31
+ else
32
+ input.to_i
33
+ end
20
34
  end
21
- end
22
- class CardError < Exception; end
35
+ # Expected errors
36
+ class CardError < Exception; end
23
37
  class Card # Represents a card in the deck
24
- attr_reader :num
25
- @@value = {"Ace" => 1, 4 => 0, 9 => 0, "Jack" => 0, "Joker" => 0, "King" => 99, "Queen" => -10}
26
- # Gives the Card's value
27
- def value
28
- @@value.default = @num.to_i
29
- return @@value[@num]
30
- end
31
- # Backup method for Card#value
32
- def _value
33
- return case @num
34
- when "Ace" then 1
35
- when 2..3 then @num
36
- when 4 then 0
37
- when 5..8 then @num
38
- when 9 then 0
39
- when 10 then 10
40
- when "Jack" then 0
41
- when "Queen" then -10
42
- when "King" then 99
43
- when "Joker" then 0
38
+ # Gives the number on the card
39
+ attr_reader :num
40
+ # Gives the Card's value
41
+ attr_reader :value
42
+ # Backup method for Card#value
43
+ def _value
44
+ return case @num
45
+ when "Ace" then 1
46
+ when 2..3 then @num
47
+ when 4 then 0
48
+ when 5..8 then @num
49
+ when 9 then 0
50
+ when 10 then 10
51
+ when "Jack" then 0
52
+ when "Queen" then -10
53
+ when "King" then 99
54
+ when "Joker" then 0
55
+ end
56
+ end
57
+ # Creates a new card with num as the attribute :num
58
+ def initialize(num)
59
+ @num, @value = num, case num
60
+ when "Ace" then 1
61
+ when 4 then 0
62
+ when 9 then 0
63
+ when "Jack" then 0
64
+ when "Joker" then 0
65
+ when "King" then 99
66
+ when "Queen" then -10
67
+ else
68
+ num.to_i
69
+ end
44
70
  end
45
- end
46
- # Creates a new card
47
- def initialize(card); @num = card; end
48
71
  end
49
- class Hand # Creates an object that holds and can play cards
50
- attr_reader :hand
51
- # Creates a new Hand
52
- def initialize; @hand = [$deck.shift, $deck.shift, $deck.shift]; end
53
- # Gameplay method
54
- def play(card)
55
- if card.num == "King"; $value = 99
56
- elsif card.num == "Joker"; $value = 0
57
- else; $value += card.value
72
+ class Hand # Creates an object that holds and can play cards. Interacts with Deck objects.
73
+ # The actual hand
74
+ attr_reader :hand
75
+ # Creates a new Hand. The deck argument tells the object which deck to use in Hand#play
76
+ def initialize(deck_in_use)
77
+ @hand, @deck = [deck_in_use.draw, deck_in_use.draw, deck_in_use.draw], deck_in_use
58
78
  end
59
- i, done = 0, false
60
- for index in @hand
61
- if index.num == card.num and not done
62
- discard = @hand[i]
63
- @hand.delete_at(i)
64
- draw = $deck.shift
65
- @hand.push(draw)
66
- $deck.push(discard)
67
- done = true
79
+ # Gameplay method. The parameter 'card' is the card being played.
80
+ def play(card)
81
+ if card.num == "King"; $value = 99
82
+ elsif card.num == "Joker"; $value = 0
83
+ else; $value += card.value
84
+ end
85
+ i, done = 0, false
86
+ for index in @hand
87
+ if index.num == card.num and not done
88
+ discard = @hand[ i ]
89
+ @hand.delete_at i
90
+ @hand.push @deck.draw
91
+ @deck.discard discard
92
+ done = true
93
+ end
94
+ i += 1
68
95
  end
69
- i += 1
70
96
  end
71
- end
72
- # Allows you to see your cards.
73
- def view
74
- print "\tThese are your cards: "
75
- @hand.each {|card| print "\t#{card.num}"}
76
- end
97
+ # Allows you to see your cards.
98
+ def view
99
+ print "\tThese are your cards: "
100
+ @hand.each {|card| print "\t#{card.num}"}
101
+ end
77
102
  end
78
- # Combines sleep and a newline.
79
- def pause(p)
80
- sleep p
81
- puts
82
- end
83
- $deck = Array.new
84
- 4.times do # Add the cards to the deck
85
- $deck.push Card.new("Ace")
86
- $deck.push Card.new("King")
87
- $deck.push Card.new("Queen")
88
- $deck.push Card.new("Jack")
89
- $deck.push Card.new(10)
90
- $deck.push Card.new(9)
91
- $deck.push Card.new(8)
92
- $deck.push Card.new(7)
93
- $deck.push Card.new(6)
94
- $deck.push Card.new(5)
95
- $deck.push Card.new(4)
96
- $deck.push Card.new(3)
97
- $deck.push Card.new(2)
103
+ # Combines sleep and a newline. 'p' is the amount of time waited.
104
+ def pause(p)
105
+ sleep p
106
+ puts
107
+ end
108
+ class Deck # Cards are stored in these objects
109
+ # The contents of the deck
110
+ attr_reader :cards
111
+ def initialize # Creates a new deck that can now be used for playing the game
112
+ @cards = Array.new
113
+ 4.times do # Add the cards to the deck
114
+ @cards.push Card.new("Ace")
115
+ @cards.push Card.new("King")
116
+ @cards.push Card.new("Queen")
117
+ @cards.push Card.new("Jack")
118
+ @cards.push Card.new(10)
119
+ @cards.push Card.new(9)
120
+ @cards.push Card.new(8)
121
+ @cards.push Card.new(7)
122
+ @cards.push Card.new(6)
123
+ @cards.push Card.new(5)
124
+ @cards.push Card.new(4)
125
+ @cards.push Card.new(3)
126
+ @cards.push Card.new(2)
127
+ end
128
+ 2.times {@cards.push Card.new("Joker")}
129
+ 50.times {@cards.shuffle!}
130
+ end
131
+ # Draw from the deck
132
+ def draw; @cards.shift; end
133
+ # Adds 'card' to the deck. Used with Hand#play.
134
+ def discard(card); @cards.push card; end
98
135
  end
99
- 2.times {$deck.push Card.new("Joker")}
100
- $deck.shuffle!
101
- __END__
136
+
@@ -1,11 +1,11 @@
1
- require_relative "spec_helper"
2
- require "99_game"
3
- describe "$deck" do
1
+ deck = Deck.new
2
+ describe Deck do
3
+ subject {Deck.new}
4
4
  describe "#length" do
5
- it "should == 54" do; expect($deck.length + 6).to eq 54; end
5
+ it "should == 54" do; expect(subject.cards.length).to eq 54; end
6
6
  end
7
7
  end
8
- describe "Card" do
8
+ describe Card do
9
9
  cards = ["Ace", "King", "Queen", "Jack", "Joker"] + (2..10).to_a
10
10
  describe "#value" do
11
11
  for card in cards
@@ -20,29 +20,29 @@ end
20
20
  describe "converter" do
21
21
  context "when a number" do
22
22
  it "should return the input as a number" do
23
- expect(converter("5")).to eq 1
23
+ expect(converter("5")).to eq 5
24
24
  end
25
25
  end
26
26
  context "when an abbreveation" do
27
27
  it "should expand it" do
28
- expect(converter("k")).to eq "King"
28
+ expect(converter("K")).to eq "King"
29
29
  end
30
30
  end
31
31
  end
32
- describe "Hand" do
33
- hand = Hand.new
32
+ describe Hand do
33
+ hand = Hand.new Deck.new
34
34
  describe "#hand" do
35
35
  describe "#length" do
36
- it "should == 3" do; expect(hand.hand.length).to eq 3; end
36
+ subject { hand.hand.length }
37
+ it "should == 3" do; expect( subject ).to eq 3; end
37
38
  end
38
39
  end
39
40
  describe "#initialize" do
40
- describe "$deck" do
41
+ describe "Deck#length" do
41
42
  it "should have three less cards after initialization" do
42
- deck1, hand, deck2 = $deck.length, Hand.new, $deck.length
43
- expect(deck1).to > deck2
43
+ deck1, hand, deck2 = deck.cards.length, Hand.new( deck ), deck.cards.length
44
+ expect( deck1 ).to be > deck2
44
45
  end
45
46
  end
46
47
  end
47
48
  end
48
-
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
- require "codeclimate-test-reporter"
2
- CodeClimate::TestReporter.start
1
+ require "99_game"
3
2
  # This file was generated by the `rspec --init` command. Conventionally, all
4
3
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
4
  # The generated `.rspec` file contains `--require spec_helper` which will cause this
@@ -14,7 +13,7 @@ CodeClimate::TestReporter.start
14
13
  #
15
14
  # The `.rspec` file also contains a few flags that are not defaults but that
16
15
  # users commonly want.
17
-
16
+ require_relative "lib/99_game.rb"
18
17
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
18
  RSpec.configure do |config|
20
19
  # The settings below are suggested to provide a good initial experience
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: 99_game
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Perlmutter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2014-08-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  This is a text-based interpretation of the card game 99. Comes with the gem in the form of an executable. Make sure to read the rules in `99_game -h` before playing
@@ -25,7 +25,7 @@ files:
25
25
  - README.md
26
26
  - bin/99_game
27
27
  - lib/99_game.rb
28
- - spec/99spec.rb
28
+ - spec/lib/99_game.rb
29
29
  - spec/spec_helper.rb
30
30
  homepage: https://rubygems.org/gems/99_game
31
31
  licenses:
@@ -40,7 +40,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: 1.8.0
43
+ version: '0'
44
44
  required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
@@ -53,6 +53,7 @@ signing_key:
53
53
  specification_version: 4
54
54
  summary: The game of 99.
55
55
  test_files:
56
- - spec/99spec.rb
56
+ - spec/lib/99_game.rb
57
57
  - ".rspec"
58
58
  - spec/spec_helper.rb
59
+ has_rdoc: