Created
August 26, 2014 20:04
-
-
Save gkilmain/cf9031a1e1f4d1293136 to your computer and use it in GitHub Desktop.
JS BJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.card { | |
width: 50px; | |
height: 100px; | |
border: 1px solid #ccc; | |
border-radius: 4px; | |
float: left; | |
margin: 5px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<link rel="stylesheet" type="text/css" href="css/test2.css"> | |
</head> | |
<body> | |
<div id="bank"></div> | |
<input type="number" id="betAmount"> | |
<div id="gamesPlayed"></div> | |
<div id="player"></div> | |
<div id="dealer"></div> | |
<script src="js/test2.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
function dealerTurn() { | |
var hand_value = evalHand('dealer'); | |
if (hand_value < 17) { | |
// take a card | |
} else if (hand_value >= 17 && hand_value <=20) { | |
// determine winner | |
} else if (hand_value === 21) { // Dealer might be delt 21 | |
// determine winner | |
} else { | |
// Dealer busts | |
// Player wins | |
} | |
} | |
function evalHand(handToEval) { | |
// loops through the cards in the, sums them up, and returns the value | |
var hand = document.getElementById(handToEval); | |
var cards_in_hand = hand.getElementsByTagName('div'); | |
var hand_value = 0; // Reset hand to zero so that the value is fresh | |
for (var i = 0; i < cards_in_hand.length; i++) { | |
hand_value += parseInt(cards_in_hand[i].firstChild.nodeValue); | |
} | |
return hand_value; | |
} | |
function cardGenerator(appendCardTo, value) { | |
var theCard = document.createElement('div'); | |
var theCardValue = document.createTextNode(value); | |
var parentEle = document.getElementById(appendCardTo); | |
theCard.setAttribute('class', 'card'); | |
theCard.appendChild(theCardValue); | |
parentEle.appendChild(theCard); | |
} | |
// Shuffle Array http://bost.ocks.org/mike/shuffle/ | |
function shuffle(array) { | |
var m = array.length, t, i; | |
// While there remain elements to shuffle… | |
while (m) { | |
// Pick a remaining element… | |
i = Math.floor(Math.random() * m--); | |
// And swap it with the current element. | |
t = array[m]; | |
array[m] = array[i]; | |
array[i] = t; | |
} | |
return array; | |
} | |
var deck = { | |
oneDeck: [2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,'a','a','a','a'] | |
} | |
function Player (name,bank,deck) { | |
this.name = name; | |
this.bank = bank; | |
this.deck = deck; | |
// set bank | |
document.getElementById('bank').innerHTML = this.bank; | |
} | |
Player.prototype.deal = function(whom) { | |
console.log('Deal called. Array length = ' + this.deck.oneDeck.length); | |
var shuffledDeck = shuffle(this.deck.oneDeck); | |
var startingHand = shuffledDeck.splice(0,2); | |
// Ace check | |
if (startingHand[0] === 'a' && startingHand[0] === 'a') { // You've been delt two aces | |
startingHand[0] = 11; | |
startingHand[1] = 1; | |
} | |
for (var i = 0; i < startingHand.length; i++) { | |
cardGenerator(whom, startingHand[i]); | |
} | |
// Check for 21 | |
var hand_value = evalHand(whom); | |
if (hand_value === 21) { | |
dealerTurn(); | |
} | |
} | |
Player.prototype.hit = function() { | |
var cardValue = document.createTextNode(this.deck.oneDeck.shift()); | |
var playerHand = document.getElementById('player'); | |
var theCard = document.createElement('div'); | |
theCard.className = 'card'; | |
theCard.appendChild(cardValue); | |
playerHand.appendChild(theCard); | |
console.log('Hit called. Array length = ' + this.deck.oneDeck.length); | |
return this.deck.oneDeck.shift(); | |
} | |
Player.prototype.determine_winner = function() { | |
console.log('dw called....'); | |
// if player wins | |
// increase bank | |
this.increase_bank(); | |
// else | |
// this.decrease() | |
//console.log(this.bank); | |
} | |
Player.prototype.bust = function() { | |
var busted = false; | |
// if number of cards is > 2 | |
} | |
Player.prototype.increase_bank = function() { | |
var result = this.bank + this.bet_amount(); | |
this.bank = result; | |
} | |
Player.prototype.decrease_bank = function() { | |
} | |
Player.prototype.bet_amount = function() { | |
var betValue = document.getElementById('betAmount').value; | |
var result = parseInt(betValue); | |
return result; | |
} | |
var player1 = new Player('George', 5000, deck); | |
player1.deal('player'); | |
// on deal lets use splice | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment