java吧 关注:1,273,006贴子:12,784,422
  • 9回复贴,共1

斗地主洗牌,顺序发牌问题,求大神...........

只看楼主收藏回复

public class PUKE {
static int [] tags=new int[54];
static String[] cards= {"方块2","方块3","方块4","方块5","方块6","方块7","方块8","方块9","方块10","方块J","方块Q","方块k","方块A"
,"黑桃2","黑桃3","黑桃4","黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃","黑桃A"
,"红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9","红桃10","红桃J","红桃Q","红桃k","红桃A",
"麻花2","麻花3","麻花4","麻花5","麻花6","麻花7","麻花8","麻花9","麻花10","麻花J","麻花Q","麻花k","麻花A","小王","大王"};
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=0;
int current=1;//当前摸牌人
for(i=0;i<54;i++)
tags[i]=0;
for(i=0;i<51;i++)
{
Random rm=new Random();
int temp=rm.nextInt(54);
if(tags[temp]!=0)
{
i--;
continue;
}
else
{
tags[temp]=current%3+1;
current++;
}
}
showW(1);showW(2);showW(3);showW(0);
}
private static void showW(int w)
{
int i=0;
if(w==0)
System.out.print("底牌:");
else
System.out.print("玩家"+w+":");
for(i=0;i<54;i++)
{
if(tags[i]==w)
System.out.print(cards[i]+" ");
}
System.out.println();
}
}
这是洗牌发牌, 发的牌是乱的怎么样发完之后 排序显示;
求解答,本人新手。


IP属地:新疆1楼2013-12-16 14:51回复
    可以把牌定义成一个类,类里面2个成员变量:牌的ID,牌的信息。接下来的排序就简单了吧


    2楼2013-12-16 15:02
    收起回复
      2025-08-07 17:28:20
      广告
      不感兴趣
      开通SVIP免广告
      新手别做这些东西,,等学了几个月后再做这些东西,,
      牌肯定是一个类,里面有牌的各种信息,最好能实现Compareable接口比较大小


      IP属地:山东3楼2013-12-16 15:26
      回复
        //调用方法如下。分牌的时候,你可以吧pokers顺序打乱,然后每个人分等量的牌,用数组保存,然后分别对每个人的牌调用排序。再决定地主之后,你把底牌给该人,然后再对该人的牌调用一次排序。
        //获取所有的牌
        Poker[] pokers = PokerUtil.getBasePokers();
        //把牌打乱,你也可以实现自己的算法,这种是最简单的但并不是高效及公平的
        PokerUtil.randPokers(pokers);
        //对整副牌排序[仅作参考,你可以类比对单个人的牌排序]
        Arrays.sort(pokers, Poker.pokerSortDesc);
        //下边是必须的类。
        class Poker {
        static class PokerSort implements Comparator<Poker> {
        private boolean desc;
        public PokerSort(boolean desc) {
        this.desc = desc;
        }
        public int compare(Poker o1, Poker o2) {
        if (o1 == null || o2 == null) {
        if (o1 != null) {
        return desc ? 1 : -1;
        }
        if (o2 != null) {
        return desc ? -1 : 1;
        }
        return 0;
        }
        int value = Double.compare(o1.index, o2.index);
        if (value == 0) {
        value = Double.compare(o1.type, o2.type);
        }
        return desc ? value : -value;
        }
        }
        public static PokerSort pokerSortAsc = new PokerSort(false);
        public static PokerSort pokerSortDesc = new PokerSort(true);
        private int index; // 0, 1, 2, ..., 12, 13, 14[13, 14 => 大王,小王]
        private String name; // A, 2, 3, ..., J, Q, K, 小王, 大王
        private int type;// 0 => 未设置, 1 => 方片,2 => 梅花, 3 => 红桃, 4 => 黑桃
        public Poker(int type, String name, int index) {
        super();
        this.type = type;
        this.name = name;
        this.index = index;
        }
        public int getIndex() {
        return index;
        }
        public String getName() {
        return name;
        }
        public int getType() {
        return type;
        }
        public String toString() {
        return String.format("%2d => %d %s", index, type, name);
        }
        }
        class PokerUtil {
        public static Poker[] getBasePokers() {
        Poker[] pokers = new Poker[54];
        String[] names = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
        "J", "Q", "K", "小王", "大王" };
        for (int i = 0; i < 13; i++) {
        pokers[i] = new Poker(1, names[i], i);
        pokers[i + 13] = new Poker(2, names[i], i);
        pokers[i + 26] = new Poker(3, names[i], i);
        pokers[i + 39] = new Poker(4, names[i], i);
        }
        pokers[52] = new Poker(0, names[13], 13);
        pokers[53] = new Poker(0, names[14], 14);
        return pokers;
        }
        public static void randPokers(Poker[] pokers) {
        if (pokers == null || pokers.length < 2) {
        return;
        }
        int indexA = 0;
        int indexB = 0;
        Poker poker = null;
        int length = pokers.length;
        Random random = new Random();
        for (int i = 1000; i > 0; i--) {
        indexA = random.nextInt(length);
        indexB = random.nextInt(length);
        poker = pokers[indexA];
        pokers[indexA] = pokers[indexB];
        pokers[indexB] = poker;
        }
        }
        }


        IP属地:山东4楼2013-12-16 16:21
        收起回复
          不明觉厉,顶一个。卤煮你的肥皂掉地上了


          来自Android客户端5楼2013-12-16 17:25
          回复