Question: create a test code for this class import java.util.ArrayList; import java.util.Arrays; import java.util.Objects; import java.util.Stack; public class ExtraCards extends Player { public ExtraCards(Card[] cards) {
create a test code for this class
import java.util.ArrayList; import java.util.Arrays; import java.util.Objects; import java.util.Stack;
public class ExtraCards extends Player {
public ExtraCards(Card[] cards) { this.hand = new ArrayList<>(Arrays.asList(cards)); }
/* play a card */ public boolean play(DiscardPile discardPile, Stack drawPile, ArrayList players) {
// go through all cards in the hand, and play the first valid one for (int i = 0; i < hand.size(); i++) { if (Objects.equals(hand.get(i).rank, discardPile.top().rank) || Objects.equals(hand.get(i).suit, discardPile.top().suit) || Objects.equals(hand.get(i).rank, Card.RANKS[8])) { discardPile.add(this.hand.remove(i)); return this.hand.size() == 0; } }
// pick up a card from the draw pile, if it isn't empty if (!drawPile.isEmpty()) { pickupCard(drawPile); return false; }
// nothing left to do, so pass System.out.println("Passing!"); return this.hand.size() == 0; } }
where the test code have to be valid in other for the game to work we are given card, player (abstract), Discardpile, crazy8 program.
ExtraCards will risk taking cards from the draw pile in an effort to get power cards. They will be clever with this though. If the next player only has 1 card left, they will keep picking a card until they get a power card (if they do not already have one) so that they can try to prevent the next player from winning. They will not take more than one extra card in the early rounds of the game. They will not take extra cards if they already have power cards in their hand.
and it must work with the Crazy8Game
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Stack;
public class Crazy8Game {
public static void main(String[] args) {
/* create the deck */
Card[] deck = new Card[52];
int index = 0;
for (int r = 2; r <= 14; r += 1) {
for (int s = 0; s < 4; s += 1) {
deck[index++] = new Card(Card.SUITS[s], Card.RANKS[r]);
}
}
/* shuffle the deck */
Random rnd = new Random();
Card swap;
for (int i = deck.length - 1; i >= 0; i = i - 1) {
int pos = rnd.nextInt(i + 1);
swap = deck[pos];
deck[pos] = deck[i];
deck[i] = swap;
}
/* players in the game */
ArrayList
players.add(new BadPlayer(Arrays.copyOfRange(deck, 0, 5)));
System.out.println("Player 0's hand: " + players.get(0));
players.add(new BadPlayer(Arrays.copyOfRange(deck, 5, 10)));
System.out.println("Player 1's hand: " + players.get(1));
players.add(new BadPlayer(Arrays.copyOfRange(deck, 10, 15)));
System.out.println("Player 2's hand: " + players.get(2));
/* discard and draw piles */
DiscardPile discardPile = new DiscardPile();
Stack
for (int i = 15; i < deck.length; i++) {
drawPile.push(deck[i]);
}
boolean win = false;
int player = -1; // start game with player 0
discardPile.add(drawPile.pop());
System.out.println("Draw pile is: " + drawPile);
System.out.println("Discard pile is: " + discardPile);
while (!win) {
player = (player + 1) % players.size();
System.out.println(" It is player " + player + "'s turn!");
System.out.println("Their hand: " + players.get(player));
Card topDraw = drawPile.isEmpty() ? new Card("None", "None") : drawPile.peek();
System.out.println("Top of draw pile: " + topDraw);
System.out.println("Top of discard pile: " + discardPile.top());
win = players.get(player).play(discardPile, drawPile, players);
// topDraw = drawPile.isEmpty() ? new Card("None", "None") : drawPile.peek();
// System.out.println("Top of draw pile: " + topDraw);
// System.out.println("Top of discard pile: " + discardPile.top());
}
System.out.println(" -------------------- Winner is player " + player + "!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
