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
Branches master
No related merge requests found
const gameState = { const gameState = {
deck: null,
playerWallet: 100, playerWallet: 100,
currentBet: null, currentBet: null,
cards: [[], []], cards: [[], []],
hiddenCard: null 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(){ function collectBet(){
let currentBet; let currentBet;
let isValidBet = false; let isValidBet = false;
...@@ -32,8 +104,12 @@ function drawCard(){ ...@@ -32,8 +104,12 @@ function drawCard(){
return rndNum; return rndNum;
} }
function handValue(cards){ function handValue(cardsObj){
let sum = 0; let sum = 0;
let cards = [];
for (let i = 0; i < cardsObj.length; i++){
cards.push(cardsObj[i].getValue());
}
if (cards.length > 0) { if (cards.length > 0) {
sum = cards.reduce( sum = cards.reduce(
(accumulator, item) => accumulator + item (accumulator, item) => accumulator + item
...@@ -58,6 +134,7 @@ function handValue(cards){ ...@@ -58,6 +134,7 @@ function handValue(cards){
} }
function resetGameState(){ function resetGameState(){
gameState.deck = new Deck();
gameState.currentBet = null; gameState.currentBet = null;
gameState.cards = [[], []]; gameState.cards = [[], []];
gameState.hiddenCard = null; gameState.hiddenCard = null;
...@@ -77,7 +154,7 @@ function updateGUI(){ ...@@ -77,7 +154,7 @@ function updateGUI(){
} }
// set values of player's cards and dealer's cards in each cell // set values of player's cards and dealer's cards in each cell
for (let i = 0; i < gameState.cards[k].length; i++){ 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 curScore = handValue(gameState.cards[k]);
let scoreElement = document.getElementById(scoreIds[k]); let scoreElement = document.getElementById(scoreIds[k]);
...@@ -114,11 +191,11 @@ function collectBetAndDealCards(){ ...@@ -114,11 +191,11 @@ function collectBetAndDealCards(){
// one is hidden // one is hidden
for(let playerIndex = 0; playerIndex < gameState.cards.length; playerIndex++){ for(let playerIndex = 0; playerIndex < gameState.cards.length; playerIndex++){
for(let cardIndex = 0; cardIndex < 2; cardIndex++){ for(let cardIndex = 0; cardIndex < 2; cardIndex++){
let newCard = gameState.deck.dealCard();
if (cardIndex === 1 && playerIndex === 1){ if (cardIndex === 1 && playerIndex === 1){
gameState.hiddenCard = drawCard(); newCard.setCardVisibility(false);
} else {
gameState.cards[playerIndex][cardIndex] = drawCard();
} }
gameState.cards[playerIndex][cardIndex] = newCard;
} }
} }
...@@ -145,7 +222,7 @@ function log(message){ ...@@ -145,7 +222,7 @@ function log(message){
} }
function hit(){ function hit(){
let curCard = drawCard(); let curCard = gameState.deck.dealCard();
gameState.cards[0].push(curCard); gameState.cards[0].push(curCard);
let curPlayerScore = handValue(gameState.cards[0]); let curPlayerScore = handValue(gameState.cards[0]);
updateGUI() updateGUI()
...@@ -161,8 +238,9 @@ function stay(){ ...@@ -161,8 +238,9 @@ function stay(){
let buttonHit = document.getElementById('button-hit'); let buttonHit = document.getElementById('button-hit');
buttonHit.disabled = true; buttonHit.disabled = true;
gameState.cards[1].push(gameState.hiddenCard); for (let i = 0; i < gameState.cards[1].length; i++){
gameState.hiddenCard = null; gameState.cards[1][i].setCardVisibility(true);
}
let curPlayerScore = handValue(gameState.cards[0]); let curPlayerScore = handValue(gameState.cards[0]);
let curDealerScore = handValue(gameState.cards[1]); let curDealerScore = handValue(gameState.cards[1]);
...@@ -174,7 +252,7 @@ function stay(){ ...@@ -174,7 +252,7 @@ function stay(){
// dealer draws a card // dealer draws a card
// --- UNTIL THE DEALER has 17 or more // --- UNTIL THE DEALER has 17 or more
while(curDealerScore < 17){ while(curDealerScore < 17){
let curCard = drawCard(); let curCard = gameState.deck.dealCard();
console.log(`The dealer got a new card with value ${curCard}`); console.log(`The dealer got a new card with value ${curCard}`);
gameState.cards[1].push(curCard); gameState.cards[1].push(curCard);
curDealerScore = handValue(gameState.cards[1]); curDealerScore = handValue(gameState.cards[1]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment