All pastes #709620 Raw Edit

Miscellany

public text v1 · immutable
#709620 ·published 2007-09-24 20:34 UTC
rendered paste body
import java.util.Random;
public class Cards {
    private String[] cards={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
    private String[] suits={"Hearts","Diamonds","Spades","Clubs"};
    private int[][] deck;
    public Cards() {
        deck = new int[13][4];
        for (int card = 0;card < 13;card++) {
            for (int suit = 0; suit < 4; suit++) {
                deck[card][suit] = 0;
            }
        }
    }
    public String dealCard() {
      Random r = new Random();
      int cardValue = r.nextInt(12);  
      int suitValue = r.nextInt(3);
      while (deck[cardValue][suitValue] != 0) {
          dealCard();
      }
      deck[cardValue][suitValue] = 1;
      return cards[cardValue] + " of " + suits[suitValue];
    }
}


---------


public class CardsTester {
    public static void main (String [] args) {
        Cards c = new Cards();
        for (int i = 0; i < 50; i++) {
            System.out.println(c.dealCard());
        }
    }
}