欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

3人梭哈

程序员文章站 2024-03-19 09:12:34
...
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Poker {

    static class Card{
        public String suit;
        public String rank;

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

        @Override
        public String toString() {
            return "("+ suit + rank+")";
        }
    }

    //一副牌
    public static List<Card> buyPoker(){
        String[] suits = {"♠","♥","♣","♦"};
        List<Card> poker = new ArrayList<>();
        for (int i = 0; i<4; i++){
            poker.add(new Card(suits[i],"A"));
            for (int j=2; j<=10; j++){
                Card card = new Card(suits[i],""+j);//""+j == String.valueOf(j)
                poker.add(card);
            }
            poker.add(new Card(suits[i],"J"));
            poker.add(new Card(suits[i],"Q"));
            poker.add(new Card(suits[i],"K"));

        }
        return poker;
    }



    public static void main(String[] args) {
        //买牌
        List<Card>poker = buyPoker();
        //洗牌
        Collections.shuffle(poker);
        System.out.println(poker);
        //发牌
        List<List<Card>> players = new ArrayList<List<Card>>();
        players.add(new ArrayList<Card>());
        players.add(new ArrayList<>());
        players.add(new ArrayList<>());
        for (int cardIndex=0; cardIndex<5; cardIndex++){
            for (int playerIndex=0; playerIndex<3; playerIndex++){
                List<Card> player = players.get(playerIndex);
                player.add(poker.remove(0));
            }
        }
        System.out.println("1号玩家手牌: ");
        System.out.println(players.get(0));
        System.out.println("2号玩家手牌: ");
        System.out.println(players.get(1));
        System.out.println("3号玩家手牌: ");
        System.out.println(players.get(2));
       // System.out.println(poker);


    }
}

 

相关标签: 新手上路