RubyJack 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) <2012> <Jeffrey Baird>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,16 @@
1
+ This is a very simple text based black jack game.
2
+
3
+ To use: install the gem, type in 'black_jack' to start running the program.
4
+
5
+ There are several commands in the game:
6
+
7
+ "deal" deals the deck
8
+ "hit" gives another card
9
+ "stay" allows the dealer to start playing
10
+ "quit" quits the game
11
+
12
+ Some known limitations to this version:
13
+
14
+ 1. The dealer makes no moves, he always stays
15
+ 2. There is no way to bet.
16
+ 3. There is no way to play an Ace as a 1 it is always worth 11.
data/bin/black_jack ADDED
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/black_jack/game'
4
+
5
+
6
+ def start_a_game
7
+ puts "What do you want to name your game?"
8
+ game_name = gets.chomp
9
+ game_game = game_name.capitalize
10
+ @game = BlackJack::Game.new(game_name)
11
+ end
12
+
13
+ def name_a_player
14
+ puts "What is your name?"
15
+ @player = BlackJack::Player.new(gets.capitalize.chomp)
16
+ end
17
+
18
+ def deal_game
19
+ @deck = BlackJack::Deck.new
20
+ @dealer = BlackJack::Dealer.new()
21
+ @deck.create_a_deck
22
+ @deck.shuffle_deck
23
+ @player.take_card(@deck)
24
+ @player.take_card(@deck)
25
+ check_bust
26
+ @dealer.take_card(@deck)
27
+ @dealer.take_card_face_down(@deck)
28
+ puts "\nYour cards are #{@player.hand}"
29
+ @dealer.show_up_cards(@dealer.hand)
30
+ end
31
+
32
+
33
+
34
+ def check_bust player=@player, dealer=@dealer
35
+ if player.bust?
36
+ determine_a_winner(player, dealer)
37
+ abort("Thanks for playing!")
38
+ else
39
+ play_game
40
+ end
41
+ end
42
+ def player_hits player=@player, deck=@deck, dealer=@dealer, game=@game
43
+ player.hit(deck)
44
+ puts "\n#{player.name} has #{player.hand} with a value of #{player.hand_value}"
45
+ check_bust player
46
+ end
47
+
48
+ def player_stays player=@player, dealer=@dealer
49
+ dealer.show_cards(dealer.hand)
50
+ puts "#{player.name} has #{player.hand} with a value of #{player.hand_value}"
51
+ puts "#{dealer.name} has #{dealer.hand} with a value of #{dealer.hand_value}"
52
+ determine_a_winner(player, dealer)
53
+ end
54
+
55
+ def player_quits
56
+ puts "are you sure you want to quit? (y/n)"
57
+ quit = gets.downcase.chomp
58
+ case quit
59
+ when 'y'
60
+ abort("play again soon!")
61
+ when 'n'
62
+ play_game
63
+ else
64
+ puts "please enter 'y' or 'n'"
65
+ end
66
+ end
67
+
68
+ def determine_a_winner player, dealer
69
+ @game.print_players
70
+ if player.hand_value > dealer.hand_value
71
+ puts "#{player.name} wins!"
72
+ abort("Thanks for playing!")
73
+ elsif player.hand_value == 21
74
+ puts "#{player.name} wins!"
75
+ abort("Thanks for playing!")
76
+ else
77
+ puts "#{dealer.name} wins!"
78
+ abort("Thanks for playing!")
79
+ end
80
+ end
81
+
82
+ def play_game player=@player, deck=@deck, dealer=@dealer
83
+ puts "Do you want to 'hit' or 'stay'?"
84
+ loop do
85
+ hit_stay = gets.downcase.chomp
86
+ case hit_stay
87
+ when 'hit'
88
+ player_hits(player, deck)
89
+ when 'stay'
90
+ player_stays
91
+ break
92
+ when 'quit'
93
+ player_quits
94
+ else
95
+ puts "please type 'hit', 'stay' or 'quit'"
96
+ end
97
+ end
98
+ end
99
+
100
+ start_a_game
101
+ name_a_player
102
+ puts "To start the game, type 'deal'"
103
+ loop do
104
+ answer = gets.chomp
105
+ case answer
106
+ when 'deal'
107
+ deal_game
108
+ play_game(@player, @deck)
109
+ break
110
+ when 'quit'
111
+ break
112
+ else
113
+ puts "please type 'deal' or 'quit'"
114
+ end
115
+ end
116
+
@@ -0,0 +1,68 @@
1
+ require_relative 'player'
2
+
3
+ module BlackJack
4
+
5
+ class Dealer < Player
6
+ # This class deals with the special player that follows the rules of
7
+ # a black jack dealer in vegas
8
+ attr_reader :in_the_hole
9
+
10
+ def initialize (name="dealer")
11
+ super(@hand)
12
+ @name = name
13
+ end
14
+
15
+ #one of the main differences detween a dealer and a regular player
16
+ #is that the dealer takes one of his cards face down (in a hash)
17
+ def take_card_face_down deck
18
+ @in_the_hole = Hash.new
19
+ @in_the_hole[:hidden] = deck.deal_a_card
20
+ self.hand << in_the_hole
21
+ self.hand
22
+ end
23
+ #I am trying(unsuccessfully) to establish the special rules for a deal
24
+ #the dealer should hit at any value of 17 or below
25
+ def dealer_play
26
+ if self.hand_value < 17
27
+ self.hit
28
+ end
29
+ end
30
+ #the purpose of this method is to convert the hidden card (in a hash)
31
+ #to a normal card
32
+ def show_cards hand
33
+ hand.each do |card|
34
+ if card.class == Hash
35
+ card = card.to_a
36
+ card = card.flatten
37
+ card = card.pop
38
+ hand.delete_if {|x| x.class == Hash}
39
+ hand << card
40
+ end
41
+ end
42
+ end
43
+ # this prints out the "up cards" of the dealer
44
+ # so that the player knows the up card
45
+ def show_up_cards hand
46
+ up_cards = []
47
+ hand.each do |card|
48
+ if card.class != Hash
49
+ up_cards << card
50
+ end
51
+ end
52
+ puts "The dealer's up-cards are #{up_cards}"
53
+ end
54
+ end
55
+
56
+ if __FILE__ == $0
57
+ deck = BlackJack::Deck.new
58
+ deck.create_a_deck
59
+ deck.shuffle_deck
60
+
61
+ dealer = BlackJack::Dealer.new()
62
+ dealer.take_card(deck)
63
+ dealer.take_card_face_down(deck)
64
+ dealer.show_up_cards(dealer.hand)
65
+ dealer.show_cards(dealer.hand)
66
+ puts "\n#{dealer.name} has #{dealer.hand} with a value of #{dealer.hand_value}"
67
+ end
68
+ end
@@ -0,0 +1,50 @@
1
+ module BlackJack
2
+ class Deck
3
+
4
+ attr_accessor :new_deck
5
+
6
+ def initialize
7
+ end
8
+
9
+ def create_a_deck
10
+ new_suits = %w(hearts spades clubs diamonds )
11
+ new_value = %w(Ace 2 3 4 5 6 7 8 9 10 Jack Queen King)
12
+ @new_deck = Array.new
13
+ #creates a new array by combining the information in the
14
+ #above two
15
+ new_value.each do |card|
16
+ new_suits.each do |suited|
17
+ @new_deck << "#{card} of #{suited}"
18
+ end
19
+ end
20
+ end
21
+
22
+ def shuffle_deck
23
+ @new_deck = @new_deck.shuffle
24
+ end
25
+
26
+ def deal_a_card new_deck=@new_deck
27
+ dealt_card = @new_deck.pop
28
+ dealt_card
29
+ end
30
+ #this method was written soley for the purpose of testing
31
+ def deal_specific_card deck=@new_deck, index_number
32
+ dealt_card = @new_deck[index_number]
33
+ dealt_card
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+
40
+ if __FILE__ == $0
41
+ deck = BlackJack::Deck.new
42
+ deck.create_a_deck
43
+ # deck.shuffle_deck
44
+ puts deck.new_deck
45
+
46
+ puts "\nPlayer was dealt a #{deck.deal_a_card}"
47
+ puts "\nPlayer was dealt a #{deck.deal_a_card}"
48
+ puts "\nPlayer was dealt a #{deck.deal_a_card}"
49
+ puts "\nPlayer was dealt a #{deck.deal_a_card}"
50
+ end
@@ -0,0 +1,70 @@
1
+ require_relative 'player'
2
+ require_relative 'dealer'
3
+
4
+ module BlackJack
5
+
6
+ #this is a fairly self-explanitory section
7
+
8
+ class Game
9
+ attr_reader :players, :game_name
10
+ def initialize game_name, num_players=2
11
+ @game_name = game_name
12
+ @num_players = num_players
13
+ @players = []
14
+ end
15
+
16
+ #adds a player to the game, doesn't really work the way I
17
+ #want it to :(
18
+
19
+ def add_player a_player
20
+ @players.push(a_player)
21
+ end
22
+
23
+ #a method to easily print out the player
24
+
25
+ def print_players(players=@players)
26
+ players.each do |player|
27
+ puts "#{player.name} has #{player.hand}"
28
+ end
29
+ end
30
+
31
+ #we don't use the two methods below (they are also in player)
32
+ #I feel like they should be here
33
+
34
+ def take_card deck=@deck, player=@player
35
+ @hand = []
36
+ @hand << deck.deal_a_card
37
+ @hand
38
+ end
39
+
40
+ def hit deck
41
+ self.take_card (deck)
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ if __FILE__ == $0
49
+ game = BlackJack::Game.new("game1")
50
+
51
+ deck = BlackJack::Deck.new
52
+ deck.create_a_deck
53
+ deck.shuffle_deck
54
+ puts deck.new_deck
55
+
56
+ player1 = BlackJack::Player.new("jeff")
57
+ player2 = BlackJack::Player.new("clare")
58
+ game.add_player(player1)
59
+ game.add_player(player2)
60
+ player1.take_card (deck)
61
+ player2.take_card(deck)
62
+ player1.take_card (deck)
63
+ player2.take_card(deck)
64
+ player1.hit(deck)
65
+ player2.hit(deck)
66
+ game.play_game
67
+ game.print_players
68
+ game.bust?(player1)
69
+ game.bust?(player2)
70
+ end
@@ -0,0 +1,115 @@
1
+ require_relative 'deck'
2
+
3
+ module BlackJack
4
+
5
+ class Player
6
+
7
+ attr_reader :name, :hand
8
+ attr_accessor :current_value, :hand_value
9
+
10
+ def initialize (name)
11
+ @name = name
12
+ @hand = []
13
+ @current_value = 0
14
+ end
15
+ #I feel like I should have a to_s method, not sure I'm doing it right
16
+ def to_s
17
+ "#{@name}'s hand is worth #{self.hand_value}"
18
+ end
19
+
20
+ def take_card deck
21
+ self.hand << deck.deal_a_card
22
+ self.hand
23
+ end
24
+
25
+ def take_specific_card(deck=@deck, index_number=0)
26
+ self.hand << deck.deal_specific_card(deck, index_number)
27
+ self.hand
28
+ end
29
+ #this might be a bit redundant but I feel like it
30
+ #makes the code more intuitive
31
+ def hit deck
32
+ self.take_card(deck)
33
+ end
34
+ #Program interates through the hand calling the card_value
35
+ #of each card
36
+ def hand_value(current_value=0)
37
+ @current_value = current_value
38
+ self.hand.each do |card|
39
+ @current_value += card_value(card)
40
+ end
41
+ @current_value
42
+ end
43
+ #When the hand has a value of over 21 it busts and declares true
44
+ #when it equals 21 it declares blackjack and returns true
45
+ def bust?
46
+ if hand_value > 21
47
+ fold_hand
48
+ puts "#{@name} is Bust!"
49
+ true
50
+ elsif hand_value == 21
51
+ puts "BlackJack!"
52
+ true
53
+ else
54
+ puts "#{@name}'s hand is worth #{self.hand_value}"
55
+ false
56
+ end
57
+ end
58
+ #gives us a method for folding a busted hand
59
+ def fold_hand
60
+ @hand.each {|card| hand.delete(card)}
61
+ @hand = []
62
+ end
63
+ #I hate this method and feel like there is a much easier
64
+ #way of doing it.
65
+ def card_value card
66
+ if card =~ /Queen/
67
+ 10
68
+ elsif card =~ /King/
69
+ 10
70
+ elsif card =~ /Jack/
71
+ 10
72
+ elsif card =~ /Ace/
73
+ 11
74
+ elsif card =~ /2/
75
+ 2
76
+ elsif card =~ /3/
77
+ 3
78
+ elsif card =~ /4/
79
+ 4
80
+ elsif card =~ /5/
81
+ 5
82
+ elsif card =~ /6/
83
+ 6
84
+ elsif card =~ /7/
85
+ 7
86
+ elsif card =~ /8/
87
+ 8
88
+ elsif card =~ /9/
89
+ 9
90
+ elsif card =~ /10/
91
+ 10
92
+ else
93
+ 0
94
+ end
95
+ end
96
+
97
+ end
98
+ end
99
+
100
+ if __FILE__ == $0
101
+ deck = BlackJack::Deck.new
102
+ deck.create_a_deck
103
+ deck.shuffle_deck
104
+ puts deck.new_deck
105
+
106
+ player1 = BlackJack::Player.new("jeff")
107
+ player2 = BlackJack::Player.new("clare")
108
+ player1.take_card (deck)
109
+ player2.take_card(deck)
110
+ player1.take_card (deck)
111
+ player2.take_card(deck)
112
+ player1.hit(deck)
113
+ puts "\n#{player1.name} has #{player1.hand} with a value of #{player1.hand_value}"
114
+ puts "\n#{player2.name} has #{player2.hand} with a value of #{player2.hand_value}"
115
+ end
@@ -0,0 +1,39 @@
1
+ require 'black_jack/game'
2
+ require 'black_jack/player'
3
+ require 'black_jack/deck'
4
+ module BlackJack
5
+
6
+ describe Deck do
7
+ before do
8
+ @game = BlackJack::Game.new("game1")
9
+ @player = BlackJack::Player.new("jeff")
10
+ @dealer = BlackJack::Dealer.new("dealer")
11
+ @deck = BlackJack::Deck.new
12
+ @new_deck = @deck.create_a_deck
13
+ end
14
+
15
+ it "shows cards" do
16
+ @dealer.take_card_face_down(@deck)
17
+ @dealer.take_card(@deck)
18
+ @dealer.show_cards(@dealer.hand).should == ["King of clubs", "King of diamonds"]
19
+ end
20
+
21
+ it "takes a card facedown" do
22
+ @dealer.take_card_face_down(@deck)
23
+ @dealer.hand.should == [{:hidden=>"King of diamonds"}]
24
+ end
25
+
26
+ it "automatically takes a hit"
27
+
28
+ it "automatically stays"
29
+
30
+ it "shows the up_cards" do
31
+ @dealer.take_card_face_down(@deck)
32
+ @dealer.take_card(@deck)
33
+ @up_cards = @dealer.show_up_cards(@dealer.hand)
34
+ @up_cards.should == "The dealer's up-cards are ['King of clubs']"
35
+ end
36
+
37
+
38
+ end
39
+ end
@@ -0,0 +1,33 @@
1
+ require 'black_jack/game'
2
+ require 'black_jack/player'
3
+ require 'black_jack/deck'
4
+ module BlackJack
5
+
6
+ describe Deck do
7
+ before do
8
+ @game = BlackJack::Game.new("game1")
9
+ @player = BlackJack::Player.new("jeff")
10
+ @dealer = BlackJack::Dealer.new("dealer")
11
+ @deck = BlackJack::Deck.new
12
+ @new_deck = @deck.create_a_deck
13
+ end
14
+
15
+ it "Has 52 cards" do
16
+ @deck.new_deck.size.should == 52
17
+ end
18
+
19
+ it "shuffles the deck" do
20
+ @deck.shuffle_deck
21
+ @new_deck[0].should_not == "Ace of hearts"
22
+ end
23
+
24
+ it "deals a card" do
25
+ @deck.deal_a_card.should == "King of diamonds"
26
+ end
27
+
28
+ it "deals a specific card" do
29
+ @deck.deal_specific_card(0).should == "Ace of hearts"
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+ require 'black_jack/game'
2
+ require 'black_jack/player'
3
+ require 'black_jack/deck'
4
+ module BlackJack
5
+
6
+ describe Game do
7
+ before do
8
+ @game = BlackJack::Game.new("game1")
9
+ @player = BlackJack::Player.new("jeff")
10
+ @dealer = BlackJack::Dealer.new("dealer")
11
+ @deck = BlackJack::Deck.new
12
+ @deck.create_a_deck
13
+ end
14
+
15
+ it "has a name" do
16
+ @game.game_name.should_not be_nil
17
+ end
18
+
19
+ it "adds a player to the game"
20
+ # @game.add_player(@player)
21
+ # @game.each{|x| puts x}.should == "jeff's hand is worth 0"
22
+
23
+ it "prints the players out"
24
+ # @game.add_player(@dealer).should == [dealers hand is worth 0]
25
+ # @game.print_players.should == "dealer has []"
26
+
27
+
28
+ it "deals out a card" do
29
+ @game.take_card(@deck).should == ["King of diamonds"]
30
+ end
31
+
32
+ it "can 'hit'" do
33
+ @game.hit(@deck).should == ["King of diamonds"]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,68 @@
1
+ require 'black_jack/game'
2
+ require 'black_jack/player'
3
+ require 'black_jack/deck'
4
+ module BlackJack
5
+
6
+ describe Game do
7
+ before do
8
+ @game = BlackJack::Game.new("game1")
9
+ @player = BlackJack::Player.new("jeff")
10
+ @dealer = BlackJack::Dealer.new("dealer")
11
+ @deck = BlackJack::Deck.new
12
+ @deck.create_a_deck
13
+ end
14
+
15
+ it "has a to_s method" do
16
+ @player.to_s.should == "jeff's hand is worth 0"
17
+ end
18
+
19
+ it "takes a card" do
20
+ @player.take_card(@deck)
21
+ @player.hand.should == ["King of diamonds"]
22
+ end
23
+
24
+ it "takes a hit" do
25
+ @player.hit(@deck)
26
+ @player.hand.should == ["King of diamonds"]
27
+ end
28
+
29
+ it "determines the point value of a hand" do
30
+ @player.hit(@deck)
31
+ @player.hand_value.should == 10
32
+ end
33
+
34
+ it "busts a player" do
35
+ @player.take_card(@deck)
36
+ @player.take_card(@deck)
37
+ @player.take_card(@deck)
38
+ @player.bust?.should be_true
39
+ end
40
+
41
+ it "doesn't bust a player" do
42
+ @player.take_card(@deck)
43
+ @player.bust?.should be_false
44
+ end
45
+
46
+ it "folds a hand" do
47
+ @player.take_card(@deck)
48
+ @player.fold_hand
49
+ @player.hand_value.should == 0
50
+ end
51
+
52
+ context "Has Black Jack" do
53
+ before do
54
+ @game = BlackJack::Game.new("game1")
55
+ @player = BlackJack::Player.new("jeff")
56
+ @dealer = BlackJack::Dealer.new("dealer")
57
+ @deck = BlackJack::Deck.new
58
+ @deck.create_a_deck
59
+ @player.take_specific_card(@deck, 0)
60
+ @player.take_card(@deck)
61
+ end
62
+ it "declares black jack" do
63
+ @player.bust?.should be_true
64
+ end
65
+ end
66
+ end
67
+ end
68
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: RubyJack
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeffrey Baird
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! 'This is a very simple text based black jack game.
31
+
32
+
33
+ To use: install the gem, type in ''black_jack'' to start running the program.
34
+
35
+
36
+ There are several commands in the game:
37
+
38
+
39
+ "deal" deals the deck
40
+
41
+ "hit" gives another card
42
+
43
+ "stay" allows the dealer to start playing
44
+
45
+ "quit" quits the game
46
+
47
+
48
+ Some known limitations to this version:
49
+
50
+
51
+ 1. The dealer makes no moves, he always stays
52
+
53
+ 2. There is no way to bet.
54
+
55
+ 3. There is no way to play an Ace as a 1 it is always worth 11.'
56
+ email: jeff@jeffreyleebaird.com
57
+ executables:
58
+ - black_jack
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - bin/black_jack
63
+ - lib/black_jack/dealer.rb
64
+ - lib/black_jack/deck.rb
65
+ - lib/black_jack/game.rb
66
+ - lib/black_jack/player.rb
67
+ - spec/black_jack/dealer_spec.rb
68
+ - spec/black_jack/deck_spec.rb
69
+ - spec/black_jack/game_spec.rb
70
+ - spec/black_jack/player_spec.rb
71
+ - LICENSE
72
+ - README
73
+ homepage: http://www.jeffreyleebaird.com
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '1.9'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.23
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: A simple text based black jack game that lacks serious features
97
+ test_files:
98
+ - spec/black_jack/dealer_spec.rb
99
+ - spec/black_jack/deck_spec.rb
100
+ - spec/black_jack/game_spec.rb
101
+ - spec/black_jack/player_spec.rb