前期斗地主案例(集合)

实体类
package com.itheima.d3_collection_test;

public class Card {
    private String number; // 点数
    private String color; // 花色
    private int size; // 大小。

    public Card() {
    }

    public Card(String number, String color, int size) {
        this.number = number;
        this.color = color;
        this.size = size;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return number + color;
    }
}

房间类

package com.itheima.d3_collection_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
// 房间类。
public class Room {
    // 主要要有一副牌:54张。
    private List allCards = new ArrayList<>(); // []

    public Room(){
        // 1、做牌
        // 把点数拿到程序中来: 类型确定,个数确定,请用数组。
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        // 把花色拿到程序中来:类型确定,个数确定,请用数组。
        String[] colors = {"♠", "♥", "♣", "♦"};
        // 2、遍历点数,再遍历花色
        int size = 0;
        for (String number : numbers) {
            size++;
            for (String color : colors) {
                // 3、创建一个牌对象封装一张牌数据。
                Card c = new Card(number, color, size);
                allCards.add(c); // 加入到集合中去。
            }
        }
        allCards.add(new Card("", "🃏", ++size));
        allCards.add(new Card("", "👲", ++size));
        System.out.println("新牌是:"  + allCards);
    }

    /**
     * 洗牌:把54张牌的顺序打乱
     */
    public void swapCard(){
        Collections.shuffle(allCards);
        System.out.println("洗牌后:" + allCards);
    }

    /**
     * 发牌操作: allCards = [5♥, Q♠, 10♣, 9♣, 2♥, A♣, J♥, 8♣, 4♠, 7♥, 6♦, K♦
     */
    public void sendCard(){
        // 1、模拟3个玩家:ArrayList
        List lhc = new ArrayList<>();
        List jmz = new ArrayList<>();
        List ryy = new ArrayList<>();

        // 2、把斗地主54张牌中的前51张牌发出去。剩余3张牌做为底牌。
        // allCards = [5♥, Q♠, 10♣, 9♣, 2♥, A♣, J♥, 8♣, 4♠, 7♥, 6♦, K♦
        //             0   1    2    3   4   5   6  7
        //             i % 3
        for (int i = 0; i < allCards.size() - 3; i++) {
            // 先拿到这个牌。
            Card c = allCards.get(i);
            // 这个牌发给谁呢?
            if(i % 3 == 0){
                // 请啊冲接牌
                lhc.add(c);
            }else if(i % 3  == 1){
                // 请阿鸠接牌
                jmz.add(c);
            }else if(i % 3 == 2){
                // 请盈盈接牌
                ryy.add(c);
            }
        }

        // 对每个玩家的牌进行排序!(拓展知识)
        // List lhc = [👲, 7♥, 9♥, K♠, 10♦, 2♣ ,A ...
        sortCards(lhc);
        sortCards(jmz);
        sortCards(ryy);

        System.out.println("令狐冲:" + lhc);
        System.out.println("鸠摩智:" + jmz);
        System.out.println("任盈盈:" + ryy);

        // 最后3张底牌。
//        Card c1 = allCards.get(51); // 52
//        Card c2 = allCards.get(52); // 53
//        Card c3 = allCards.get(53); // 54
        // 拓展:可以直接把最后3张底牌截取出来作为一个新的集合返回。
        List lastThreeCards  = allCards.subList(allCards.size() - 3 , allCards.size()); // 截取集合的(包前不包后)
        System.out.println("最后3张底牌是:" + lastThreeCards);

        // 模拟抢地主。
        jmz.addAll(lastThreeCards);
        sortCards(jmz);
        System.out.println("鸠摩智抢完地主后:" + jmz);
    }

    public void sortCards(List cards) {
        Collections.sort(cards, new Comparator() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getSize() - o1.getSize(); // 升序
            }
        });
        // 简化
    }
}

主函数

package com.itheima.d3_collection_test;
/**
    目标:斗地主游戏的案例开发。
    业务需求分析:
        业务: 总共有54张牌。
        点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2"
        花色: "♠", "♥", "♣", "♦"
        大小王: "👲" , "🃏"
        点数分别要组合4种花色,大小王各一张。
        斗地主:发出51张牌,剩下3张作为底牌。
 */
public class GameDemo {
    public static void main(String[] args) {
        // 1、首先:每张牌是一个牌对象,所以我们需要先定义一个牌类。
        // 2、每个房间是一个对象,定义一个房间类。
        Room r = new Room();
        // 3、洗牌
        r.swapCard();
        // 4、开始发牌
        r.sendCard();
    }
}
posted @ 2022-12-09 18:33  四四一百六  阅读(36)  评论(0)    收藏  举报