Javascript [SOLVED]: Card game in Javascript, constructor function error

Javascript [SOLVED]: Card game in Javascript, constructor function error

Home Forums Scripting Javascript Tutorials Javascript [SOLVED]: Card game in Javascript, constructor function error

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245704

    Cloudy Point
    Keymaster

    QuestionQuestion

    I’m creating a basic card game (21) in JavaScript where each player has a Hand of cards. The players start out with 2 cards, but I want to add new cards depending on the total value of the cards and adjust for whether there is an ace present or not.

    Unfortunately, I get this error: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory. I think the problem has to to with the this.newCard method, I just can’t seem to understand why it is not working.

    function Hand () {
        this.cards = []
        this.cardsAtStart = function() {
            this.cards.push(new DealCards, new DealCards)
            return this.cards
        }
        this.checkValue = function () {
            let cardsInHand = this.cardsAtStart()
    
            let ace = false
            let value = 0
            for (let i = 0; i < cardsInHand.length; i++) {
                if (cardsInHand[i].cardnumber === 'A' && !ace) {
                    ace = true
                    value = value + 13
                }
                value = value + cardsInHand[i].cardvalue
            }
            if (ace && value  > 21) {
                value = value - 13
            }
            console.log(value)
    
            return value
        }
        this.newCard = function () {
            let value = this.checkValue()
            console.log(value)
            while (value < 15) {
                this.cards.push(new DealCards)
            }
            if (value > 15) {
                endGame()
            }
            console.log(this.cards)
        }
    }
    

    A card object looks like this:
    { suit: '?', cardnumber: 'K', cardvalue: 13 }

    #245705

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You don’t check the value each loop:

    this.newCard = function () {
        let value = this.checkValue()
        console.log(value)
        while (value < 15) {
            this.cards.push(new DealCards);
            // check value again here!
            value = this.checkValue()
        }
        if (value > 15) {
            endGame()
        }
        console.log(this.cards)
    }
    

    But also modify checkValue to move out the cardsAtStart out of there and into the constructor.


    Alternatively, check the value like this:

    while (value < 15) {
      this.cards.push(new DealCards);
      value = this.cards.reduce((all, current) => all + current.cardvalue, 0),
    }
    

    Scratch that, the Ace logic messes with my reducer. Stick to original plan. Move call to cardsAtStart into the constructor method. Then use checkValue to just check value.

    Source: https://stackoverflow.com/questions/48029471/card-game-in-javascript-constructor-function-error
    Author: Zlatko
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.