-
Notifications
You must be signed in to change notification settings - Fork 20
added responses to assignments #10
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
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 = []; | ||
this.drawCard = function(){ | ||
let cardNumber = Math.floor(Math.random() * 52); | ||
this.hand.push(blackjackDeck[cardNumber]); | ||
} | ||
} | ||
}; //TODO | ||
|
||
// CREATE TWO NEW CardPlayers | ||
const dealer; // TODO | ||
const player; // TODO | ||
const dealer = new CardPlayer('Dealer'); // TODO | ||
const player = new CardPlayer('Player') // TODO | ||
|
||
|
||
|
||
/** | ||
* Calculates the score of a Blackjack hand | ||
|
@@ -18,9 +32,38 @@ const player; // TODO | |
* @returns {number} blackJackScore.total | ||
* @returns {boolean} blackJackScore.isSoft | ||
*/ | ||
|
||
const calcPoints = (hand) => { | ||
// CREATE FUNCTION HERE | ||
let aceAmount = 0; | ||
let blackJackScore ={ | ||
isSoft: true , | ||
total: 0 | ||
} | ||
|
||
for (let i = 0; i < hand.length; i++){ | ||
if(hand[i].displayVal === 'Ace'){ | ||
aceAmount += 1; | ||
} | ||
if(hand[i].displayVal === 'Ace'){ | ||
if(aceAmount > 1){ | ||
if(hand[i].displayVal === 'Ace'){ | ||
aceAmount += 1 | ||
hand[i].val = 1 | ||
}else{ | ||
continue | ||
} | ||
} | ||
} | ||
blackJackScore.total +=hand[i].val | ||
} | ||
|
||
if(aceAmount > 0){ | ||
blackJackScore.isSoft = true; | ||
return blackJackScore.total; | ||
}else{ | ||
blackJackScore.isSoft = false; | ||
return blackJackScore.total | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -30,7 +73,13 @@ const calcPoints = (hand) => { | |
* @returns {boolean} whether dealer should draw another card | ||
*/ | ||
const dealerShouldDraw = (dealerHand) => { | ||
// CREATE FUNCTION HERE | ||
let dealerTotal = function dealerTotalFunc (dealerHand){ | ||
calcPoints(); | ||
return (score > 16); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't see where this score is coming from . . . |
||
} | ||
|
||
return dealerTotal; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since JavaScript is really flexible, so it will totally allow weird things like this to go through and the result is a sort of false-positive and no errors getting thrown. This function should get a full refactor, but here's the steps you are aiming for: It should take a 'hand' object as an argument . . . . |
||
|
||
|
||
} | ||
|
||
|
@@ -41,8 +90,16 @@ 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 playerScore = player.calcPoints(hand); | ||
let dealerScore = dealer.calcPoints(dealerHand);*/ | ||
|
||
if(playerScore <= 21){ | ||
return playerwin; | ||
} else if (dealerScore <= 21){ | ||
return dealerwin; | ||
} else if (playerScore === dealerScore){ | ||
return "It's a draw"; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -72,7 +129,7 @@ const startGame = function() { | |
player.drawCard(); | ||
dealer.drawCard(); | ||
|
||
let playerScore = calcPoints(player.hand).total; | ||
let playerScore = calcPoints(player.hand); | ||
showHand(player); | ||
while (playerScore < 21 && confirm(getMessage(playerScore, dealer.hand[0]))) { | ||
player.drawCard(); | ||
|
@@ -84,17 +141,21 @@ const startGame = function() { | |
} | ||
console.log(`Player stands at ${playerScore}`); | ||
|
||
let dealerScore = calcPoints(dealer.hand).total; | ||
let dealerScore = calcPoints(dealer.hand); | ||
while (dealerScore < 21 && dealerShouldDraw(dealer.hand)) { | ||
dealer.drawCard(); | ||
dealerScore = calcPoints(dealer.hand).total; | ||
dealerScore = calcPoints(dealer.hand); | ||
showHand(dealer); | ||
} | ||
if (dealerScore > 21) { | ||
return 'Dealer went over 21 - you win!'; | ||
} | ||
|
||
|
||
console.log(`Dealer stands at ${dealerScore}`); | ||
|
||
|
||
return determineWinner(playerScore, dealerScore); | ||
|
||
} | ||
// console.log(startGame()); | ||
console.log(startGame()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,20 @@ const cards = [ | |
* @param {array} cards | ||
* @return {string} displayVal | ||
*/ | ||
const cardsWorthTen = cards => {}; | ||
|
||
|
||
|
||
const cardsWorthTen = cards => { | ||
|
||
const cardValsFilter = cards.filter(item => item.val===10); | ||
|
||
const cardDisplayVals = cardValsFilter.map(a => a.displayVal); | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ope, who's this little semicolon sneaking in here? JavaScript is good at trying to ignore weird syntax errors, but still good to keep your code clean so that debugging is easier. Are you using VSCode? Get in the habit of using it's auto-formatting tools as much as possible. It will enforce auto-indentations and even help you find syntax mistakes and allow for overall more readable code which makes it easier to reason about and refactor. tools called 'Linters' are specifically for this kind of thing, you could try one called Prettier that integrates right into VSCode. |
||
let cardDisplayValsString = cardDisplayVals.join (', ') | ||
|
||
return cardDisplayValsString; | ||
|
||
}; | ||
|
||
console.log(cardsWorthTen(cards)); | ||
// should return/log "10, Jack, Queen, King" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,121 @@ | |
* Returns an array of 52 Cards | ||
* @returns {Array} deck - a deck of cards | ||
*/ | ||
|
||
/*Create a function getDeck that returns an array of 52 cards. There are four suits (hearts, spades, clubs, diamonds). Each suit will have 13 cards: | ||
|
||
2 – 10 will have a val equal to the number | ||
'Jack', 'Queen', and 'King' will all have a val of 10 | ||
'Ace' will have a val of 11 | ||
*/ | ||
|
||
/*const getDeck = () => { | ||
const suits = ['hearts', 'spades', 'clubs', 'diamonds']; | ||
const cards = []; | ||
|
||
suits.forEach(cardGenerate); | ||
|
||
|
||
function cardGenerate(suit){ | ||
let cardCount = 0 | ||
|
||
for (let i = 0; i < 13; i++){ | ||
cardCount += 1; | ||
cards.push(cardCount); | ||
} | ||
} | ||
console.log(cards); | ||
return cards; | ||
|
||
};*/ | ||
|
||
const getDeck = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this certainly gets the job done in a thoroughly imperative way . . . but take a step back and look at ways you might be able to refactor this down. There's a lot of repeated code in here, which always highlights an opportunity to make a reusable function that can be called each time needed. But even more so, think of the values that are available to you when you execute a loop. Or perhaps small data-sets you can define ahead of time that you can put to work using index references in the loops, like saying Not to say this is not a good start . . . a lot of coding is basically figuring out your first working mental model, then stepping back to review the whole, and look for ways to refactor for increased simplicity or readability. Most code you see out in the world started out looking really different and finally comes together after multiple revisions, and that's a great exercise. |
||
const suits = ['hearts', 'spades', 'clubs', 'diamonds']; | ||
const cards = []; | ||
|
||
let cardCount = 0 | ||
|
||
for(let i = 0; i < 13; i++){ | ||
if(i === 0){ | ||
cardCount += 1; | ||
cards.push({val : 11, suit: suits[0], displayVal : 'Ace'}) | ||
} if(i > 0 && i < 10){ | ||
cardCount += 1; | ||
cards.push({val : cardCount, suit: suits[0], displayVal : cardCount.toString()}); | ||
} if(i === 10){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[0], displayVal : 'Jack'}); | ||
} if(i === 11){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[0], displayVal : 'Queen'}); | ||
} if(i === 12){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[0], displayVal : 'King'}); | ||
} | ||
} | ||
cardCount = 0; | ||
for(let i = 0; i < 13; i++){ | ||
if(i === 0){ | ||
cardCount += 1; | ||
cards.push({val : 11, suit: suits[1], displayVal : 'Ace'}) | ||
} if(i > 0 && i < 10){ | ||
cardCount += 1; | ||
cards.push({val : cardCount, suit: suits[1], displayVal : cardCount.toString()}); | ||
} if(i === 10){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[1], displayVal : 'Jack'}); | ||
} if(i === 11){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[1], displayVal : 'Queen'}); | ||
} if(i === 12){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[1], displayVal : 'King'}); | ||
} | ||
} | ||
|
||
cardCount = 0; | ||
for(let i = 0; i < 13; i++){ | ||
if(i === 0){ | ||
cardCount += 1; | ||
cards.push({val : 11, suit: suits[2], displayVal : 'Ace'}) | ||
} if(i > 0 && i < 10){ | ||
cardCount += 1; | ||
cards.push({val : cardCount, suit: suits[2], displayVal : cardCount.toString()}); | ||
} if(i === 10){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[2], displayVal : 'Jack'}); | ||
} if(i === 11){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[2], displayVal : 'Queen'}); | ||
} if(i === 12){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[2], displayVal : 'King'}); | ||
} | ||
} | ||
|
||
cardCount = 0; | ||
for(let i = 0; i < 13; i++){ | ||
if(i === 0){ | ||
cardCount += 1; | ||
cards.push({val : 11, suit: suits[3], displayVal : 'Ace'}) | ||
} if(i > 0 && i < 10){ | ||
cardCount += 1; | ||
cards.push({val : cardCount, suit: suits[3], displayVal : cardCount.toString()}); | ||
} if(i === 10){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[3], displayVal : 'Jack'}); | ||
} if(i === 11){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[3], displayVal : 'Queen'}); | ||
} if(i === 12){ | ||
cardCount += 1; | ||
cards.push({val : 10, suit: suits[3], displayVal : 'King'}); | ||
} | ||
} | ||
|
||
return cards; | ||
|
||
}; | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,4 +58,23 @@ const cards = [ | |
* | ||
* @param {array} deck A deck of cards | ||
*/ | ||
const logCardDeck = deck => {}; | ||
|
||
|
||
|
||
const logCardDeck = deck => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So check this one out . . .. as written this function is not a reusable piece of code. You defined a parameter for 'deck' which would allow you to call this function with any external |
||
|
||
let vals = cards.map(a => a.val); | ||
let dispVals = cards.map(a => a.displayVal); | ||
let suits = cards.map(a => a.suit); | ||
|
||
|
||
const cardValsFilter = cards.filter(item => item.suit !== false ); | ||
let cardCount = cardValsFilter; | ||
|
||
for (i = 0; i < cardCount.length; i++){ | ||
console.log(`this is Card # ${Object.keys(cardCount)[i]}: It has a Value of ${vals[i]}, it has a Display Value of ${dispVals[i]}, and it belongs to the ${suits[i]} suit.`) | ||
} | ||
|
||
}; | ||
|
||
logCardDeck(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm this seems to be digging too deep to get the desired result. All you need from this is get the score object from
calcPoints(dealerHand)
and then see how it compares to the rules in the specification (<=16, >17, or 17 && isSoft).The function inside the function is not entirely necessary given the other code available.