import java.util.Scanner; public class Player { private Deck hand; private boolean human; public Player(boolean human) { this.human = human; hand = new Deck(true); } public void giveCard(Card c, boolean tell) { hand.add(c); // for human players, we print the card that was drawn if (tell && human) { System.out.println("You drew the " + c); } } public boolean hasWon() { return hand.getSize() == 0; } public int numCards() { return hand.getSize(); } // called when it's players turn to play a card // if no card can be playerd, we return null public Card playCard(Card upCard) { if (human) { System.out.println("Up card is: " + upCard); System.out.println("Hand:"); System.out.println("0. Draw Card"); hand.print(); // keep looping until they choose a valid option while (true) { System.out.println("What card to play?"); Scanner in = new Scanner(System.in); int position = in.nextInt(); // they want a card instead if (position == 0) { return null; } if (position > 0 && position <= hand.getSize()) { return hand.take(position - 1); } } } else { // this is the AI for non-human players // do the first non-wild card that matches for (int i = 0; i < hand.getSize(); i++) { Card c = hand.get(i); if (c.getRank() == 8) { continue; } if (c.getRank() == upCard.getRank() || c.getSuit() == upCard.getSuit()) { return hand.take(i); } } // if we have an 8 return that for (int i = 0; i < hand.getSize(); i++) { if (hand.get(i).getRank() == 8) { return hand.take(i); } } // none matched, return null return null; } } }