99_game 2.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGI0ZGE5NGE3ODcyMzkxYTRhNmNjNDAyNWM2MjhhNzExYTljNjdiYQ==
4
+ ODY3N2NlOTFlNjdhNTI4YThiMDFjOTNhNTg5NmZiYzcwY2VmNDVhZg==
5
5
  data.tar.gz: !binary |-
6
- MzBlZmM2MzJkODQ0NmNiNjI5ZThiZTY2MjRlNzQ3ZjU2ZDEzZWM5ZQ==
6
+ MWQzN2Y0NTJkYjBiNzBiZWE4ZDA2MzQwYmZkNmFiOTE5MWFmOWM5NQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWExNmUzMzRhOTQzZWFiMjRlMGE5YjQwMjhhYzVjMGQzNWMzMDM2MGQwYzEx
10
- YTA3OTM5YTg2NGJmNjJmZDUyYjZiNGE0OGQ5YmUwNDQ0NTg4ZjFlYTYzOThl
11
- OTcxYjk2ZjM5OTE5NWI0ZTYxNWMyY2Y3NzdmMjFmMmM2NGNmZTg=
9
+ NjMzZWQ5NmRjMzVlOGQ3OTBhMDY0NWYxYjhjODc3Y2Q1MWQzYTUyZDgxODdh
10
+ MTllN2NiNWQxYWE2YTg3NDM2NzNmNjc3NmU5MDZmYjY1YzRmZWJmNTU0YzI1
11
+ ZDI0ZjFjODI1MDBiZDA2YjZmOWJjY2QwMTI3ZmM4YTlmZjgyMmE=
12
12
  data.tar.gz: !binary |-
13
- NWZiZDA1NjdiYWM2YmNiMjk1NGIwYTdlZmY1ZDVjYjllY2M2NzZlZGRjZTQ1
14
- ZTkxYTRiODQ2NDJmNWNiOTcwNGMwMjQ3ZmU2NTUzZWFjYjgwZDU2MTk2NDk3
15
- ZDEyMDMwOGEzY2QxMGZmNzZjNjU5NDA0NjFlYzA5MTZhNTcyMjI=
13
+ MmZjOTQzZTcyNDA0ZTUyMGZmOTM1ODk2OTM0MDQ1OWM0ZWMwYTU5NmI4NmJl
14
+ YjAyODdjOWFhNjI3NDJiZmVhMmM2ZjhmMzkwMjRjNTc1MDExMzViM2Y2MDRh
15
+ YjdhNzU5NGRiNzU3NDgxMzMyYWU3OTBiNjc3YjM2ZjhjNTQ1MGM=
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) {{{2014}}} {{{Zachary Roth Perlmutter}}}
3
+ Copyright (c) 2016 Zachary Roth Perlmutter
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/bin/99_game CHANGED
@@ -1,12 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
2
  require '99_game'
