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

实现斗地主发牌功能

程序员文章站 2022-04-14 11:49:26
编程实现控制台版支持三人对战的斗地主游戏(暂时只能发牌)。代码分段定义一些等会要用到的变量 private String cardA; //用户A private String cardB; //用户B private String cardC; //用户C private List A = new ArrayList(); //用户A的手牌 private List B = new ArrayList(); //用户B的手牌 privat...

编程实现控制台版支持三人对战的斗地主游戏(暂时只能发牌)。

(1)首先准备 54 张扑克牌并打乱顺序。

(2)由三个玩家交替摸牌,每人 17 张扑克牌,最后三张留作底牌。

(3)查看三个玩家手中的扑克牌和底牌。

(4)其中玩家手中的扑克牌需要按照大小顺序打印,规则如下:

手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3

代码分段

定义一些等会要用到的变量

 	private String cardA;   //用户A
    private String cardB;   //用户B
    private String cardC;   //用户C
    private List A = new ArrayList();   //用户A的手牌
    private List B = new ArrayList();   //用户B的手牌
    private List C = new ArrayList();   //用户C的手牌
    private List safeCards = new ArrayList();//最后三张底牌
    private Map map = null; //空的扑克牌

用于生成扑克牌的方法。

	/**
     * 生成一副扑克牌
     * @return
     */
    public static Map playingCard(){
        String[] color = new String[]{"♠", "♥", "♣", "♦" };
        String[] num = new String[]{"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        String[] ghost = new String[]{"大王","小王"};
        Map<Integer,String> map = new HashMap<>();
        map.put(0,ghost[0]);
        map.put(1,ghost[1]);
        int index = 2;
        //注入
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < color.length; j++) {
                map.put(index,color[j]+num[i]);
                index++;
            }
        }
        return map;
    }

