需求:
使用面向对象完成扑克牌的生成,分别对应3位玩家,每人17张牌,底牌3张。最后再将手牌以点数、花色"♠,♥,♣,♦"降序排列
思路:
涉及到面向对象我们需要创建牌堆作为一个对象,拥有花色、点数俩个属性。
1.生成牌堆
定义花色和点数为一个数组,.split(",")是以逗号分开再放进数组,和一个一个个写"3","4","5"同样的。在上方定义poker集合,双for循环new Card的同时分别把花色和点数赋给集合当中,循环条件长度可以互换,这里效果是:♣2 , ♦A , ♥A ,但注意放入集合时,需要对应放入i 和 j,否则会出现下标越界,由于大小王没有花色,需要单独在添加完其他排后添加
2.洗牌
调用Collections的shuffle();方法,传入集合参数,他会自动打乱集合里的数据,不会产生新的集合,打印洗好牌之后的牌,这里在Card对象里重写了toString方法,只打ians
3.发牌
使用poker.subList(从哪个下标开始截取;结束),遵循前闭后开,0~17,只取0~16,得到每个人的牌,底牌为3张
4.排序
由于有3副牌堆,加上底牌,我们进行排序就需要定义一个方法,传入每个人的手牌集合。在这之前,我们需要考虑用什么来进行排序,因为扑克牌的特殊性,3-10,且2比这些牌都大。我们需要使用到一个下标,所以在Card类中定义了点数的下标和花色的下标,就可以同时完成对点数以及花色的排序。在生成牌堆时利用之前的for循环把下标加入进去,由于大小王没有花色,所以把花色定义为空的同时花色下标也为0(升序0排在第一位)。
我们再来说一说排序匿名内部类里面的逻辑,首先通过传入每个人手牌的集合调用sort方法,同时new Comparator<Card>()定义泛型为Card,产生内部类
这时不用太关注compare传入的参数是什么,我们只需要写出顺序返回值就可以了,当o2.getPointIndex() == o1.getPointIndex时,说明点数重复我们就需要按照花色来进行排序,返回点数排序的顺序值 return o2.getTypeIndex() - o1.getTypeIndex();,另一种情况就是点数不同,直接将点数返回降序值即可 return o2.getPointIndex() - o1.getPointIndex();
实现代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class DouDiZhuMain {
private List<Card> poker = new ArrayList<>();
public static void main(String[] args) {
DouDiZhuMain douDiZhuMain = new DouDiZhuMain();
douDiZhuMain.GenerateTheCard();//生成
douDiZhuMain.shufflePoker();//洗牌
douDiZhuMain.DeingPoker();//发牌
}
//生成牌堆
public void GenerateTheCard() {
String point[] = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
String type[] = "♠,♥,♣,♦".split(",");
for (int i = 0; i < point.length; i++) {
for (int j = 0; j < type.length; j++) {
poker.add(new Card(point[i], type[j], i, j));
}
}
poker.add(new Card("小王", "", 13, 0));
poker.add(new Card("大王", "", 14, 0));
}
//洗牌
public void shufflePoker() {
Collections.shuffle(poker);
System.out.println(poker);
}
//发牌
public void DeingPoker() {
List player1 = poker.subList(0, 17);
List player2 = poker.subList(17, 34);
List player3 = poker.subList(34, 51);
List cardPoker = poker.subList(51, 54);
sort(player1);
sort(player2);
sort(player3);
sort(cardPoker);
System.out.println("玩家一:" + player1 + "\n玩家二:" + player2 + "\n玩家三:" + player3 + "\n底牌:" + cardPoker);
}
//排序
public void sort(List list) {
list.sort(new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
if (o2.getPointIndex() == o1.getPointIndex()) {
return o2.getTypeIndex() - o1.getTypeIndex();
} else {
return o2.getPointIndex() - o1.getPointIndex();
}
}
});
}
}
public class Card {
private String point;
private String type;
private int pointIndex;
private int typeIndex;
public Card() {
}
public Card(String point, String type) {
this.point = point;
this.type = type;
}
public Card(String point, String type, int pointIndex, int typeIndex) {
this.point = point;
this.type = type;
this.pointIndex = pointIndex;
this.typeIndex = typeIndex;
}
@Override
public String toString() {
return this.type + this.point + " ";//打印时只打印花色和值
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPointIndex() {
return pointIndex;
}
public void setPointIndex(int pointIndex) {
this.pointIndex = pointIndex;
}
public int getTypeIndex() {
return typeIndex;
}
public void setTypeIndex(int typeIndex) {
this.typeIndex = typeIndex;
}
}
(本文为接触Java不久的菜鸟所写,如有误区,还望各位大佬多多指教,菜鸟虚心受教)