Skip to content

Instantly share code, notes, and snippets.

@gkilmain
Created August 19, 2014 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkilmain/1c5e955358ba73f92087 to your computer and use it in GitHub Desktop.
Save gkilmain/1c5e955358ba73f92087 to your computer and use it in GitHub Desktop.
test.js
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="css/test.css">
</head>
<body>
<div class="container">
<div id="dealersHand" class="cf"></div>
<div id="playersHand" class="cf"></div>
<div id="buttonWrapper" class="buttons-wrapper">
</div>
</div>
<script src="js/test.js"></script>
</body>
</html>
// Creates a Call To Action div
// Add button class to create basic button style
// @params c - the class
// @params i - the id e.g. ctaHit, ctaStand etc
// @params t - the text
function ctaGenerator(c, i, t) {
var cta = document.createElement('div');
cta.setAttribute('class', c);
cta.setAttribute('id', i);
var textNode = document.createTextNode(t);
cta.appendChild(textNode);
return cta;
}
// Generates one card
// @appendCardTo - append card to id dealerHand or playerHand
// @value - number value of the card
function cardGenerator(appendCardTo, value) {
var theCard = document.createElement('div');
theCard.setAttribute('class', 'card');
var theCardValue = document.createTextNode(value);
theCard.appendChild(theCardValue);
// Append it to player or dealer
var parentEle = document.getElementById(appendCardTo);
parentEle.appendChild(theCard);
}
var defaultButtons = [
{
buttonClass: 'button',
buttonId: 'ctaHit',
buttonText: 'Hit'
},
{
buttonClass: 'button',
buttonId: 'ctaStand',
buttonText: 'Stand'
},
{
buttonClass: 'button',
buttonId: 'ctaDouble',
buttonText: 'Double'
}
];
function showDefaultButtons(obj) {
console.log('showDefaultButtons called......');
for ( i = 0; i < obj.length; i++ ) {
ctaGenerator(obj[i].buttonClass, obj[i].buttonId, obj[i].buttonText);
}
}
function deal(name) {
var deck = [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,11,11,11,11];
console.log('Hello' + name);
console.log('Dealing cards...');
// Dealer gets two cards
for ( i = 0; i < 2; i++ ) {
var card = deck[Math.floor(Math.random()*deck.length)];
cardGenerator('dealersHand',card);
}
// Player gets two cards
for ( i = 0; i < 2; i++ ) {
var card = deck[Math.floor(Math.random()*deck.length)];
cardGenerator('playersHand',card);
}
showDefaultButtons(defaultButtons);
}
window.onload = function() {
// Parent div
var parentElement = document.getElementById('buttonWrapper');
// Create button
var startButton = ctaGenerator('button', 'ctaStart', 'Start');
parentElement.appendChild(startButton);
// Show the name input element
startButton.onclick = function() {
// Hide the start button
startButton.style.display = 'none';
// Create input element
var inputEle = document.createElement('input');
inputEle.setAttribute('class', 'player-name');
// append it
parentElement.appendChild(inputEle);
var submitName = ctaGenerator('button', 'ctaSubmit', 'Enter');
parentElement.appendChild(submitName);
submitName.onclick = function() {
var playerName = inputEle.value;
submitName.style.display = 'none';
inputEle.style.display = 'none';
deal(playerName);
}
}
}
// Click Start game button
// Remove start game button
// Show input box
// Enter in your name
// Click done
// Click deal button
// Deal cards
// Remove deal button
// Show player action buttons hit, stand, double, surrender
// if both hole cards are the same show split button
// == Actions
// = Hit
// Get a card from the deck
// Evaluate player hand and return score
// If score is > 21 alert the player has busted
// = Remove buttons
// = Dealers turn
// If score is <= 21
// = Show player action buttons
//
// = Stand
// Store player score in a variable to be compared to dealer score
// Dealers turn
//
// = Double
// get the current bet amount and double it.
//
// = Surrender
// Show dealer hand
// Reduce score by half amount bet
// End game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment