Skip to content

Commit

Permalink
Added betting and AJAX for player hit/stay and dealer next
Browse files Browse the repository at this point in the history
  • Loading branch information
asatwal committed May 26, 2014
1 parent 8259b89 commit ae34529
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 130 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion card_game_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module CardGameUtils
BLACKJACK_SCORE = 21
DEALER_MIN_SCORE = 17
DEALER_NAME = 'Dealer'

PLAYER_INITIAL_FUNDS = 1000
end
200 changes: 124 additions & 76 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,104 +18,105 @@ def get_image_name(card)
end


def display_winner(dealer, player)

if (dealer.total_score > player.total_score )
@error = "Dealer has won #{player.name}!"
elsif (dealer.total_score < player.total_score )
@success = "#{player.name} has won Dealer!"
else
@success = "#{player.name} and Dealer draw!"
def display_winner?(player, dealer = nil)

dealer_win = false
player_win = false

if (player.total_score == CardGameUtils::BLACKJACK_SCORE)
@winner_msg = "Blackjack! #{player.name} Won! Well done."
player_win = true
elsif (player.total_score > CardGameUtils::BLACKJACK_SCORE)
@loser_msg = "#{player.name} Lost! You have busted."
dealer_win = true
elsif !dealer.nil? && dealer.total_score >= CardGameUtils::DEALER_MIN_SCORE
if (dealer.total_score > CardGameUtils::BLACKJACK_SCORE)
@winner_msg = "#{player.name} Won! Dealer has busted."
player_win = true
elsif (dealer.total_score == CardGameUtils::BLACKJACK_SCORE)
@loser_msg = "Blackjack! Dealer Won! #{player.name} Lost."
dealer_win = true
elsif (dealer.total_score > player.total_score)
@loser_msg = "Dealer Won! #{player.name} Lost."
dealer_win = true
elsif (dealer.total_score < player.total_score)
@winner_msg = "#{player.name} Won! Dealer Lost."
player_win = true
else
@winner_msg = "#{player.name} and Dealer draw!"
# No winners but must retrun sucess here to control flow
return true
end
end

if player_win
player.funds += session[:player_bet].to_i
elsif dealer_win
player.funds -= session[:player_bet].to_i
end

return dealer_win || player_win
end

end

before do
@show_player_buttons = false
@show_dealer_buttons = false
@show_dealer_first_card = false
@player_turn = false
@dealer_turn = false
end

get '/' do

if session[:player_name]
redirect '/player/game'
if session[:player]
redirect '/player/bet'
else
redirect '/player/new'
end
end


get '/player/game' do

deck = Deck.new

3.times { deck.shuffle }

player = Player.new(session[:player_name])
player = Marshal.load(session[:player])
dealer = Player.new(CardGameUtils::DEALER_NAME)

player.clear_cards

2.times do
player.hit(deck.deal)
dealer.hit(deck.deal)
end

session[:player] = Marshal.dump(player)
@player_turn = true unless display_winner?(player)

session[:deck] = Marshal.dump(deck)
session[:dealer] = Marshal.dump(dealer)

if (player.total_score == CardGameUtils::BLACKJACK_SCORE)
@success = "Blackjack! Well done #{player.name}! You have won"
else
@show_player_buttons = true
end
session[:player] = Marshal.dump(player)

erb :play_game

end

get '/player/new' do
erb :player_new
end

post '/player/new' do

if params[:player_name].empty?
@error = "Name is required"
halt erb :player_new
end

session[:player_name] = params[:player_name]
redirect '/player/game'
end

get '/player/game/new' do
session.clear
redirect '/player/new'
end

post '/player/game/hit' do

player = Marshal.load(session[:player])
deck = Marshal.load(session[:deck])

player.hit(deck.deal)

unless display_winner?(player)
@player_turn = true
@player_msg = "#{player.name} has just hit."
end

session[:player] = Marshal.dump(player)
session[:deck] = Marshal.dump(deck)

if (player.total_score > CardGameUtils::BLACKJACK_SCORE)
@error = "Sorry #{player.name}! You have busted!"
@show_dealer_first_card = true
elsif (player.total_score == CardGameUtils::BLACKJACK_SCORE)
@success = "Blackjack! Well done #{player.name}! You have won"
@show_dealer_first_card = true
else
@show_player_buttons = true
@success = "#{session[:player_name]} has just hit!"
end

erb :play_game
erb :play_game, layout: false

end

Expand All @@ -125,19 +126,14 @@ def display_winner(dealer, player)
dealer = Marshal.load(session[:dealer])
player = Marshal.load(session[:player])

