查找并从Int数组中删除对

时间:2018-07-10 16:27:01

标签: java arrays poker

我正在编写一个扑克手评估器,并且我在天真的解决方案的直观性方面苦苦挣扎(很明显!)。我的方法之一是评估给定的手牌是否存在:

int[] handValues = {5,7,8,8,11};

是手工类型的“一对”-也就是说,数组中是否有一对。

您中的一些熟悉扑克的人会知道,如果两个玩家的手型相同,则最高的一对获胜(如果这两个对具有相同的价值,那么它将变为最高的非配对,依此类推上...)。出于这个原因,我需要添加一个数字来表示对,然后将handValues的其余部分添加到topValues的单独数组中,非对值按降序排列。

例如,在上面的示例中,我的topValues数组为

8, 11, 7, 5

幸运的是,该数组已经按升序排序,所以理想的情况是:

1 if(pair exists in handValues) {
2     topValues.add(pair number)
3     handValues.remove(pair number)
4.    topValues.add(handValues.reverse)
5. }

但是我不确定如何实现,甚至不确定。现在,我必须使用一种非常冗长而愚蠢的方法,如下所示。它有效,但是我希望有更好的东西!任何帮助将不胜感激。

private boolean isOnePair() {
    // 1st case: 1 & 2
    if (this.handValues[0] == this.handValues[1]) {
        this.topValues.add(this.handValues[0]);
        this.topValues.add(this.handValues[4]);
        this.topValues.add(this.handValues[3]);
        this.topValues.add(this.handValues[2]);
        return true;
    }

    // 2nd case: 2 & 3
    if (this.handValues[1] == this.handValues[2]) {
        this.topValues.add(this.handValues[1]);
        this.topValues.add(this.handValues[4]);
        this.topValues.add(this.handValues[3]);
        this.topValues.add(this.handValues[0]);
        return true;
    }

    // 3rd case: 3 & 4
    if (this.handValues[2] == this.handValues[3]) {
        this.topValues.add(this.handValues[2]);
        this.topValues.add(this.handValues[4]);
        this.topValues.add(this.handValues[1]);
        this.topValues.add(this.handValues[0]);
        return true;
    }

    // 4th case: 4 & 5
    if (this.handValues[3] == this.handValues[4]) {
        this.topValues.add(this.handValues[3]);
        this.topValues.add(this.handValues[2]);
        this.topValues.add(this.handValues[1]);
        this.topValues.add(this.handValues[0]);
        return true;
    }

    return false;
}

2 个答案:

答案 0 :(得分:0)

如果已排序,请遍历数组一次!它更干净,更容易。不要使用最后一个元素,只需last element - 1

    public static void main(String args[]) throws IOException {

        int[] handValues = {5,7,6,11,11};
        System.out.println(isOnePair(handValues));
}

    private static boolean isOnePair(int[] values) {
       for(int i=0; i<values.length-1; i++)
       {
           if(values[i] == values[i+1])
           {
               return true;
           }
       }
        return false;
    }

输出:

true

答案 1 :(得分:0)

如果您想每次从数组中删除该对,则可以在包含某些字符集(整数)的任何数组中使用逻辑来进行处理。 它的内容很少:-

  1. 迭代给定数组并将其存储在hasmap中。
  2. 具有条件,即key是数组中的整数,而value是其出现在数组中的次数。
  3. 现在仅将这些值存储在新的哈希图中,该哈希图中的值对小于2,即唯一 4将哈希表转换为数组。

所以最后您可以输入数组并作为输出,它将提供一个具有唯一字符集的新数组来工作

public static void main(String[] args) {

    int[] st = {1,2,3,4,5,6,7,7};   
    HashMap<Integer,Integer> hm = new HashMap<>();

    for (Integer c : st) {
        Integer count = hm.get(c);
        if(count == null)
          hm.put(c,1);
        else
          hm.put(c,hm.get(c)+1);
    }

    System.out.println(hm);          
    HashMap<Integer, Integer> hm1 = new HashMap<>();
    Set<Entry<Integer,Integer>> es = hm.entrySet();

    for(Entry<Integer,Integer> en : es) {
        if(en.getValue() < 2)
            hm1.put(en.getKey(), n.getValue());
    }

    System.out.println("---------"+hm1);
    Object[] ar = hm1.keySet().toArray();

    for(Object i : ar) {        
        System.out.println(i);      
    }

    System.out.println("Length-"+ar.length);
}