大家可以参考我的,我写的比较简单,也没有做优化,适合初学者(和老师打印输出的不一样)

来源:7-1 简易扑克牌游戏

fewer_xie

2018-08-24 10:20

public class Test {

    public static List<Card> cardList = new ArrayList<Card>();
    public static String[] cardNum = new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    public static String[] cardClass = new String[]{"方块","梅花","红桃","黑桃"};
    public static Player player1;
    public static Player player2;
    public static void main(String[] args) {
        initPlayer();
        initCard();
        showCard();
        shuffleCard();
        drawCard();
        showHandCard();
        compareCard();
    }

    public static void initPlayer(){
        System.out.println("初始化玩家");
        player1 = new Player("赌神");
        player2 = new Player("赌圣");
    }

    public static void initCard(){
        System.out.println("\n创建一副牌\n");
        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 4; j++) {
                cardList.add(new Card(cardNum[i],cardClass[j],i*13+j));
            }
        }
    }

    public static void showCard(){
        System.out.println("查看牌\n");
        int count = 0;
        for (Card card : cardList){
            System.out.print(card.toString() + "\t");
            if(++count % 10 == 0 || count == 52){
                System.out.println();
            }
        }
    }

    public static void shuffleCard(){
        System.out.println("\n洗牌\n");
        Collections.shuffle(cardList);
    }

    public static void drawCard(){
        System.out.println("双方抽牌\n");
        List<Card> player1CardList = new ArrayList<Card>();
        List<Card> player2CardList = new ArrayList<Card>();
        for (int i = 52; i > 48; i--) {
            int j = (int)(Math.random()*i);
            player1CardList.add(cardList.get(j));
            cardList.remove(j);
            j = (int)(Math.random()*--i);
            player2CardList.add(cardList.get(j));
            cardList.remove(j);
        }
        player1.setCardList(player1CardList);
        player2.setCardList(player2CardList);
    }

    public static void showHandCard(){
        System.out.println("双方公开手牌\n");
        System.out.println("player1" + player1.getName() + "的手牌是");
        System.out.println(player1.getCardList() + "\n");
        System.out.println("player2" + player2.getName() + "的手牌是");
        System.out.println(player2.getCardList() + "\n");
    }

    public static void compareCard(){
        System.out.println("双方比较手牌\n");
        Card player1MaxCard = new Card();
        Card player2MaxCard = new Card();
        if (player1.getCardList().get(0).getWeight() > player1.getCardList().get(1).getWeight()){
            player1MaxCard = player1.getCardList().get(0);
        } else {
            player1MaxCard = player1.getCardList().get(1);
        }
        System.out.println("player1最大的牌是" + player1MaxCard);
        if (player2.getCardList().get(0).getWeight() > player2.getCardList().get(1).getWeight()){
            player2MaxCard = player2.getCardList().get(0);
        } else {
            player2MaxCard = player2.getCardList().get(1);
        }
        System.out.println("\nplayer2最大的牌是" + player2MaxCard);
        if (player1MaxCard.getWeight() > player2MaxCard.getWeight()){
            System.out.println("\n场上最大的牌是" + player1MaxCard);
            System.out.println("\n胜者是player1" + player1.getName());
        } else {
            System.out.println("\n场上最大的牌是" + player2MaxCard);
            System.out.println("\n胜者是player2" + player2.getName());
        }
        System.out.println("\n游戏结束");
    }

}


public class Player {

    private String name;
    private List<Card> cardList;

    public Player() {

    }

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Card> getCardList() {
        return cardList;
    }

    public void setCardList(List<Card> cardList) {
        this.cardList = cardList;
    }

}

public class Card {

    private String num;
    private String type;
    private int weight;

    public Card() {

    }

    public Card(String num, String type, int weight) {
        this.num = num;
        this.type = type;
        this.weight = weight;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return type + num;
    }
}


写回答 关注

5回答

  • 慕婉清9163763
    2018-08-25 21:13:46
    已采纳

    写的通俗易懂 好!!

  • qq_慕仰7543500
    2020-02-18 13:54:30

    想问一下 这个换成对象数组怎么写


  • Silent_Walke
    2018-09-29 19:00:20

    条理清晰,也很简单

  • qq_陌上花开缓缓归_0
    2018-09-04 20:24:56

    原来weight是用来比较的,但是我怎么没看到weight有在哪赋值吗


    我头上有犄角...

    第28行 weight=i*13+j

    2018-09-16 10:05:00

    共 1 条回复 >

  • fewer_xie
    2018-08-24 10:20:30

    先自问自答一波

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题