-
Notifications
You must be signed in to change notification settings - Fork 896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现一个将 52 张牌随机均等的分给四个人,比如输入 [0,1,2,3....51] ,输出[[1,2,16...],[4,3,6..],[....],[....]] #157
Comments
function shuffle(arr) { const arr = Array.from({ length: 52 }).map((item, index) => index) |
function deal() {
const cards = [];
for(let i = 0; i < 52; i++) {
cards.push(i);
}
cards.sort((a, b) => (Math.ceil(Math.random() * 52) - 1) - b);
return [cards.slice(0, 13), cards.slice(13, 26), cards.slice(26, 39), cards.slice(39, 52)];
} |
function genPlayingCards(num = 52) {
const playingCards = []
for (let i = 0; i < num; i++) {
playingCards.push(i)
}
return playingCards
}
function licensing(playingCards, persons = 4) {
const len = playingCards.length
let temp
for (let i = 0; i < len; i++) {
const index = Math.floor(Math.random() * (len - i))
temp = playingCards[len - i - 1]
playingCards[len - i - 1] = playingCards[index]
playingCards[index] = temp
}
const n = Math.floor(len / persons)
return [playingCards.slice(0, n), playingCards.slice(n, 2 * n), playingCards.slice(2 * n, 3 * n), playingCards.slice(3 * n, 4 * n)]
}
console.log(licensing(genPlayingCards())); |
|
No description provided.
The text was updated successfully, but these errors were encountered: