java 学习 day4 今天写个斗地主

斗地主案例,自己写的,和网上大神差距还是很大

需求:通过程序实现斗地主过程中的洗牌,发牌和看牌
思路:
  1:创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现
  2:往牌盒里面装牌
  3:洗牌,也就是把牌打撒,用Collections的shuffle()方法实现
  4:发牌,也就是遍历集合,给三个玩家发牌
  5:看牌,也就是三个玩家分别遍历自己的牌

import java.util.ArrayList;
import java.util.Collections;

public class PokerDemo01 {
    public static void main(String[] args) {
        // 创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现
        ArrayList<String> array = new ArrayList<>();

        // 往牌盒里面装牌
        /*
            ♦2,♦3,……,♦K,♦A
            ♣2,……
            ♥2,……
            ♠,……
            大王,小王
        */
        // 定义花色数组
        String[] colors = {"♦","♣","❤","♠"};
        // 定义点数数组
        String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        for (String color : colors) {
            for (String number : numbers) {
                array.add(color + number);
            }
        }
        array.add("大王");
        array.add("小王");

        // 洗牌,也就是把牌打撒,用Collections的shuffle()方法实现
        Collections.shuffle(array);

        // 发牌,也就是遍历集合,给三个玩家发牌
        ArrayList<String> lqxarray = new ArrayList<>();
        ArrayList<String> lyarray = new ArrayList<>();
        ArrayList<String> fqyarray = new ArrayList<>();
        ArrayList<String> dparray = new ArrayList<>();

        for (int i = 0; i < array.size(); i++) {
            String poker = array.get(i);

            if (i >= array.size() - 3) {
                dparray.add(poker);
            }else if (i % 3 == 0) {
                lqxarray.add(poker);
            }else if (i % 3 == 1) {
                lyarray.add(poker);
            }else {
                fqyarray.add(poker);
            }
        }

        // 看牌,也就是三个玩家分别遍历自己的牌
        lookPoker("lqx", lqxarray);
        lookPoker("ly", lyarray);
        lookPoker("fqy", fqyarray);
        lookPoker("dp", dparray);

//        System.out.println(array);
    }

    // 看牌的方法
    public static void lookPoker(String name, ArrayList<String> array) {
        System.out.print(name + "的牌是:");
        for (String poker : array) {
            System.out.print("\t" + poker);
        }
        System.out.println();
    }
}

斗地主案例升级版

需求:通过程序实现斗地主过程中的洗牌,发牌,看牌和抢地主
要求:对牌进行排序
思路:
  1:创建HashMap,键是编号,值是牌
  2:创建ArrayList,存储编号
  3:创建花色数组和点数数组
  4:从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
  5:洗牌(洗的是编号),用Collections的shuffle()方法实现
  6:发牌(发的是也编号,为了保证编号是排序,创建TreeSet集合接收)
  7:定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
  8:调用看牌方法

package com.zyj.demo_01;

import java.util.*;

