Rosetta Code Challenges - Deal cards for FreeCell

Tell us what’s happening:

dealFreeCell(seed) should return an array of length 7.
Failed:dealFreeCell(1) should return an array identical to example “Game #1
Failed:dealFreeCell(617) should return an array identical to example “Game #617

Your code so far

function dealFreeCell(dealNumber) {
    const MODULUS = 2 ** 31;
    const MULTIPLIER = 214013;
    const INCREMENT = 2531011;
    const CARD_COUNT = 52;
    const COLUMN_COUNT = 8;

    // Initialize the deck
    const suits = ['C', 'D', 'H', 'S'];
    const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'];
    let deck = [];
    for (let suit of suits) {
        for (let rank of ranks) {
            deck.push(rank + suit);
        }
    }

    // Seed the RNG
    let state = dealNumber;

    // Shuffle the deck using the RNG
    for (let i = CARD_COUNT - 1; i > 0; i--) {
        state = (MULTIPLIER * state + INCREMENT) % MODULUS;
        let rand = Math.floor(state / 2 ** 16) % (i + 1);
        [deck[i], deck[rand]] = [deck[rand], deck[i]];
    }

    // Deal the cards into 8 columns
    let columns = Array.from({ length: COLUMN_COUNT }, () => []);
    for (let i = 0; i < CARD_COUNT; i++) {
        columns[i % COLUMN_COUNT].push(deck[i]);
    }

    // Convert 8 columns into 7 columns
    let result = [];
    for (let i = 0; i < 7; i++) {
        result.push(columns[i].concat(columns[7][i] || []));
    }

    return { columns: result };
}

// Example usage:
console.log(dealFreeCell(1)); // Should match "Game #1"
console.log(dealFreeCell(617)); // Should match "Game #617"

  

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0

Challenge Information:

Rosetta Code Challenges - Deal cards for FreeCell

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!