其他分享
首页 > 其他分享> > 学校作业-斗地主随机发牌并保留底牌且对玩家手牌排序

学校作业-斗地主随机发牌并保留底牌且对玩家手牌排序

作者:互联网

学校作业-斗地主随机发牌并保留底牌且对玩家手牌排序

斗地主系统

该系统是学校作业,java实现的不完善斗地主系统,仅实现了发牌功能。

效果

如下:
玩家手牌和底牌堆

Card类

Card类主代码

public class Card {
    public int cardSuit;
    public int cardPoints;

    public Card(int suit, int points) {
        this.cardSuit = suit;
        this.cardPoints = points;
    }

    @Override
    public String toString() {
        switch (this.cardPoints) {
            case 11:
                return Suit.valueOf(this.cardSuit) + " " + "J";
            case 12:
                return Suit.valueOf(this.cardSuit) + " " + "Q";
            case 13:
                return Suit.valueOf(this.cardSuit) + " " + "K";
            case 14:
                return Suit.valueOf(this.cardSuit) + " " + "A";
            case 15:
                return Suit.valueOf(this.cardSuit) + " " + "2";
            case 16:
                return Suit.valueOf(this.cardSuit) + ""; //小王
            case 17:
                return Suit.valueOf(this.cardSuit) + ""; //大王 为了代码阅读完整性也写上来了
            default:
                return Suit.valueOf(this.cardSuit) + " " + (this.cardPoints);
        }

    }

花色通过枚举类实现

    enum Suit {
        Spade(0), Heart(1), Diamond(2), Club(3), redjoker(4), blackjoker(5); //黑桃(Spade)、红桃(Heart)、方块(Diamond)、梅花(Club)大王(red joker)小王(black joker)
        private final int value;

        private Suit(int value) {
            this.value = value;
        }

        public static Suit valueOf(int value) {
            switch (value) {
                case 0:
                    return Suit.Spade;
                case 1:
                    return Suit.Heart;
                case 2:
                    return Suit.Diamond;
                case 3:
                    return Suit.Club;
                case 4:
                    return Suit.redjoker;
                case 5:
                    return Suit.blackjoker;
                default:
                    return null;
            }
        }

        @Override
        public String toString() {
            switch (value) {
                case 0:
                    return "Spade黑桃♠";
                case 1:
                    return "Heart红桃♥";
                case 2:
                    return "Diamond方块♦";
                case 3:
                    return "Club梅花♣";
                case 4:
                    return "red joker 大王";
                case 5:
                    return "black joker 小王";
                default:
                    return null;
            }
        }
    }

Card类完整代码:

package com.ysysimon;

public class Card {
    public int cardSuit;
    public int cardPoints;

    public Card(int suit, int points) {
        this.cardSuit = suit;
        this.cardPoints = points;
    }

    @Override
    public String toString() {
        switch (this.cardPoints) {
            case 11:
                return Suit.valueOf(this.cardSuit) + " " + "J";
            case 12:
                return Suit.valueOf(this.cardSuit) + " " + "Q";
            case 13:
                return Suit.valueOf(this.cardSuit) + " " + "K";
            case 14:
                return Suit.valueOf(this.cardSuit) + " " + "A";
            case 15:
                return Suit.valueOf(this.cardSuit) + " " + "2";
            case 16:
                return Suit.valueOf(this.cardSuit) + ""; //小王
            case 17:
                return Suit.valueOf(this.cardSuit) + ""; //大王 为了代码阅读完整性也写上来了
            default:
                return Suit.valueOf(this.cardSuit) + " " + (this.cardPoints);
        }

    }

    enum Suit {
        Spade(0), Heart(1), Diamond(2), Club(3), redjoker(4), blackjoker(5); //黑桃(Spade)、红桃(Heart)、方块(Diamond)、梅花(Club)大王(red joker)小王(black joker)
        private final int value;

        private Suit(int value) {
            this.value = value;
        }

