Ad Unit (Iklan) BIG

shuffle a deck of cards game in basic code javascript


some basic quick implementation of a deck of cards written in javascript..

//suffle array list function
function shuffle(arrval) {
    var curindx = arrval.length, tempval, ranindx;
    while (0 !== curindx) {
    ranindx = Math.floor(Math.random() * curindx);
    curindx -= 1;
    tempval = arrval[curindx];
    arrval[curindx] = arrval[ranindx];
    arrval[ranindx] = tempval;
  }
  return arrval;
}

//divide array list multiple parts using chunk_arr function
function chunk_arr(arr_lst, chunk_size){
    var results = [];
    while (arr_lst.length) {
        results.push(arr_lst.splice(0, chunk_size));
    }
    return results;
}

//array list of items
var arr = ['ace of spades','two of spades','three of spades','four of spades','five of spades','six of spades','seven of spades','eight of spades','nine of spades','ten of spades','jack of spades','queen of spades','king of spades','ace of hearts','two of hearts','three of hearts','four of hearts','five of hearts','six of hearts','seven of hearts','eight of hearts','nine of hearts','ten of hearts','jack of hearts','queen of hearts','king of hearts','ace of clubs','two of clubs','three of clubs','four of clubs','five of clubs','six of clubs','seven of clubs','eight of clubs','nine of clubs','ten of clubs','jack of clubs','queen of clubs','king of clubs','ace of diamonds','two of diamonds','three of diamonds','four of diamonds','five of diamonds','six of diamonds','seven of diamonds','eight of diamonds','nine of diamonds','ten of diamonds','jack of diamonds','queen of diamonds','king of diamonds'];

//suffle array list item using function
var arrlst = shuffle(arr);

while(arrlst.length < 52){
var r = Math.floor(Math.random() * 52) + 1;
if(arrlst.indexOf(r) == -1) arrlst.push(r);
}

var res = arrlst.splice(0,52);
var finresult = chunk_arr(res, 13);
console.log(finresult);

Post a Comment

0 Comments