-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRound.java
246 lines (202 loc) · 6.07 KB
/
Round.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/** Class which contains all method which handle a single
round in a game */
public class Round{
/** Instance Variables */
private int index = 0;
private int noPlayers, noDraws;
private DeckClass deck;
private UserClass [] usersInGame = new UserClass [noPlayers];
private int [] round ;
private boolean winner;
private int winningPlayerIndex;
private String [] categoryNames;
private int [] playerWinners;
private boolean playerOneStillIn = false;
/** Constructor */
public Round(int index, int noPlayers, UserClass [] usersInGame, DeckClass communalDeck, String [] cats){
this.index = index;
this.noPlayers = noPlayers;
this.usersInGame = usersInGame;
this.deck = communalDeck;
this.categoryNames = cats;
this.setUpRoundArray();
this.findWinnerDraw();
}
/**
* Sets up the round array.
* The round array is an integer array of all the players
* card values for a selected category.
*/
public void setUpRoundArray() {
playerWinners = new int [noPlayers];
for (int i=0; i< playerWinners.length; i++){
playerWinners[i] = 0;
}
round = new int [noPlayers];
for (int i=0; i<noPlayers; i++)
{
if( usersInGame[i].numberOfCards() !=0){
if (index ==1){
round[i] = usersInGame[i].topCard().getCatOne();
}
else if (index == 2){
round[i] = usersInGame[i].topCard().getCatTwo();
}
else if (index ==3){
round[i] = usersInGame[i].topCard().getCatThree();
}
else if (index ==4){
round[i] = usersInGame[i].topCard().getCatFour();
}
else if (index ==5){
round[i] = usersInGame[i].topCard().getCatFive();
}
}
//Card value for a selected category is set to
//-1 if a player is eliminated
else if (usersInGame[i].numberOfCards() == 0){
round[i] = -1;
}
}
//TESTS 5
System.out.println("\nTEST 5: PRINT CURRENT TOP CARD");
if (usersInGame[0].numberOfCards()!=0){
System.out.println("User Player cards are " +usersInGame[0].printCard(0));
}
for (int i=1; i<usersInGame.length; i++) {
if (usersInGame[i].numberOfCards()!=0) {
System.out.println("Computer Player " + (i+1) +" cards are " +usersInGame[i].printCard(0));
}
}System.out.println(""); //used for test formating
//Tests 6
System.out.println("TEST 6: PRINT CATEOGRY AND SELECTED VALUES\n\nCategory in play is " + categoryNames[index]);
for (int i=0; i<round.length; i++) {
System.out.println("Value of category played by player " +(i+1) +" is " +round[i]);
}System.out.println(""); //used for test formting
}
/**
* Method to find a winner or draw result.
* Uses maxinum value algorithm to find the largest
* value in the round array.
* Checks that there is no duplicate of this value.
* If duplicate: draw
* If no duplicate: win
*/
public void findWinnerDraw() {
winner= true;
//Maxinum value algorithm
int largestValue =round[0];
int increment=1;
int possibleWinner =0;
while (increment<round.length) {
if (round[increment]>largestValue) {
largestValue = round[increment];
possibleWinner= increment;
}
increment++;
}
//System.out.println("WE FOUND OUR WINNING VALUE " +largestValue + " HELD BY PLAYER "+possibleWinner);
//Tests for duplicate of largest value in round array
boolean test = false;
for (int i = 0; i < noPlayers; i++){
if (largestValue==round[i]){
test = false;
if (possibleWinner == i){
test = true;
}
if (!test){
winner = false;
// System.out.println("We have a tie");
noDraws++;
}
}
}
//Method which hands out cards
//depending on the result of the round
handOutCards(possibleWinner);
}
/**
* Method which distributes cards depending on the
* result of the round
* @param possibleWinner number of the winning player if there was a win
*/
public void handOutCards(int possibleWinner) {
/* There is a winner:
* -add all cards for the other players deck to the winning players deck
* -remove cards from losing players deck
* -add all communal cards to the winning players deck
* -remove all cards from communal deck.
*/
if (winner==true) {
winningPlayerIndex = possibleWinner;
playerWinners[winningPlayerIndex] = 1;
for (int i=0; i<noPlayers; i++)
{
if (usersInGame[i].numberOfCards() != 0){
usersInGame[winningPlayerIndex].addCard(usersInGame[i].topCard());
usersInGame[i].deleteCard();
}
}
int n = deck.getDeckCount();
CardClass [] pile = deck.getPile();
if ( n>0){
for (int j =0; j <n; j++){
usersInGame[winningPlayerIndex].addCard(pile[j]);
}
}
int communalDeckTotal = deck.getDeckCount();
deck.clear();
//System.out.println("Adding was successful" + usersInGame[winningPlayerIndex].numberOfCards());
//System.out.println("Deleting was successful" + usersInGame[1].numberOfCards());
//System.out.println(" INDEX of wining player: " +winningPlayerIndex);
}
/** There is a draw:
* -add all players cards to communal deck
* -delete players cards
*/
if (winner==false) {
winningPlayerIndex = -1;
//TEST PRINT 4: PRINT CONTENTS OF COMMUNAL PILE
System.out.println("\nTEST 4: PRINT COMMUNAL PILE:");
for (int i=0; i<usersInGame.length; i++) {
if (usersInGame[i].numberOfCards() != 0){
deck.addCard(usersInGame[i].topCard());
System.out.println(usersInGame[i].topCard());
usersInGame[i].deleteCard();
}
}
System.out.println("");
}
if (usersInGame[0].numberOfCards() == 0) {
playerOneStillIn = true;
}
}
/**
* Accessor for winningPlayerIndex
* @return the index of the winning player
*/
public int getWinner() {
return winningPlayerIndex;
}
/**
* Accessor for noDraws
* @return the number of draws
*/
public int getNoDraws() {
return noDraws;
}
/**
* Accessor for playerWinners
* @return number of time each player has won a round
*/
public int [] getPlayerWinners() {
return playerWinners;
}
/**
* Accessor for playerOneStillIn
* @return flag to show if player one is still in the game
*/
public boolean getPlayerOneStillIn() {
return playerOneStillIn;
}
}