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 'http://www.rubygems.org'

gem 'sinatra'
#gem 'reloader'

group :development do
gem 'pry-byebug'

end
34 changes: 34 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
GEM
remote: http://www.rubygems.org/
specs:
byebug (9.0.6)
coderay (1.1.1)
method_source (0.8.2)
mustermann (1.0.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.4.2)
byebug (~> 9.0)
pry (~> 0.10)
rack (2.0.2)
rack-protection (2.0.0)
rack
sinatra (2.0.0)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.0)
tilt (~> 2.0)
slop (3.6.0)
tilt (2.0.7)

PLATFORMS
ruby

DEPENDENCIES
pry-byebug
sinatra

BUNDLED WITH
1.14.6
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
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)

Chad Lucas
28 changes: 28 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'sinatra'
#require 'sinatra/reloader' if development?
require './helpers/blackjack_helper.rb'
require './blackjack.rb'

helpers BlackjackHelper
enable :sessions

get '/' do
"hello world"
erb :home
end

get '/blackjack' do
game = Blackjack.new
cards = game.deal_cards
sessions[:player_hand] = game.get_player_score
erb :"blackjack", :locals => {:cards => cards}

end

post '/blackjack' do
if params[:hit])
game.add_card
end
redirect to("blackjack")

end
99 changes: 99 additions & 0 deletions blackjack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'pry'
class Blackjack
attr_accessor :deck, :player_hand, :computer_hand, :player_score, :sum
SUIT = %w(clubs diamonds spades hearts)
VALUE = %w(2 3 4 5 6 7 8 9 10 J Q K A)
#create card struct with suit & value
Card = Struct.new(:suit, :value)
#sets up player and computer hands
def initialize
build_deck
@player_hand = []
@computer_hand = []
@sum = 0
end
#iterates through suit and value and creates card struct
#and puts them into array
def build_deck
@deck = []
SUIT.each do |type|
VALUE.each do |val|
@card = Card.new(type, val)
@deck << @card
end
end
@deck
end

def deal_cards
deal_player_hand
deal_computer_hand
end

def deal_player_hand
0...2.times do
@deck.shuffle!
@player_hand << @deck.pop
end
@player_hand
end

def deal_computer_hand
0...2.times do
@computer_hand << @deck.pop
end
@computer_hand
end


def get_player_score

@player_hand.map do |card|

card.value = check_if_face_card_or_ace?(card)

@player_score = sum_up_cards(@player_hand)
end
@player_score
end

def get_computer_score
@computer_hand.map do |card|
card.value = check_if_face_card_or_ace(card)
end
@computer_score = sum_up_cards(@computer_hand)
end


def check_if_face_card_or_ace?(card)
if card.value == "J" || card.value == "Q" || card.value == "K"
card.value = 10
elsif card.value == "A" && @sum < 21
card.value = 11
end
card.value
end

def sum_up_cards(cards)
@sum = 0

cards.map do |card|
@sum += card.value.to_i
end
@sum

end





end


game = Blackjack.new

game.deal_cards
game.get_player_score


4 changes: 4 additions & 0 deletions helpers/blackjack_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module BlackjackHelper


end
10 changes: 10 additions & 0 deletions views/blackjack.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Blackjack</h1>

<%= cards %>

<form action="/blackjack" method="post">
<input type="radio" name="hit" value="hit">hit
<input type="radio" name="stay" value="stay">stay
<br>
<input type ="submit" value="Submit">
</form>
1 change: 1 addition & 0 deletions views/home.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Welcome to Blackjack</h1>
11 changes: 11 additions & 0 deletions views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<title>Blackjack</title>
</head>

<body>
<%= erb :"shared/navbar" %>
<%= yield %>
</body>
</html>
7 changes: 7 additions & 0 deletions views/shared/navbar.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blackjack">Blackjack</a></li>

</ul>
</nav>