实现思路,定义扑克牌的实体类,定义玩家实体类,写一个主方法进行发牌操作,并且自定义比较器进行牌的排序。
//扑克牌实体类
public class Puke {private String num; //牌号private String huase;//花色public Puke(String num, String huase) {super();this.num = num;this.huase = huase;}public Puke() {super();}public String getNum() {return num;}public void setNum(String num) {this.num = num;}public String getHuase() {return huase;}public void setHuase(String huase) {this.huase = huase;}@Overridepublic String toString() {return "Puke [num=" + num + ", huase=" + huase + "]";}}
import java.util.List;//玩家实体类
public class Player {private int id;private String name; //名字private List<Puke> card; //手牌public Player(int id, String name, List<Puke> card) {super();this.id = id;this.name = name;this.card = card;}public Player() {super();}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 List<Puke> getCard() {return card;}public void setCard(List<Puke> card) {this.card = card;}@Overridepublic String toString() {return "Player [id=" + id + ", name=" + name + ", card=" + card + "]";}}
import java.util.Comparator;//自定义比较器
public class MyComparator implements Comparator<Puke> {@Overridepublic int compare(Puke pu1, Puke pu2) {MyComparator myComparator = new MyComparator(); // TODO Auto-generated method stub//比较两个数字的大小//num正数pu1 > pu2 num=0 pu1 > pu2 num负数 pu1 < pu2int num = pu1.getNum().compareTo(pu2.getNum());//如果两个牌数字相同,则进行花色比较if (num == 0) {//黑桃>红桃>梅花>方块if (myComparator.compareToFlower(pu1) > myComparator.compareToFlower(pu2)) {num = -1;}else if (myComparator.compareToFlower(pu1) < myComparator.compareToFlower(pu2)) {num = 1;}}return num;}//定义比较规则public int compareToFlower(Puke pk1) {int num1 = 0;switch (pk1.getHuase()) {case "黑桃":num1 = 4;break;case "红桃":num1 = 3;break;case "梅花":num1 = 2;break;case "方块":num1 = 1;break;default:num1 = -1;break;}return num1;}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;public class FaPai {public static void main(String[] args) {//存储牌List<Puke> pukes = new ArrayList<Puke>();String huase1[] = {"红桃","黑桃","梅花","方块"};String jqk[] = {"J","Q","K","A"};//给牌赋值for (int i = 2; i < 11; i++) {for (int j = 0; j < huase1.length; j++) {Puke puke = new Puke();puke.setNum(""+i);puke.setHuase(huase1[j]);pukes.add(puke);}}//给特殊牌赋值for (String teshu : jqk) {for (String hua1 : huase1) {Puke puke = new Puke();puke.setNum(""+teshu);puke.setHuase(hua1);pukes.add(puke);}}//大王小王赋值Puke puke11 = new Puke();puke11.setNum("大王");pukes.add(puke11);Puke puke12 = new Puke();puke12.setNum("小王");pukes.add(puke12);//洗牌Collections.shuffle(pukes);//遍历pukes.forEach(s1 -> System.out.print(" " + s1.getNum()+" "+s1.getHuase()));//发牌Puke[] man1 = new Puke[17];Puke[] man2 = new Puke[17];Puke[] man3 = new Puke[20];Puke[] dizhupai = new Puke[3];//三个玩家Player[] players = {new Player(1,"张三",new ArrayList<>()),new Player(2,"李四",new ArrayList<>()),new Player(3,"王五",new ArrayList<>())};//地主牌for (int i = 0; i < 3; i++) {int random = new Random().nextInt(pukes.size());dizhupai[i] = pukes.get(random);pukes.remove(random);}//发牌for (int i = 0; i < pukes.size(); i++) {players[i%3].getCard().add(pukes.get(i));}//判断地主int dizhu = new Random().nextInt(3);//给地主发牌for (int i = 0; i < dizhupai.length; i++) {players[dizhu].getCard().add(dizhupai[i]);}//进行排序for (Player player : players) {//排序List<Puke> card2= player.getCard().stream().sorted(new MyComparator()).collect(Collectors.toList());//修改cardplayer.setCard(card2);}System.out.println();for (Player player : players) {System.out.println(player.toString());}}
}
运行结果: