Skip to content

Week-4 Assignment #21

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 6 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
7 changes: 7 additions & 0 deletions blackjack.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<title>Blackjack</title>
</head>
<body>

<div>Player Hand:</div>
<div id="playerDiv"></div>
<br />
<div>Dealer Hand:</div>
<div id="dealerDiv"></div>

<script src="createCardDeck.js"></script>
<script src="blackjack.js"></script>
</body>
Expand Down
76 changes: 66 additions & 10 deletions blackjack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ const blackjackDeck = getDeck();
* @constructor
* @param {string} name - The name of the player
*/
class CardPlayer {}; //TODO
class CardPlayer {
constructor(name) {
this.name = name;
this.hand = [];
this.cardElementId = '';
}

drawCard = () => {
this.hand.push(blackjackDeck[Math.floor(Math.random() * 52)]);
document.getElementById(this.cardElementId).innerText = this.hand.map((card) => `${card.suit}-${card.displayVal}`).join(', ');
};
};

// CREATE TWO NEW CardPlayers
const dealer; // TODO
const player; // TODO
const dealer = new CardPlayer('dealer');
dealer.cardElementId = 'dealerDiv';
const player = new CardPlayer('player');
player.cardElementId = 'playerDiv';

/**
* Calculates the score of a Blackjack hand
Expand All @@ -19,8 +32,36 @@ const player; // TODO
* @returns {boolean} blackJackScore.isSoft
*/
const calcPoints = (hand) => {
// CREATE FUNCTION HERE

let isSoft = false;
let total = 0;
hand.forEach((card) => total += card.val);
let aceCollection = hand.filter((p) => p.displayVal === 'Ace');

if (total <= 21) {
isSoft = (aceCollection !== null && aceCollection.length > 0);
}
else if (total > 21) {
if (aceCollection !== null) {
let aceCount = aceCollection.length;
if (aceCount === 1) // Only one ace is present
{
total -= 10;
}
else // multiple aces
{
total -= 10 * (aceCount - 1);
if (total > 21) {
total -= 10;
}
else { // There remains one ace which is still used as 11
isSoft = true;
}
}
}
}

return { isSoft, total };
}

/**
Expand All @@ -30,8 +71,14 @@ const calcPoints = (hand) => {
* @returns {boolean} whether dealer should draw another card
*/
const dealerShouldDraw = (dealerHand) => {
// CREATE FUNCTION HERE

let currentDealerState = calcPoints(dealerHand).total;
let isSoft = calcPoints(dealerHand).isSoft;
if(currentDealerState < 17 || (currentDealerState === 17 && isSoft)){
return true;
}
else {
return false;
}
}

/**
Expand All @@ -41,8 +88,17 @@ 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 result = '';
if (playerScore > dealerScore) {
result = 'Winner: Player';
}
else if (playerScore < dealerScore) {
result = 'Winner: Dealer';
}
else {
result = 'Result: Tie';
}
return `Player Score: ${playerScore}, Dealer Score: ${dealerScore}, ${result}`;
}

/**
Expand All @@ -66,7 +122,7 @@ const showHand = (player) => {
/**
* Runs Blackjack Game
*/
const startGame = function() {
const startGame = function () {
player.drawCard();
dealer.drawCard();
player.drawCard();
Expand Down Expand Up @@ -97,4 +153,4 @@ const startGame = function() {

return determineWinner(playerScore, dealerScore);
}
// console.log(startGame());
console.log(startGame());
8 changes: 7 additions & 1 deletion cardsWorthTen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const cards = [
* @param {array} cards
* @return {string} displayVal
*/
const cardsWorthTen = cards => {};
const cardsWorthTen = cards => {
const Arr = [];
for(card of cards){
(card.val === 10)? Arr.push(`${card.displayVal}`): '';
}
return Arr;
};

console.log(cardsWorthTen(cards));
// should return/log "10, Jack, Queen, King"
65 changes: 63 additions & 2 deletions createCardDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,76 @@
* @returns {Array} deck - a deck of cards
*/
const getDeck = () => {
const suits = ['hearts', 'spades', 'clubs', 'diamonds'];
var cardsArr = [];

}
for (let suit of suits) {
for (let j = 2; j <= 11; j++) {

switch (j) {
case 10:
cardsArr.push({
val: j,
displayVal: `${j}`,
suit: suit
});

cardsArr.push({
val: j,
displayVal: 'Jack',
suit: suit
});

cardsArr.push({
val: j,
displayVal: 'Queen',
suit: suit
});

cardsArr.push({
val: j,
displayVal: 'King',
suit: suit
});

break;
case 11:
cardsArr.push({
val: j,
displayVal: 'Ace',
suit: suit
});
break;
default:
cardsArr.push({
val: j,
displayVal: `${j}`,
suit: suit
});
break;
}
}
}
return cardsArr;
}


// CHECKS
const deck = getDeck();
console.log(`Deck length equals 52? ${deck.length === 52}`);


// CHECKS
const cardsWorthTenfunction = () => {
const cards = getDeck();
return cards
.filter((card) => card.val === 10)
.map(card => `${card.suit} - ${card.displayVal}`)
.join(', ');
}

const cardsWorthTen = cardsWorthTenfunction();
console.log(`cardsWorthTen : ${cardsWorthTen}`);

const randomCard = deck[Math.floor(Math.random() * 52)];

const cardHasVal = randomCard && randomCard.val && typeof randomCard.val === 'number';
Expand Down
20 changes: 16 additions & 4 deletions foodIsCooked.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
* @param {string} doneness
* @returns {boolean} isCooked
*/
const foodIsCooked = function(kind, internalTemp, doneness) {
// Write function HERE
const foodIsCooked = function(kind, internalTemp, doneness) {
//const {kind, internalTemp, doneness} = this;
if(kind === 'beef'){
if(doneness === 'rare'){
return (internalTemp > 125);
}
else if(doneness === 'medium'){
return (internalTemp > 135);
}
else if(doneness === 'well'){
return (internalTemp > 155);
}
}
else if(kind === 'chicken'){
return (internalTemp > 165);
}

}



// Test function
console.log(foodIsCooked('chicken', 90)); // should be false
console.log(foodIsCooked('chicken', 190)); // should be true
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 logCardDeck = deck => {

for(let card of deck){
let keys = Object.keys(card);
for(let key of keys){
console.log(`${key} : ${card[key]}`);
}
}
};

const result = logCardDeck(cards);
//console.log(result);