Compare Poker Card Value
Introduction
To demonstrate the power of object oriented programming, I write a Java program to compare the value of 2 poker cards, using inheritance.
For normal card, from small to large 2 3 4 ... 10 K Q K Ace, eg. 2 of diamond < 3 of diamond For big2 game, 2 is the biggest value, 3 is the smallest. 3 4 5 ... Q K A 2, eg. 2 of diamond > 3 of diamond
I do not create 2 different classes for normal card and big2 card, but inherited the big2 card from normal card (standard card). Many of the properties of the 2 classes are the same. New big2 card comparison can be created with little code added to the new class.
To simplify the program, I do not shuffle the cards, just use the first 2 cards for comparison. You can modified to compare 2 random cards.
class Card { … }, class CompareNormalCard use Card.
class BigTwoCard extends Card { … }, class CompareBigTwoCard use BigTwoCard.
For normal card, from small to large 2 3 4 ... 10 K Q K Ace, eg. 2 of diamond < 3 of diamond For big2 game, 2 is the biggest value, 3 is the smallest. 3 4 5 ... Q K A 2, eg. 2 of diamond > 3 of diamond
I do not create 2 different classes for normal card and big2 card, but inherited the big2 card from normal card (standard card). Many of the properties of the 2 classes are the same. New big2 card comparison can be created with little code added to the new class.
To simplify the program, I do not shuffle the cards, just use the first 2 cards for comparison. You can modified to compare 2 random cards.
class Card { … }, class CompareNormalCard use Card.
class BigTwoCard extends Card { … }, class CompareBigTwoCard use BigTwoCard.
Card.java
$ edit Card.java class Card { private int id; // 0-51, id and value are the same for normal card protected int f, v; // f is diamond, club etc, v is 0-9TJKQA // use protected because the value was referred inside sub class Card() {} // constructor Card(int n) { this.setId(n); } void setId(int n) { this.id= n%52; // id not >= 52 f= this.id/13; // pre-calculated for getValue(), to save time v= this.id%13; } int getId() { return this.id; } // value and id are the same for normal card int getValue() { return this.id; } // return same value==0, this card smaller -1, this card bigger 1 int isCompare(Card card) { if (this.getValue()>card.getValue()) return 1; else if (this.getValue()<card.getValue()) return -1; return 0; } // for display as D2 DJ DQ DK DA, C2 CJ ... // D2 is 2 of diamond String getString() { int f; // suit 0-Diamond, 1-Club, 2-Heart, 3-Spade int v; // 0-2, 1=3... 8=T 9=J 10=Q 11=K 12=A String pattern, number; f= id/13; // duplicate in setId(). f is protected, can be changed, but id is private v= id%13; switch(f) { case 0: pattern= "D"; break; case 1: pattern= "C"; break; case 2: pattern= "H"; break; case 3: pattern= "S"; break; default: pattern= "x"; } if (v>=0 && v<8) number= "" + (v+2); else if (v==8) number= "T"; else if (v==9) number= "J"; else if (v==10) number= "Q"; else if (v==11) number= "K"; else if (v==12) number= "A"; else number= "0"; return pattern+number; } }
CompareNormalCard.java
$ edit CompareNormalCard.java public class CompareNormalCard { public static void main(String[] args) { Card card1= new Card(0), card2= new Card(1); String symbol; if (card1.isCompare(card2)>0) symbol= ">"; else symbol= "<"; System.out.println(card1.getString() + symbol + card2.getString()); } }
$ javac CompareNormalCard.java && java CompareNormalCard
==>D2<D3
==>D2<D3
Note: Diamond 2 is smaller than diamond 3
to be continue...CompareBigTwoCard.java