Skip to content

Commit 7eaf420

Browse files
authored
Merge pull request #184 from nu-cs-sqe/refactor/reordered-functions
Reordered functions according to Clean Code
2 parents b9c7481 + a613253 commit 7eaf420

File tree

4 files changed

+375
-379
lines changed

4 files changed

+375
-379
lines changed

src/main/java/explodingwildcats/CardPile.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,6 @@ public Card[] peek() {
8080
return cards.toArray(new Card[0]);
8181
}
8282

83-
/**
84-
* Checks if a specified card is in the pile list.
85-
*
86-
* @return true if the card is in the pile, false otherwise.
87-
*/
88-
public boolean contains(Card c) {
89-
return cardList.contains(c);
90-
}
91-
9283
/**
9384
* Sets the ith card in the pile to c.
9485
*
@@ -143,4 +134,13 @@ public boolean removeCardFromPile(Card card, boolean isPlayerHand) {
143134
}
144135
return false;
145136
}
137+
138+
/**
139+
* Checks if a specified card is in the pile list.
140+
*
141+
* @return true if the card is in the pile, false otherwise.
142+
*/
143+
public boolean contains(Card c) {
144+
return cardList.contains(c);
145+
}
146146
}

src/main/java/explodingwildcats/GameEngine.java

+112-114
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ public GameEngine(PlayerFactory playerFactory,
5555
isTurnOrderReversed = false;
5656
}
5757

58+
public int getNumberOfPlayers() {
59+
return numOfPlayers;
60+
}
61+
62+
public List<Player> getPlayers() {
63+
return new ArrayList<>(players);
64+
}
65+
66+
/**
67+
* Getter for draw pile cards.
68+
*/
69+
public Card[] getDrawPile() {
70+
return drawPile.getCards();
71+
}
72+
73+
/**
74+
* Getter for isTurnOrderReversed.
75+
*/
76+
public boolean getIsTurnOrderReversed() {
77+
return isTurnOrderReversed;
78+
}
79+
5880
/**
5981
* Adds the starting deck of cards to the drawPile.
6082
*/
@@ -113,26 +135,6 @@ public void setUpPlayers(int numberOfPlayers, String[] names) {
113135
}
114136
}
115137

116-
public int getNumberOfPlayers() {
117-
return numOfPlayers;
118-
}
119-
120-
public List<Player> getPlayers() {
121-
return new ArrayList<>(players);
122-
}
123-
124-
/**
125-
* Returns whether the game is over.
126-
*
127-
* @return whether the game is over.
128-
*/
129-
public boolean isGameOver() {
130-
if (numOfPlayers > 1) {
131-
return false;
132-
}
133-
return true;
134-
}
135-
136138
/**
137139
* Add defuse cards to both player hands and the draw pile.
138140
*/
@@ -181,24 +183,29 @@ public void insertExplodingAndImplodingCards() {
181183
}
182184