3
- deck = Deck.new
3
+ include CardDeck
4
+ deck = Deck.new jokers: true
4
5
  BEGIN { # Looks at its arguements
5
6
  ARGV[0] = "-h" if ARGV[0] == "--help"
6
7
  ARGV[0] = "-v" if ARGV[0] == "--version"
7
8
  case ARGV[0]
8
9
  when "-v"
9
- puts "2.0.1"
10
+ puts "3.1.2"
10
11
  exit
11
12
  when "-h"
12
13
  puts "\u00B7 Commands"
@@ -22,7 +23,7 @@ BEGIN { # Looks at its arguements
22
23
  puts "\t\u00B7 Your goal is to get your opponent to bring the value over 99 by playing 1 of your 3 cards."
23
24
  puts "\t\u00B7 A card will usually increase the value by itself, but there are a few exceptions:"
24
25
  puts "\t\t\u00B7 Aces are worth 1"
25
- puts "\t\t\u00B7 1 - 10 are worth themselves, with the exception of 4 and 9"
26
+ puts "\t\t\u00B7 2 - 10 are worth themselves, with the exception of 4 and 9"
26
27
  puts "\t\t\u00B7 4, 9, and Jacks are worth 0"
27
28
  puts "\t\t\u00B7 Queens decrease the value by 10"
28
29
  puts "\t\t\u00B7 Kings set the value to 99"
@@ -31,15 +32,15 @@ BEGIN { # Looks at its arguements
31
32
  end
32
33
  }
33
34
  END { # Thanks for playing
34
- sleep(1.5)
35
+ sleep 1.5
35
36
  puts "\n\tThanks for playing 99!"
36
- sleep(2.5)
37
+ sleep 2.5
37
38
  }
38
- $value, value1, value2, value3, dealer, user = 0,0,0,0, Hand.new(deck), Hand.new(deck)
39
+ $value, value1, value2, value3, dealer, user = 0,0,0,0, Hand.new, Hand.new
39
40
  loop do
40
41
  puts "\n\tIt is the dealer's turn!"
41
42
  i = 1
42
- for card in dealer.hand
43
+ for card in dealer.cards
43
44
  case i
44
45
  when 1 then test card, $value, value1
45
46
  when 2 then test card, $value, value2
@@ -48,20 +49,19 @@ loop do
48
49
  i += 1
49
50
  end
50
51
  if value1 >= value2 && value1 >= value3
51
- $card = dealer.hand[0].num
52
- dealer.play dealer.hand[0]
53
- elsif value1 <= value2 && value2 >= value3
54
- $card = dealer.hand[1].num
55
- dealer.play dealer.hand[1]
52
+ $card = dealer.cards[0].num
53
+ dealer.play dealer.cards[0]
54
+ elsif value2 >= value1 && value2 >= value3
55
+ $card = dealer.cards[1].num
56
+ dealer.play dealer.cards[1]
56
57
  else
57
- $card = dealer.hand[2].num
58
- dealer.play(dealer.hand[2])
58
+ $card = dealer.cards[2].num
59
+ dealer.play dealer.cards[2]
59
60
  end
60
- pause(1.5)
61
+ pause 1.5
61
62
  puts "\tThe dealer played a(n) #{$card}"
62
63
  pause(0.5)
63
- puts "\tThe value is now #{$value}"
64
- puts
64
+ puts "\tThe value is now #{$value}\n"
65
65
  pause(1.5)
66
66
  if $value > 99 # Runs when you win and exits loop
67
67
  puts "\tYou win!"
@@ -73,9 +73,9 @@ loop do
73
73
  pause 0.5
74
74
  print "\tPick a card to play by typing in the name of the card => "
75
75
  input, turn = gets.chomp, true
76
- for card in user.hand
76
+ for card in user.cards
77
77
  if card.num == converter(input) && turn
78
- user.play(card)
78
+ user.play card
79
79
  turn = false
80
80
  end
81
81
  end
data/lib/99_game.rb CHANGED
@@ -1,10 +1,21 @@
1
- # Used by the CPU to determine which card to play. Parameter card needs to be an instance of Card.
1
+ autoload :CardDeck, "card_deck"
2
+ autoload :Card, "card"
3
+ autoload :Hand, "hand"
4
+
5
+ =begin
6
+ @param card [CardDeck::Card]
7
+ @param actual_value [Integer]
8
+ @param test_value [Integer]
9
+ @return [Integer]
10
+ @note Used by the CPU to determine which card to play. Parameter card needs to be an instance of Card.
11
+ =end
2
12
  def test(card, actual_value, test_value)
3
13
  if card.num == "King"
4
14
  test_value = 99
5
15
  elsif card.num == "Joker"
6
16
  test_value = 0
7
- else; test_value = actual_value + card.value
17
+ else
18
+ test_value = actual_value + card.value
8
19
  end
9
20
  test_value = -100 if test_value > 99
10
21
  return test_value
@@ -12,126 +23,43 @@
12
23
  # Tests if obj is not nil.
13
24
  def not_nil?(obj)
14
25
  if obj.nil?
15
- return false
26
+ false
16
27
  else
17
- return true
28
+ true
18
29
  end
19
30
  end
20
31
  # 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
21
- def converter(input)
22
- abbrev = {"$" => "Joker", "K" => "King", "J" => "Jack", "Q" => "Queen", "A" => "Ace"}
23
- raise(CardError, "Input cannot be blank") if input == String.new
24
- if input.to_i == 0
25
- case input.capitalize
26
- when "$" then "Joker"
27
- when "K" then "King"
28
- when "J" then "Jack"
29
- when "Q" then "Queen"
30
- when "A" then "Ace"
31
- end
32
- else
33
- input.to_i
34
- end
35
- end
36
32
  # Expected errors
37
- class CardError < Exception; end
38
- class Card # Represents a card in the deck
39
- # Gives the number on the card
40
- attr_reader :num
41
- # Gives the Card's value
42
- attr_reader :value
43
- # Backup method for Card#value
44
- def _value
45
- return case @num
46
- when "Ace" then 1
47
- when 2..3 then @num
48
- when 4 then 0
49
- when 5..8 then @num
50
- when 9 then 0
51
- when 10 then 10
52
- when "Jack" then 0
53
- when "Queen" then -10
54
- when "King" then 99
55
- when "Joker" then 0
56
- end
57
- end
58
- # Creates a new card with num as the attribute :num
59
- def initialize(num)
60
- @num, @value = num, case num
61
- when "Ace" then 1
62
- when 4 then 0
63
- when 9 then 0
64
- when "Jack" then 0
65
- when "Joker" then 0
66
- when "King" then 99
67
- when "Queen" then -10
68
- else
69
- num.to_i
70
- end
71
- end
72
- end
73
- class Hand # Creates an object that holds and can play cards. Interacts with Deck objects.
74
- # The actual hand
75
- attr_accessor :hand
76
- # Creates a new Hand. The deck argument tells the object which deck to use in Hand#play
77
- def initialize(deck_in_use)
78
- @hand, @deck = [deck_in_use.draw, deck_in_use.draw, deck_in_use.draw], deck_in_use
79
- end
80
- # Gameplay method. The parameter 'card' is the card being played.
81
- def play(card)
82
- if card.num == "King"; $value = 99
83
- elsif card.num == "Joker"; $value = 0
84
- else; $value += card.value
85
- end
86
- i, done = 0, false
87
- for index in @hand
88
- if index.num == card.num and not done
89
- discard = @hand[ i ]
90
- @hand.delete_at i
91
- @hand.push @deck.draw
92
- @deck.discard discard
93
- done = true
94
- end
95
- i += 1
96
- end
97
- end
98
- # Allows you to see your cards.
99
- def view
100
- print "\tThese are your cards: "
101
- @hand.each {|card| print "\t#{card.num}"}
102
- end
103
- end
104
- # Combines sleep and a newline. 'p' is the amount of time waited.
105
- def pause(p)
33
+ class CardError < Exception; end
34
+ =begin
35
+ Combines sleep and a newline
36
+ @param p [Integer] amount of time to sleep
37
+ @return [void]
38
+ =end
39
+ def pause(p)
106
40
  sleep p
107
41
  puts
108
- end
109
- class Deck # Cards are stored in these objects
110
- # The contents of the deck
111
- attr_reader :cards
112
- def initialize # Creates a new deck that can now be used for playing the game
113
- @cards = Array.new
114
- 4.times do # Add the cards to the deck
115
- @cards.push Card.new("Ace")
116
- @cards.push Card.new("King")
117
- @cards.push Card.new("Queen")
118
- @cards.push Card.new("Jack")
119
- @cards.push Card.new(10)
120
- @cards.push Card.new(9)
121
- @cards.push Card.new(8)
122
- @cards.push Card.new(7)
123
- @cards.push Card.new(6)
124
- @cards.push Card.new(5)
125
- @cards.push Card.new(4)
126
- @cards.push Card.new(3)
127
- @cards.push Card.new(2)
42
+ end
43
+ =begin
44
+ @param input [String]
45
+ @return [String, Integer]
46
+ If parameter input is an abbreviation, input is converted to what it stands for.
47
+ Otherwise, it simply returns a capitalized version of input.
48
+ @raise [CardError] if input is nil or an emtpy string
49
+ =end
50
+ def converter(input)
51
+ abbrev = {"$" => "Joker", "K" => "King", "J" => "Jack", "Q" => "Queen", "A" => "Ace"}
52
+ raise(CardError, "Input cannot be blank") if input == String.new
53
+ if input.to_i == 0
54
+ case input.capitalize
55
+ when ?$ then "Joker"
56
+ when ?K then "King"
57
+ when ?J then "Jack"
58
+ when ?Q then "Queen"
59
+ when ?A then "Ace"
128
60
  end
129
- 2.times {@cards.push Card.new("Joker")}
130
- 50.times {@cards.shuffle!}
61
+ else
62
+ input.to_i
131
63
  end
132
- # Draw from the deck
133
- def draw; @cards.shift; end
134
- # Adds 'card' to the deck. Used with Hand#play.
135
- def discard(card); @cards.push card; end
136
64
  end
137
65
 
data/lib/card.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "card_deck"
2
+ class CardDeck::Card # Represents a card in the deck
3
+ # Value of the card
4
+ def value
5
+ case @num
6
+ when "Ace" then 1
7
+ when 2..3 then @num
8
+ when 4 then 0
9
+ when 5..8 then @num
10
+ when 9 then 0
11
+ when 10 then 10
12
+ when "Jack" then 0
13
+ when "Queen" then -10
14
+ when "King" then 99
15
+ when "Joker" then 0
16
+ end
17
+ end
18
+ end
data/lib/hand.rb ADDED
@@ -0,0 +1,44 @@
1
+ require_relative "card.rb"
2
+ class Hand # Creates an object that holds and can play cards. Interacts with Deck objects.
3
+ @@deck = Deck.new.cards.shuffle!
4
+ attr_accessor :cards # @return [Array<CardDeck::Card>]
5
+ def initialize
6
+ @cards = Array.new(3) {@@deck.shift}
7
+ end
8
+
9
+ =begin
10
+ @param card [CardDeck::Card] the card played
11
+ @return [void]
12
+ @note Gameplay method
13
+ =end
14
+ def play(card)
15
+ if card.num == "King"
16
+ $value = 99
17
+ elsif card.num == "Joker"
18
+ $value = 0
19
+ else
20
+ $value += card.value
21
+ end
22
+ i, done = 0, false
23
+ for index in @cards
24
+ if index.num == card.num and not done
25
+ discard = @cards[i]
26
+ @cards.delete_at i
27
+ @cards.push @@deck.shift
28
+ @@deck.push discard
29
+ done = true
30
+ end
31
+ i += 1
32
+ end
33
+ end
34
+
35
+ =begin
36
+ @return [void]
37
+ Displays cards
38
+ =end
39
+ def view
40
+ print "\tThese are your cards: "
41
+ @cards.each {|card| print "\t#{card.num}"}
42
+ end
43
+ alias inspect cards
44
+ end
@@ -0,0 +1,2 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
@@ -1,48 +1,12 @@
1
- deck = Deck.new
2
- describe Deck do
3
- subject {Deck.new}
4
- describe "#length" do
5
- it "should == 54" do; expect(subject.cards.length).to eq 54; end
6
- end
7
- end
8
- describe Card do
9
- cards = ["Ace", "King", "Queen", "Jack", "Joker"] + (2..10).to_a
10
- describe "#value" do
11
- for card in cards
12
- describe "#{card}" do
13
- it "should == #{Card.new(card)._value}" do;
14
- expect(Card.new(card).value).to eq Card.new(card)._value
15
- end
16
- end
17
- end
18
- end
19
- end
1
+ require "spec_helper"
2
+ require "99_game"
20
3
  describe "converter" do
21
4
  context "when a number" do
22
- it "should return the input as a number" do
23
- expect(converter("5")).to eq 5
24
- end
5
+ subject { converter ?5 }
6
+ it { is_expected.to eq 5 }
25
7
  end
26
8
  context "when an abbreveation" do
27
- it "should expand it" do
28
- expect(converter("K")).to eq "King"
29
- end
30
- end
31
- end
32
- describe Hand do
33
- hand = Hand.new Deck.new
34
- describe "#hand" do
35
- describe "#length" do
36
- subject { hand.hand.length }
37
- it "should == 3" do; expect( subject ).to eq 3; end
38
- end
39
- end
40
- describe "#initialize" do
41
- describe "Deck#length" do
42
- it "should have three less cards after initialization" do
43
- deck1, hand, deck2 = deck.cards.length, Hand.new( deck ), deck.cards.length
44
- expect( deck1 ).to be > deck2
45
- end
46
- end
9
+ subject { converter ?K }
10
+ it {is_expected.to eq "King"}
47
11
  end
48
12
  end
@@ -0,0 +1,9 @@
1
+ require "spec_helper.rb"
2
+ require "99_game"
3
+ create_deck = proc {Deck.new jokers: true}
4
+ deck = create_deck.call
5
+ describe Deck do
6
+ describe create_deck.call.cards.length do
7
+ it {is_expected.to eq 54}
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+ require "hand"
3
+ describe "CPU" do
4
+ s = Hand.new
5
+ s.cards = [Card.new("King", Card::Spades), Card.new("Joker"), Card.new(2, Card::Diamonds)]
6
+ describe "test" do
7
+ tests = proc {|index| test(s.cards[index], 50, nil)}
8
+ v1 = tests.call 0
9
+ v2 = tests.call 1
10
+ v3 = tests.call 2
11
+ describe v1 do
12
+ it {is_expected.to be > v2}
13
+ it {is_expected.to be > v3}
14
+ end
15
+ end
16
+ end
17
+ describe Hand do
18
+ describe "#new" do
19
+ describe "#cards" do
20
+ subject {Hand.new.cards}
21
+ describe "#length" do
22
+ subject {Hand.new.cards.length}
23
+ it {is_expected.to be 3}
24
+ end
25
+ end
26
+ end
27
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,6 @@
1
+ require_relative "codeclimate.rb"
1
2
  require "99_game"
3
+ include CardDeck
2
4
  # This file was generated by the `rspec --init` command. Conventionally, all
3
5
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
6
  # The generated `.rspec` file contains `--require spec_helper` which will cause this
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: 99_game
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Zachary Perlmutter
7
+ - Zachary Roth Perlmutter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-19 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2016-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: card_deck
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-its
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  description: ! 'This is a text-based interpretation of the card game 99. Comes with
14
56
  the gem in the form of an executable. Make sure to read the rules in `99_game -h`
15
57
  before playing.
@@ -20,15 +62,19 @@ executables:
20
62
  - 99_game
21
63
  extensions: []
22
64
  extra_rdoc_files:
65
+ - LICENSE.md
23
66
  - README.md
24
- - LICENSE.txt
25
67
  files:
26
- - .rspec
27
- - LICENSE.txt
68
+ - LICENSE.md
28
69
  - README.md
29
70
  - bin/99_game
30
71
  - lib/99_game.rb
72
+ - lib/card.rb
73
+ - lib/hand.rb
74
+ - spec/codeclimate.rb
31
75
  - spec/lib/99_game_spec.rb
76
+ - spec/lib/deck_spec.rb
77
+ - spec/lib/hand_spec.rb
32
78
  - spec/spec_helper.rb
33
79
  homepage: https://rubygems.org/gems/99_game
34
80
  licenses:
@@ -42,7 +88,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
88
  requirements:
43
89
  - - ! '>='
44
90
  - !ruby/object:Gem::Version
45
- version: 1.9.3
91
+ version: 1.9.2
46
92
  required_rubygems_version: !ruby/object:Gem::Requirement
47
93
  requirements:
48
94
  - - ! '>='
@@ -50,11 +96,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
96
  version: '0'
51
97
  requirements: []
52
98
  rubyforge_project:
53
- rubygems_version: 2.4.2
99
+ rubygems_version: 2.4.5
54
100
  signing_key:
55
101
  specification_version: 4
56
102
  summary: The game of 99.
57
103
  test_files:
104
+ - spec/codeclimate.rb
58
105
  - spec/lib/99_game_spec.rb
59
- - .rspec
106
+ - spec/lib/deck_spec.rb
107
+ - spec/lib/hand_spec.rb
60
108
  - spec/spec_helper.rb
data/.rspec DELETED
@@ -1,4 +0,0 @@
1
- --color
2
- --require spec_helper.rb
3
- --warnings
4
- --format doc