各位大大们好,我是JAVA新手,现在在做一个简单的扑克游戏的练习,类似升级,但不需要大小王.我在定义基础类时出了问题.我定义的基础类有:Value,Suit,Card,Pack,Pile.
Value是放牌的点数,从A到K
Suit是放四个花色
Card是生成单张牌
Pack是生成整副牌,除了大小王
Pile是玩家手里的牌我现在遇到的问题是,如何生成一副不重复的牌?我开始是用ArrayList作为Pack的容器,但不知道如何确保这一副牌里不重复,后来查了下API文档,发现HashSet是可以保证集合内的元素唯一性,但好像用在这里不太对.我也想过笨方法,就是把这52张牌分别定义在一个集合里,不过好像就有违面对对象的概念.现在是在是不知道该怎么办了.麻烦各位指点一下.下面附我的源码,应该比较垃圾,麻烦大家顺便也给指点下.public enum Value {
/**
* Defines all the value for the cards
*/
TWO ("2"),
THREE ("3"),
FOUR ("4"),
FIVE ("5"),
SIX ("6"),
SEVEN ("7"),
EIGHT ("8"),
NINE ("9"),
TEN ("10"),
JACK ("J"),
QUEEN ("Q"),
KING ("K"),
ACE ("A");
/*
* Defines the field of Class Value
*/
private String value;
/*
* Defines the constructor of Class Value
*/
private Value(String value) {
this.value = value;
}
public String getValue() {
return value:
}
/**
* @return returns the name of this value
*/
public String toString() {
return value;
}}
public enum Suit {
/**
* Defines all the suits for the cards
*/
SPADES("Spades"),
HEARTS("Hearts"),
CLUBS("Clubs"),
DIAMONDS("Diamonds");
/**
* Defines the field of the Class Suit
*/
private String suit;
/**
* Defines the constructor of the Class Suit
* @param suit
*/
private Suit(String suit) {
this.suit = suit;
}
public String getSuit() {
return suit;
}
/**
* @return returns the name of this suit
*/
public String toString() {
return suit;
}}
public class Card {
/**
* Defines the field of the Class Card
*/
private Suit suit;
private Value value;
/**
* Defines the constructor of Class Card
* @param suit the instance suit of the card
* @param value the instance value of the card
*/
public Card(Suit suit, Value value) {
this.suit = suit;
this.value = value;
}
/**
*
* @return
*/
public Suit getSuit() {
return suit;
}
/**
*
* @return
*/
public Value getValue() {
return value;
}
}
public class Pack {
/**
* Defines the field of Class Pack
*/
private ArrayList<Card> pack;
private int index;
/**
* Defines the constructor of Class Pack
*/
public Pack() {
pack = new ArrayList<Card>();
index = 0;
}
public void addCard(Card card) {
for(int i=0; i<52; i++) {
pack.add(card);
}
}
/**
* Shuffle the cards in the pack
*/
public void shuffle() {
Collections.shuffle( pack );
}
/**
* Get the number of cards in the pack
* @return the number of cards in the pack
*/
public int getSizeOfPack() {
return pack.size();
}
/**
* Deal a card from pack to pile
* @return the card to be dealt, or no card to be dealt
*/
public Card dealCard() {
if ( index >= pack.size() )
return null;
else
return (Card) pack.get( index++ );
}
/**
* See the pack is empty
* @return true: empty
*/
public boolean isEmpty() {
if ( index >= pack.size() )
return true;
else
return false;
}
/**
* Restore the pack the full pack status
*/
public void restorePack() {
index = 0;
}
}
Value是放牌的点数,从A到K
Suit是放四个花色
Card是生成单张牌
Pack是生成整副牌,除了大小王
Pile是玩家手里的牌我现在遇到的问题是,如何生成一副不重复的牌?我开始是用ArrayList作为Pack的容器,但不知道如何确保这一副牌里不重复,后来查了下API文档,发现HashSet是可以保证集合内的元素唯一性,但好像用在这里不太对.我也想过笨方法,就是把这52张牌分别定义在一个集合里,不过好像就有违面对对象的概念.现在是在是不知道该怎么办了.麻烦各位指点一下.下面附我的源码,应该比较垃圾,麻烦大家顺便也给指点下.public enum Value {
/**
* Defines all the value for the cards
*/
TWO ("2"),
THREE ("3"),
FOUR ("4"),
FIVE ("5"),
SIX ("6"),
SEVEN ("7"),
EIGHT ("8"),
NINE ("9"),
TEN ("10"),
JACK ("J"),
QUEEN ("Q"),
KING ("K"),
ACE ("A");
/*
* Defines the field of Class Value
*/
private String value;
/*
* Defines the constructor of Class Value
*/
private Value(String value) {
this.value = value;
}
public String getValue() {
return value:
}
/**
* @return returns the name of this value
*/
public String toString() {
return value;
}}
public enum Suit {
/**
* Defines all the suits for the cards
*/
SPADES("Spades"),
HEARTS("Hearts"),
CLUBS("Clubs"),
DIAMONDS("Diamonds");
/**
* Defines the field of the Class Suit
*/
private String suit;
/**
* Defines the constructor of the Class Suit
* @param suit
*/
private Suit(String suit) {
this.suit = suit;
}
public String getSuit() {
return suit;
}
/**
* @return returns the name of this suit
*/
public String toString() {
return suit;
}}
public class Card {
/**
* Defines the field of the Class Card
*/
private Suit suit;
private Value value;
/**
* Defines the constructor of Class Card
* @param suit the instance suit of the card
* @param value the instance value of the card
*/
public Card(Suit suit, Value value) {
this.suit = suit;
this.value = value;
}
/**
*
* @return
*/
public Suit getSuit() {
return suit;
}
/**
*
* @return
*/
public Value getValue() {
return value;
}
}
public class Pack {
/**
* Defines the field of Class Pack
*/
private ArrayList<Card> pack;
private int index;
/**
* Defines the constructor of Class Pack
*/
public Pack() {
pack = new ArrayList<Card>();
index = 0;
}
public void addCard(Card card) {
for(int i=0; i<52; i++) {
pack.add(card);
}
}
/**
* Shuffle the cards in the pack
*/
public void shuffle() {
Collections.shuffle( pack );
}
/**
* Get the number of cards in the pack
* @return the number of cards in the pack
*/
public int getSizeOfPack() {
return pack.size();
}
/**
* Deal a card from pack to pile
* @return the card to be dealt, or no card to be dealt
*/
public Card dealCard() {
if ( index >= pack.size() )
return null;
else
return (Card) pack.get( index++ );
}
/**
* See the pack is empty
* @return true: empty
*/
public boolean isEmpty() {
if ( index >= pack.size() )
return true;
else
return false;
}
/**
* Restore the pack the full pack status
*/
public void restorePack() {
index = 0;
}
}
解决方案 »
- 感觉很奇怪,希望有人帮我看看。
- StringBuilder被过于神话,包括java,.net2个阵营我都看见有人神话他
- 搞JOGL的朋友进来看看
- 关于提高程序代码可维护性指数
- jb里面的UI设计器 放的位置怎么不能动啊
- 过年前高分求购OA(含JAVA源代码)
- java中怎样实现远程监控中的获取对方屏幕?
- 我为什么运行不了sun j2sdkee1.3 doc中的sample :Converter?
- 问一个很蠢的问题:如:void main(String[] args);为什么一定要带上(如String[] args)参数
- java web server2.0那里有下载!谢谢了!最高告诉我如何使用。。。一个初学者
- 这是个什么错误啊!!!
- 如何在JScrollPane放置JPanel
你需要的只是在顺序连号生成的牌中,random一下
循环52张牌,每张牌生成一个5000以内的随机整数,然后把那张牌放在生成的随机数上,然后排序随机数的大小,就可以洗牌了。
用Random类取得随机数。