Skip to content

Rick Dalton HW 4 complete #17

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 1 commit 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
80 changes: 73 additions & 7 deletions blackjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ const blackjackDeck = getDeck();
* @constructor
* @param {string} name - The name of the player
*/
class CardPlayer {}; //TODO
class CardPlayer {
hand;

constructor (name)
{
this.name = name
this.hand = []
}
drawCard ()
{
this.hand.push(blackjackDeck[Math.floor(Math.random() * 52)])
}
}; //TODO

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

/**
* Calculates the score of a Blackjack hand
Expand All @@ -19,8 +31,48 @@ const player; // TODO
* @returns {boolean} blackJackScore.isSoft
*/
const calcPoints = (hand) => {
// CREATE FUNCTION HERE
let blackJackScore = {total: 0, isSoft: true}
let aceCount = 0;
for (let card of hand)
{
if (card.displayVal != 'Ace' )
{
blackJackScore.total += card.val;
}
else
{
aceCount++
}
}
if (aceCount > 0)
{
if (aceCount > 1)
{
if (blackJackScore.total <= (21 - 11 - (aceCount - 1)))
{
blackJackScore.total += 11 + aceCount - 1
blackJackScore.isSoft = true;
}
else
{
blackJackScore.total += aceCount
}
}
else
{
if (blackJackScore.total + 11 <= 21)
{
blackJackScore.total += 11;
blackJackScore.isSoft = true;
}
else
{
blackJackScore.total++;
}
}

}
return blackJackScore;
}

/**
Expand All @@ -30,7 +82,8 @@ const calcPoints = (hand) => {
* @returns {boolean} whether dealer should draw another card
*/
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE
let blackJackScore = calcPoints(dealerHand)
return !!(blackJackScore.total <= 16 || (blackJackScore.total === 17 && blackJackScore.isSoft));

}

Expand All @@ -41,7 +94,20 @@ const dealerShouldDraw = (dealerHand) => {
* @returns {string} Shows the player's score, the dealer's score, and who wins
*/
const determineWinner = (playerScore, dealerScore) => {
// CREATE FUNCTION HERE
let winner = ''
if (playerScore <= 21 && playerScore === dealerScore)
{
winner = 'tied'
}
else if (playerScore <= 21 && playerScore > dealerScore)
{
winner = 'player'
}
else
{
winner = 'dealer'
}
return `${playerScore} ${dealerScore} ${winner}`

}

Expand Down Expand Up @@ -97,4 +163,4 @@ const startGame = function() {

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
console.log(startGame());
18 changes: 17 additions & 1 deletion cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};
const cardsWorthTen = cards => {
let result = ''
let addComma = false;
for (let i = 0; i < cards.length; i++)
{
if (cards[i].val === 10)
{
if (addComma)
{
result += ', '
}
result += cards[i].displayVal
addComma = true
}
}
return result
};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
30 changes: 28 additions & 2 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,37 @@
* @returns {Array} deck - a deck of cards
*/
const getDeck = () => {

let cardSuits = ['diamonds', 'hearts', 'clubs', 'spades']
let cardValueMap = new Map();
cardValueMap.set('Ace', 11);
cardValueMap.set('2', 2);
cardValueMap.set('3', 3);
cardValueMap.set('4', 4);
cardValueMap.set('5', 5);
cardValueMap.set('6', 6);
cardValueMap.set('7', 7);
cardValueMap.set('8', 8);
cardValueMap.set('9', 9);
cardValueMap.set('10', 10);
cardValueMap.set("Jack", 10);
cardValueMap.set("Queen", 10);
cardValueMap.set("King", 10);
let cards = new Array(52);
let deckIdx = 0;
for (const item of cardSuits) {
cardValueMap.forEach((value, key) => {
//let card = {val: value, displayVal: key, suit: item}
//console.log(`${card.val} ${card.displayValue} ${card.suit}`)
//cards[deckIdx] = card
cards[deckIdx] = {val: value, displayVal: key, suit: item}
//console.log(`${cards[deckIdx].val} ${cards[deckIdx].displayValue} ${cards[deckIdx].suit}`)
deckIdx++
});
}
return cards
}



// CHECKS
const deck = getDeck();
console.log(`Deck length equals 52? ${deck.length === 52}`);
Expand Down
16 changes: 14 additions & 2 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
// Write function HERE

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


Expand Down
6 changes: 5 additions & 1 deletion logCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ const cards = [
*
* @param {array} deck A deck of cards
*/
const logCardDeck = deck => {};
const logCardDeck = deck => {
deck.forEach(function (card){
console.log(`val: ${card.val} displayVal: ${card.displayVal} suit: ${card.suit}`)
})
};