斗地主游戏的案例开发

Posted 蜡笔小心_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了斗地主游戏的案例开发相关的知识,希望对你有一定的参考价值。

package com.itheima._08斗地主游戏洗牌发牌看牌;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 目标:斗地主游戏的案例开发。
 * <p>
 * 业务需求分析:
 * 斗地主的做牌,洗牌,发牌,排序(拓展知识), 看牌
 * 业务:总共有54张牌。
 * 点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2"
 * 花色: "♠", "♥", "♣", "♦"
 * 大小王: "👲" , ""
 * 点数分别要组合4种花色,大小王各一张。
 * 斗地主:发出51张牌,剩下3张作为底牌。
 * <p>
 * 功能:
 * 1.做牌。
 * 2.洗牌
 * 3.定义3个玩家。
 * 4.发牌。
 * 5.排序(拓展,了解)
 * 6.看牌。
 * <p>
 * 用面向对象设计案例:
 * a.定义一个牌类,代表牌对象。 一个牌对象代表一张牌。
 * b.定义一个集合存储54张牌,集合只需要一个(因为牌只需要一副)
 */
public class GameDemo 
    /**
     * a.定义一个静态集合,存储54张牌对象,集合只需要一个
     */
    public static final List<Card> ALL_CARDS = new ArrayList<>();

    /** b.做牌 */
    static 
        // 1.定义一个数组存储牌的点数,类型确定,个数确定请用数组存储!
        String[] numbers = "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2";
        // 2.定义一个数组存储牌的花色,类型确定,个数确定请用数组存储!
        String[] colors = "♠", "♥", "♣", "♦";
        Integer index = 0;
        // 3.先遍历点数与四种花色组装成牌对象存入到集合中去
        for (String number : numbers) 
            // 遍历花色
            for (String color : colors) 
                // 创建一张牌对象封装点数和花色
                Card card = new Card(number, color, index++);
                ALL_CARDS.add(card);
            
        
        // 4.单独加入大小王
        Collections.addAll(ALL_CARDS, new Card("", "🃏", index++), new Card("", "👲", index++));
        System.out.println("输出新牌:" + ALL_CARDS);
    

    public static void main(String[] args) 
        //打乱牌顺序
        Collections.shuffle(ALL_CARDS);
        System.out.println("ALL_CARDS = " + ALL_CARDS);

        List<Card> xiaoXin = new ArrayList<>();
        List<Card> niNI = new ArrayList<>();
        List<Card> xiaoBai = new ArrayList<>();
        // 最后三张底牌
        List<Card> lastThree = ALL_CARDS.subList(ALL_CARDS.size() - 3, ALL_CARDS.size());
        //对下标进行取余%
        for (int i = 0; i < ALL_CARDS.size() - 3; i++) 
            if (i % 3 == 0) 
                xiaoXin.add(ALL_CARDS.get(i));
             else if (i % 3 == 1) 
                niNI.add(ALL_CARDS.get(i));
             else if (i % 3 == 2) 
                xiaoBai.add(ALL_CARDS.get(i));
            
        



        //第一种stream流java8新特性 
        [Java高阶进阶之Java函数式编程-Stream-Lambda表达式](https://blog.csdn.net/fo_xi/article/details/126847375?spm=1001.2014.3001.5501)
        System.out.println("小新:" + xiaoXin.stream().sorted().collect(Collectors.toList()));
        System.out.println("小白:" + xiaoBai.stream().sorted().collect(Collectors.toList()));
        System.out.println("妮妮:" + niNI.stream().sorted().collect(Collectors.toList()));
        System.out.println("底牌" + lastThree.stream().sorted().collect(Collectors.toList()));

        sortCard(xiaoXin);
        sortCard(xiaoBai);
        sortCard(niNI);
        sortCard(lastThree);
        //第二种
        System.out.println("小新:" + xiaoXin);
        System.out.println("小白:" + xiaoBai);
        System.out.println("妮妮:" + niNI);
        System.out.println("底牌" + lastThree);




    
    /*对牌的List集合进行排序*/
    private static void sortCard(List<Card> cards) 
        Collections.sort(cards, new Comparator<Card>() 
            @Override
            public int compare(Card o1, Card o2) 
                return o1.getIndex() - o2.getIndex();
            
        );
    


// 牌类
public class Card implements Comparable<Card>
    private String number;
    private String color;
    private int index;//存储牌在新牌中的索引
    public int getIndex() 
        return index;
    

    public void setIndex(int index) 
        this.index = index;
    
    public Card(String number, String color,Integer index) 
        this.number = number;
        this.color = color;
        this.index=index;
    

    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;
    

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

    @Override
    public int compareTo(Card o) 
        return this.index - o.getIndex();
    

以上是关于斗地主游戏的案例开发的主要内容,如果未能解决你的问题,请参考以下文章

Java斗地主案例

NOIP2015斗地主

P2668 斗地主

NOIP2015 斗地主

NoIP 2015斗地主

[NOIp 2015]斗地主