Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial Commit

  • Loading branch information...
commit 40c1c6fdd1e6f14cbd1f0a4f99d6544d6a5afe37 0 parents
@jwo jwo authored
10 .travis.yml
@@ -0,0 +1,10 @@
+language: ruby
+rvm:
+ - 1.9.3
+
+script:
+ - bundle exec rspec blackjack
+
+notifications:
+ email:
+ - jesse@comalproductions.com
3  Gemfile
@@ -0,0 +1,3 @@
+source 'http://rubygems.org'
+
+gem 'rspec'
18 Gemfile.lock
@@ -0,0 +1,18 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.3)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.2)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rspec
7 LICENSE
@@ -0,0 +1,7 @@
+Copyright (c) 2012 Jesse Wolgamott
+
+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:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+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.
34 README.md
@@ -0,0 +1,34 @@
+Episode1 Summer 2012- BlackJack
+=====================
+
+A Casino game. No gambling, just skill!
+
+Panda Level
+-----------
+
+1. Play the game by running this code:
+
+```
+irb
+require "./blackjack"
+game = Game.new
+game.hit
+game.stand
+```
+
+2. Change the Card's to_s to show "Q5" instead of 5-queen"
+
+
+Tiger Level
+-----------
+
+1. Complete the Panda assignment
+2. If a player busts (goes over 21), the game should #standfor the player
+
+
+Eagle Level
+------------
+
+1. The dealer hand should not not show both cards until the player has stood (It should be like "XX", "Q5")
+
+Copyright: Jesse Wolgamott, MIT License (See LICENSE)
242 blackjack.rb
@@ -0,0 +1,242 @@
+require 'rspec'
+class Card
+
+ attr_reader :suit, :value
+ def initialize(suit, value)
+ @suit = suit
+ @value = value
+ end
+
+ def value
+ return 10 if ["J", "Q", "K"].include?(@value)
+ return 11 if @value == "A"
+ return @value
+ end
+
+ def to_s
+ "#{@value}-#{suit}"
+ end
+
+end
+
+
+class Deck
+ attr_reader :cards
+
+ def initialize
+ @cards = Deck.build_cards
+ end
+
+ def self.build_cards
+ cards = []
+ [:clubs, :diamonds, :spades, :hearts].each do |suit|
+ (2..10).each do |number|
+ cards << Card.new(suit, number)
+ end
+ ["J", "Q", "K", "A"].each do |facecard|
+ cards << Card.new(suit, facecard)
+ end
+ end
+ cards.shuffle
+ end
+end
+
+class Hand
+ attr_reader :cards
+
+ def initialize
+ @cards = []
+ end
+ def hit!(deck)
+ @cards << deck.cards.shift
+ end
+
+ def value
+ cards.inject(0) {|sum, card| sum += card.value }
+ end
+
+ def play_as_dealer(deck)
+ if value < 16
+ hit!(deck)
+ play_as_dealer(deck)
+ end
+ end
+end
+
+class Game
+ attr_reader :player_hand, :dealer_hand
+ def initialize
+ @deck = Deck.new
+ @player_hand = Hand.new
+ @dealer_hand = Hand.new
+ 2.times { @player_hand.hit!(@deck) }
+ 2.times { @dealer_hand.hit!(@deck) }
+ end
+
+ def hit
+ @player_hand.hit!(@deck)
+ end
+
+ def stand
+ @dealer_hand.play_as_dealer(@deck)
+ @winner = determine_winner(@player_hand.value, @dealer_hand.value)
+ end
+
+ def status
+ {:player_cards=> @player_hand.cards,
+ :player_value => @player_hand.value,
+ :dealer_cards => @dealer_hand.cards,
+ :dealer_value => @dealer_hand.value,
+ :winner => @winner}
+ end
+
+ def determine_winner(player_value, dealer_value)
+ return :dealer if player_value > 21
+ return :player if dealer_value > 21
+ if player_value == dealer_value
+ :push
+ elsif player_value > dealer_value
+ :player
+ else
+ :dealer
+ end
+ end
+
+ def inspect
+ status
+ end
+end
+
+
+describe Card do
+
+ it "should accept suit and value when building" do
+ card = Card.new(:clubs, 10)
+ card.suit.should eq(:clubs)
+ card.value.should eq(10)
+ end
+
+ it "should have a value of 10 for facecards" do
+ facecards = ["J", "Q", "K"]
+ facecards.each do |facecard|
+ card = Card.new(:hearts, facecard)
+ card.value.should eq(10)
+ end
+ end
+ it "should have a value of 4 for the 4-clubs" do
+ card = Card.new(:clubs, 4)
+ card.value.should eq(4)
+ end
+
+ it "should return 11 for Ace" do
+ card = Card.new(:diamonds, "A")
+ card.value.should eq(11)
+ end
+
+ it "should be formatted nicely" do
+ card = Card.new(:diamonds, "A")
+ card.to_s.should eq("A-diamonds")
+ end
+end
+
+
+describe Deck do
+
+ it "should build 52 cards" do
+ Deck.build_cards.length.should eq(52)
+ end
+
+ it "should have 52 cards when new deck" do
+ Deck.new.cards.length.should eq(52)
+ end
+
+end
+
+
+describe Hand do
+
+ it "should calculate the value correctly" do
+ deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 10)])
+ hand = Hand.new
+ 2.times { hand.hit!(deck) }
+ hand.value.should eq(14)
+ end
+
+ it "should take from the top of the deck" do
+ club4 = Card.new(:clubs, 4)
+ diamond7 = Card.new(:diamonds, 7)
+ clubK = Card.new(:clubs, "K")
+
+ deck = mock(:deck, :cards => [club4, diamond7, clubK])
+ hand = Hand.new
+ 2.times { hand.hit!(deck) }
+ hand.cards.should eq([club4, diamond7])
+
+ end
+
+ describe "#play_as_dealer" do
+ it "should hit blow 16" do
+ deck = mock(:deck, :cards => [Card.new(:clubs, 4), Card.new(:diamonds, 4), Card.new(:clubs, 2), Card.new(:hearts, 6)])
+ hand = Hand.new
+ 2.times { hand.hit!(deck) }
+ hand.play_as_dealer(deck)
+ hand.value.should eq(16)
+ end
+ it "should not hit above" do
+ deck = mock(:deck, :cards => [Card.new(:clubs, 8), Card.new(:diamonds, 9)])
+ hand = Hand.new
+ 2.times { hand.hit!(deck) }
+ hand.play_as_dealer(deck)
+ hand.value.should eq(17)
+ end
+ it "should stop on 21" do
+ deck = mock(:deck, :cards => [Card.new(:clubs, 4),
+ Card.new(:diamonds, 7),
+ Card.new(:clubs, "K")])
+ hand = Hand.new
+ 2.times { hand.hit!(deck) }
+ hand.play_as_dealer(deck)
+ hand.value.should eq(21)
+ end
+ end
+end
+
+
+describe Game do
+
+ it "should have a players hand" do
+ Game.new.player_hand.cards.length.should eq(2)
+ end
+ it "should have a dealers hand" do
+ Game.new.dealer_hand.cards.length.should eq(2)
+ end
+ it "should have a status" do
+ Game.new.status.should_not be_nil
+ end
+ it "should hit when I tell it to" do
+ game = Game.new
+ game.hit
+ game.player_hand.cards.length.should eq(3)
+ end
+
+ it "should play the dealer hand when I stand" do
+ game = Game.new
+ game.stand
+ game.status[:winner].should_not be_nil
+ end
+
+ describe "#determine_winner" do
+ it "should have dealer win when player busts" do
+ Game.new.determine_winner(22, 15).should eq(:dealer)
+ end
+ it "should player win if dealer busts" do
+ Game.new.determine_winner(18, 22).should eq(:player)
+ end
+ it "should have player win if player > dealer" do
+ Game.new.determine_winner(18, 16).should eq(:player)
+ end
+ it "should have push if tie" do
+ Game.new.determine_winner(16, 16).should eq(:push)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.