Javascript Blackjack Github
Functional Blackjack Implementation on JavaScript. GitHub Gist: instantly share code, notes, and snippets. Cout blackjack are fairly simple and your only opponent in the game is the dealer. Each player is dealt a two cards face up while the dealer only has one card face up. The goal is to have your two cards added total to be higher than the dealer’s two cards and under 21. Modesta Naciute blackjack card game Javascript project. Checking if an unfinished game exists on the server. Please grab a coffee. JavaScript HTML Game from Scratch Blackjack Learn how to create a web application from scratch.
A javascript class representing a game of Blackjack
blackjack.js
consthttps=require('https') |
module.exports=classBlackJackGame{ |
constructor(){ |
this.game={ |
deckId: null, |
remaining: null, |
player: { |
hand: [], |
count: 0 |
}, |
dealer: { |
hand: [], |
count: 0 |
}, |
} |
} |
newDeck(){ |
returnnewPromise((resolve,reject)=>{ |
https.get('https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1',(response)=>{ |
let_body=' |
response.on('data',(data)=>{ |
_body+=data |
}) |
response.on('end',()=>{ |
this.game.deckId=JSON.parse(_body).deck_id |
console.log(`New Deck Created ${this.game.deckId}`) |
resolve() |
}) |
}) |
}) |
} |
dealCard(dealer =false){ |
if(this.game.deckId){ |
returnnewPromise((resolve,reject)=>{ |
https.get(`https://deckofcardsapi.com/api/deck/${this.game.deckId}/draw/?count=1`,(response)=>{ |
let_body=' |
response.on('data',(data)=>{ |
_body+=data |
}) |
response.on('end',()=>{ |
const_response=JSON.parse(_body) |
if(dealer){ |
this.assignCard('dealer',_response) |
}else{ |
this.assignCard('player',_response) |
} |
this.remaining=_response.remaining |
resolve(_response.cards[0]) |
}) |
}) |
}) |
} |
} |
assignCard(person,response){ |
const_card=response.cards[0] |
const{ code }=_card |
const_cardValue=this.getValueOfCard(_card) |
this.game[person].hand.push(code) |
this.game[person].count+=_cardValue |
console.log(`Assigning card ${code} to ${person}`) |
} |
getValueOfCard(card){ |
constVALUES_MAPPED={ |
JACK: 10, |
QUEEN: 10, |
KING: 10, |
ACE: 10, |
} |
const_value=card.value |
const_integer=parseInt(_value) |
return_integer ? _integer : VALUES_MAPPED[_value] |
} |
getPlayerHand(person){ |
returnthis.game[person].hand |
} |
getPlayerHandCount(person){ |
returnthis.game[person].count |
} |
getWinner(){ |
const_playerCount=this.getPlayerHandCount('player') |
const_dealerCount=this.getPlayerHandCount('dealer') |
return_playerCount>_dealerCount ? 'player' : 'dealer' |
} |
isPersonBust(person){ |
returnthis.getPlayerHandCount(person)>21 |
} |
isThereWinner(){ |
if(this.isPersonBust('dealer')){ |
console.log('Dealer Busts') |
return{player: this.game.player.count} |
}elseif(this.isPersonBust('player')){ |
console.log('Player Busts') |
return{dealer: this.game.dealer.count} |
}else{ |
console.log('No Winner Yet') |
return{ |
none: { |
player: this.game.player.count, |
dealer: this.game.dealer.count |
} |
} |
} |
} |
} |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Minecraft Javascript Github
Blackjack
/*Object Oriented Blackjack |
A Game has: Players |
Dealer |
Deck |
A Player / Dealer has: Score |
Cards |
A Score has: Game Logic |
Current Score |
A Deck has: Cards |
*/ |
function Game() { |
this.currentTurnIndex = 0; |
this.deck = new Deck(); |
} |
function Deck() { |
this.cards = []; |
this.cardsDrawn = 0; |
var suits = ['spades', 'diamonds', 'hearts', 'clubs']; |
var names = ['ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king']; |
for (var suit in suits) { |
for (var name in names) { |
this.cards.push(new Card(names[name], suits[suit])); |
} |
} |
} |
Deck.prototype.getCard = function () { |
if (this.cards.length this.cardsDrawn) { |
return null; |
} //case: check if all cards drawn |
var random = Math.floor(Math.random() * (this.cards.length - this.cardsDrawn)); |
var temp = this.cards[random]; |
//swap chosen card with card last in array |
this.cards[random] = this.cards[this.cards.length - this.cardsDrawn - 1]; |
this.cards[this.cards.length - this.cardsDrawn - 1] = temp; |
this.cardsDrawn++; |
return temp; |
}; |
function Card(name, suit) { |
this.name = name; |
this.suit = suit; |
} |
Card.prototype.image = function () { |
return 'http://www.jonarnaldo.com/sandbox/deck_images/' + name + '_of_' + suit + '.png'; |
}; |
Card.prototype.value = function () { |
if (this.name 'jack' 'queen' 'king') { |
return [10]; |
} else if (this.name 'ace') { |
return [1, 11]; |
} else { |
return parseInt(this.name, 10); |
} |
}; |
function Player() { |
//this.name; |
this.cards = []; |
} |
Player.prototype.addCard = function () { |
this.cards.push(deck.getCard()); |
}; |
Player.prototype.score = function () { |
var score = 0; |
var aces = []; |
for (var i = 0; i < this.cards.length; i++) { |
var value = this.cards[i].value() // value array ex.[10] |
if (value.length 1) { |
score += value[0]; |
} else { |
aces.push(value); |
} |
} |
for (var j = 0; j < aces.length; j++) { |
if (score + aces[j].value[1] <= 21) { |
score + aces[j].value[1]; |
} else { |
score + aces[j].value[0]; |
} |
} |
return score; |
}; |
var deck = new Deck(); |
var player1 = new Player(); |
$('#getCard').click(function () { |
player1.addCard(); |
var cardName = player1.cards[player1.cards.length-1].name; |
var cardSuit = player1.cards[player1.cards.length-1].suit; |
$('#table').append(cardName + cardSuit); |
}); |
commented Jun 13, 2017
My coding bootcamp is making me code a Blackjack Hand Calculator. I have to turn it in soon. I'm failing. JavaScipt is fking me. |
Github Javascript Examples
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment