-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDeck.java
135 lines (126 loc) · 4.03 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
/**
* A Class to represent a deck of cards
*
* @author Team 3
* @version 1.0
*/
public class Deck {
protected ArrayList<Card> deck = new ArrayList<Card>();
protected ArrayList<Card> discarded = new ArrayList<Card>();
protected boolean shuffle = false;
/**
* This method shuffles the cards in the deck
*/
public void shuffle() {
Collections.shuffle(this.deck);// Randomly shuffles the deck
}
/**
* This method takes a parameter x and draws that many cards from a deck
*
* @param x
* The amount of cards to draw from the deck
* @return An ArrayList of drawn cards
*/
public ArrayList<Card> drawCards(int x) {
ArrayList<Card> drawnCards = new ArrayList<Card>();
if(x < deck.size())
for (int i = 0; i < x; i++) {
drawnCards.add(deck.remove(0));
if(deck.size()==0){
if(false == this.rebuildDeck()){
JOptionPane.showMessageDialog(null,"There are no" +
" more cards!", "Alert!",JOptionPane
.INFORMATION_MESSAGE);
return drawnCards;
}
}
}
else{
rebuildDeck();
}
return drawnCards;
}
/**
* This is the constructor for Deck class
*
* @param type
* The type of deck being created either Train or Dest
* @param path
* The path of the file that is being read in to create the Deck
*/
public Deck(String type, String path) {
try (Scanner sc = new Scanner(new File(path))) {
while (sc.hasNext()) {
if (type.equals("Train")) {
int cardCount = 0;
String[] line = sc.nextLine().split(",");
try {
cardCount = Integer.parseInt(line[1]);
} catch (Exception e) {
System.err.println("(ERR TrainDeck)" + line[1]
+ ": Cannot parse cardCount to int");
}
for (int i = 0; i < cardCount; i++)
deck.add(new Train(RouteColor.valueOf(line[0])));
}
if (type.equals("Dest")) {
deck.add(new Dest(sc.nextLine(), ","));
}
}
} catch (Exception e) {
System.err
.println("Cannot find file from the follwing path " + path);
}
// shuffle();
}
/**
* This method rebuilds the deck by taking the discarded cards adding them
* to the deck and then clearing the discarded pile
*
*/
public boolean rebuildDeck() {
if(this.discarded.size()==0)return false;
this.deck.addAll(discarded);
Card c = deck.get(0);
if (c instanceof Train) {
this.shuffle = true;
}
shuffle();
this.discarded.clear();
shuffle = true;
return true;
}
/**
* This method returns the deck size
*/
public int size() {
return this.deck.size();
}
/**
* This method adds discarded cards to the discarded ArrayList
*
* @param x
* An ArrayList of cards that are going to be discarded
*/
public void discard(ArrayList<Card> x) {
this.discarded.addAll(x);
}
/**
* returns the first locomotive in the deck
*
* @return the forst locomotive in this deck
*/
public Card getFirstLocomotive(){
for(int i=0; i < deck.size(); i++){
Card c = deck.get(i);
if( c instanceof Train)
if(((Train)c).color == RouteColor.NEUTRAL)
return deck.remove(i);
}
return null;
}
}