Java Card Game

It’s not always easy to figure out how to approach even simple subjects like card games. Here’s one approach that might work for you. The example is a simple card game where the goal is to draw the highest card. To keep the code simple it’s not graphical and it doesn’t check to see if certain cards have already been drawn from the deck. It also only executes once instead of in a loop until the player is finished playing. A feature that could be added is a loop and a score count and a betting system of some kind. I will include some of these features in other games I make, but for now you have the code. The first three classes are the basic game. Remember to place each class within it’s own *.java file or the program won’t compile. The last two classes are a revised version of those classes to make the game more procedural and adding a technique to check to see whether a card already has been drawn so that the same card can not be drawn twice.

public class CardGame {

    
    public static void main(String[] args) {
        
    Deck mydeck = new Deck();
    
    
    int userdraw = (int)(Math.random() * (53));
    System.out.println(“You drew the… ” + mydeck.cardsindeck[userdraw].name);
    
    
    int computerdraw = (int)(Math.random() * (53));
    System.out.println(“The computer drew the… ” + mydeck.cardsindeck[computerdraw].name);
    
    int usercardvalue = mydeck.cardsindeck[userdraw].value;
    int computercardvalue = mydeck.cardsindeck[computerdraw].value;
    
    if (usercardvalue > computercardvalue){
        System.out.println(“You drew the high card. You win.”);
    }
    else if (usercardvalue < computercardvalue){
        System.out.println(“The computer drew the high card. You lose.”);
    }
    else if (usercardvalue == computercardvalue){
        System.out.println(“The cards the two of you have drawn are of equal value. The game is a draw.”);
    }
    
    }
}

public class Card {
int value;
int suitnumber;
String suit;
String name;

    
    public Card(int v, int sn, String s, String n) {
    value = v;
    suitnumber = sn;
    suit = s;
    name = n;
    }
 
}
public class Deck {
String s = “Spades”;
String h = “Hearts”;
String c = “Clubs”;
String d = “Diamonds”;
Card[] cardsindeck = new Card[54];
{

cardsindeck[0] = new Card(0,0,””, “Joker”);
cardsindeck[1] = new Card(1, 1, s, “Ace of Spades”);
cardsindeck[2]= new Card(2,1,s, “Two of Spades”);
cardsindeck[3] = new Card(3,1,s, “Three of Spades”);
cardsindeck[4] = new Card(4,1,s, “Four of Spades”);
cardsindeck[5] = new Card(5,1,s, “Five of Spades”);
cardsindeck[6] = new Card(6,1,s, “Six of Spades”);
cardsindeck[7] = new Card(7,1,s, “Seven of Spades”);
cardsindeck[8]= new Card(8,1,s, “Eight of Spades”);
cardsindeck[9] = new Card(9,1,s, “Nine of Spades”);
cardsindeck[10] = new Card(10,1,s,”Ten of Spades”);
cardsindeck[11] = new Card(11,1,s,”Jack of Spades”);
cardsindeck[12]= new Card(12,1,s,”Queen of Spades”);
cardsindeck[13] = new Card(13,1,s, “King of Spades”);
cardsindeck[14] = new Card(1,2,h, “Ace of Hearts”);
cardsindeck[15] = new Card(2,2,h, “Two of Hearts”);
cardsindeck[16] = new Card(3,2,h, “Three of Hearts”);
cardsindeck[17] = new Card(4,2,h, “Four of Hearts”);
cardsindeck[18] = new Card(5,2,h, “Five of Hearts”);
cardsindeck[19] = new Card(6,2,h, “Six of Hearts”);
cardsindeck[20] = new Card(7,2,h, “Seven of Hearts”);
cardsindeck[21] = new Card(8,2,h, “Eight of Hearts”);
cardsindeck[22] = new Card(9,2,h, “Nine of Hearts”);
cardsindeck[23] = new Card(10,2,h, “Ten of Hearts”);
cardsindeck[24] = new Card(11,2,h, “Jack of Hearts”);
cardsindeck[25] = new Card(12,2,h, “Queen of Hearts”);
cardsindeck[26] = new Card(13,2,h, “King of Hearts”);
cardsindeck[27] = new Card(1,3,h, “Ace of Clubs”);
cardsindeck[28] = new Card(2,3,h, “Two of Clubs”);
cardsindeck[29] = new Card(3,3,h, “Three of Clubs”);
cardsindeck[30] = new Card(4,3,h, “Four of Clubs”);
cardsindeck[31] = new Card(5,3,h, “Five of Clubs”);
cardsindeck[32] = new Card(6,3,h, “Six of Clubs”);
cardsindeck[33] = new Card(7,3,h, “Seven of Clubs”);
cardsindeck[34] = new Card(8,3,h, “Eight of Clubs”);
cardsindeck[35] = new Card(9,3,h, “Nine of Clubs”);
cardsindeck[36] = new Card(10,3,h, “Ten of Clubs”);
cardsindeck[37] = new Card(11,3,h, “Jack of Clubs”);
cardsindeck[38] = new Card(12,3,h, “Queen of Clubs”);
cardsindeck[39] = new Card(13,3,h, “King of Clubs”);
cardsindeck[40] = new Card(1,4,h, “Ace of Diamonds”);
cardsindeck[41] = new Card(2,4,h, “Two of Diamonds”);
cardsindeck[42] = new Card(3,4,h, “Three of Diamonds”);
cardsindeck[43] = new Card(4,4,h, “Four of Diamonds”);
cardsindeck[44] = new Card(5,4,h, “Five of Diamonds”);
cardsindeck[45] = new Card(6,4,h, “Six of Diamonds”);
cardsindeck[46]= new Card(7,4,h, “Seven of Diamonds”);
cardsindeck[47] = new Card(8,4,h, “Eight of Diamonds”);
cardsindeck[48] = new Card(9,4,h, “Nine of Diamonds”);
cardsindeck[49] = new Card(10,4,h, “Ten of Diamonds”);
cardsindeck[50] = new Card(11,4,h, “Jack of Diamonds”);
cardsindeck[51] = new Card(12,4,h, “Queen of Diamonds”);
cardsindeck[52] = new Card(13,4,h, “King of Diamonds”);
cardsindeck[53] = new Card(0, 0, “”, “Joker”);

}}
public class CardGame {

