-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrick.java
More file actions
213 lines (168 loc) · 6.52 KB
/
Copy pathTrick.java
File metadata and controls
213 lines (168 loc) · 6.52 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Module: CMP-5015Y Programming 2
// Assignment: Coursework 1
//
// Author: Matthew Taylor
//
// Description: Stores data about each trick
package whist;
import java.util.ArrayList;
import whist.Card.*;
public class Trick {
// use three arraylists to hold all the data for the tricks
// all three arraylists are updated at the same time, so the position in
// all of them is consistent
private ArrayList<Card> cards = new ArrayList<>(); // holds cards
private ArrayList<Integer> players = new ArrayList<>(); // holds player numbers
private ArrayList<Boolean> lead = new ArrayList<>(); // holds if card is lead card
// stores the trump suit of the trick
private static Suit trumps;
// constructor
public Trick(Suit trump) {
trumps = trump;
}
// getter to return the number of cards currently in the trick
public int getNumOfCardsInTrick() {
return cards.size();
}
// getter methods
public ArrayList<Card> getCards() {
return cards;
}
public ArrayList<Integer> getPlayers() {
return players;
}
public ArrayList<Boolean> getLeadCard() {
return lead;
}
// gets the player number (0 to 3) at a position in the trick
public int getPlayerAt(int i) {
// check for exceptions
if(i < 0 || i > players.size())
throw new ArrayIndexOutOfBoundsException("No such player exists");
return players.get(i);
}
// gets the card number (0 to 3) at a position in the trick
public Card getCardAt(int i) {
// check for exceptions
if(i < 0 || i > cards.size())
throw new ArrayIndexOutOfBoundsException("No such card exists");
return cards.get(i);
}
// gets the lead indicator (0 to 3) at a position in the trick
public boolean getLeadAt(int i) {
// check for exceptions
if(i < 0 || i > lead.size())
throw new ArrayIndexOutOfBoundsException("No such element exists");
return lead.get(i);
}
public Suit getLeadSuit() {
return cards.get(0).getSuit();
}
// adds a card to the trick
public void setCard(Card c, int playerNum) {
cards.add(c);
players.add(playerNum);
// if first card added, make it lead card
if(cards.size() == 1)
lead.add(true);
else
lead.add(false);
}
// determines winning player and returns the player number
// returns -1 if no players are winning (no cards played into trick)
public int findWinner() {
// initial check to see if any cards have been played
// return -1 if nothing has been played
if(cards.isEmpty())
return -1;
int winningIndex = 0; // holds index of winning card
Suit leadSuit = cards.get(0).getSuit(); // get suit of lead card
boolean trumpsFound = false;
// loop through all the cards
for(int i = 0; i < cards.size(); i++) {
Card currentlyWinning = cards.get(winningIndex);
// if suit matches trump suit
if(cards.get(i).getSuit() == trumps) {
// if currently winning card isn't a trump suit card then
// set winning card to a trump and set trump found flag
if(currentlyWinning.getSuit() != trumps) {
winningIndex = i;
trumpsFound = true;
}
// compare ranks
if(cards.get(i).getRank().compareTo(currentlyWinning.getRank()) > 0) {
winningIndex = i;
}
}
// if suit matches lead suit
else if(cards.get(i).getSuit() == leadSuit && trumpsFound == false) {
// compare ranks
if(cards.get(i).getRank().compareTo(currentlyWinning.getRank()) > 0) {
winningIndex = i; // if greater, update winning index
}
}
}
return players.get(winningIndex);
}
// returns a card corresponding to the player that played it
public Card getCard(int playerNum) {
int index = -1;
for(int i = 0; i < players.size(); i++) {
if(playerNum == players.get(i)) {
index = i;
}
}
return cards.get(index);
}
// returns the trump suit of the trick
public Suit getTrumpSuit() {
return trumps;
}
@Override
// returns basic details of trick
public String toString() {
StringBuilder s = new StringBuilder();
// check if trick is currently empty
if(cards.isEmpty()) {
s.append("No cards currently played into trick.\n");
}
else {
// loop through trick
for(int i = 0; i < cards.size(); i++) {
s.append("Card " + (i+1) + " by Player " + (players.get(i) + 1));
s.append(": " + cards.get(i) + "\n");
}
}
return s.toString();
}
// test harness
public static void main(String[] args) {
Trick t = new Trick(Suit.HEARTS); // create test trick
// add test elements to trick
t.setCard(new Card(Rank.SIX, Suit.HEARTS), 0);
t.setCard(new Card(Rank.SEVEN, Suit.HEARTS), 1);
t.setCard(new Card(Rank.FIVE, Suit.HEARTS), 2);
t.setCard(new Card(Rank.EIGHT, Suit.HEARTS), 3);
// get getCard()
System.out.print("getCard(2) test: ");
System.out.println(t.getCard(2));
// test getCards
System.out.print("getCards() test: ");
System.out.println(t.getCards());
// test getPlayers
System.out.print("getPlayers() test: ");
System.out.println(t.getPlayers());
// test getLead
System.out.print("getLead() test: ");
System.out.println(t.getLeadCard());
// test getLeadSuit
System.out.print("getLeadSuit() test: ");
System.out.println(t.getLeadSuit());
// test numCardsInTrick
System.out.println("Number of cards in trick is " +
t.getNumOfCardsInTrick());
// test toString()
System.out.println("Testing toString(): ");
System.out.println(t);
}
}