/****************************************************************************** * Compilation: javac Player.java * Execution: java -cp .:cards.jar Player * Dependencies: Card.java * * Implement a player that holds a pile of cards. * * % java Player * North 7C 6D JS * ******************************************************************************/ public class Player { final static int MAX_CARDS = 52; private Card[] cards; // the cards private int N; // number of cards private int x, y; // location to draw piles private String name; // player's name public Player(String name, int x, int y) { this.name = name; this.x = x; this.y = y; this.N = 0; this.cards = new Card[MAX_CARDS]; } public void dealTo(Card c) { cards[N++] = c; } // insertion sort cards in descending order public void sort() { for (int i = 0; i < N; i++) { for (int j = i; j > 0; j--) { if (cards[j-1].less(cards[j])) { Card swap = cards[j]; cards[j] = cards[j-1]; cards[j-1] = swap; } } } } // draw the pile of cards, with the first one centered at (x, y) public void draw() { for (int i = 0; i < N; i++) { // 17 seems like a good intercard distance cards[i].drawFront(x + i*17, y); } } public String toString() { String s = name + " "; for (int i = 0; i < N; i++) s = s + cards[i] + " "; return s; } // calculate points in a bridge hand public int points() { int sum = 0; // calculate high card points (Ace = 4, King = 3, Queen = 2, Jack = 1) for (int i = 0; i < N; i++) { int rank = cards[i].rank(); if (rank == 12) sum = sum + 4; else if (rank == 11) sum = sum + 3; else if (rank == 10) sum = sum + 2; else if (rank == 9) sum = sum + 1; } // calculate points for suits with 0 or 1 card int[] suits = new int[4]; for (int i = 0; i < N; i++) suits[cards[i].suit()]++; for (int j = 0; j < 4; j++) { if (suits[j] == 0) sum = sum + 2; else if (suits[j] == 1) sum = sum + 1; } return sum; } // test client public static void main(String[] args) { Player N = new Player("North", 375, 650); N.dealTo(new Card(17)); N.dealTo(new Card(5)); N.dealTo(new Card(48)); // sort by suit, and then rank N.sort(); StdOut.println(N); } }