0

I'm doing a card game as an assignment and I'm stuck at the last hurdle. The problem I have is that I need a condition control to see and determine the value of a card dependent of the outcome. The ACE in this game could be worth 1 or 14 dependant on what is most beneficial for the outcome. The game is some sort of black jack but each card has its face value apart from the ACE which can be worth both 1 and 14. I can check and set the value to either 1 or 14 if the sum of the picked cards is 7 or less. 7+14=21 and most beneficial for the game. But I can't get the code correct for the opposite: if the first card is an ACE and its value set to 14 and the second card is a 9 then the result will be 23 and the player is busted. I need the ACE to be worth 1 then... The default value of an ACE is 1. The code I have so far is:

this.dealCard = function () {
  this.dealedHand.push(shuffledDeck.shift())
  let sum = 0
  this.dealedHand.forEach(function (card) {
    if (card.value === 1 && sum <= 7) {
      card.value = 14
    } //Another condition clause to reverse that and let the ACE be 1 again...
    sum = sum + card.value
  })
  this.dealedHandSum = sum
}

for (let i = 0; i < 2; i++) {
  this.dealCard()
}

while (this.dealedHandSum < player.stopAtValue) {
  this.dealCard()
}

This is a function to sum the cards shifted in to the Hand.

Everything else is working as it should but I want to prevent this from happening:

Johanna: A♠ K♣  27
Johanna got 27 and is busted!
Thomas Bengtsson
  • 399
  • 2
  • 10
  • 22
  • 2
    Possible duplicate of [Is there an elegant way to deal with the Ace in Blackjack?](https://stackoverflow.com/questions/837951/is-there-an-elegant-way-to-deal-with-the-ace-in-blackjack) – GottZ Oct 20 '17 at 19:36

1 Answers1

1

Just count aces at their highest value, then reduce them if the player is busted. This means you have to keep track of how many aces they have, but that's not too hard:

this.dealCard = function () {
    this.dealedHand.push(shuffledDeck.shift())
    let sum = 0
    let aces = [] // Keep track of each ace in the hand
    this.dealedHand.forEach(function (card) {
        if (/* Card is Ace */) {
            card.value = 14 // Count each ace at its highest value
            aces.push(card)
        }
        sum = sum + card.value
    })
    while (aces.length && sum > 21) { // If busted, and we have aces, reduce their value
        sum -= 13
        let ace = aces.shift()
        ace.value = 1 
    }
    this.dealedHandSum = sum
}
CRice
  • 29,968
  • 4
  • 57
  • 70
  • Well, we are almost there. It works as it should but if I run the debugger and an ace shows up that card gets the card.value=14 and the numAces = 1. If we then gets busted it does subtract 13 from the sum but the card still retains it's value if 14 and when finally sum it up we still gets busted. – Thomas Bengtsson Oct 20 '17 at 20:22
  • I've edited the post to reflect that. Instead of just keeping the number of aces, I guess we need to track the actual cards themselves. So if you get an ace, push it into an array, then if busted, reduce the sum, remove an ace from the array, and change its value back to one. – CRice Oct 20 '17 at 20:32
  • Thanks. Working really well with one ace in the hand but with two we get this: A♠ A♥ 10♥ 25 which should be 1+1+10=12 instead of 1+14+10=25 – Thomas Bengtsson Oct 20 '17 at 20:43
  • Hmm. I'm not seeing any reason why it wouldn't work with two aces. Is there some additional code you haven't posted that's performing the sum again? – CRice Oct 20 '17 at 20:54
  • This pass gets an error saying: Cannot set property 'value' of undefined pointing to ace.value = 1 Johanna: 8♠ A♣ A♥ 7♥ 17 I have these loops as well to initiate the dealing of cards: --------------- for (let i = 0; i < 2; i++) { this.dealCard() } ------------------------ `while (this.dealedHandSum < player.stopAtValue) { this.dealCard() }` – Thomas Bengtsson Oct 20 '17 at 21:00
  • My guess is that something else must be going on that isn't visible in this code snippet. The while loop content will only execute if there is an ace in the array (due to the aces.length check), and aces.shift() should only be undefined if the array is empty (these facts contradict each other). – CRice Oct 20 '17 at 21:07
  • One ace is fine enough but if you get two aces and the sum is less than the player.stopAtValue it all spins out of control. Or if the first card is an ace then it doesn't subtract the 13 – Thomas Bengtsson Oct 20 '17 at 21:10
  • What does your condition for `/* Card is Ace */` look like? I left it out in the snippet but it may be important now. That is to say, is it still `card.value === 1 && sum <= 7`, or something else now? – CRice Oct 20 '17 at 21:17
  • `if (card.value === 1) { card.value = 14 aces.push(card) }` – Thomas Bengtsson Oct 20 '17 at 21:18
  • AHA! Thats what I thought it might be. Change it to `card.value === 1 || card.value === 14` – CRice Oct 20 '17 at 21:18