GameDemo_fapai类
public class GameDemo_fapai {
/**
* 分析业务需求
* 总共有54张牌
* 点数:"3""4""5""6""7""8""9""10"以""Q""K""A""2
* 花色:"♣", "♦", "♥", "♠"
* 大小王:"大🤡“ ”小🤡“
* 斗地主:发出51张牌,剩下3张做为底牌。
*/
public static void main(String[] args) {
Room room = new Room();
room.start();
}
}
Room类
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Room {
//必须有一副牌
private List<Card> allCard = new ArrayList<>();
public Room(){
//1.54张牌
//a.点数
String[] Number = new String[]{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//b.花色
String[] color = new String[]{"♣", "♦", "♥", "♠"};
//c.遍历点数,再遍历花色
int size=0;
for (String num : Number) {
size++;
for (String col : color) {
allCard.add(new Card(num,col,size));
}
}
Card card1 = new Card("","大🤡",++size);
Card card2 = new Card("","小🤡",++size);
Collections.addAll(allCard,card1,card2);
System.out.println("新牌"+allCard);
}
public void start() {
ArrayList<Card> zhangsan = new ArrayList<>();
ArrayList<Card> lisi = new ArrayList<>();
ArrayList<Card> wangwu= new ArrayList<>();
//洗牌
Collections.shuffle(allCard);
System.out.println("洗牌后"+allCard);
//发牌
for (int i = 0; i < allCard.size()-3; i++) {
Card card = allCard.get(i);
if(i%3==0){
zhangsan.add(card);
}else if(i%3==1){
lisi.add(card);
} else if(i%3==2){
wangwu.add(card);
}
}
//理牌
Collections.sort(zhangsan);
Collections.sort(lisi);
Collections.sort(wangwu);
//看牌
System.out.println("张三的牌"+zhangsan);
System.out.println("李四的牌"+lisi);
System.out.println("王五的牌"+wangwu);
List<Card> dizhu = allCard.subList(allCard.size() - 3, allCard.size());
System.out.println(dizhu);
lisi.addAll(dizhu);
Collections.sort(lisi);
System.out.println("李四抢完地主"+lisi);
}
}
Card类
public class Card implements Comparable{
private String number;
private String color;
private int size;
public Card() {
}
public Card(String number, String color, int size) {
this.number = number;
this.color = color;
this.size = size;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Override
public String toString() {
return color+number;
}
@Override
public int compareTo(Object o) {
Card a = (Card) o;
return (this.size-a.size);
}
}