Deal Cards for Freecell Rosetta Code

Tell us what’s happening:

Hey, guys. I can’t understand what’s to be done by this challenge.
First it says theres 32768 in rand range but state mod 2^16 doubles this range
Maybe I’m seeding the wrong way

Thanks for your help :slight_smile:

Your code so far


const pow = Math.pow;

const state = (n) => 
!n?214013:(214013*state(n-1)+2531011)%pow(2,31);

const rand = (n) => state(n)%pow(2,16);

let cards = [
'AC','AD','AH','AS','2C','2D','2H','2S',
'3C','3D','3H','3S','4C','4D','4H','4S',
'5C','5D','5H','5S','6C','6D','6H','6S',
'7C','7D','7H','7S','8C','8D','8H','8S',
'9C','9D','9H','9S','TC','TD','TH','TS',
'JC','JD','JH','JS','QC','QD','QH','QS',
'KC','KD','KH','KS'
];

const dealFreeCell=(seed)=>{
let deal = [];
let res = [[],[],[],[],[],[],[]];
while(cards.length){
  let index = rand(seed)%cards.length;
  [ cards[index] , cards[cards.length-1] ] =
  [ cards[cards.length-1] , cards[index] ]
  deal.push(cards.pop());
}

for(let i=0;i<6;i++)
  for(let j=0;j<8;j++)
      res[i].push(deal.shift());

for(let i=0;i<4;i++)
  res[6].push(deal.shift());

return res;
}

console.log(dealFreeCell(1));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Deal cards for FreeCell

Link to the challenge:

rand is state divided by 2**16, not mod of it.

seed is directly used for calculating only first state (seed is kind of state 0 or even state -1), later with each iteration, nth state is calculated based on n - 1 state.

1 Like