Skip to content

Nell's Week 4 assignment #11

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

drawCard() {
const deck = getDeck();
const randomCard = deck[Math.floor(Math.random() * 52)];
this.hand.push(randomCard);
}
}

// CREATE TWO NEW CardPlayers
const dealer; // TODO
const player; // TODO
const dealer = new CardPlayer("dealer");
const player = new CardPlayer(prompt("Please enter your name:", "player 1"));

/**
* Calculates value of hand
* @param {Array} hand - Array of card objects with val, displayVal, suit properties
* @param {Object} blackJackScore
*/
function countHand(hand, blackJackScore) {
for (card of hand) {
blackJackScore.total += card.val;
}

return blackJackScore.total;
}

/**
* Checks if hand is soft
* @param {Array} hand - Array of 90card objects with val, displayVal, suit properties
* @param {Object} blackJackScore
*/
function checkSoft(hand, blackJackScore) {
//Check if it is soft
let filteredAces = hand.filter(
(card) =>
(card.displayVal.toString().toLowerCase() === "ace") & (card.val === 11)
);
//debugger;
if (filteredAces.length === 1) {
blackJackScore.isSoft = true;
} else if (filteredAces.length >= 2) {
for (let index = 1; index < filteredAces.length; index++) {
filteredAces[index].val = 1;
}
blackJackScore.isSoft = true;
}
return blackJackScore.isSoft;
}

/**
* Calculates the score of a Blackjack hand
Expand All @@ -19,54 +66,85 @@ const player; // TODO
* @returns {boolean} blackJackScore.isSoft
*/
const calcPoints = (hand) => {
// CREATE FUNCTION HERE
//cant use blackJackScore variable
const blackJackScore = { total: 0, isSoft: false };
let total = 0;
let isSoft = false;
checkSoft(hand, blackJackScore);
countHand(hand, blackJackScore);

}
//This seems wrong but it is working correctly. TODO refactor this
total = blackJackScore.total;
isSoft = blackJackScore.isSoft;
return { 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

}
// If the dealer's hand is 16 points or less, the dealer must draw another card
// If the dealer's hand is exactly 17 points, and the dealer has an Ace valued at 11, the dealer must draw another card
// Otherwise if the dealer's hand is 17 points or more, the dealer will end her turn
const blackJackScore = { total: 0, isSoft: false };
if (countHand(dealerHand, blackJackScore) < 16) {
return true;
} else if (
countHand(dealerHand, blackJackScore) === 17 &&
dealerHand.isSoft
) {
return true;
} else {
return false;
}
};

/**
* Determines the winner if both player and dealer stand
* @param {number} playerScore
* @param {number} dealerScore
* @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

}
if ((playerScore = dealerScore)) {
return `Player and dealer both have ${playerScore}. This is a tie.`;
} else if (playerScore > dealerScore) {
return `Player has ${playerScore} and dealer has ${dealerScore}. Player wins.`;
} else {
return `Player has ${playerScore} and dealer has ${dealerScore}. Dealer wins.`;
}
};

/**
* Creates user prompt to ask if they'd like to draw a card
* @param {number} count
* @param {string} dealerCard
* @param {number} count
* @param {string} dealerCard
*/
const getMessage = (count, dealerCard) => {
return `Dealer showing ${dealerCard.displayVal}, your count is ${count}. Draw card?`
}
return `Dealer showing ${dealerCard.displayVal}, your count is ${count}. Draw card?`;
};

/**
* Logs the player's hand to the console
* @param {CardPlayer} player
* @param {CardPlayer} player
*/
const showHand = (player) => {
const displayHand = player.hand.map((card) => card.displayVal);
console.log(`${player.name}'s hand is ${displayHand.join(', ')} (${calcPoints(player.hand).total})`);
}
console.log(
`${player.name}'s hand is ${displayHand.join(", ")} (${
calcPoints(player.hand).total
})`
);
};

