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)
-
1It'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.Spencer Wieczorek– Spencer Wieczorek2016年08月02日 00:40:46 +00:00Commented Aug 2, 2016 at 0:40
4 Answers 4
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.
Comments
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?
1 Comment
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] = [];
}
Comments
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: [],
});