        public static Suit valueOf(int value) {
            switch (value) {
                case 0:
                    return Suit.Spade;
                case 1:
                    return Suit.Heart;
                case 2:
                    return Suit.Diamond;
                case 3:
                    return Suit.Club;
                case 4:
                    return Suit.redjoker;
                case 5:
                    return Suit.blackjoker;
                default:
                    return null;
            }
        }

        @Override
        public String toString() {
            switch (value) {
                case 0:
                    return "Spade黑桃♠";
                case 1:
                    return "Heart红桃♥";
                case 2:
                    return "Diamond方块♦";
                case 3:
                    return "Club梅花♣";
                case 4:
                    return "red joker 大王";
                case 5:
                    return "black joker 小王";
                default:
                    return null;
            }
        }
    }
}

玩家类

package com.ysysimon;

import java.util.ArrayList;

public class Player {
    public String playerID;
    public ArrayList<Card> cards;

    public Player(String playerID){
        this.playerID = playerID;
        this.cards = new ArrayList<Card>();
    }
    public void showCards(){
        System.out.println(cards);
    }
}

Main

完整代码:

package com.ysysimon;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
	// GameBegin
        //初始化牌堆
        ArrayList<Card> cardHeap = new ArrayList<Card>();
        int jokerCounter = 4;
        for (int i = 3; i < 18; i++) {

            if(i < 16){
                for (int j = 0; j < 4 ; j++) {
                    Card card = new Card(j,i);
                    cardHeap.add(card);
                }
            }
            else{

                Card card = new Card(jokerCounter,i);
                cardHeap.add(card);
                jokerCounter++;
            }
        }
        //System.out.println(cardHeap);
        //初始化牌堆结束

        //初始化玩家
        Player player1 = new Player("玩家1");
        Player player2 = new Player("玩家2");
        Player player3 = new Player("玩家3");
        //初始化玩家结束

        //开始发牌
        Random random = new Random();

        ArrayList<Card> holecardHeap = new ArrayList<Card>(); //底牌堆
        int choose;
        for (int i = 0; i <3 ; i++) { //随机抽取3张牌作为底牌
            choose = random.nextInt(cardHeap.size());
            holecardHeap.add(cardHeap.get(choose));
            cardHeap.remove(choose);
        }
        //System.out.println(cardHeap);
        //System.out.println(holecardHeap);

        while (!cardHeap.isEmpty()){  //为玩家发牌
            choose = random.nextInt(cardHeap.size());
            player1.cards.add(cardHeap.get(choose));
            cardHeap.remove(choose);

            choose = random.nextInt(cardHeap.size());
            player2.cards.add(cardHeap.get(choose));
            cardHeap.remove(choose);

            choose = random.nextInt(cardHeap.size());
            player3.cards.add(cardHeap.get(choose));
            cardHeap.remove(choose);
        }
        //对玩家手牌进行升序排序

        sortCards(player1.cards);
        sortCards(player2.cards);
        sortCards(player3.cards);

        player1.showCards();
        player2.showCards();
        player3.showCards();

        System.out.println("底牌: "+holecardHeap);

        //发牌结束



    }

    /* 重写排序 */
    public static void sortCards(ArrayList<Card> cards){
        Collections.sort(cards, new Comparator<Card>() {

            @Override
            public int compare(Card o1, Card o2) {
                // 升序
                return o1.cardPoints-o2.cardPoints;
                // 降序
                // return o2.getAge()-o1.getAge();
                // return o2.getAge().compareTo(o1.getAge());
            }

        });
    }

}


其中重写了Comparator以实现自定义类的List的排序

  /* 重写排序 */
    public static void sortCards(ArrayList<Card> cards){
        Collections.sort(cards, new Comparator<Card>() {

            @Override
            public int compare(Card o1, Card o2) {
                // 升序
                return o1.cardPoints-o2.cardPoints;
                // 降序
                // return o2.getAge()-o1.getAge();
                // return o2.getAge().compareTo(o1.getAge());
            }

        });
    }

标签:case,return,斗地主,int,手牌,public,cardSuit,Suit,发牌
来源: https://blog.csdn.net/qq_36060389/article/details/106958984