Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'
gem 'sinatra'
# Note: ERB is native to Ruby so doesn't need to be installed

# only run this in the "development" environment
group :development do
gem 'pry-byebug' # for debugging
gem 'shotgun'
end
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
GEM
remote: https://rubygems.org/
specs:
byebug (9.0.5)
coderay (1.1.1)
method_source (0.8.2)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.4.0)
byebug (~> 9.0)
pry (~> 0.10)
rack (1.6.4)
rack-protection (1.5.3)
rack
shotgun (0.9.1)
rack (>= 1.0)
sinatra (1.4.7)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
slop (3.6.0)
tilt (2.0.5)

PLATFORMS
ruby

DEPENDENCIES
pry-byebug
shotgun
sinatra

BUNDLED WITH
1.12.5
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
Hit me baby one more time?

[A Blackjack game using the Ruby Sinatra web application framework which uses object oriented programming, cookies, sessions, and JSON from the Viking Code School](http://www.vikingcodeschool.com)

# animated gif
![blackjack](blackjack_animated.gif)
60 changes: 60 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'sinatra'
require 'erb'
require 'pry-byebug'
require './helpers/deck_helpers.rb'
require './player.rb'
require './deck.rb'
helpers DeckHelper
enable :sessions

get '/' do
reset_bankroll
erb :home
end

get '/bet' do
erb :bet
end

post '/bet' do
if valid_bet(params[:bet].to_i)
set_player_bet(params[:bet].to_i)
create_deck(reset_bankroll: false)
if !player_blackjack && !dealer_blackjack
erb :blackjack
elsif player_blackjack && !dealer_blackjack
@blackjack = true
binding.pry
redirect "/blackjack/stay"
else
redirect "/blackjack/stay"
end
else
@error = "INSUFFICIENT_AMOUNT"
erb :bet
end
end

post '/blackjack/hit' do
deal_player
if player_busted? || player_blackjack
redirect "/blackjack/stay"
else
erb :blackjack
end
end

post '/blackjack/stay' do
if !player_busted?
while dealer_count < 17
deal_dealer
end
end
redirect "/blackjack/stay"
end

get '/blackjack/stay' do
@winner = compare_hands
@busted = busted_player
erb :gameover
end
Binary file added blackjack_animated.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions deck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Deck

attr_reader :cards
def initialize(cards = nil)
suits = ['H', 'D', 'S', 'C']
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
cards.nil? ? @cards = suits.product(values).shuffle! : @cards = cards
end

def pop
cards.pop
end
end
130 changes: 130 additions & 0 deletions helpers/deck_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
module DeckHelper
BLACKJACK_AMT = 21

def create_deck(params = {})
@deck = Deck.new
@dealer = Player.new("Dealer", [])
@player = Player.new("Player", [])
session[:deck] = @deck.cards
session[:dealer_cards] = @dealer.hand
session[:player_cards] = @player.hand
session[:player_bankroll] = @player.bankroll if params[:reset_bankroll]
session[:blackjack_cards] = [['S', 'J'], ['S', 'A']]
2.times { deal_dealer ; deal_player }
end

def new_hand
@dealer = Player.new("Dealer", [])
@player = Player.new("Player", [])
session[:dealer_cards] = @dealer.hand
session[:player_cards] = @player.hand
2.times { deal_dealer ; deal_player }
end

def reset_bankroll
session[:player_bankroll] = Player.new("Player", []).bankroll
end

def dealer_hand
Player.new("Dealer", session[:dealer_cards]).hand
end

def player_hand
Player.new("Player", session[:player_cards]).hand
end

def deal_dealer
session[:dealer_cards] << Deck.new(session[:deck]).pop
end

def deal_player
session[:player_cards] << Deck.new(session[:deck]).pop
end

def player_busted?
Player.new("Player", session[:player_cards]).busted?
end

def dealer_busted?
Player.new("Dealer", session[:dealer_cards]).busted?
end

def player_count
Player.new("Player", session[:player_cards]).calculate_total
end

def dealer_count
Player.new("Dealer", session[:dealer_cards]).calculate_total
end

def player_blackjack
Player.new("Player", session[:player_cards]).calculate_total == 21
end

def dealer_blackjack
Player.new("Player", session[:dealer_cards]).calculate_total == 21
end

def busted_player
return "Player" if player_busted?
return "Dealer" if dealer_busted?
end

def set_player_bet(amount)
session[:player_bet] = amount
session[:player_bankroll] = session[:player_bankroll].to_i - amount.to_i
end

def valid_bet(amount)
amount <= Player.new("Player", session[:player_cards]).bankroll
end

def get_player_bet
session[:player_bet]
end

def player_bankroll
session[:player_bankroll]
end

def compare_hands
pl_cnt = player_count
dl_cnt = dealer_count
if pl_cnt > 21
"Dealer"
elsif dl_cnt > 21
session[:player_bankroll] += 2*session[:player_bet]
"Player"
elsif pl_cnt > dl_cnt
session[:player_bankroll] += 2*session[:player_bet]
session[:player_bankroll] += session[:player_bet] if player_blackjack
"Player"
elsif pl_cnt < dl_cnt
"Dealer"
else
session[:player_bankroll] += session[:player_bet]
nil
end
end

def card_image(card)
suit = case card[0]
when 'H' then 'hearts'
when 'D' then 'diamonds'
when 'S' then 'spades'
when 'C' then 'clubs'
end

value = card[1]
if ['J', 'Q', 'K', 'A'].include?(value)
value = case value
when 'J' then 'jack'
when 'Q' then 'queen'
when 'K' then 'king'
when 'A' then 'ace'
end
end
"<img src='/images/cards/#{suit}_#{value}.jpg' class=card_image/>"
end

end
41 changes: 41 additions & 0 deletions player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'pry'
class Player
attr_accessor :name, :hand, :bankroll
def initialize(name, hand)
@name = name
@hand = hand
@bankroll = 1000
end

def bet(amount)
@bet = amount
end

def deal(deck)
@hand << deck.pop
end

def busted?
calculate_total > 21
end

def calculate_total
lcl_hand = hand
arr = lcl_hand.map{|e| e[1] }
total = 0
arr.each do |value|
if value == "A"
total += 11
elsif value.to_i == 0 # J, Q, K
total += 10
else
total += value.to_i
end
end
#correct for Aces
arr.select{|e| e == "A"}.count.times do
total -= 10 if total > 21
end
total
end
end
Binary file added public/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_ace.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_jack.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_king.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/clubs_queen.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/cover.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cards/diamonds_ace.jpg
Binary file added public/images/cards/diamonds_jack.jpg
Binary file added public/images/cards/diamonds_king.jpg
Binary file added public/images/cards/diamonds_queen.jpg
Binary file added public/images/cards/hearts_10.jpg
Binary file added public/images/cards/hearts_2.jpg
Binary file added public/images/cards/hearts_3.jpg
Binary file added public/images/cards/hearts_4.jpg
Binary file added public/images/cards/hearts_5.jpg
Binary file added public/images/cards/hearts_6.jpg
Binary file added public/images/cards/hearts_7.jpg
Binary file added public/images/cards/hearts_8.jpg
Binary file added public/images/cards/hearts_9.jpg
Binary file added public/images/cards/hearts_ace.jpg
Binary file added public/images/cards/hearts_jack.jpg
Binary file added public/images/cards/hearts_king.jpg
Binary file added public/images/cards/hearts_queen.jpg
Binary file added public/images/cards/joker_black.jpg
Binary file added public/images/cards/joker_red.jpg
Binary file added public/images/cards/spades_10.jpg
Binary file added public/images/cards/spades_2.jpg
Binary file added public/images/cards/spades_3.jpg
Binary file added public/images/cards/spades_4.jpg
Binary file added public/images/cards/spades_5.jpg
Binary file added public/images/cards/spades_6.jpg
Binary file added public/images/cards/spades_7.jpg
Binary file added public/images/cards/spades_8.jpg
Binary file added public/images/cards/spades_9.jpg
Binary file added public/images/cards/spades_ace.jpg
Binary file added public/images/cards/spades_jack.jpg
Binary file added public/images/cards/spades_king.jpg
Binary file added public/images/cards/spades_queen.jpg
48 changes: 48 additions & 0 deletions public/reset.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
HTML5 display-role reset for older browsers
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
32 changes: 32 additions & 0 deletions public/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
body {
background-color: #ccc;
font-family: helvetica, Arial, sans-serif;
}
.my-navbar {
margin-bottom: 10px;
}
.icon {
width: 32px;
height: 32px;
}
nav p {
display: inline-block;
margin-top: 12px;
font-size: 18px;
font-weight: bold;
}
.brand {
margin-top: 8px;
padding-top: 0;
}
.right {
float: right;
}

.bet-button {
margin-top: 10px;
}

img.card_image {
margin: 0px 5px 5px 0px;
}
Loading