    static int userdraws, computerdraws;
    public static void main(String[] args) {
    
    Deck mydeck = new Deck();
    dodraw(mydeck);
    int usercardvalue = mydeck.cardsindeck[userdraws].value;
    int computercardvalue = mydeck.cardsindeck[computerdraws].value;
    docompare(usercardvalue, computercardvalue);
    }//ends main method
    
    
    public static void dodraw(Deck mydeck){
        userdraws = userdraw(mydeck);
        computerdraws = computerdraws(mydeck);    
    }//ends dodraw method
    
    public static int userdraw(Deck mydeck){
        int userdraw = (int)(Math.random() * (53));
        mydeck.cardsindeck[userdraw].drawn = true;
        System.out.println(“You drew the… ” + mydeck.cardsindeck[userdraw].name);
        return userdraw;
    }//ends userdraw method
    
    public static int computerdraws(Deck mydeck){
        int computerdraws = (int)(Math.random() * (53));
        while (mydeck.cardsindeck[computerdraws].checkIfDrawn() == true){
            computerdraws = (int)(Math.random() * (53));
        }//ends while which checks to see if card has already been drawn & draws another
        System.out.println(“The computer drew the… ” + mydeck.cardsindeck[computerdraws].name);
        return computerdraws;
    }//ends computerdraws method

    public static void docompare(int usercardvalue, int computercardvalue){
        if (usercardvalue > computercardvalue){
        System.out.println(“You drew the high card. You win.”);
    }//ends if
    else if (usercardvalue < computercardvalue){
        System.out.println(“The computer drew the high card. You lose.”);
    }//ends if
    else if (usercardvalue == computercardvalue){
        System.out.println(“The cards the two of you have drawn are of equal value. The game is a draw.”);
    }//ends if
    }//ends docompare method
}//ends CardGame class

public class Card {
int value;
int suitnumber;
String suit;
String name;
Boolean drawn = false;

public Card(int v, int sn, String s, String n) {
value = v;
suitnumber = sn;
suit = s;
name = n;
}
public boolean checkIfDrawn(){
if (drawn == true){
return true;
}
else return false;
}

}

 

 

Leave a Reply