Skip to content

Khanh completed Assignment 4 and checked in #14

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
142 changes: 132 additions & 10 deletions blackjack.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
// start a card deck with 52 cards
const blackjackDeck = getDeck();

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

// draw a random card and add to the player hand array
drawCard(){
let randomCard = blackjackDeck[Math.floor(Math.random() * 52)];

this.hand.push(randomCard);
console.log(this.hand);
}
}

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

/**
* Calculates the score of a Blackjack hand
Expand All @@ -20,7 +33,49 @@ const player; // TODO
*/
const calcPoints = (hand) => {
// CREATE FUNCTION HERE

let returnObj = {total: 0, isSoft: false};
let pointTotal = 0;
let checkIsSoft = false;
let aceCount = 0;

//loop through the object card array
const hd = Object.values(hand);

//loop through array and log every card object
for (let c of hd)
{
//if it is an Ace then increase AceCount by 1
let s = c.displayVal;
if (s ==='Ace')
{
aceCount = parseInt(aceCount) + 1;
}
//if aceCount equal or greater than 2 then set value of Ace card to 1
// else leave as is
if (parseInt(aceCount) >= 2)
{
c.val = 1;
}
//if pointTotal > 21 and if it is an Ace
//and its value is 11 then set its val to 1
if (parseInt(pointTotal) + parseInt(c.val) > 21 && s === 'Ace' && parseInt(c.val) == 11)
{
c.val = 1;
}
//set checkIsSoft to true if there is an Ace card
// and its value is 11
if (parseInt(aceCount) == 1 && parseInt(c.val) == 11)
{
checkIsSoft = true;
}
//calculate running point total
pointTotal = parseInt(pointTotal) + parseInt(c.val);
}

//setup return object
returnObj = {total: pointTotal, isSoft: checkIsSoft};
//return the object
return returnObj;
}

/**
Expand All @@ -31,9 +86,19 @@ const calcPoints = (hand) => {
*/
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE

// define boolean type return var
let needAnotherDraw = false;
//call calcPoints function to get dealer total score and isSoft property value
const dHand = calcPoints(dealerHand);
//if the dealer hand is 16 or less, dealer must draw another card
needAnotherDraw = (dHand.total < 16) ? true : false;
//if the dealer hand is exactly 17 and the dealer has an Ace value at 11 aka
// isSoft value is true then the dealer must draw another card
needAnotherDraw = (dHand.isSoft) ? true : false;
//Otherwise the dealer hand is 17 or more, the dealer can not draw another card
// no need to evaluate use default which is false
return needAnotherDraw;
}

/**
* Determines the winner if both player and dealer stand
* @param {number} playerScore
Expand All @@ -42,7 +107,23 @@ const dealerShouldDraw = (dealerHand) => {
*/
const determineWinner = (playerScore, dealerScore) => {
// CREATE FUNCTION HERE
let rtrStr = "";
let winner = "";

//Compare scores between player and dealer and determine the winner or a tie
// return string with player's score, dealer's score and who wins
if (parseInt(playerScore) > parseInt(dealerScore))
{
winner = "Player";
}
else if (parseInt(playerScore) < parseInt(dealerScore)) {
winner = "Dealer";
}
else{
winner = "Tie";
}
rtrStr = `Player Score: ${playerScore}, Dealer Score: ${dealerScore}, Winner: ${winner}`;
return rtrStr;
}

/**
Expand All @@ -60,7 +141,15 @@ const getMessage = (count, dealerCard) => {
*/
const showHand = (player) => {
const displayHand = player.hand.map((card) => card.displayVal);
console.log(`${player.name}'s hand is ${displayHand.join(', ')} (${calcPoints(player.hand).total})`);

let logStr = `${player.name}'s hand is ${displayHand.join(', ')} (${calcPoints(player.hand).total})`;
//log to console
console.log(logStr);
// log to browser
//get the HTML content of the document
const myBody = document.body.innerHTML;
//change the <body> of a document
document.body.innerHTML = logStr;
}