public class PokerDemo02 {
    public static <Int> void main(String[] args) {
        // 创建HashMap,键是编号,值是牌
        HashMap<Integer, String> hm = new HashMap<>();

        // 创建ArrayList,存储编号
        ArrayList<Integer> array = new ArrayList<>();

        // 创建花色数组和点数数组
        String[] colors = {"♦","♣","❤","♠"};
        // 定义点数数组
        String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

        // 从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
        int index = 0;
        for (String number : numbers) {
            for (String color : colors) {
                hm.put(index, color + number);
                array.add(index);
                index++;
            }
        }
        hm.put(index,"小王");
        array.add(index);
        index++;
        hm.put(index,"大王");
        array.add(index);

        // 洗牌(洗的是编号),用Collections的shuffle()方法实现
        Collections.shuffle(array);

        // 发牌(发的是也编号,为了保证编号是排序,创建TreeSet集合接收)
        TreeSet<Integer> lqxSet = new TreeSet<>();
        TreeSet<Integer> lySet = new TreeSet<>();
        TreeSet<Integer> fqySet = new TreeSet<>();
        TreeSet<Integer> dpSet = new TreeSet<>();

        for (int i = 0; i < array.size(); i++) {
            int x = array.get(i);
            if (i >= array.size() - 3) {
                dpSet.add(x);
            }else if (i % 3 == 0) {
                lqxSet.add(x);
            }else if (i % 3 == 1) {
                lySet.add(x);
            }else {
                fqySet.add(x);
            }
        }

        // 随机一个人先地主,可以选择要或者不要,不要给下一位,都不要重新发牌
        // 创建一个列表,存储三个人的名字
        ArrayList storeName = storeName("lqx", "ly", "fqy");
        // 创建一个哈希表,存储对应三个人的名字和对应的集合,以及底牌
        HashMap<String, TreeSet<Integer>> correspond = new HashMap<>();
        // 添加集合
        correspond.put("lqx", lqxSet);
        correspond.put("ly", lySet);
        correspond.put("fqy", fqySet);
        correspond.put("dp", dpSet);

        // 随机地主,并询问是否要地主,如果都不要地主,那么重新洗牌,继续询问是否要地主。
        int again = 1;
        while (again == 1) {
            Collections.shuffle(storeName);
            System.out.println(storeName);
            again = divide(storeName, correspond);
        }

        // 调用看牌方法
        lookPoker("lqx", lqxSet, hm);
        lookPoker("ly", lySet, hm);
        lookPoker("fqy", fqySet, hm);
        lookPoker("dp", dpSet, hm);
    }

    // 定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
    public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
        System.out.print(name + "的牌是:");
        for (Integer key : ts) {
            String poker = hm.get(key);
            System.out.print("\t" + poker);
        }
        System.out.println();
    }

    // 存储三个选手的名字
    public static ArrayList storeName(String... name) {
        ArrayList<String> strings = new ArrayList<>();
        for (String s : name) {
            strings.add(s);
        }
        return strings;
    }

    // 询问是否抢地主
    public static int robLandlord(String landlord) {
        System.out.println(landlord + "是否抢地主? 抢:1;不要:0");
        Scanner scanner = new Scanner(System.in);
        int wantLandlord = scanner.nextInt();
        return wantLandlord;
    }

    // 专门用来把地主牌分给地主
    public static void sendLandlord(String landlord, HashMap<String, TreeSet<Integer>> hm) {
        for (int i = 0; i < 3; i++) {
            TreeSet<Integer> dz = hm.get(landlord);
            TreeSet<Integer> dp = hm.get("dp");
            ArrayList<Integer> dzPoker = new ArrayList<>(dp);
            dz.add(dzPoker.get(i));
        }
    }

    // 地主牌划归给地主,  landlord:地主,  wantLandlord:抢不抢地主
    public static Integer divide(ArrayList storeName, HashMap<String, TreeSet<Integer>> hm) {
        String landlord_first = storeName.get(0).toString();
        int wantLandlord = robLandlord(landlord_first);
        if (wantLandlord == 1) {
            sendLandlord(landlord_first, hm);
        }else if (wantLandlord == 0) {
            String landlord_next = storeName.get(1).toString();
            int wantLandlord_next = robLandlord(landlord_next);
            if (wantLandlord_next == 1) {
                sendLandlord(landlord_next, hm);
            }else if (wantLandlord_next == 0) {
                String landlord_last = storeName.get(2).toString();
                int wantLandlord_last = robLandlord(landlord_last);
                if (wantLandlord_last == 1) {
                    sendLandlord(landlord_last, hm);
                }else if (wantLandlord_last == 0) {
                    System.out.println("重新洗牌");
                    return 1;
                }else {
                    System.out.println("请输入正确的数字!");
                }
            }else {
                System.out.println("请输入正确的数字!");
            }
        }else {
            System.out.println("请输入正确的数字!");
        }
        return 0;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值