2

So I have some code here: First a simple random # generator and a array-choosing function:

function Rand(min, max) {
 return parseFloat(Math.floor(Math.random() * max - min + 1))) + parseFloat(min);
}
function Choose(arr) {
 //Returns an element from an array at random.
 return arr[Math.floor(Math.random() * arr.length)];
}

and second a Card shuffler:

function CardDeck() {
 var Cd = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"];
 var H = [];
 var S = [];
 var D = [];
 var C = [];
 var Result = [];
 var Dk = document.getElementById("Deck Count").value;
 for (i = 0; i < Cd.length; i++) {
 S[i] = Cd[i] + " of Spades";
 H[i] = Cd[i] + " of Hearts";
 C[i] = Cd[i] + " of Clubs";
 D[i] = Cd[i] + " of Diamonds";
 }
 if (Dk == "4") {
 P = S.concat(C, D, H);
 } else if (Dk == "5") {
 var St = [];
 for (i = 0; i < Cd.length; i++) {
 St[i] = Cd[i] + " of Stars";
 }
 P = S.concat(C, D, H, St);
 } else if (Dk == "6") {
 var Rk = [];
 var Wh = [];
 for (i = 0; i < Cd.length; i++) {
 Rk[i] = Cd[i] + " of Rackets";
 Wh[i] = Cd[i] + " of Wheels";
 }
 P = S.concat(C, D, H, Rk, Wh);
 }
 for (i = 0; i < Dk * Cd.length; i++) {
 var Q = Choose(P);
 R = P.indexOf(Q);
 Result[i] = (i + 1) + ": " + Q;
 P = P.slice(0, R).concat(P.slice(R + 1));
 }
 document.getElementById("Cards").innerHTML = Result.join("\n");
}

Is there an easy way to make this faster or at least declare the arrays faster instead of just doing a=[],b=[]... for each as that can be time-consuming and tedious.

Additionally, is there a way to get a better RNG with a longer period in base JS (no libraries, as this was built on base JS)

Carson Ip
1,94617 silver badges29 bronze badges
asked Aug 2, 2016 at 0:36
1
  • 1
    It's not faster necessarily, but it would probably be more convenient to use an object with these arrays as properties, or use an array of arrays. Commented Aug 2, 2016 at 0:40

4 Answers 4

2

You can avoid typing var over and over like this: var H=[], S=[], D=[], C=[]; But otherwise it's hard to get more concise than what you have.

answered Aug 2, 2016 at 0:42
Sign up to request clarification or add additional context in comments.

Comments

0

If you store the suit names in another array it eliminates a lot of variables; then using apply with concat can get them all into Result in one shot:

function CardDeck() {
 var Cd = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"];
 var Result = [];
 var Dk = document.getElementById("Deck Count").value;
 // Put the suit names into a new array
 var suitNames = [" of Spades", " of Hearts", " of Clubs", " of Diamonds"];
 if (Dk == "5") {
 suitNames.push(" of Stars");
 } else if (Dk == "6") {
 suitNames.push(" of Rackets");
 suitNames.push(" of Wheels");
 }
 // Create all suit stacks as an array of arrays
 var suits = [];
 for (var i = 0; i < Cd.length; i++) {
 for(var j = 0; j < suitNames.length; j++) {
 if(i===0) suits.push([]);
 suits[j].push(Cd[i] + suitNames[j]);
 }
 }
 // Combine all cards into a single array
 var P = Array.prototype.concat.apply([], suits);
 // Shuffle the deck
 for (i = 0; i < P.length; i++) {
 var Q = Choose(P);
 R = P.indexOf(Q);
 Result[i] = (i + 1) + ": " + Q;
 P = P.slice(0, R).concat(P.slice(R + 1));
 }
 document.getElementById("Cards").innerHTML = Result.join("\n");
}

Here's a running sample: https://jsfiddle.net/b4rf3ne2/

Is that closer to what you're looking for?

answered Aug 2, 2016 at 2:10

1 Comment

Yes this is perfect as it also allows for many suits to be made at once! Thank you.
0

You could do something like: declare one array-root, then declare a lot of subarrays in a for loop:

var arrays = [];
var nmbOfAr = 20;
for(int i = 0; i < nmbOfAr; i++){
 arrays[i] = [];
}
answered Aug 2, 2016 at 0:59

Comments

0

Using ES6 I think we can make marginal improvements.

let {S, H, C, D} = Cd.reduce((state, cardType, idx) => {
 state.S.push(cardType + ' of Spades');
 state.H.push(cardType + ' of Hearts');
 state.C.push(cardType + ' of Clubs');
 state.C.push(cardType + ' of Diamonds');
 return state; 
}, {
 S: [],
 H: [],
 C: [],
 D: [],
});
answered Aug 2, 2016 at 2:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.