@show_dealer_first_card = true

if (dealer.total_score >= CardGameUtils::DEALER_MIN_SCORE)
# Game over
display_winner(dealer, player)
unless display_winner?(player, dealer)
@dealer_turn = true
@player_msg = "#{player.name} decided to stay."
else
@show_dealer_buttons = true
@success = "#{session[:player_name]} decided to stay!"
session[:player] = Marshal.dump(player)
end



erb :play_game
erb :play_game, layout: false

end

Expand All @@ -148,29 +144,81 @@ def display_winner(dealer, player)
deck = Marshal.load(session[:deck])
player = Marshal.load(session[:player])

@show_dealer_first_card = true

dealer.hit(deck.deal)

if (dealer.total_score > CardGameUtils::BLACKJACK_SCORE)
@error = "Dealer has busted!"
elsif (dealer.total_score == CardGameUtils::BLACKJACK_SCORE)
@success = "Blackjack! Dealer has won!"
elsif (dealer.total_score < CardGameUtils::DEALER_MIN_SCORE)
@success = "Dealer has just hit!"
@show_dealer_buttons = true
unless display_winner?(player, dealer)
@dealer_turn = true
@player_msg = "Dealer has just hit."
else
# Dealer min score reached - Gamover
display_winner(dealer, player)
session[:player] = Marshal.dump(player)
end

session[:dealer] = Marshal.dump(dealer)
session[:deck] = Marshal.dump(deck)

erb :play_game, layout: false

end

get '/player/bet' do
erb :player_bet
end

post '/player/bet' do

player = Marshal.load(session[:player])

if params[:player_bet].empty?
@error = "Bet amount is required"
halt erb :player_bet
elsif params[:player_bet].to_i <= 0
@error = "Bet amount must be greater than zero"
halt erb :player_bet
elsif params[:player_bet].to_i > player.funds
@error = "Bet amount must be less than your funds."
halt erb :player_bet
end

session[:player_bet] = params[:player_bet].to_i

redirect '/player/game'
end

erb :play_game

get '/player/new' do
erb :player_new
end

post '/player/new' do

if params[:player_name].empty?
@error = "Name is required"
halt erb :player_new
end

player = Player.new(params[:player_name])

session[:player] = Marshal.dump(player)

redirect '/player/bet'
end

get '/player/game/start' do
session.clear
redirect '/player/new'
end


get '/player/game/new' do
player = Marshal.load(session[:player])

player.funds = CardGameUtils::PLAYER_INITIAL_FUNDS

session[:player] = Marshal.dump(player)

redirect '/player/bet'
end




9 changes: 9 additions & 0 deletions player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ class Player

attr_accessor :name
attr_accessor :cards
attr_accessor :funds


def initialize(name)
@cards = []
@name = name
@funds = CardGameUtils::PLAYER_INITIAL_FUNDS
end


Expand All @@ -24,6 +26,13 @@ def hit(card)
end
end

def clear_cards

if !@cards.empty?
@cards.clear
end
end

def total_score
score = 0

Expand Down
20 changes: 18 additions & 2 deletions public/application.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@

form#hit_form {
form#player_hit_form {
float:left;
margin-right:10px;
}

form#stay_form {
form#player_stay_form {
float:left;
margin-right:20px;
}

form#play_again_form {
float:left;
margin-right:20px;
}

form#new_game_form {
float:left;
margin-right:20px;
}

form#bet_form {
float:left;
margin-right:20px;
}

img.card {
Expand Down
41 changes: 41 additions & 0 deletions public/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
$(document).ready(function(){
player_hit();
player_stay();
dealer_hit();
});

function player_hit() {
$(document).on("click", "form#player_hit_form input", function() {
$.ajax({
type: 'POST',
url: '/player/game/hit'
}).done(function(msg){
$("div#play_game").replaceWith(msg);
});
return false;
});
}

function player_stay() {
$(document).on("click", "form#player_stay_form input", function() {
$.ajax({
type: 'POST',
url: '/player/game/stay'
}).done(function(msg){
$("div#play_game").replaceWith(msg);
});
return false;
});
}

function dealer_hit() {
$(document).on("click", "form#dealer_hit_form input", function() {
$.ajax({
type: 'POST',
url: '/dealer/game/hit'
}).done(function(msg){
$("div#play_game").replaceWith(msg);
});
return false;
});
}
Binary file added views/.DS_Store
Binary file not shown.
Loading

0 comments on commit ae34529

Please sign in to comment.