Java实现斗地主的发牌以及展示

2021/11/16 9:11:04

本文主要是介绍Java实现斗地主的发牌以及展示,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

整体框架
集合1 接收所有牌
集合2 将牌乱序 分成三份
集合3 排序 进行展示

具体代码内容:
定义玩家

public class People {
    private String name;
    private boolean state=false;
    private ArrayList<String> poker=new ArrayList<String>();

    public People() {
    }

    public People(String name , boolean state) {
        this.name = name;
        this.state = state;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }

    public ArrayList<String> getPoker() {
        return poker;
    }

    public void setPoker(String poker) {
        this.poker.add(poker);
    }
}

2.接收所有牌 分牌 排序

public class PokerDate {
    //拼牌
    public int a = (int) (Math.random() * 3);//地主随机发牌
    public void dateNmuber(ArrayList array) {
        String[] s1 = {"♦" , "♠" , "♣" , "♥"};
        String[] s2 = {"2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A"};
        String[] s3 = {"大王" , "小王"};
        for (int i = 0; i < s1.length; i++) {
            for (int j = 0; j < s2.length; j++) {
                array.add(s1[i] + s2[j]);
            }
        }
        array.add(s3[0]);
        array.add(s3[1]);
    }

    //发牌
    public void pokerDeal(ArrayList array) throws IOException {
        People a1 = new People("张三" , false);
        People a2 = new People("李四" , false);
        People a3 = new People("王五" , false);
//        ArrayList<String> a1 = new ArrayList<String>();
//        ArrayList<String> a2 = new ArrayList<String>();
//        ArrayList<String> a3 = new ArrayList<String>();
        ArrayList<String> num = new ArrayList<String>();
        Collections.shuffle(array);
        System.out.println(array);
        for (int i = 0; i < array.size(); i++) {
            if (i >= array.size() - 3) {
                switch (a) {
                    case 0:
                        a1.setState(true);
                        a1.setPoker((String) array.get(i));
                        break;
                    case 1:
                        a2.setState(true);
                        a2.setPoker((String) array.get(i));
                        break;
                    case 2:
                        a3.setState(true);
                        a3.setPoker((String) array.get(i));
                        break;
                }
            } else if (i % 3 == 0) {
                a1.setPoker((String) array.get(i)); //打牌第一个人
            } else if (i % 3 == 1) {
                a2.setPoker((String) array.get(i));//打牌第二个人
            } else if (i % 3 == 2) {
                a3.setPoker((String) array.get(i));//打牌第三个
            }
        }
        //地主牌发放
        show(a1);
        show(a2);
        show(a3);
    }

    //手牌展示
    public static void show(People people) throws IOException {
        System.out.println("玩家:" + people.getName());
        System.out.println("是否是地主:" + people.isState());
        ArrayList<String> poker = people.getPoker();
        TreeSet<String> pokerCeshi = new TreeSet<String>(new Comparator<String>() {
            @Override
            public int compare(String o1 , String o2) {
                int i = 0, j = 0;
                //判断是否为10,Q,A;ASCII值重写定义,方便排序
                if (o1.length() == 3)
                    i = 58;//确定数字10排在9的后面,9的ASCII值是57
                else if (o1.charAt(1) == 'K') i = 85;
                else if (o1.charAt(1) == 'A') i = 90;
                else i = o1.charAt(1);
                if (o2.length() == 3)
                    j = 58;//确定数字10排在9的后面
                else if (o2.charAt(1) == 'K') j = 85;
                else if (o2.charAt(1) == 'A') j = 90;
                else {
                    j = o2.charAt(1);
                }
//                int a='J';ASCII值是//74
//                int b='Q';ASCII值是//81
//                int c='K';ASCII值是//75
//                int d='A';ASCII值是//65
                int num1 = i - j;
                int num2 = num1 == 0 ? o1.charAt(0) - o2.charAt(0) : num1;
                return num2;
            }
        });
        //排序
        for (String s : poker) {
            pokerCeshi.add(s);
        }
        for (String s : pokerCeshi) {
            System.out.print(s + " ");
        }
        System.out.println();
        //输出到文件fos.txt
        FilePutDome.filePut(people.getName(),people.isState(),pokerCeshi);
    }
}

3.输出流

public class FilePutDome {
    public static void filePut(String name, boolean state, Set poker) throws IOException {
//        String name = people.getName();
//        boolean state = people.isState();
        String s = String.valueOf(poker);
        BufferedWriter  fos=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("fos.txt",true)));
        fos.write("玩家:");
        fos.write(name);
        fos.newLine();
        fos.write("是否是地主");
        fos.write(String.valueOf(state));
        fos.newLine();
        fos.write("手牌为:");
        fos.write(s);
        fos.close();

    }
}

4.文档用来记录每个玩家的收到的牌

玩家:张三
是否是地主true
手牌为:[♠2, ♠3, ♣3, ♥3, ♦3, ♦4, ♠5, ♣5, ♥5, ♦6, ♥7, ♥8, ♠9, ♥9, ♠10, ♦J, ♦Q, ♠A, ♣A, 大王]
玩家:李四
是否是地主false
手牌为:[♦2, ♦5, ♥6, ♠8, ♣8, ♦8, ♦9, ♣10, ♥10, ♠J, ♣J, ♣Q, ♥Q, ♣K, ♥K, ♦K, ♥A]
玩家:王五
是否是地主false
手牌为:[♣2, ♥2, ♠4, ♣4, ♥4, ♠6, ♣6, ♠7, ♣7, ♦7, ♣9, ♦10, ♥J, ♠Q, ♠K, ♦A, 小王]

5.实现类

public class PokerDome{
        public static void main(String[] args) throws IOException {
            ArrayList<String> array = new ArrayList<String>();
            PokerDate  pokerDate=new PokerDate();
            pokerDate.dateNmuber(array);//堆牌;
            pokerDate.pokerDeal(array);//发牌
        }
}

最终实现效果:
在这里插入图片描述不足之处:只有牌的分发以及展示。



这篇关于Java实现斗地主的发牌以及展示的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程