在扑克中直接检测

时间:2014-01-26 00:05:58

标签: java poker

好的,我有以下定义的方法straight(ArrayList<Card> l)。它可以工作,但我真的想改进它,因为我的算法运行速度很慢。

这里的想法是:获取卡的所有值并将它们添加到arraylist,然后检查arraylist是否包含直道列表。如果是,将其添加到散列图中,添加的最后一个数组将是最好的直接(如果有3个直:1 2 3 4 5 6 7)

卡片由2个数组定义:值和套装:

private String[] name = {"Joker", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
private String[] suit = {"Club", "Diamond", "Spade", "Heart"};

所以我有两个问题:

1,有没有改进下面的代码?

2,我为了好玩这个项目,我希望这是多个玩家。那么服务器应该检测每个玩家的最佳手牌,还是玩家做到这一点,然后通过一对形式(String,ArrayList bestHand)将结果发送回服务器?

public ArrayList<Card> straight(ArrayList<Card> l){
        ArrayList<Card> straight = new ArrayList<>();
        ArrayList<Card> card_value = this.removeDuplicate(l);
        ArrayList<Integer> temp_value = this.getCardValue(card_value);

        ArrayList<Integer[]> possible_straight = new ArrayList<>();
        possible_straight.add(new Integer[]{1, 2, 3, 4, 5});
        possible_straight.add(new Integer[]{2, 3, 4, 5, 6}); 
        possible_straight.add(new Integer[]{3, 4, 5, 6, 7});
        possible_straight.add(new Integer[]{4, 5, 6, 7, 8});
        possible_straight.add(new Integer[]{5, 6, 7, 8, 9});
        possible_straight.add(new Integer[]{6, 7, 8, 9, 10});
        possible_straight.add(new Integer[]{7, 8, 9, 10, 11});
        possible_straight.add(new Integer[]{8, 9, 10, 11, 12});
        possible_straight.add(new Integer[]{9, 10, 11, 12, 13});
        possible_straight.add(new Integer[]{10, 11, 12, 13, 1});

        HashMap<Integer, Integer[]> check_list = new HashMap<>();
        int controller = 0;

        for (int i = 0; i < possible_straight.size(); i++){
            Integer[] dummy = possible_straight.get(i);
            if (temp_value.containsAll(Arrays.asList(dummy))){
                check_list.put(controller++, dummy);
            }
        }

        if (!check_list.isEmpty()){
            for (int k = 0; k < check_list.get((controller - 1)).length; k++){
                for (int m = 0; m < card_value.size(); m++){
                    Card temp_card = card_value.get(m);
                    if (temp_card.getValue() == check_list.get(controller - 1)[k]){
                      straight.add(temp_card);
                    }
                }
            }
        }

        return straight;
    }

2 个答案:

答案 0 :(得分:0)

对你的第一个问题的回答:除了特殊情况10,11,12,13,1之外,你可以通过简单地对数组进行排序然后检查每个元素以查看它是否只是元素一个来做到这一点在它之前。以下代码是您应该需要的所有内容,除了处理该特殊情况:(请注意,此解决方案返回一个int列表,而不是一个卡列表。您可能希望修改它以返回卡片。)

ArrayList<int[]> straights = new ArrayList();
sort(l)//Implement some sort of sorting algorithm here.
int consecutive=0;
for(int i=1;i<l.size();i++){
    if(l.get(i).getValue()-l.get(i-1).getValue()<=1)){
        consecutive++;
        if(consecutive>=5){
            int[] values=new int[5];
            values[0]=l.get(i).getValue()-4;
            for(int j=1;j<5;j++)values[j]=values[0]+j;
            straights.add(values);
        }
    }
}

对列表进行排序后,会查看列表中是否有任何卡片与之前的卡片不完全相同;如果有,则返回一个空列表,如果没有,则返回原始列表。

答案 1 :(得分:0)

您可以检索所有可能的直道:

输出

[0, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 12, 13, 14]
Straight?: [[0, 1, 2, 3, 4], [4, 5, 6, 7, 8], [8, 9, 10, 11, 12]]

StrightDetector.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StraightDetector {
    public static void main(String[] args) {
        List<Card> cards = new ArrayList<Card>();
        int lengthOfStrait = 5, numberOfStrights = 3;
        for (int i = 0; i < lengthOfStrait * numberOfStrights; i++) {
            cards.add(new Card(i));
            // Insert a duplicate after every straight. 
            if (i % (lengthOfStrait - 1) == 0) cards.add(new Card(i));
        }
        System.out.println(cards);
        List<List<Card>> strights = findStrights(cards, lengthOfStrait);
        System.out.println("Straight?: " + (!strights.isEmpty() ? strights : "Nope..."));
    }

    public static List<List<Card>> findStrights(List<Card> cards, int length) {
        List<List<Card>> straights = new ArrayList<List<Card>>();
        Collections.sort(cards);
        int consecutive = 1;
        for (int i = 1; i < cards.size(); i++) {
            if (Math.abs(cards.get(i).getRank() - cards.get(i - 1).getRank()) >= 1) {
                consecutive++;
                if (consecutive >= length) {
                    List<Card> values = new ArrayList<Card>();
                    for (int j = length - 1; j >= 0; j--) values.add(cards.get(i - j));
                    straights.add(values);
                }
            } else
                consecutive = 1;
        }
        return straights;
    }
}

Card.java

public class Card implements Comparable<Card>, Comparator<Card> {
    private int rank;
    public int getRank() { return rank; }
    public void setRank(int rank) { this.rank = rank; }

    public Card(int rank) {
        this.rank = rank;
    }

    @Override
    public int compare(Card card1, Card card2) {
        return card1.compareTo(card2);
    }
    @Override
    public int compareTo(Card other) {
        return Integer.compare(getRank(), other.getRank());
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        return prime * result + rank;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        Card other = (Card) obj;
        if (rank != other.rank) return false;
        return true;
    }
    @Override
    public String toString() {
        return String.format("%d", rank);
    }
}