Skip to content

Week 4 assignment Joann Cahill #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 132 additions & 10 deletions blackjack.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
const blackjackDeck = getDeck();


/**
* Represents a card player (including dealer).
* @constructor
* @param {string} name - The name of the player
*/
class CardPlayer {}; //TODO

// CREATE TWO NEW CardPlayers
const dealer; // TODO
const player; // TODO
class CardPlayer {
constructor(name) {
this.name = name;
this.hand = [];
this.drawCard = function () {
let randomCard = deck[Math.floor(Math.random() * 52)];
this.hand.push(randomCard);
console.log(randomCard);
//end of drawCard function

};
//end of CardPlayer constructor

};


introduce() {
console.log(`Card Player is ${this.name}. Card Play hand includes: ${this.hand}`);
//end of introduce function
};

//end of CardPlayer class
}; //TODO



//CREATE TWO NEW CardPlayers
const dealer = new CardPlayer('Dealer Jane'); // TODO

const player = new CardPlayer('Player John'); // TODO

player.introduce();

dealer.introduce();


/**
* Calculates the score of a Blackjack hand
Expand All @@ -18,9 +50,72 @@ const player; // TODO
* @returns {number} blackJackScore.total
* @returns {boolean} blackJackScore.isSoft
*/


// TODO
const calcPoints = (hand) => {
// CREATE FUNCTION HERE

let blackJackScore = {
total: 0,
isSoft: false
}
//isSoft - boolean - true if there is an Ace in the hand that is being counted as 11 points.
//false if the hand has no Aces, or if all Aces are counting as 1 point

//determine whether the hand is soft
const isSoft = function (hand) {
//if every in the hand is not an ace, then the hand is not soft
if (hand.every((card) => card.displayVal !== 'Ace')) {
blackJackScore.isSoft = false;
} else if (hand.some((card) => card.displayVal === 'Ace')) {
//if some cards are an ace but no card has a value of 11 then hand is not soft
if (hand.val !== 11) {
blackJackScore.isSoft = false;
}
//if any card has a value of eleven there must be at least one Ace making the hand soft
else if (hand.val === 11) {
blackJackScore.isSoft = true;
}
}
//end of isSoft function
else {
blackJackScore.isSoft = 'false';
}
return (blackJackScore.isSoft);
};

//calc the total for the hand
function blackJackTotal(hand) {

blackJackScore.total = 0;

for (let i = 0; i < hand.length; i++) {
if (hand[i].displayVal === 'Ace' && hand[i].val === 11 && (blackJackScore.total + hand[i].val) > 21) {
blackJackScore.total += 1;
} else if (hand[i].displayVal === 'Ace' && hand[i].val === 11 && (blackJackScore.total + hand[i].val) <= 21) {
blackJackScore.total += hand[i].val;
} else {
blackJackScore.total += hand[i].val;
}
//end of calcPoints for loop to calc blackJackScore.total
}

//end of blackJackTotal function
return (blackJackScore.total)
}
//call the isSOft function

isSoft(hand);

//call the blackJackTotal function

blackJackTotal(hand);

//return results of the function

return blackJackScore;

//end of calPoints function
}

/**
Expand All @@ -29,23 +124,48 @@ const calcPoints = (hand) => {
* @param {Array} dealerHand Array of card objects with val, displayVal, suit properties
* @returns {boolean} whether dealer should draw another card
*/
// debugger
//TODO
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE
let dealerPoints = calcPoints(dealerHand);


if (dealerPoints.total <= 16) {
return true;
} else if (dealerPoints.total === 17 && dealerPoints.isSoft === true) {
return true;
} else if (dealerPoints.total > 17) {
return false;
}
}


/**
* Determines the winner if both player and dealer stand
* @param {number} playerScore
* @param {number} dealerScore
* @returns {string} Shows the player's score, the dealer's score, and who wins
*/
//TODO


const determineWinner = (playerScore, dealerScore) => {
// CREATE FUNCTION HERE

}
if (dealerScore > 21) {
return `Player score was ${playerScore}. Dealer Score was ${dealerScore}. Dealer scored more than 21 - Player wins`
} else if (playerScore <= 21 && (playerScore > dealerScore)) {
return `Player score was ${playerScore}. Dealer Score was ${dealerScore}. Player wins.`
} else if (playerScore === dealerScore) {
return `Player score was ${playerScore}. Dealer Score was ${dealerScore}. Player and Dealer are tied.`
} else {
return `Player score was ${playerScore}. Dealer Score was ${dealerScore}. Dealer wins.`
}

