public static void main(String[] args) {
Room m = new Room();
//游戏启动,开始洗牌、发牌
m.start();
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Room {
//一副牌
private List<Card> allCards = new ArrayList<>();
public Room(){
//做出54张牌,存入集合allCards
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] colors = {"♠","♥","♣","♦"};
int size = 0;//每张牌的大小
//遍历点数,再遍历花色
for (String number : numbers) {
size++;
for (String color : colors) {
Card c = new Card(number,color,size);
allCards.add(c);
}
}
//单独存入大小王
Card smallKing = new Card("","小王",++size);
Card bigKing = new Card("","大王",++size);
Collections.addAll(allCards,smallKing,bigKing);
System.out.println("新牌" + allCards);
}
/**
* 游戏启动
*/
public void start() {
//1.洗牌allCards
Collections.shuffle(allCards);
System.out.println("洗牌后" + allCards);
//2.发牌 定义三个玩家, ArrayList定义
List<Card> cxk = new ArrayList<>();
List<Card> lkh = new ArrayList<>();
List<Card> wyf = new ArrayList<>();
//正式发牌给这三个玩家,依次发出51张牌,剩余3张做底牌
for (int i = 0; i < allCards.size(); i++) {
Card c = allCards.get(i);
//判断发牌给谁
if(i % 3 == 0){
cxk.add(c);
}else if(i % 3 == 1){
lkh.add(c);
}else if(i % 3 == 2){
wyf.add(c);
}
}
//3.对三个玩家的牌进行排序
sortCards(cxk);
sortCards(lkh);
sortCards(wyf);
//4.看牌
System.out.println("阿坤" + cxk);
System.out.println("阿翰" + lkh);
System.out.println("阿凡" + wyf);
List<Card> lastThreeCards = allCards.subList(allCards.size() - 3, allCards.size());//51 52 53
System.out.println("底牌" + lastThreeCards);
lkh.addAll(lastThreeCards);
sortCards(lkh);
System.out.println("阿翰抢到地主后:" + lkh);
}
//给牌进行排序
private void sortCards(List<Card> cards) {
Collections.sort(cards, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o2.getSize() - o1.getSize();//降序排序
}
});
}
}
public class Card {
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 + "--->" + size;
}
}