/**
* Runs Blackjack Game
*/
const startGame = function() {
const startGame = function () {
player.drawCard();
dealer.drawCard();
player.drawCard();
Expand All @@ -80,7 +158,7 @@ const startGame = function() {
showHand(player);
}
if (playerScore > 21) {
return 'You went over 21 - you lose!';
return "You went over 21 - you lose!";
}
console.log(`Player stands at ${playerScore}`);

Expand All @@ -91,10 +169,10 @@ const startGame = function() {
showHand(dealer);
}
if (dealerScore > 21) {
return 'Dealer went over 21 - you win!';
return "Dealer went over 21 - you win!";
}
console.log(`Dealer stands at ${dealerScore}`);

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
};
console.log(startGame());
11 changes: 9 additions & 2 deletions cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const cards = [
{ val: 10, displayVal: "Jack", suit: "hearts" },
{ val: 10, displayVal: "Queen", suit: "hearts" },
{ val: 10, displayVal: "King", suit: "hearts" },
{ val: 11, displayVal: "Ace", suit: "hearts" }
{ val: 11, displayVal: "Ace", suit: "hearts" },
];

/**
Expand All @@ -21,7 +21,14 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};
const cardsWorthTen = (cards) => {
let filteredCards = cards.filter((card) => card.val === 10);
let nameFilteredCards = [];

filteredCards.map((item) => nameFilteredCards.push(`${item.displayVal}`));

return nameFilteredCards.join();
};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
57 changes: 50 additions & 7 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,67 @@
* @returns {Array} deck - a deck of cards
*/
const getDeck = () => {
const suits = ["hearts", "diamonds", "spades", "clubs"];
let cards = [];
let displayVal = "";
let val = "";

}

for (let index = 0; index < suits.length; index++) {
for (let j = 1; j <= 13; j++) {
switch (j) {
case 1:
displayVal = "ace";
val = 11;
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
displayVal = j;
val = j;
break;
case 11:
displayVal = "jack";
val = 10;
break;
case 12:
displayVal = "queen";
val = 10;
break;
case 13:
displayVal = "king";
val = 10;
default:
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)];

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

const cardHasSuit = randomCard && randomCard.suit && typeof randomCard.suit === 'string';
const cardHasSuit =
randomCard && randomCard.suit && typeof randomCard.suit === "string";
console.log(`Random card has suit? ${cardHasSuit}`);

const cardHasDisplayVal = randomCard &&
const cardHasDisplayVal =
randomCard &&
randomCard.displayVal &&
typeof randomCard.displayVal === 'string';
console.log(`Random card has display value? ${cardHasDisplayVal}`);
typeof randomCard.displayVal === "string";
console.log(`Random card has display value? ${cardHasDisplayVal}`);
75 changes: 66 additions & 9 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,77 @@
/**
* Determines whether meat temperature is high enough
* @param {string} kind
* @param {number} internalTemp
* @param {string} kind
* @param {number} internalTemp
* @param {string} doneness
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
const foodIsCooked = function (kind, internalTemp, doneness) {
// Write function HERE
let isCooked = false;
let isOverCooked = false;

}
if (!doneness) {
doneness = "";
}

switch (kind) {
case "chicken":
if (internalTemp >= 165) {
isCooked = true;
}
break;
case "beef":
if (doneness === "rare" && internalTemp >= 125 && internalTemp < 135) {
isCooked = true;
} else if (doneness === "rare" && internalTemp >= 135) {
isCooked = true;
isOverCooked = true;
} else if (
doneness === "medium" &&
internalTemp >= 135 &&
internalTemp < 155
) {
isCooked = true;
} else if (doneness === "medium" && internalTemp >= 155) {
isCooked = true;
isOverCooked = true;
} else if (doneness === "well" && internalTemp >= 155) {
isCooked = true;
}
break;
default:
console.log(`${kind} is not a recognized kind of meat.`);
}

if (isCooked && !isOverCooked) {
console.log(`The ${kind} at ${internalTemp} is cooked ${doneness}`);
} else if (isCooked && isOverCooked) {
console.log(
`The ${kind} at ${internalTemp} is over cooked to be ${doneness}`
);
} else {
console.log(`The ${kind} at ${internalTemp} is not cooked ${doneness}`);
}

return isCooked;
};

// Test function
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("chicken", 164)); // should be false
console.log(foodIsCooked("chicken", 165)); // should be true
console.log(foodIsCooked("chicken", 166)); // should be true
console.log(foodIsCooked("beef", 124, "rare")); // should be false
console.log(foodIsCooked("beef", 125, "rare")); // should be true
console.log(foodIsCooked("beef", 135, "rare")); // should be true but over cooked
console.log(foodIsCooked("beef", 136, "rare")); // should be true but over cooked
console.log(foodIsCooked("beef", 134, "medium")); // should be false
console.log(foodIsCooked("beef", 135, "medium")); // should be true
console.log(foodIsCooked("beef", 136, "medium")); // should be true
console.log(foodIsCooked("beef", 154, "medium")); // should be true
console.log(foodIsCooked("beef", 155, "medium")); // should be true
console.log(foodIsCooked("beef", 156, "medium")); // should be true but over cooked
console.log(foodIsCooked("beef", 135, "medium")); // should be true
console.log(foodIsCooked("beef", 138, "medium")); // should be true
console.log(foodIsCooked("beef", 154, "well")); // should be false
console.log(foodIsCooked("beef", 155, "well")); // should be true
console.log(foodIsCooked("beef", 156, "well")); // should be true
Loading