import hsa.Console;

public class Bridge extends Console {
    private Deck deck;

    /**
     * The four people playing the game.
     */
    private Player[] players;

    public static final int NORTH = 0;
    public static final int EAST  = 1;
    public static final int SOUTH = 2;
    public static final int WEST  = 3;

    public Bridge() {
        super();
        this.deck = new Deck();
        this.deck.shuffle();

        this.players = new Player[4];
        for (int i = 0; i < 4; i++) {
            this.players[i] = new Player(this.deck.deal(13));
        }
    }

    /**
     * Display a hand of cards
     *
     * @param hand the player's cards
     */
    public void show(Card[] hand) {
        this.print("Your hand: ");
        for (Card c : hand) {
            this.print(c + " ");
        }

        this.println();
    }

    public Card readCard(String prompt) {
        this.print(prompt);

        String s = "";
        Card c;
        try {
            s = this.readLine(); // Read the input
            c = new Card(s); // Try to convert it to a Card
        } catch (MalformedCardException e) { // If the user entered a bad string
            this.print(s + " is not a valid card. "); // Print an error message
            c = this.readCard(prompt); // Try again
        }

        return c;
    }

    /**
     * Play a trick, retrieving a card from each player.
     *
     * @param first the first player to go, one of Bridge.NORTH, Bridge.EAST,
     *              Bridge.SOUTH, and Bridge.WEST
     * @param trump the trump suit, one of Card.CLUBS, Card.DIAMONDS,
     *              Card.DIAMONDS, and Card.SPADES
     * @return the player that won
     */
    public int trick(int first, int trump) {
        Card[] played = new Card[4];
        Card entered;
        int pos;

        boolean valid;

        boolean firstCard = true;
        for (int i = first; i != (first+4) % 4 || firstCard; i = (i+1) % 4) {
            this.clear();
            this.println("Press a key to start the turn.");
            this.getChar();
            this.clear();

            // Print the cards played so far
            for (int j = 0; j < 4; j++) {
                if (j == NORTH)      { this.print("North: "); }
                else if (j == EAST)  { this.print("East: "); }
                else if (j == SOUTH) { this.print("South: "); }
                else if (j == WEST)  { this.print("West: "); }

                this.println(played[j] != null ? played[j].toString() : "");
            }
            this.println();

            this.show(this.players[i].hand()); // Print the hand

            entered = this.readCard("Choose a card: ");
            pos = this.players[i].find(entered);

            // Validate card
            for ( ; pos < 0; pos = this.players[i].find(entered)) {
                this.print(entered.toString() + " is not in your hand. ");
                entered = this.readCard("Choose a card: ");
            }

            played[i] = this.players[i].playCard(pos);

            firstCard = false;
        }

        this.clear();
        this.println("North: " + played[0]);
        this.println("East: " + played[1]);
        this.println("South: " + played[2]);
        this.println("West: " + played[3]);
        this.getChar();

        return 0;
    }

    public static void main(String[] args) {
        Bridge game = new Bridge();
        int lead = 3;
        for (int i = 0; i < 13; i++) {
            lead = game.trick(lead, 0);
        }
        game.close();
    }
}

@@ -1754,130 +1754,217 @@
 Card
-%5B%5D playe
+ entered, lead = null; // lea
 d =
+=
  n
-ew Card%5B4%5D;%0A        Card entered;%0A        int pos;%0A%0A        boolean valid;%0A%0A        boolean firstCard = true;
+ull indicates this is the first trick%0A        Card%5B%5D played = new Card%5B4%5D;%0A        int pos;%0A%0A        boolean firstCard = true; // Needed so that the for repeats at least once
 %0A   
@@ -2866,15 +2866,56 @@
 ( ; 
-pos %3C 0
+!Bridge.playable(entered, this.players%5Bi%5D, lead)
 ; po
@@ -3005,26 +3005,22 @@
 + %22 
-is not in your han
+can't be playe
 d. %22
@@ -3149,24 +3149,89 @@
 Card(pos);%0A%0A
+            if (firstCard) %7B%0A                lead = entered;%0A    
             
@@ -3249,16 +3249,30 @@
  false;%0A
+            %7D%0A
         
@@ -3516,32 +3516,600 @@
 eturn 0;%0A    %7D%0A%0A
+    /**%0A     * Check if it's legal to play a card.%0A     *%0A     * @param check the %7B@code Card%7D to test%0A     * @param p the person playing the card, before removing it from their hand%0A     * @param lead the %7B@code Card%7D lead, or %7B@code null%7D if this is the first%0A     *             card of the trick%0A     */%0A    public static boolean playable(Card check, Player p, Card lead) %7B%0A        int leadSuit = lead == null ? check.suit() : lead.suit(); // Any suit can be played if this is the card lead%0A%0A        return !(p.find(check) %3E= 0 && check.suit() != leadSuit);%0A    %7D%0A%0A
     public stati

Check if the card played followed suit.