纸牌
public class PlayingCards {
private static final String[] CARD_VALUE = new String[]{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
private static final String[] CARD_COLOR = new String[]{"红桃", "黑桃", "方片", "梅花"};
public static void main(String[] args) {
try {
System.out.println("============= 请输入玩家多少人?=============");
Scanner scanner = new Scanner(System.in);
int player = scanner.nextInt();
System.out.println("============= 请输入纸牌多少套?=============");
int count = scanner.nextInt();
List<Player> result = PlayingCards.licensing(player, count);
result.forEach(System.out::println);
} catch (Exception e) {
System.out.println("控制台输入数据非法");
}
}
private static List<Player> licensing(int number, int count) {
List<Player> result = new ArrayList<>();
for (int i = 1; i <= number; i++) {
String name = "玩家".concat(String.valueOf(i));
result.add(new Player(IdentityGenerator.getUuid(), name));
}
List<Card> cards = PlayingCards.shuffle(count);
PlayingCards.assign(cards, result);
for (Player player : result) {
List<Card> list = player.getCards();
list.sort(new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o1.index - o2.index;
}
});
}
return result;
}
private static List<Card> shuffle(int count) {
List<Card> result = new ArrayList<>();
for (int i = 0; i < count; i++) {
Arrays.stream(CARD_COLOR).forEach(color -> {
for (int index = 1; index <= CARD_VALUE.length; index++) {
result.add(new Card(IdentityGenerator.getUuid(), index, color, CARD_VALUE[index - 1], 0));
}
});
result.add(new Card(IdentityGenerator.getUuid(), 14, "", "小王", 1));
result.add(new Card(IdentityGenerator.getUuid(), 15, "", "大王", 2));
}
Collections.shuffle(result);
return result;
}
private static void assign(List<Card> cards, List<Player> players) {
for (int i = 0; i < cards.size(); i++) {
int index = i % players.size();
Player player = players.get(index);
player.getCards().add(cards.get(i));
}
}
static class Player {
private String uuid;
private String name;
private List<Card> cards = new ArrayList<>();
public Player(String uuid, String name) {
this.uuid = uuid;
this.name = name;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Card> getCards() {
return cards;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
@Override
public String toString() {
return "Player{" + name + ", cards=" + cards.toString() + "}";
}
}
static class Card {
private String uuid;
private String color;
private int index;
private String value;
private int type = 0;
public Card(String uuid, int index, String color, String value, int type) {
this.uuid = uuid;
this.color = color;
this.index = index;
this.value = value;
this.type = type;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return type == 0 ? (color + value) : value;
}
}
}