Make sure everyone has a black and red card

This commit is contained in:
Anthony Wang 2021-05-09 17:23:37 -05:00
parent 6306da1abd
commit 9580139a78
Signed by: a
GPG key ID: 6FD3502572299774
2 changed files with 20 additions and 5 deletions

View file

@ -55,16 +55,31 @@ export default class Game {
for (let i = 1; i <= 13; ++i)
for (let j = 0; j < 4; ++j)
cards.push({rank: i, suit: j});
// Shuffle cards
for (let i = 0; i < 52; ++i) {
const handSize = 5 - Math.floor(this.room.clients.length/7);
// Shuffle red cards
for (let i = 0; i < 26; ++i) {
const j = Math.floor(Math.random() * (i+1));
[cards[i], cards[j]] = [cards[j], cards[i]];
}
const handSize = 5 - Math.floor(this.room.clients.length/7);
// Shuffle black cards
for (let i = 0; i < 26; ++i) {
const j = Math.floor(Math.random() * (i+1));
[cards[i+26], cards[j+26]] = [cards[j+26], cards[i+26]];
}
for (let i = 0; i < this.room.clients.length; ++i) {
this.players.push(new Player(this, this.room.clients[i]));
this.players[i].cards = cards.slice(i * handSize, (i + 1) * handSize);
this.players[i].cards.push(cards[i], cards[i+26]); // Make sure everyone has a red and black card
}
const remainingCards = [];
for (let i = this.room.clients.length; i < 26; ++i)
remainingCards.push(cards[i], cards[i+26]);
// Shuffle remaining cards
for (let i = 0; i < remainingCards.length; ++i) {
const j = Math.floor(Math.random() * (i+1));
[remainingCards[i], remainingCards[j]] = [remainingCards[j], remainingCards[i]];
}
for (let i = 0; i < this.room.clients.length; ++i)
this.players[i].cards = remainingCards.slice(i*(handSize-2), (i+1)*(handSize-2));
const startingPlayer = this.players[0]; // Pick a random starting player instead??
this.playerTurn = this.players.indexOf(startingPlayer);
this.playersFinished = this.room.clients.length;

View file

@ -1,7 +1,7 @@
enum Suit {
Clubs,
Diamonds,
Hearts,
Clubs,
Spades
}