-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.java
45 lines (41 loc) · 1.47 KB
/
Deck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Random;
public class Deck {
private DynamicCardArray initialCards;
private Random rng;
public Deck(){
this.rng = new Random();
this.initialCards = new DynamicCardArray();
for(Type type : Type.values()){
for(int i = 0; i < type.getCount(); i++){
this.initialCards.addCard(new Card(type));
}
}
}
public Card drawTopCard(){
int pos = this.initialCards.length()-1;
Card cardRemoved = initialCards.getCardAtIndex(pos);
initialCards.removeCardAtIndex(pos);
return cardRemoved;
}
//Deal cards to players based on the number of players
public void dealCards(Player[] players){
for(int i = 0; i < players.length; i++){
for(int j = 0; j < players[i].cardPerPlayer(); j++){
players[i].addCardToHand(this.drawTopCard());
}
}
}
//Shuffle the deck
public void shuffle(){
for(int i = 0; i < this.initialCards.length(); i++){
//Find random position to swap with
int pos = this.rng.nextInt(this.initialCards.length() - 1);
//Temp card to swap
Card temp = this.initialCards.getCardAtIndex(i);
//Swap
this.initialCards.setCardAtIndex(i, this.initialCards.getCardAtIndex(pos));
//Set the random position to the temp card
this.initialCards.setCardAtIndex(pos, temp);
}
}
}