实现洗牌发牌功能,并把牌存进全局变量A、B、C数组里。

    /**
     * 实现洗牌发牌功能
     */
	public void Shuffle(){
        List<Integer> list = new ArrayList<>(map.keySet());
        Collections.shuffle(list);
        safeCards = new ArrayList<>(list.subList(list.size() - 3, list.size()));
        list.removeAll(safeCards);
        Iterator<Integer> iterator = list.iterator();
        int a;
        int b;
        int c;
        while(iterator.hasNext()) {
            a = iterator.next();
            b = iterator.next();
            c = iterator.next();
            A.add(a);
            B.add(b);
            C.add(c);
            System.out.println("给" + cardA + "发一张[" + map.get(a) + "]");
            System.out.println("给" + cardB + "发一张[" + map.get(b) + "]");
            System.out.println("给" + cardC + "发一张[" + map.get(c) + "]");
        }
        iterator = safeCards.iterator();
        System.out.print("剩余底牌[");
        while(iterator.hasNext()) {
            System.out.print(map.get(iterator.next()));
            if (iterator.hasNext()){
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

把手牌list转换成String类型,便于查看(实现了排序功能)

 /**
     *  把手牌list转换成String类型,便于查看
     * @param list 用户的手牌
     * @param name 用户的名字
     * @return
     */
    public String show(List list,String name){
        Collections.sort(list);
        StringBuilder sb = new StringBuilder();
        sb.append(name + "[");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            sb.append(map.get(iterator.next()));
            if (iterator.hasNext()){
                sb.append(",");
            }
        }
        sb.append("]");
        return sb+"";
    }

查看玩家名字

	/**
     * getCardA、getCardB、getCardC查看玩家名字
     */
    public String getCardA() {
        return cardA;
    }

    public String getCardB() {
        return cardB;
    }

    public String getCardC() {
        return cardC;
    }

用于更换玩家

/**
     * setCardA、setCardB、setCardC 更换玩家
     * @param cardA
     */
    public void setCardA(String cardA) {
        System.out.println("一号玩家[" + this.cardA + "]更换[" + cardA + "]");
        this.cardA = cardA;
    }
    public void setCardB(String cardB) {
        System.out.println("二号玩家[" + this.cardB + "]更换[" + cardB + "]");
        this.cardB = cardB;
    }
    public void setCardC(String cardC) {
        System.out.println("三号玩家[" + this.cardC + "]更换[" + cardC + "]");
        this.cardC = cardC;
    }

查看玩家的手牌

/**
     * getA、getB、getC 看扑克牌
     * getSafeCards 看底牌
     */
    public void getA() {
        System.out.println(show(A,cardA));
    }
    public void getB() {
        System.out.println(show(B,cardB));
    }
    public void getC() {
        System.out.println(show(C,cardC));
    }
    public void getSafeCards(){
        System.out.println(show(safeCards,"底牌"));
    }

构造方法:定义三个玩家,并获取一副新扑克牌

    /**
     * 构造方法定义三个玩家,并获取一副新扑克牌
     * @param cardA
     * @param cardB
     * @param cardC
     */
    public Test3(String cardA, String cardB, String cardC) {
        map = playingCard();    //生成一副新的扑克牌
        this.cardA = cardA;     //用户A取名
        this.cardB = cardB;     //用户B取名
        this.cardC = cardC;     //用户C取名
    }

代码总汇

功能类

package test3;

import java.util.*;

/**
 * @author cyh
 * @version 1.0
 * @date 2021/1/5 8:48
 */
public class Test3 {
    private String cardA;   //用户A
    private String cardB;   //用户B
    private String cardC;   //用户C
    private List A = new ArrayList();   //用户A的手牌
    private List B = new ArrayList();   //用户B的手牌
    private List C = new ArrayList();   //用户C的手牌
    private List safeCards = new ArrayList();//最后三张底牌
    private Map map = null; //空的扑克牌

    /**
     * 构造方法定义三个玩家,并获取一副新扑克牌
     * @param cardA
     * @param cardB
     * @param cardC
     */
    public Test3(String cardA, String cardB, String cardC) {
        map = playingCard();    //生成一副新的扑克牌
        this.cardA = cardA;     //用户A取名
        this.cardB = cardB;     //用户B取名
        this.cardC = cardC;     //用户C取名
    }

    /**
     * 生成一副扑克牌
     * @return
     */
    public static Map playingCard(){
        String[] color = new String[]{"♠", "♥", "♣", "♦" };
        String[] num = new String[]{"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        String[] ghost = new String[]{"大王","小王"};
        Map<Integer,String> map = new HashMap<>();
        map.put(0,ghost[0]);
        map.put(1,ghost[1]);
        int index = 2;
        //注入
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < color.length; j++) {
                map.put(index,color[j]+num[i]);
                index++;
            }
        }
        return map;
    }

    /**
     * 实现洗牌发牌功能
     */
    public void Shuffle(){
        List<Integer> list = new ArrayList<>(map.keySet());
        Collections.shuffle(list);
        safeCards = new ArrayList<>(list.subList(list.size() - 3, list.size()));
        list.removeAll(safeCards);
        Iterator<Integer> iterator = list.iterator();
        int a;
        int b;
        int c;
        while(iterator.hasNext()) {
            a = iterator.next();
            b = iterator.next();
            c = iterator.next();
            A.add(a);
            B.add(b);
            C.add(c);
            System.out.println("给" + cardA + "发一张[" + map.get(a) + "]");
            System.out.println("给" + cardB + "发一张[" + map.get(b) + "]");
            System.out.println("给" + cardC + "发一张[" + map.get(c) + "]");
        }
        iterator = safeCards.iterator();
        System.out.print("剩余底牌[");
        while(iterator.hasNext()) {
            System.out.print(map.get(iterator.next()));
            if (iterator.hasNext()){
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

    /**
     *  把手牌list转换成String类型,便于查看
     * @param list 用户的手牌
     * @param name 用户的名字
     * @return
     */
    public String show(List list,String name){
        Collections.sort(list);
        StringBuilder sb = new StringBuilder();
        sb.append(name + "[");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            sb.append(map.get(iterator.next()));
            if (iterator.hasNext()){
                sb.append(",");
            }
        }
        sb.append("]");
        return sb+"";
    }

    /**
     * getA、getB、getC 看扑克牌
     * getSafeCards 看底牌
     */
    public void getA() {
        System.out.println(show(A,cardA));
    }
    public void getB() {
        System.out.println(show(B,cardB));
    }
    public void getC() {
        System.out.println(show(C,cardC));
    }
    public void getSafeCards(){
        System.out.println(show(safeCards,"底牌"));
    }

    /**
     * getCardA、getCardB、getCardC查看玩家名字
     */
    public String getCardA() {
        return cardA;
    }

    public String getCardB() {
        return cardB;
    }

    public String getCardC() {
        return cardC;
    }

    /**
     * setCardA、setCardB、setCardC 更换玩家
     * @param cardA
     */
    public void setCardA(String cardA) {
        System.out.println("一号玩家[" + this.cardA + "]更换[" + cardA + "]");
        this.cardA = cardA;
    }
    public void setCardB(String cardB) {
        System.out.println("二号玩家[" + this.cardB + "]更换[" + cardB + "]");
        this.cardB = cardB;
    }
    public void setCardC(String cardC) {
        System.out.println("三号玩家[" + this.cardC + "]更换[" + cardC + "]");
        this.cardC = cardC;
    }
}

实习类

package test3;

import java.util.Scanner;

/**
 * @author cyh
 * @version 1.0
 * @date 2021/1/5 10:17
 */
public class Test3Test {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        System.out.println("-------------------");
        System.out.println("[欢迎来到拉勾博彩平台]");
        System.out.println("[请输入参加玩家的名字]");
        Test3 t = new Test3(sc.next(),sc.next(),sc.next());
        System.out.println("[ 三位玩家名字分别是 ]");
        System.out.println("[ 玩家一:" + t.getCardA() + " ]");
        System.out.println("[ 玩家二:" + t.getCardB() + " ]");
        System.out.println("[ 玩家三:" + t.getCardC() + " ]");
        System.out.println("-------------------");
        System.out.println("-------------------");
        commence:while (true){
            System.out.println("[需要更换玩家请按E ]");
            System.out.println("[不需要请输入任意键]");
            if("e".equalsIgnoreCase(sc.next())){
                System.out.println("[请输入更换的位置]");
                System.out.println("[  输入整数数字  ]");
                int opt = sc.nextInt();
                opt:while (true){
                    switch (opt){
                        case 1:
                            System.out.println("[ 正在更换玩家一 ]");
                            System.out.println("[ 原来的用户名:" + t.getCardA() + " ]");
                            System.out.println("[输入更换的用户名]");
                            t.setCardA(sc.next());
                            break opt;
                        case 2:
                            System.out.println("[ 正在更换玩家二 ]");
                            System.out.println("[ 原来的用户名:" + t.getCardB() + " ]");
                            System.out.println("[输入更换的用户名]");
                            t.setCardB(sc.next());
                            break opt;
                        case 3:
                            System.out.println("[ 正在更换玩家三 ]");
                            System.out.println("[ 原来的用户名:" + t.getCardC() + " ]");
                            System.out.println("[输入更换的用户名]");
                            t.setCardC(sc.next());
                            break opt;
                        default:
                    }
                }
            }else {
                break commence;
            }
        }
        System.out.println("------------游戏开始------------");
        t.Shuffle();
        System.out.println("------------发牌结束------------");
        t.getA();
        t.getB();
        t.getC();
        t.getSafeCards();
    }
}

效果图

实现斗地主发牌功能
实现斗地主发牌功能

本文地址:https://blog.csdn.net/Guai_Ka/article/details/112217155