/**
};
// determineWinner(playerScore, dealerScore)

/**DELIVERED CODE STARTS HERE
* Creates user prompt to ask if they'd like to draw a card
* @param {number} count
* @param {string} dealerCard
Expand All @@ -66,7 +186,9 @@ const showHand = (player) => {
/**
* Runs Blackjack Game
*/
const startGame = function() {

// debugger
const startGame = function () {
player.drawCard();
dealer.drawCard();
player.drawCard();
Expand Down Expand Up @@ -97,4 +219,4 @@ const startGame = function() {

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
console.log(startGame());
99 changes: 83 additions & 16 deletions cardsWorthTen.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,68 @@
const cards = [
{ val: 2, displayVal: "2", suit: "hearts" },
{ val: 3, displayVal: "3", suit: "hearts" },
{ val: 4, displayVal: "4", suit: "hearts" },
{ val: 5, displayVal: "5", suit: "hearts" },
{ val: 6, displayVal: "6", suit: "hearts" },
{ val: 7, displayVal: "7", suit: "hearts" },
{ val: 8, displayVal: "8", suit: "hearts" },
{ val: 9, displayVal: "9", suit: "hearts" },
{ val: 10, displayVal: "10", suit: "hearts" },
{ val: 10, displayVal: "Jack", suit: "hearts" },
{ val: 10, displayVal: "Queen", suit: "hearts" },
{ val: 10, displayVal: "King", suit: "hearts" },
{ val: 11, displayVal: "Ace", suit: "hearts" }
const cards = [{
val: 2,
displayVal: "2",
suit: "hearts"
},
{
val: 3,
displayVal: "3",
suit: "hearts"
},
{
val: 4,
displayVal: "4",
suit: "hearts"
},
{
val: 5,
displayVal: "5",
suit: "hearts"
},
{
val: 6,
displayVal: "6",
suit: "hearts"
},
{
val: 7,
displayVal: "7",
suit: "hearts"
},
{
val: 8,
displayVal: "8",
suit: "hearts"
},
{
val: 9,
displayVal: "9",
suit: "hearts"
},
{
val: 10,
displayVal: "10",
suit: "hearts"
},
{
val: 10,
displayVal: "Jack",
suit: "hearts"
},
{
val: 10,
displayVal: "Queen",
suit: "hearts"
},
{
val: 10,
displayVal: "King",
suit: "hearts"
},
{
val: 11,
displayVal: "Ace",
suit: "hearts"
}
];

/**
Expand All @@ -20,8 +71,24 @@ const cards = [
*
* @param {array} cards
* @return {string} displayVal
*
*
*/
const cardsWorthTen = cards => {};
const cardsWorthTen = cards => {

let filteredCards = cards.filter((card) => card.val === 10);
let stringVal = [];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also a hint on how you might filter values and make a string . . . You have the array of card objects already. In your loop below you are reaching the value correctly. Now instead of console logging them, tack them on to the end of a new collection. No single way to do this, you could start with an empty string and concatenate on to the end . . or perhaps make an empty array that you push the new values into, and then later use Array.join(', ') to convert the array into a string of those values separated by a comma and a space.

for (let i = 0; i < filteredCards.length; i++) {

stringVal.push( `${filteredCards[i].displayVal}`);

};


return stringVal.join(", ")
};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"

// should return/log "10, Jack, Queen, King"
55 changes: 53 additions & 2 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,62 @@
*/
const getDeck = () => {

}
const suits = ['clubs', 'diamonds', 'hearts', 'spades'];
let cards = [];

//loop thru four suits and associate 13 cards with each
for (let i = 0; i < suits.length; i++) {

let displayVal;
let val;

// CHECKS
//create a new card with value 1-13 for each suit
for (let j = 1; j <= 13; j++) {
switch (true) {
case (j === 1):
displayVal = 'Ace';
val = 11;
break;
case (j > 1 && j <= 10):
displayVal = j;
val = j;
break;
case (j === 11):
displayVal = 'Jack';
val = 10;
break;
case (j === 12):
displayVal = 'Queen';
val = 10;
break;
case (j === 13):
displayVal = 'King';
val = 10;
break;
//end of switch statement
}

//push the card deck into the card array
cards.push({
index: j,
val: val,
displayVal: displayVal,
suit: suits[i]
});
//end of four loop j to create 13 cards for each suit
};
//end of loop i to create four suits of cards
};

return cards;
};

console.log(getDeck());

//CHECKS
const deck = getDeck();


console.log(`Deck length equals 52? ${deck.length === 52}`);

const randomCard = deck[Math.floor(Math.random() * 52)];
Expand All @@ -23,4 +73,5 @@ console.log(`Random card has suit? ${cardHasSuit}`);
const cardHasDisplayVal = randomCard &&
randomCard.displayVal &&
typeof randomCard.displayVal === 'string';
console.log(randomCard);
console.log(`Random card has display value? ${cardHasDisplayVal}`);
Loading