实现扑克牌 就是斗地主欢迎大家讨论,
实现扑克牌类Card实现
1 创建54张扑克牌,
2 洗牌
3 发牌到3个Player人
Player属性: String name, Card[] cards = {};
人每次接收拍时候需要扩展cards数组
4 实现抽取3张牌的算法
另求,JAVA的学习方法!!!!!!!!!!!! --------------------编程问答-------------------- 这里需要建立三个类:扑克牌Card 玩家Player 洗牌生成器CardDemo 代码如下:
public class Card {
private int rank;//huashu
private int suit;//dianshu
//点数
public static final int CLUB=0;//方块
public static final int DIAMOND=1;//MEI
public static final int HEART=2;//HONG
public static final int SPADE=3;//HEI
public static final int JOKER=4;
//花色
public static final int THREE=0;
public static final int FOUR=1;
public static final int FIVE=2;
public static final int SIX=3;
public static final int SEVEN=4;
public static final int EIGHT=5;
public static final int NINE=6;
public static final int TEN=7;
public static final int JACK=8;//j
public static final int QUEEN=9;//q
public static final int KING=10;//k
public static final int ACE=11;//A
public static final int DEUCE=12;//2
public static final int BLACK=13;//XIAO WANG
public static final int COLOR=14;//DA WANG
public Card(){}
public Card(int rank,int suit){
setRank(rank);
setSuit(suit);
}
private final static String[]RANK_NAMES={"方块","梅花","红桃","黑桃",""};
private final static String[]SUIT_NAMES={"3","4","5","6","7","8","9","10","J","Q","K","A","2","小王","大王"};
public String toString(){
return RANK_NAMES[this.rank]+SUIT_NAMES[this.suit];
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
if (rank>=CLUB && rank<=JOKER){
this.rank = rank;
}else{
System.out.println("rank error");
}
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
if (suit>=THREE && suit<=COLOR){
this.suit = suit;
}else{
System.out.println("suit error");
}
}
@Override
public boolean equals(Object obj) {
if (obj==null)return false;
else if (obj instanceof Card){
Card c=(Card)obj;
return c.rank==this.rank&&c.suit==this.suit;
}
return false;
}
public int hashCode() {
int type=41;
return 31*type+this.rank ;
}
}
、、、、、、、、、、、、、、、、、、、、、
import java.util.ArrayList;
import java.util.List;
public class Player {
// List list=new ArrayList();
private int id;
private String name;
private List cards;
public Player() {
super();
}
public Player(int id, String name, List cards) {
super();
this.setId(id);
this.setName(name);
this.setCards(cards);
}
public List getCard() {
return cards;
}
public void setCards(List cards) {
this.cards = cards;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return id+":"+name+":"+cards;
}
}
,,,,,,,,,,,,,,,,,,,,,,,
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
public class CardDemo {
public static void main(String[] args) {
List<Card> cards=new ArrayList<Card>();
//Cards代表一整套扑克牌
for (int rank=Card.THREE;rank<=Card.DEUCE;rank++){
//每种花色添加3-2张13张牌
cards.add(new Card(Card.CLUB,rank));
cards.add(new Card(Card.DIAMOND,rank));
cards.add(new Card(Card.HEART,rank));
cards.add(new Card(Card.SPADE,rank));
}
cards.add(new Card(Card.JOKER,Card.BLACK));
cards.add(new Card(Card.JOKER,Card.COLOR));
Collections.shuffle(cards);
// System.out.println(cards.size());
List<Card> pl1=new ArrayList<Card>();
List<Card> pl2=new ArrayList<Card>();
List<Card> pl3=new ArrayList<Card>();
for (int i=0;i<cards.size()-3;i=i+3){
pl1.add(cards.get(i));
pl2.add(cards.get(i+1));
pl3.add(cards.get(i+2));
}
for (int i=0;i<cards.size()-3;i++){
cards.remove(i);
i--;
}
Player p1=new Player(1,"james",pl1);
Player p2=new Player(2,"wade",pl2);
Player p3=new Player(3,"pool",pl3);
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
System.out.println("底牌为:"+cards);
//洗牌
//找api Collections类里面有大乱算法
//自己写一个player类,id,name,cards(表示玩家手里的牌)
//List<Player> players=new ArrayList<Player>();
//创建三位玩家
//把现在已经洗好的牌发给三位玩家,并且留三张底牌
//显示各位玩家手里的牌以及底牌
}
} --------------------编程问答-------------------- List<Card> cards=new ArrayList<Card>();
这是什么意思啊,LIST在JAVA中是数组吗??????? --------------------编程问答-------------------- Object 你也用了,我们才学到接口,我的JAVA怎么学的真差,你有什么好的方法啊!!!! --------------------编程问答--------------------
是容器,跟数组差不多,楼主还没学吗<Card>是泛型,就是把Card对象放到list中
我也学的不是很好,主要是多练,多敲代码,时间长了,回过头来,感到这些就比第一次看时容易了。。。 --------------------编程问答--------------------
这是一个接口,ArrayList 是实现List的实现类、ArrayList 是一个数组列表集合类 在java.util包下面。
补充:Java , Java SE