public class Card {
private String number;
private String color;
//每张牌的大小
private int size;
@Override
public String toString() {
return color+number;
}
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;
}
public Card() {
}
public Card(String number, String color, int size) {
this.number = number;
this.color = color;
this.size = size;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Room {
//必须有一副牌
List<Card> allCards = new ArrayList<>();
public Room(){
//做出24张牌,存入allCards
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","1","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);
}
}
allCards.add(new Card("","小王",size++));
allCards.add(new Card("","大王",size++));
System.out.println("新牌:"+allCards);
}
public void start() {
//洗牌
Collections.shuffle(allCards);
System.out.println("洗牌后:"+allCards);
//发牌,定义三个玩家
List<Card> people11 = new ArrayList<>();
List<Card> people22 = new ArrayList<>();
List<Card> people33 = new ArrayList<>();
for (int i = 0; i < allCards.size()-3; i++) {
Card c= allCards.get(i);
if(i%3==0){
people11.add(c);
}else if(i%3==1){
people22.add(c);
}else{
people33.add(c);
}
}
//对三个玩家的牌进行排序
sortCards(people11);
sortCards(people22);
sortCards(people33);
//看牌
System.out.println("第一个玩家:"+people11);
System.out.println("第二个玩家:"+people22);
System.out.println("第三个玩家:"+people33);
System.out.println("三张底牌:"+allCards.subList(allCards.size()-3,allCards.size()));
List<Card> lastThreeCards = allCards.subList(allCards.size()-3,allCards.size());
//一号玩家抢到了地主
people11.addAll(lastThreeCards);
sortCards(people11);
System.out.println("第一个玩家抢到地主后:"+people11);
}
//进行排序
private void sortCards(List<Card> cards) {
Collections.sort(cards, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o1.getSize()-o2.getSize();
}
});
}
}
public class GameDemo {
public static void main(String[] args) {
Room room = new Room();
//启动游戏
room.start();
}
}