/**
Expand All @@ -71,30 +160,63 @@ const startGame = function() {
dealer.drawCard();
player.drawCard();
dealer.drawCard();

//boolean to flag quick win - only 2 cards draw and get exactly 21 points
let isQuickWin = false;
let playerScore = calcPoints(player.hand).total;
showHand(player);
while (playerScore < 21 && confirm(getMessage(playerScore, dealer.hand[0]))) {
player.drawCard();
playerScore = calcPoints(player.hand).total;
showHand(player);
//if player play first two cards and total points equals exactly 21
// no more drawing, exit loop to declare player is the winner

if (player.hand.count == 2 && playerScore == 21)
{
isQuickWin = true;
break;
}

}
if (playerScore > 21) {
return 'You went over 21 - you lose!';
}
//exit function to declare player is the winner

else if (isQuickWin)
{
return 'Player card total is 21 after 2 draws - Player win';
}

console.log(`Player stands at ${playerScore}`);

let dealerScore = calcPoints(dealer.hand).total;
while (dealerScore < 21 && dealerShouldDraw(dealer.hand)) {
dealer.drawCard();
dealerScore = calcPoints(dealer.hand).total;
showHand(dealer);
//if dealer play first two cards and total points equals exactly 21
// no more drawing, exit loop to declare dealer is the winner

if (dealer.hand.count == 2 && dealerScore == 21)
{
isQuickWin = true;
break;
}

}
if (dealerScore > 21) {
return 'Dealer went over 21 - you win!';
}
//exit function to declare dealer is the winner

else if (isQuickWin)
{
return 'Dealer card total is 21 after 2 draws - Dealer win';
}

console.log(`Dealer stands at ${dealerScore}`);

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

/**
* Takes an array of cards and returns a string of the card display
* values where the value is equal to 10
*
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};

const cardsWorthTen = cards => {
// value to search
const searchVal = 10;
//use array filter to return only item where the value is equal to 10
const arrFilter = cards.filter((item) => item.val == searchVal);
// use array reduce function to flatten the array
// and return a string
let strRet = arrFilter.reduce(
(accum,index) => `${accum} ${index.displayVal},`)
// format return string as desired
strRet = `${searchVal}, ${strRet.slice(0,strRet.length -1)}`;
//return result string
return strRet;
}

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
47 changes: 41 additions & 6 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@
* Returns an array of 52 Cards
* @returns {Array} deck - a deck of cards
*/
const getDeck = () => {

const getDeck = () => {
//array to hold four suits
const suitsArr = ['hearts', 'spades', 'clubs', 'diamonds'];
// array to hold card symbol
const cardSymbolArr = ['Jack', 'Queen', 'King', 'Ace'];
const cardArr = [];
// Loop through each suit in the array
// create each set of 13 cards in each suit
// 9 cards for value from 2 - 10
// 3 cards for 3 symbols in the cardSymbol array
// 1 card for Ace symbol
for (let s in suitsArr)
{

//loop from 2 to 10 value
for (let i = 2; i <= 10; i++)
{
//add card to deck
cardArr.push({val: i, displayVal: i.toString(), suit: suitsArr[s]});

}
// loop through each symbol in the cardSymbolArr
for (let c in cardSymbolArr)
{
//if symbol is an Ace, set displayVal to 11
if (cardSymbolArr[c] === 'Ace')
{

cardArr.push({val: 11, displayVal: cardSymbolArr[c], suit: suitsArr[s]});
}
// if symbol is not an Ace, set displayVal to 10
else
{
cardArr.push({val: 10, displayVal: cardSymbolArr[c], suit: suitsArr[s]});
}
}
}

return cardArr;
}



// CHECKS
const deck = getDeck();
console.log(`Deck length equals 52? ${deck.length === 52}`);
Expand All @@ -21,6 +56,6 @@ const cardHasSuit = randomCard && randomCard.suit && typeof randomCard.suit ===
console.log(`Random card has suit? ${cardHasSuit}`);

const cardHasDisplayVal = randomCard &&
randomCard.displayVal &&
typeof randomCard.displayVal === 'string';
randomCard.displayVal &&
typeof randomCard.displayVal === 'string';
console.log(`Random card has display value? ${cardHasDisplayVal}`);
28 changes: 26 additions & 2 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,33 @@
* @param {string} doneness
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
const foodIsCooked = function(kind, internalTemp, doneness) {
// Write function HERE

//check for chicken
if (kind.toLowerCase() ==='chicken')
{
return internalTemp > 165;
}
//check for beef
else if (kind.toLowerCase() ==='beef')
{
//if rare
if (doneness.toLowerCase() ==='rare'){
return internalTemp > 125;
}
//if medium
else if (doneness.toLowerCase() ==='medium')
{
return internalTemp > 135;
}
//if well
else if (doneness.toLowerCase() ==='well')
{
return internalTemp > 155;
}
}
// other cases
else return false;
}


Expand Down
12 changes: 11 additions & 1 deletion logCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ const cards = [
*
* @param {array} deck A deck of cards
*/
const logCardDeck = deck => {};
const logCardDeck = deck => {
// define an array of object value
const cd = Object.values(deck);
//loop through array and log every card object
for (let c of cd)
{
console.log(c);
}
};

logCardDeck(cards);