Skip to content
Snippets Groups Projects
Commit 694a9cab authored by Jonathan Vitale's avatar Jonathan Vitale
Browse files

Final code for the game

parent 89904609
No related branches found
No related tags found
No related merge requests found
const gameState = {
deck: null,
playerWallet: 100,
currentBet: null,
cards: [[], []],
hiddenCard: null
}
class Card{
constructor(face, suit){
this.face = face;
this.suit = suit;
this.visible = true;
}
setCardVisibility(visibility){
this.visible = visibility;
}
getValue(){
if (this.visible === true){
if (!isNaN(Number(this.face))){
return Number(this.face);
} else if (this.face === 'ace'){
return 11;
} else {
return 10;
}
} else {
return 0;
}
}
draw(){
if (this.visible){
return `<img style="background-color: white; width: 70px;" src="cards/${this.face}_of_${this.suit}.png" />`;
} else {
return `<img style="background-color: white; width: 70px;" src="cards/back.png" />`
}
}
}
class Deck{
constructor(){
this.buildDeck();
this.shuffle();
}
buildDeck(){
this.deck = [];
let suits = ['hearts', 'clubs', 'spades', 'diamonds'];
let faces = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'jack', 'queen', 'king', 'ace'];
for (let i = 0; i < suits.length; i++){
for (let j = 0; j < faces.length; j++){
this.deck.push(new Card(faces[j], suits[i]));
}
}
}
shuffle(){
/* Randomize array in-place using Durstenfeld shuffle algorithm */
for (let i = this.deck.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let temp = this.deck[i];
this.deck[i] = this.deck[j];
this.deck[j] = temp;
}
}
dealCard(){
let newCard = this.deck.pop();
if (this.deck.length <= 26){
this.buildDeck();
this.shuffle();
}
return newCard;
}
}
function collectBet(){
let currentBet;
let isValidBet = false;
......@@ -32,8 +104,12 @@ function drawCard(){
return rndNum;
}
function handValue(cards){
function handValue(cardsObj){
let sum = 0;
let cards = [];
for (let i = 0; i < cardsObj.length; i++){
cards.push(cardsObj[i].getValue());
}
if (cards.length > 0) {
sum = cards.reduce(
(accumulator, item) => accumulator + item
......@@ -58,6 +134,7 @@ function handValue(cards){
}
function resetGameState(){
gameState.deck = new Deck();
gameState.currentBet = null;
gameState.cards = [[], []];
gameState.hiddenCard = null;
......@@ -77,7 +154,7 @@ function updateGUI(){
}
// set values of player's cards and dealer's cards in each cell
for (let i = 0; i < gameState.cards[k].length; i++){
cells[i].innerHTML = gameState.cards[k][i];
cells[i].innerHTML = gameState.cards[k][i].draw();
}
let curScore = handValue(gameState.cards[k]);
let scoreElement = document.getElementById(scoreIds[k]);
......@@ -114,11 +191,11 @@ function collectBetAndDealCards(){
// one is hidden
for(let playerIndex = 0; playerIndex < gameState.cards.length; playerIndex++){
for(let cardIndex = 0; cardIndex < 2; cardIndex++){
let newCard = gameState.deck.dealCard();
if (cardIndex === 1 && playerIndex === 1){
gameState.hiddenCard = drawCard();
} else {
gameState.cards[playerIndex][cardIndex] = drawCard();
newCard.setCardVisibility(false);
}
gameState.cards[playerIndex][cardIndex] = newCard;
}
}
......@@ -145,7 +222,7 @@ function log(message){
}
function hit(){
let curCard = drawCard();
let curCard = gameState.deck.dealCard();
gameState.cards[0].push(curCard);
let curPlayerScore = handValue(gameState.cards[0]);
updateGUI()
......@@ -161,8 +238,9 @@ function stay(){
let buttonHit = document.getElementById('button-hit');
buttonHit.disabled = true;
gameState.cards[1].push(gameState.hiddenCard);
gameState.hiddenCard = null;
for (let i = 0; i < gameState.cards[1].length; i++){
gameState.cards[1][i].setCardVisibility(true);
}
let curPlayerScore = handValue(gameState.cards[0]);
let curDealerScore = handValue(gameState.cards[1]);
......@@ -174,7 +252,7 @@ function stay(){
// dealer draws a card
// --- UNTIL THE DEALER has 17 or more
while(curDealerScore < 17){
let curCard = drawCard();
let curCard = gameState.deck.dealCard();
console.log(`The dealer got a new card with value ${curCard}`);
gameState.cards[1].push(curCard);
curDealerScore = handValue(gameState.cards[1]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment