import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card {
public String colors;//花色
public int num;//数字
public Card(String colors, int num) {
this.colors = colors;
this.num = num;
}
@Override
public String toString() {
/*return "Card{" +
"colors='" + colors + '\'' +
", num=" + num +
'}';*/
return "["+colors+","+num+"]";
}
}
class BuyCard {
public String[] colors = {"♥","♠","♦","♣"};
List<Card> list = new ArrayList<>();
public List<Card> Cards() {
for (int i = 0; i < colors.length; i++) {
for (int j = 1; j <= 13; j++) {
Card card = new Card(colors[i],j);
list.add(card);
}
}
return list;
}
public void swap(List<Card> list,int i,int n) {
Card tmp = list.get(i);
list.set(i,list.get(n));
list.set(n,tmp);
}
public void shuffle (List<Card> list){//洗牌时只能从后往前进行交换
Random random = new Random();
for (int i = list.size()-1; i > 0; i--) {
int n = random.nextInt(i);
swap(list,i,n);
}
}
}
public class Main {
public static void main(String[] args) {
BuyCard buyCard = new BuyCard();
List<Card> list = buyCard.Cards();
System.out.println("所有牌面:");
System.out.println(list);
System.out.println("洗牌:");
buyCard.shuffle(list);
System.out.println(list);
//三个人 一人五张牌 轮流发放
List<List<Card>> hands = new ArrayList<>();
hands.add(new ArrayList<>());
hands.add(new ArrayList<>());
hands.add(new ArrayList<>());
for (int i = 0; i < 5; i++) {//i代表每个人的牌数
for (int j = 0; j < 3; j++) {//j代表人数
hands.get(j).add(list.remove(0));
}
}
System.out.println("1手中的牌:");
System.out.println(hands.get(0));
System.out.println("2手中的牌:");
System.out.println(hands.get(1));
System.out.println("3手中的牌:");
System.out.println(hands.get(2));
System.out.println("剩余的牌:");
System.out.println(list);
}
}
java-扑克牌
最新推荐文章于 2022-04-09 17:48:40 发布