2025年挖坑游戏发牌方法

挖坑游戏发牌方法枚举牌的花色 public enum Paise HEI HONG MEI FANG private String name private Paise String name this

大家好,我是讯享网,很高兴认识大家。

枚举牌的花色♠,♥,♣,♦

public enum Paise { HEI("♠"),HONG("♥"),MEI("♣"),FANG("♦"); private String name; private Paise(String name) { this.name=name; } public String toString() { return this.name; } }
讯享网

牌的属性类

讯享网​ public class Pai implements Comparable<Pai> { private final int num; private final Paise color; public Pai(int num, Paise color) { this.num = num; this.color = color; } @Override public int compareTo(Pai o) { return 0; } public int getNum() { return num; } public Paise getColor() { return color; } @Override public String toString() { String aa = "" + num; if (num > 10) { if (num == 11) aa = "J"; else if (num == 12) aa = "Q"; else if (num == 13) aa = "K"; else if (num == 14) aa = "A"; else if (num == 15) aa = "2"; else if (num == 16) aa = "3"; } String res = "(" + color + aa + ")"; return res; } } ​

方法类

import java.util.Arrays; public class ArrayList { private Comparable[] wer; private int count; public ArrayList() { this(10); } public ArrayList(int length) { wer=new Comparable[length]; } public void add(Comparable data) { wer[count++]=data; if(count>wer.length) kuorong(); } private void kuorong() { Comparable[] res=new Comparable[wer.length*3/2]; System.arraycopy(wer, 0, res, 0, res.length); this.wer=res; } public Comparable delete(int index) { if(index>=count||index<0) throw new ArrayIndexOutOfBoundsException(); Comparable res=wer[index]; System.arraycopy(wer, index+1, wer, index, wer.length-1-index); wer[count-1]=null; count--; return res; } public Comparable[] getDate() { Comparable[] res=new Comparable[count]; System.arraycopy(wer, 0, res, 0, count); return wer=res; } public int count() { return this.count; } } 

牌盒存牌方法和发牌方法

讯享网import java.util.Arrays; import java.util.Comparator; import java.util.Random; public class PaiHe { private ArrayList paise=new ArrayList(52); Random r=new Random(); public ArrayList getPaise() { return paise; } public PaiHe() { for(Paise tmp:Paise.values()) { for(int k=4;k<17;k++) { paise.add(new Pai(k,tmp)); } } } public Pai[] FaPai() { Pai[] res=new Pai[16]; for(int i=0;i<res.length;i++) { int pos=r.nextInt(paise.count());; Object pp=paise.delete(pos); if(pp!=null&&pp instanceof Pai) { Pai p=(Pai) pp; res[i]=p; } } return res; } } 

发牌测试类

import java.util.Arrays; import java.util.Comparator; public class Youxi { public static void main(String[] args) { PaiHe ph = new PaiHe(); for (int i = 0; i < 3; i++) { Pai[] res = ph.FaPai(); Arrays.sort(res, new Comparator<Pai>() { public int compare(Pai m1, Pai m2) { int pp = m1.getNum() - m2.getNum(); if (pp == 0) { pp = m1.getColor().compareTo(m2.getColor()); } return pp; } }); System.out.println(Arrays.toString(res)); } System.out.println("底牌为:"); System.out.println(Arrays.toString(ph.getPaise().getDate())); } }

小讯
上一篇 2025-01-19 23:03
下一篇 2025-04-11 08:40

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/19046.html