Skip to content

Complete exercises #18

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 10 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
144 changes: 134 additions & 10 deletions blackjack.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@

const blackjackDeck = getDeck();

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

hand;

constructor(name) {
this.name = name
this.hand = []
}

drawCard(){

let randomElement = blackjackDeck[Math.floor(Math.random()*blackjackDeck.length)]

console.log(randomElement)

this.hand.push(randomElement)

console.log(this.hand)

}

} //TODO

// CREATE TWO NEW CardPlayers
const dealer; // TODO
const player; // TODO

const dealer = new CardPlayer('Dealer');
const player = new CardPlayer('Player');

/**
* Calculates the score of a Blackjack hand
Expand All @@ -19,44 +42,139 @@ const player; // TODO
* @returns {boolean} blackJackScore.isSoft
*/
const calcPoints = (hand) => {
let total_=0
let isSoft=false
// let tot = 0
// let countOne=0
let countEleven=0

// CREATE FUNCTION HERE
for (let thing in hand){

// console.log(hand[thing]['val'])

total_+=hand[thing]['val']

}

let count = 0

for (let thing in hand){

if (hand[thing]['displayVal']==='ace'){
count+=1
}

}

if (total_>21 && count === 1){

const index = hand.findIndex(element=> element.displayVal === 'ace')

hand[index]['val']=1

} else if (total_>21 && count>1){

for (let thing in hand){

if (hand[thing]['val']=== 11){

hand[thing]['val']=1

}

}

}

for (let thing in hand){

if (hand[thing]['displayVal']==='ace' && hand[thing]['val']===11){
countEleven +=1
}

}


if (count===0){
isSoft = false
}else if (countEleven !==0){
isSoft = false
}else if (countEleven >= 1){
isSoft = true
}

//console.log(total_)
// console.log(isSoft)

return {
total: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

let shouldDraw = true

let totalPoints_ = calcPoints(dealerHand).total;

console.log(totalPoints_)

let isSoft_ = calcPoints(dealerHand).isSoft;

console.log(`is soft ${isSoft_}`)

return shouldDraw

}

/**
* 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
let winner =""
console.log(playerScore)
console.log(dealerScore)
console.log(winner)
if (playerScore === dealerScore){
winner="it is a tie!"

} else if (playerScore>dealerScore){
winner="player"
} else {
winner ="dealer"
}

// console.log(`playerScore is ${playerScore}, dealerScore is ${dealerScore}, winner is ${winner}`)
return `playerScore is ${playerScore}, dealerScore is ${dealerScore}, winner is ${winner}`

}

/**
* 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?`
}

/**
* 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);
Expand All @@ -73,6 +191,8 @@ const startGame = function() {
dealer.drawCard();

let playerScore = calcPoints(player.hand).total;
// let whatever = calcPoints(player.hand).isSoft;
// console.log(`whatever ${whatever}`)
showHand(player);
while (playerScore < 21 && confirm(getMessage(playerScore, dealer.hand[0]))) {
player.drawCard();
Expand All @@ -95,6 +215,10 @@ const startGame = function() {
}
console.log(`Dealer stands at ${dealerScore}`);

// code below does not execute because a return happens before getting here

return determineWinner(playerScore, dealerScore);

}
// console.log(startGame());
console.log(startGame());

9 changes: 8 additions & 1 deletion cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};

const cardsWorthTen = cards => {

// return cards.filter((card)=> card.val ===10).map(item=>item.displayVal).join(", ")

return `"${cards.filter((card)=> card.val ===10).map(item=>item.displayVal).join(", ")}"`

};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
13 changes: 11 additions & 2 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
*/
const getDeck = () => {

let suits = ['hearts', 'spades', 'clubs', 'diamonds']
let values = {'2':2, '3':3, '4':4, '5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'jack':10,'queen':10,'king':10,'ace':11}
let cards =[]
for (let suit of suits){
for (let value in values){
const card =
{ val: values[value], displayVal: value, suit: suit }
cards.push(card)
}
}
return cards
}



// CHECKS
const deck = getDeck();
console.log(`Deck length equals 52? ${deck.length === 52}`);
Expand Down
6 changes: 5 additions & 1 deletion foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
// Write function HERE
// I interpret greater than as >
return (kind === 'chicken' && internalTemp > 165) ||
(kind === 'beef' && doneness === 'rare' && internalTemp > 125) ||
(kind === 'beef' && doneness === 'medium' && internalTemp > 135) ||
(kind === 'beef' && doneness === 'well' && internalTemp > 155);

}

Expand Down
13 changes: 12 additions & 1 deletion logCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ const cards = [
*
* @param {array} deck A deck of cards
*/
const logCardDeck = deck => {};

const deck = Object.values(cards);

const logCardDeck = deck => {

for (let value in deck){
console.log(`value = ${deck[value]['val']} Display Value = ${deck[value]['displayVal']} Suit = ${deck[value]['suit']}`);
}

};

logCardDeck(deck);