183185
/**
184-
* Getter for draw pile cards.
186+
* Method for shuffling the draw pile.
185187
*/
186-
public Card[] getDrawPile() {
187-
return drawPile.getCards();
188+
public void shuffleDrawPile() {
189+
drawPile.shuffle();
188190
}
189191

190192
/**
191-
* Getter method for drawPile.peek().
193+
* Returns whether the game is over.
194+
*
195+
* @return whether the game is over.
192196
*/
193-
public Card[] peekDrawPile() {
194-
return drawPile.peek();
197+
public boolean isGameOver() {
198+
if (numOfPlayers > 1) {
199+
return false;
200+
}
201+
return true;
195202
}
196203

197204
/**
198-
* Method for shuffling the draw pile.
205+
* Getter method for drawPile.peek().
199206
*/
200-
public void shuffleDrawPile() {
201-
drawPile.shuffle();
207+
public Card[] peekDrawPile() {
208+
return drawPile.peek();
202209
}
203210

204211
/**
@@ -208,13 +215,6 @@ public void reverseTurnOrder() {
208215
isTurnOrderReversed = !isTurnOrderReversed;
209216
}
210217

211-
/**
212-
* Getter for isTurnOrderReversed.
213-
*/
214-
public boolean getIsTurnOrderReversed() {
215-
return isTurnOrderReversed;
216-
}
217-
218218
/**
219219
* Replace the top cards in the draw pile with the cards in toSet.
220220
*
@@ -249,34 +249,7 @@ public Card popTopCard() {
249249
}
250250

251251
/**
252-
* Checks if a player has a specified card.
253-
*
254-
* @param card card to check if a player has.
255-
* @param playerIndex index of the player in the players list.
256-
*/
257-
public boolean playerHasCard(Card card, int playerIndex) {
258-
Player targetPlayer = getPlayerByIndex(playerIndex);
259-
return targetPlayer.hasCard(card);
260-
}
261-
262-
/**
263-
* Checks if a player a number of the specified card.
264-
*
265-
* @param card card to check if a player has.
266-
* @param playerIndex index of the player in the players list.
267-
* @param numCards the number of cards to check for.
268-
*/
269-
public boolean playerHasAtLeastCards(Card card, int playerIndex, int numCards) {
270-
Player player = getPlayerByIndex(playerIndex);
271-
if (numCards <= 0) {
272-
throw new IllegalArgumentException("Number of cards must be greater than 0.");
273-
}
274-
return Arrays.stream(player.getHand()).filter(c -> c == card)
275-
.count() >= numCards;
276-
}
277-
278-
/**
279-
* TODO: eliminates the player at that index.
252+
* Eliminates the player at that index.
280253
*/
281254
public void eliminatePlayer(int playerIndex) {
282255
if (playerIndex < 0 || playerIndex >= numOfPlayers) {
@@ -302,6 +275,17 @@ public void removeCardFromPlayer(Card card, int playerIndex) {
302275
player.removeCardFromHand(card);
303276
}
304277

278+
/**
279+
* Checks if a player has a specified card.
280+
*
281+
* @param card card to check if a player has.
282+
* @param playerIndex index of the player in the players list.
283+
*/
284+
public boolean playerHasCard(Card card, int playerIndex) {
285+
Player targetPlayer = getPlayerByIndex(playerIndex);
286+
return targetPlayer.hasCard(card);
287+
}
288+
305289
/**
306290
* Returns the player at the index in the list or errors if it doesn't exist.
307291
*
@@ -325,60 +309,12 @@ public void discardCard(Card card) {
325309
}
326310

327311
/**
328-
* TODO: add the specified card to a specified location in the card pile.
312+
* Add the specified card to a specified location in the card pile.
329313
*/
330314
public void addCardToDrawPileAt(Card card, int index) {
331315
drawPile.addCardAt(card, index);
332316
}
333317

334-
/**
335-
* Returns the correct card based on the String name.
336-
*
337-
* @param cardName the String version of the card.
338-
* @return the Card.
339-
*/
340-
public Card getCardByName(String cardName) {
341-
switch (cardName) {
342-
case "attack":
343-
return Card.ATTACK;
344-
case "skip":
345-
return Card.SKIP;
346-
case "targeted attack":
347-
return Card.TARGETED_ATTACK;
348-
case "shuffle":
349-
return Card.SHUFFLE;
350-
case "see the future":
351-
return Card.SEE_THE_FUTURE;
352-
case "reverse":
353-
return Card.REVERSE;
354-
case "draw from bottom":
355-
return Card.DRAW_FROM_BOTTOM;
356-
case "alter the future":
357-
return Card.ALTER_THE_FUTURE;
358-
case "nope":
359-
return Card.NOPE;
360-
case "taco cat":
361-
return Card.TACO_CAT;
362-
case "beard cat":
363-
return Card.BEARD_CAT;
364-
case "rainbow cat":
365-
return Card.RAINBOW_CAT;
366-
case "feral cat":
367-
return Card.FERAL_CAT;
368-
case "hairy potato cat":
369-
return Card.HAIRY_POTATO_CAT;
370-
case "exploding kitten":
371-
return Card.EXPLODE;
372-
case "imploding kitten":
373-
return Card.IMPLODE;
374-
case "defuse":
375-
return Card.DEFUSE;
376-
default:
377-
throw new IllegalArgumentException("Could not parse input.");
378-
}
379-
}
380-
381-
382318
/**
383319
* Gets the index of the player in the GameEngine's Player List by their name.
384320
*
@@ -394,7 +330,6 @@ public int getPlayerIndexByName(String name) {
394330
throw new NoSuchElementException("No player with that name could be found.");
395331
}
396332

397-
398333
/**
399334
* Validates that the player has the given cards and that they can be played as a combo.
400335
*
@@ -447,4 +382,67 @@ public Card[] validateComboCards(String[] cards, int currPlayerIndex) {
447382

448383
return returnCards;
449384
}
385+
386+
/**
387+
* Checks if a player a number of the specified card.
388+
*
389+
* @param card card to check if a player has.
390+
* @param playerIndex index of the player in the players list.
391+
* @param numCards the number of cards to check for.
392+
*/
393+
public boolean playerHasAtLeastCards(Card card, int playerIndex, int numCards) {
394+
Player player = getPlayerByIndex(playerIndex);
395+
if (numCards <= 0) {
396+
throw new IllegalArgumentException("Number of cards must be greater than 0.");
397+
}
398+
return Arrays.stream(player.getHand()).filter(c -> c == card)
399+
.count() >= numCards;
400+
}
401+
402+
/**
403+
* Returns the correct card based on the String name.
404+
*
405+
* @param cardName the String version of the card.
406+
* @return the Card.
407+
*/
408+
public Card getCardByName(String cardName) {
409+
switch (cardName) {
410+
case "attack":
411+
return Card.ATTACK;
412+
case "skip":
413+
return Card.SKIP;
414+
case "targeted attack":
415+
return Card.TARGETED_ATTACK;
416+
case "shuffle":
417+
return Card.SHUFFLE;
418+
case "see the future":
419+
return Card.SEE_THE_FUTURE;
420+
case "reverse":
421+
return Card.REVERSE;
422+
case "draw from bottom":
423+
return Card.DRAW_FROM_BOTTOM;
424+
case "alter the future":
425+
return Card.ALTER_THE_FUTURE;
426+
case "nope":
427+
return Card.NOPE;
428+
case "taco cat":
429+
return Card.TACO_CAT;
430+
case "beard cat":
431+
return Card.BEARD_CAT;
432+
case "rainbow cat":
433+
return Card.RAINBOW_CAT;
434+
case "feral cat":
435+
return Card.FERAL_CAT;
436+
case "hairy potato cat":
437+
return Card.HAIRY_POTATO_CAT;
438+
case "exploding kitten":
439+
return Card.EXPLODE;
440+
case "imploding kitten":
441+
return Card.IMPLODE;
442+
case "defuse":
443+
return Card.DEFUSE;
444+
default:
445+
throw new IllegalArgumentException("Could not parse input.");
446+
}
447+
}
450448
}

0 commit comments

Comments
 (0)