Skip to content

start of homework #8

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 3 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
74 changes: 62 additions & 12 deletions blackjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,94 @@ const blackjackDeck = getDeck();
* @constructor
* @param {string} name - The name of the player
*/
class CardPlayer {}; //TODO
class CardPlayer {
constructor(name) {
this.name = name;
this.hand = [];
// this.hand = [{val: 2, displayVal: 2, suit: 'clubs'},{val: 11, displayVal: 'ace', suit: 'clubs'},{val: 11, displayVal: 'ace', suit: 'clubs'}];
}
drawCard() {
const randomCard = blackjackDeck[Math.floor(Math.random() * 52)];
return this.hand.push(randomCard);
}
};

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

console.log("dealer:", dealer.hand)
console.log("player:", player.hand)
/**
* Calculates the score of a Blackjack hand
* @param {Array} hand - Array of card objects with val, displayVal, suit properties
* @returns {Object} blackJackScore
* @returns {number} blackJackScore.total
* @returns {boolean} blackJackScore.isSoft
*/


// i went through a few iterations of this function. this one works, but i feel like
// i can simplify this quite a bit (remove the handcards array, etc).
const calcPoints = (hand) => {
// CREATE FUNCTION HERE
let hand1 = []
let handcards = []
let isSoft = false;
// const acePresent = hand.find(element => element.displayVal === 'ace');
hand.forEach(card => {
if (card.displayVal === 'ace') {
if (hand1.displayVal.includes('ace')) {
card.val = 1;
hand1.push(card)
handcards.push(card.displayVal)
isSoft = true
} else {
hand1.push(card)
handcards.push(card.displayVal)
}
}
else {
hand1.push(card)
handcards.push(card.displayVal)
}
});

}
let total = hand1.reduce((previousValue, currentValue) => previousValue + currentValue.val, 0);
return blackJackScore = {total, isSoft};
};

/**
* Determines whether the dealer should draw another card.
*
* @param {Array} dealerHand Array of card objects with val, displayVal, suit properties
* @returns {boolean} whether dealer should draw another card
*/
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE

}
const dealerShouldDraw = (dealerHand) => {
const dealerScore = calcPoints(dealerHand);
if (dealerScore.total <= 16 || (dealerScore.total === 17 && dealerScore.isSoft == false)) {
return true;
} else {
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
*/
const determineWinner = (playerScore, dealerScore) => {
// CREATE FUNCTION HERE

const determineWinner = (playerScore, dealerScore) => {
console.log("player's score: ", playerScore, "dealer's score: ", dealerScore);
if (playerScore > dealerScore) {
return console.log(player, " wins")
} else if (playerScore < dealerScore) {
return console.log(dealer, " wins")
} else if (playerScore === dealerScore) {
return console.log("no winner, PUSH");
}
}

/**
Expand All @@ -66,7 +116,7 @@ const showHand = (player) => {
/**
* Runs Blackjack Game
*/
const startGame = function() {
const startGame = function () {
player.drawCard();
dealer.drawCard();
player.drawCard();
Expand Down Expand Up @@ -97,4 +147,4 @@ const startGame = function() {

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
console.log(startGame());
24 changes: 21 additions & 3 deletions cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,25 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
// two solutions below, please let me know which one is preferred.

// solution one uses map method

isEqualToTen = [];

function cardsWorthTenMapFunction(cards) {
if (cards.val === 10) {
isEqualToTen.push(cards.displayVal)
return cards.displayVal;
}
}

cards.map(cardsWorthTenMapFunction);

console.log(isEqualToTen);

// solution two uses filter method

const cardsWorthTenFilter = cards.filter((cardsWorthTen) => cardsWorthTen.val === 10);
console.log(cardsWorthTenFilter);
45 changes: 40 additions & 5 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,53 @@
* Returns an array of 52 Cards
* @returns {Array} deck - a deck of cards
*/
const getDeck = () => {

}


const getDeck = () => {
const suits = ['hearts', 'diamonds', 'spades', 'clubs'];
const cards = [];

for (let index = 0; index < suits.length; index++) {
for (let j = 1; j <= 13; j++) {
let displayVal;
let val;
switch (true) {
case (j === 11):
displayVal = "jack";
val = 10;
break;
case (j === 12):
displayVal = "queen";
val = 10;
break;
case (j === 13):
displayVal = "king";
val = 10;
break;
case (j === 1):
displayVal = "ace";
val = 11;
break;
default:
displayVal = j;
val = j;
break;
}
cards.push({
val,
displayVal,
suit: suits[index]
})
}
}
return cards;
};

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

const randomCard = deck[Math.floor(Math.random() * 52)];

console.log(randomCard);
const cardHasVal = randomCard && randomCard.val && typeof randomCard.val === 'number';
console.log(`Random card has val? ${cardHasVal}`);

Expand Down
25 changes: 22 additions & 3 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
// Write function HERE

}
if (kind === 'chicken' && internalTemp >= 165) {
return true
} else {
return false
};
if (kind === 'beef' && doneness === 'rare' && internalTemp >=125) {
return true
} else {
return false
};
if (kind === 'beef' && doneness === 'medium' && internalTemp >=135) {
return true
} else {
return false
};
if (kind === 'beef' && doneness === 'well' && internalTemp >=155) {
return true
} else {
return false
};
};



Expand All @@ -17,4 +36,4 @@ console.log(foodIsCooked('chicken', 90)); // should be false
console.log(foodIsCooked('chicken', 190)); // should be true
console.log(foodIsCooked('beef', 138, 'well')); // should be false
console.log(foodIsCooked('beef', 138, 'medium')); // should be true
console.log(foodIsCooked('beef', 138, 'rare')); // should be true
console.log(foodIsCooked('beef', 138, 'rare')); // should be false
8 changes: 7 additions & 1 deletion logCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ const cards = [
*
* @param {array} deck A deck of cards
*/
const logCardDeck = deck => {};
const logCardDeck = deck => {
deck.forEach(cards =>
console.log(`${cards.val} ${cards.displayVal} ${cards.suit}`)
);
};
logCardDeck(cards);