Make sure everyone has a black and red card
This commit is contained in:
parent
6306da1abd
commit
9580139a78
2 changed files with 20 additions and 5 deletions
|
@ -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;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
enum Suit {
|
||||
Clubs,
|
||||
Diamonds,
|
||||
Hearts,
|
||||
Clubs,
|
||||
Spades
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue