Skip to content

Instantly share code, notes, and snippets.

@dflemstr
Created May 26, 2010 18:41
Show Gist options
  • Save dflemstr/414869 to your computer and use it in GitHub Desktop.
Save dflemstr/414869 to your computer and use it in GitHub Desktop.
public class Card {
private int number, colors;
private static String[] Colors = { "Hearts", "Spades", "Diamonds", "Clubs" };
private static String[] Number = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Knave", "Queen", "King" };
Card(int Cardcolors, int Cardnumber){
this.number=Cardnumber;
this.colors=Cardcolors;
}
public @Override String toString(){
return Number[number] + " of " + Colors[colors];
}
public int getRank() {
return number;
}
public int getSuit() {
return colors;
}
}
import java.util.Random;
public class Deck {
private Card[] cards;
int i;
Main m = new Main();
int y = m.GetInput();
Deck(){
i=y;
cards = new Card[52];
int x=0;
for (int a=0; a<=3; a++){
for (int b=0; b<=12; b++){
cards[x] = new Card(a,b);
x++;
}
}
}
public Card drawFromDeck(){
Random generator = new Random();
int index=0;
do{
index = generator.nextInt( 52 );
} while (cards[index] == null);
i--;
Card temp = cards[index];
cards[index]= null;
return temp;
}
public int getTotalCards(){
return i;
}
}
import java.util.*;
public class Main {
Scanner s = new Scanner(System.in);
public int GetInput(){
System.out.println("How many cards would you like to draw?");
String p = s.nextLine();
int y = Integer.parseInt(p);
return y;
}
public static void main(String[] args) {
Deck d = new Deck();
Card C;
while (d.getTotalCards()!= 0 ){
C = d.drawFromDeck();
System.out.println( C.toString() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment