I’ve learned Java for 1 month. This time, I’d like to create a poker game. There are two questions about my program. I hope somebody can help me to fix it.
this is probably more than you need, but you can do a lot with enums:
package sample;
import java.util.*;
enum Rank {
joker(new Character('*'),"Joker"),aceLow(new Character('a'),"Ace(low)"),deuce(new Character('2'),"Deuce"),trey(new Character('3'),"Trey"),four(new Character('4'),"Four"),five(new Character('5'),"Five"),six(new Character('6'),"Six"),seven(new Character('7'),"Seven"),eight(new Character('8'),"Eight"),nine(new Character('9'),"Nine"),ten(new Character('T'),"Ten"),jack(new Character('J'),"Jack"),queen(new Character('Q'),"Queen"),king(new Character('K'),"King"),aceHigh(new Character('A'),"Ace");
Rank(final Character c,final String s) {
this.c=c;
this.s=s;
}
public static Rank fromInt(int n) {
for(Rank r:Rank.values())
if(r.ordinal()==n)
return r;
return null;
}
public static Rank fromCharacter(char c) {
for(Rank r:Rank.values())
if(r.c==c)
return r;
return null;
}
public static Rank[] fromCharacters(final String cards) {
final Rank[] rank=new Rank[5];
for(int i=0;i<5;i++)
rank[i]=Rank.fromCharacter(cards.charAt(i));
return rank;
}
public Character toCharacter() {
return c;
}
public String toString() {
return s;
}
public static String toString(final Rank[] rank) {
String s="";
for(Rank r:rank)
s+=r.toCharacter();
return s;
}
final Character c;
final String s;
}
enum Suit {
joker(new Character('*'),"Joker"),clubs(new Character('c'/* try character for a real dimond? */),"Club"),diamonds(new Character('d'),"Diamond"),hearts(new Character('h'),"Heart"),spades(new Character('s'),"Spade");
Suit(final Character c,final String s) {
this.c=c;
this.s=s;
}
public static Suit fromCharacter(char c) {
for(Suit s:Suit.values())
if(s.c==c)
return s;
return null;
}
public Character toCharacter() {
return c;
}
public String toString() {
return s;
}
public static String toString(final Suit[] suit) {
String s_="";
for(Suit s:suit)
s_+=s.toCharacter();
return s_;
}
public static boolean areSuited(final Suit[] suit) {
final int n=suit.length;
for(int i=0;i0)
s+=", ";
s+=card[i].toCharacters();
}
return s;
}
public String toCharacters() {
return ""+rank().toCharacter()+suit().toCharacter();
}
public static Card instance(Rank rank,Suit suit) { // some hack for speed
final int n=4*rank.ordinal()+suit.ordinal();
//System.out.println(rank+" "+suit+" "+n);
return element[4*rank.ordinal()+suit.ordinal()];
}
static Card[] create(int standards,int jokers) {
int cards=0;
Card card[]=new Card[standards*52+jokers];
for(int i=0;i