99_game 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +4 -0
- data/bin/99_game +104 -0
- data/lib/99_game.rb +82 -0
- data/spec/99spec.rb +47 -0
- data/spec/spec_helper.rb +76 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9c5d20b0cbe719d8cf5fd9cae95971c6dca3387e
|
4
|
+
data.tar.gz: 49e8a864cfdd018697f99c31088caff00c64428e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8cc6dc0ca981f7aca288293d6ede7ebe3b22ff6e3e504d579b8a248cff51e4c02bfce91b2c2afc27b2aaff9b05ab95ca363793ac67c67b7dda0fad9deb07f313
|
7
|
+
data.tar.gz: 0aa825f60a99830defe20fd030c2fc672287afb9381f5ee333091863cac7c9fcebde401bfa498a68ad32eb12f1611c9745df262442e959523a7172cbcd20da32
|
data/.rspec
ADDED
data/bin/99_game
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require '99'
|
3
|
+
BEGIN { # Rules of the game
|
4
|
+
puts "\n\t\t\t\tThe Game Of 99\n\t\t\t\t--------------\n\n"
|
5
|
+
sleep(1)
|
6
|
+
print "\tWould you like to see the rules? (y/n) => "
|
7
|
+
begin
|
8
|
+
if gets.chomp.include?("y")
|
9
|
+
puts
|
10
|
+
puts "\tGoal: Your goal is to get your opponent to bring the value over 99 by\nplaying 1 of your 3 cards."
|
11
|
+
sleep(3); puts
|
12
|
+
puts "\tCards: A card will usually increase the value by itself, but there are a few exceptions."
|
13
|
+
sleep(3); puts
|
14
|
+
puts "\t\tValues"
|
15
|
+
puts "\t\t------"
|
16
|
+
sleep(1)
|
17
|
+
puts "\n\t\u00B7 Aces are worth 1"
|
18
|
+
sleep(3)
|
19
|
+
puts "\t\u00B7 1 - 10 are worth themselves, with the exception of 4 and 9"
|
20
|
+
sleep(3)
|
21
|
+
puts "\t\u00B7 4, 9, and Jacks are worth 0"
|
22
|
+
sleep(3)
|
23
|
+
puts "\t\u00B7 Queens decrease the value by 10"
|
24
|
+
sleep(3)
|
25
|
+
puts "\t\u00B7 Kings set the value to 99"
|
26
|
+
sleep(3)
|
27
|
+
puts "\t\u00B7 Jokers set the value to 0"
|
28
|
+
sleep(3); puts
|
29
|
+
print "Press Enter to begin"; gets.chomp
|
30
|
+
else; sleep(2)
|
31
|
+
end
|
32
|
+
rescue Exception; puts "Continuing"
|
33
|
+
end
|
34
|
+
puts
|
35
|
+
puts
|
36
|
+
}
|
37
|
+
END { # Thanks for playing
|
38
|
+
sleep(2)
|
39
|
+
puts "\n\tThanks for playing 99!"
|
40
|
+
sleep(3)
|
41
|
+
}
|
42
|
+
def pause(p)
|
43
|
+
sleep(p)
|
44
|
+
puts
|
45
|
+
end
|
46
|
+
$value, value1, value2, value3, dealer, user = 0,0,0,0, Hand.new, Hand.new
|
47
|
+
loop do
|
48
|
+
$deck.shuffle!
|
49
|
+
puts
|
50
|
+
puts "\tIt is the dealer's turn!"
|
51
|
+
def test(card, value) # argument 'card' needs to be an instance of Card
|
52
|
+
if card.num == "King"; value = 99
|
53
|
+
elsif card.num == "Joker"; value = -1
|
54
|
+
else; value = $value + card.value
|
55
|
+
end
|
56
|
+
value = -100 if value > 99
|
57
|
+
end
|
58
|
+
i = 1
|
59
|
+
for card in dealer.hand
|
60
|
+
case i
|
61
|
+
when 1 then test(card, value1)
|
62
|
+
when 2 then test(card, value2)
|
63
|
+
when 3 then test(card, value3)
|
64
|
+
end
|
65
|
+
i += 1
|
66
|
+
end
|
67
|
+
if value1 >= value2 and value1 >= value3
|
68
|
+
$card = dealer.hand[0].num
|
69
|
+
dealer.play(dealer.hand[0])
|
70
|
+
elsif value2 >= value3
|
71
|
+
$card = dealer.hand[1].num
|
72
|
+
dealer.play(dealer.hand[1])
|
73
|
+
else
|
74
|
+
$card = dealer.hand[2].num
|
75
|
+
dealer.play(dealer.hand[2])
|
76
|
+
end
|
77
|
+
pause(2)
|
78
|
+
puts "\tThe dealer played a(n) #{$card}"
|
79
|
+
pause(0.5)
|
80
|
+
puts "\tThe value is now #{$value}"
|
81
|
+
puts
|
82
|
+
pause(2)
|
83
|
+
if $value > 99 # Runs when you win and exits loop
|
84
|
+
puts "\tYou win!"
|
85
|
+
break
|
86
|
+
end
|
87
|
+
puts "\tIt's your turn!"
|
88
|
+
pause(1.5)
|
89
|
+
user.view
|
90
|
+
pause(0.5)
|
91
|
+
print "\tPick a card to play by typing in the name of the card => "
|
92
|
+
user.play(UserCard.new(gets.chomp))
|
93
|
+
pause(1.5)
|
94
|
+
puts "\tYou drew a(n) #{user.hand[2].num}"
|
95
|
+
pause(0.5)
|
96
|
+
puts "\tThe value is now #{$value}"
|
97
|
+
pause(2)
|
98
|
+
if $value > 99 # Runs when dealer wins and exits loop
|
99
|
+
puts "\tYou lose"
|
100
|
+
break
|
101
|
+
end
|
102
|
+
end
|
103
|
+
__END__
|
104
|
+
Programmed by: Zachary Perlmutter
|
data/lib/99_game.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
class CardError < Exception; end
|
2
|
+
class Card # Represents a card in the deck
|
3
|
+
attr_reader :num
|
4
|
+
@@value = {"Ace" => 1, 4 => 0, 9 => 0, "Jack" => 0, "Joker" => 0, "King" => 99, "Queen" => -10}
|
5
|
+
def value
|
6
|
+
@@value.default = @num.to_i
|
7
|
+
return @@value[@num]
|
8
|
+
end
|
9
|
+
def _value
|
10
|
+
return case @num
|
11
|
+
when "Ace" then 1
|
12
|
+
when 2..3 then @num
|
13
|
+
when 4 then 0
|
14
|
+
when 5..8 then @num
|
15
|
+
when 9 then 0
|
16
|
+
when 10 then 10
|
17
|
+
when "Jack" then 0
|
18
|
+
when "Queen" then -10
|
19
|
+
when "King" then 99
|
20
|
+
when "Joker" then 0
|
21
|
+
end
|
22
|
+
end
|
23
|
+
def initialize(card); @num = card; end
|
24
|
+
end
|
25
|
+
class UserCard < Card # This class converts user input into a Card-like object
|
26
|
+
attr_reader :num
|
27
|
+
@@num = {King: "King", K: "King", Ace: "Ace", A: "Ace", Queen: "Queen", Q: "Queen", Jack: "Jack", J: "Jack", Joker: "Joker", "$".to_sym => "Joker"}
|
28
|
+
def _num; @@num; end
|
29
|
+
def initialize(number=""); @@num.default, @num = number.to_i, @@num[number.capitalize.to_sym]; end
|
30
|
+
end
|
31
|
+
class Hand # Creates a object that holds and can play cards
|
32
|
+
attr_reader :hand
|
33
|
+
def initialize; @hand = [$deck.shift, $deck.shift, $deck.shift]; end
|
34
|
+
def play(card)
|
35
|
+
legal = false
|
36
|
+
for cards in @hand; legal = true if cards.num == card.num; end
|
37
|
+
raise CardError, "\aCard not allowed\a" unless legal
|
38
|
+
if card.num == "King"; $value = 99
|
39
|
+
elsif card.num == "Joker"; $value = 0
|
40
|
+
else; $value += card.value
|
41
|
+
end
|
42
|
+
i, done = 0, false
|
43
|
+
for index in @hand
|
44
|
+
if index.num == card.num and not done
|
45
|
+
discard = @hand[i]
|
46
|
+
@hand.delete_at(i)
|
47
|
+
draw = $deck.shift
|
48
|
+
@hand.push(draw)
|
49
|
+
$deck.push(discard)
|
50
|
+
done = true
|
51
|
+
end
|
52
|
+
i += 1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
def view
|
56
|
+
print "\tThese are your cards: "
|
57
|
+
@hand.each {|card| print "\t#{card.num}"}
|
58
|
+
end
|
59
|
+
end
|
60
|
+
def pause(p)
|
61
|
+
sleep(p)
|
62
|
+
puts
|
63
|
+
end
|
64
|
+
$deck = Array.new
|
65
|
+
4.times do # Add the cards to the deck
|
66
|
+
$deck.push(Card.new("Ace"))
|
67
|
+
$deck.push(Card.new("King"))
|
68
|
+
$deck.push(Card.new("Queen"))
|
69
|
+
$deck.push(Card.new("Jack"))
|
70
|
+
$deck.push(Card.new(10))
|
71
|
+
$deck.push(Card.new(9))
|
72
|
+
$deck.push(Card.new(8))
|
73
|
+
$deck.push(Card.new(7))
|
74
|
+
$deck.push(Card.new(6))
|
75
|
+
$deck.push(Card.new(5))
|
76
|
+
$deck.push(Card.new(4))
|
77
|
+
$deck.push(Card.new(3))
|
78
|
+
$deck.push(Card.new(2))
|
79
|
+
end
|
80
|
+
2.times {$deck.push(Card.new("Joker"))}
|
81
|
+
$deck.shuffle!
|
82
|
+
__END__
|
data/spec/99spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
require "99"
|
3
|
+
describe "$deck" do
|
4
|
+
describe "#length" do
|
5
|
+
it "should == 54" do; expect($deck.length + 6).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
|
20
|
+
describe "UserCard" do
|
21
|
+
describe "#num" do
|
22
|
+
for key in UserCard.new._num.keys
|
23
|
+
describe "#{key}" do
|
24
|
+
it "should == #{UserCard.new._num[key]}" do;
|
25
|
+
expect(UserCard.new(key.to_s).num).to eq UserCard.new._num[key]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
describe "Hand" do
|
32
|
+
hand = Hand.new
|
33
|
+
describe "#hand" do
|
34
|
+
describe "#length" do
|
35
|
+
it "should == 3" do; expect(hand.hand.length).to eq 3; end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
describe "#initialize" do
|
39
|
+
describe "$deck" do
|
40
|
+
it "should have three less cards after initialization" do
|
41
|
+
deck1, hand, deck2 = $deck.length, Hand.new, $deck.length
|
42
|
+
expect(deck1).to > deck2
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,76 @@
|
|
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 this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, make a
|
10
|
+
# separate helper file that requires this one and then use it only in the specs
|
11
|
+
# that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
RSpec.configure do |config|
|
18
|
+
# The settings below are suggested to provide a good initial experience
|
19
|
+
# with RSpec, but feel free to customize to your heart's content.
|
20
|
+
# These two settings work together to allow you to limit a spec run
|
21
|
+
# to individual examples or groups you care about by tagging them with
|
22
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
23
|
+
# get run.
|
24
|
+
config.filter_run :focus
|
25
|
+
config.run_all_when_everything_filtered = true
|
26
|
+
|
27
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
28
|
+
# file, and it's useful to allow more verbose output when running an
|
29
|
+
# individual spec file.
|
30
|
+
if config.files_to_run.one?
|
31
|
+
# Use the documentation formatter for detailed output,
|
32
|
+
# unless a formatter has already been configured
|
33
|
+
# (e.g. via a command-line flag).
|
34
|
+
config.default_formatter = 'doc'
|
35
|
+
end
|
36
|
+
|
37
|
+
# Print the 10 slowest examples and example groups at the
|
38
|
+
# end of the spec run, to help surface which specs are running
|
39
|
+
# particularly slow.
|
40
|
+
config.profile_examples = 10
|
41
|
+
|
42
|
+
# Run specs in random order to surface order dependencies. If you find an
|
43
|
+
# order dependency and want to debug it, you can fix the order by providing
|
44
|
+
# the seed, which is printed after each run.
|
45
|
+
# --seed 1234
|
46
|
+
config.order = :random
|
47
|
+
|
48
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
49
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
50
|
+
# test failures related to randomization by passing the same `--seed` value
|
51
|
+
# as the one that triggered the failure.
|
52
|
+
Kernel.srand config.seed
|
53
|
+
|
54
|
+
# rspec-expectations config goes here. You can use an alternate
|
55
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
56
|
+
# assertions if you prefer.
|
57
|
+
config.expect_with :rspec do |expectations|
|
58
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
59
|
+
# For more details, see:
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
61
|
+
expectations.syntax = :expect
|
62
|
+
end
|
63
|
+
|
64
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
65
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
66
|
+
config.mock_with :rspec do |mocks|
|
67
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
68
|
+
# For more details, see:
|
69
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
70
|
+
mocks.syntax = :expect
|
71
|
+
|
72
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
73
|
+
# a real object. This is generally recommended.
|
74
|
+
mocks.verify_partial_doubles = true
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: 99_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zachary Perlmutter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
This is a text-based interpretation of the card game 99.
|
15
|
+
email: zrp200@gmail.com
|
16
|
+
executables:
|
17
|
+
- 99_game
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- bin/99_game
|
23
|
+
- lib/99_game.rb
|
24
|
+
- spec/99spec.rb
|
25
|
+
- spec/spec_helper.rb
|
26
|
+
homepage: https://rubygems.org/gems/99_game
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 2.0.0
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.3.0
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: The game of 99.
|
50
|
+
test_files:
|
51
|
+
- spec/99spec.rb
|
52
|
+
- ".rspec"
|
53
|
+
- spec/spec_helper.rb
|