Java模拟实现扑克牌打牌

觉得有用的记得三连哦亲!!

代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card{
    private int rank;
    private String suit;

    public Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public String toString() {
        return "["+this.suit+" "+this.rank+"]";
    }
}



public class TestDemo {

    public static void swap(Listcards,int i,int j){
        Card tmp=cards.get(i);
        cards.set(i,cards.get(j));
        cards.set(j,tmp);
    }

    public static void shuffle(List cards){
        int i = cards.size();
        for (i=i-1; i >0 ; i--) {
            Random random=new Random();
            int rand=random.nextInt(i);
            swap(cards,i,rand);
        }
    }

    public static final String[] suits={"♥","♦","♣","♥"};

    //没有大小王

    public static List buyCard(){
        ArrayList cards=new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                cards.add(new Card(j,suits[i]));
            }
        }
        return cards;
    }

    public static void main(String[] args) {
        List cards=buyCard();
        System.out.println("买牌"+cards);

        shuffle(cards);
        System.out.println("洗牌"+cards);

        ArrayList> hand=new ArrayList<>();

        List hand1=new ArrayList<>();
        List hand2=new ArrayList<>();
        List hand3=new ArrayList<>();

        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card=cards.remove(0);
                hand.get(j).add(card);
            }
        }

        System.out.println("第1个人的牌"+hand1);
        System.out.println("第2个人的牌"+hand2);
        System.out.println("第3个人的牌"+hand3);
        System.out.println("剩下的牌"+cards);

    }

    public static void main1(String[] args) {
        Card card=new Card(3,"♥");
        card.toString();
    }
}

这是运行之后的结果:

 有问题可以私聊我,都会回复的

你可能感兴趣的:(java,servlet,jvm)