2014-10-28 72 views
-2

所以我正在打扑克游戏,并且已经完成了我的直接和双手,但不知道从哪一种开始我的3种。任何帮助将在这里apprciated是我的扑克手包。如果我能完成这三只手,我终于可以继续前进,所以这只手让我完全陷入了僵局。java中的三种扑克手

package edu.rcc.hand; 

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

import edu.rcc.deck.Card; 

public class Hand implements Comparable<Object> { 

//Card[] theCards = new Card[10]; 
private ArrayList<Card> theCards = new ArrayList<Card>(); 

public Hand(ArrayList<Card> theCards) { 
    this.theCards = theCards; 
} 

public String toString() { 
    String s = ""; 
    for (Card c : theCards) { 
     s += c.toString() + ", "; 
    } 
    return s; 
} 

private ArrayList<Card> getHandInNumericalOrder() { 
    ArrayList<Card> sorted = new ArrayList<Card>(); 
    sorted.addAll(this.theCards); 
    Collections.sort(sorted, new Comparator<Card>() { 

     @Override 
     public int compare(Card o1, Card o2) { 
      Integer o1Value = o1.getNumericalValue(); 
      Integer o2Value = o2.getNumericalValue(); 
      return o1Value.compareTo(o2Value); 

     } 

    }); 
    return sorted; 
} 

/** 
* a method to get cards that have the same value 
*/ 
private boolean isPair(ArrayList<Card> sortedCards) { 

    Card previousCard = null; 
    for (Card c : sortedCards) { 

     if (previousCard != null && previousCard.isSameFace(c)) { 
      return true; 
     } else { 
      previousCard = c; 
     } 


    } 
    return false; 
} 


private boolean ThreeOfKind(ArrayList<Card> sortedCards) 
{ 

    Card previousCard = null; 
    for (Card c : sortedCards) { 


     if (previousCard != null && previousCard.isSameFace(c)) { 
      return true; 
     } else { 
      previousCard = c; 
     } 


    } 
    return false; 
} 

/** 
* if the sum of the integer array is equal to the following: 
* 1 - 
* @return 
*/ 
private ArrayList<Integer> numberOfSameCards() { 
    ArrayList<Card> sortedCards = this.getHandInNumericalOrder(); 

    ArrayList<Integer> cardCounts = new ArrayList<Integer>(); 
    int numbSame = 1; 
    for (int i = 0; i < sortedCards.size() - 1; ++i) { 
     if (sortedCards.get(i).isSameFace(sortedCards.get(i+1))) { 
      ++numbSame; 
     } else { 
      for (int j = 0; j < numbSame; ++j) { 
       cardCounts.add(numbSame); 
      } 
      numbSame = 1; 
     } 
    } 
    for (int i = 0; i < numbSame; ++i) { 
     cardCounts.add(numbSame); 
    } 


    return cardCounts; 
} 

/** 
* 5 - high card || strait || strait flush || flush 
* 7 - pair 
* 9 - 2 pair 
* 11 - 3 of a kind 
* 13 - full house 
* 17 - four of a kind 
* @param inputArray 
* @return 
*/ 
private int getSum(ArrayList<Integer> inputArray) { 
    int sum = 0; 
    for (int i : inputArray) { 
     sum += i; 
    } 
    return sum; 
} 

/** 
* 0 - high card 
* 1 - pair 
* 2 - 2 pair 
* 3 - three of a kind 
* 4 - strait 
* 5 - flush 
* 6 - full house 
* 7 - four of a kind 
* 8 - strait flush 
* @return 
*/ 
private int getRank() { 
    ArrayList<Card> sorted = this.getHandInNumericalOrder(); 
    int rawSum = getSum(numberOfSameCards()); 
    switch (rawSum) { 
     case 7: 
      return 1; 
     case 9: 
      return 2; 
     case 11: 
      return 3; 
     case 13: 
      return 6; 
     case 17: 
      return 7; 
     default: 
      //TODO CHECK FOR ALL OTHER HAND TYPES 
    } 
    boolean isFlush = isFlush(sorted); 
    boolean isStrait = isStrait(sorted); 
    if (isFlush && isStrait) { 
     return 8; 
    } else if (isFlush) { 
     return 5; 
    } else if (isStrait) { 
     return 4; 
    } else { 
     return 0; 
    } 
} 

private boolean isFlush(ArrayList<Card> theCards) { 

    for (int i = 0; i < theCards.size() - 1; i++) { 
     if (!theCards.get(i).isSameSuit(theCards.get(i + 1))) { 
      return false; 
     } 
    } 
    return true; 

} 

/** 
* 
* @param sortedCards - all have unique faces 
* @return 
*/ 
private boolean isStrait(ArrayList<Card> sortedCards) { 

    for (int i = 0; i < theCards.size(); i++) { 

     int current = theCards.get(i).getNumericalValue(); 

     int next = theCards.get(i + 1).getNumericalValue(); 

     if (current + 1 == next || 
       (current == 5 && next == 14)) { 
      continue; 
     } else { 
      return false; 
     } 

    } 
    return true; 

} 

/** 
* 0 -> equal 
* 1 -> this > o 
* -1 -> this < o 
*/ 
@Override 
public int compareTo(Object o) { 
    if (!(o instanceof Hand)) { 
     return 1; 
    } 
    Hand other = (Hand)o; 


    boolean isThisPair = other.isPair(other.getHandInNumericalOrder()); 
    boolean isThisPair2 = this.isPair(this.getHandInNumericalOrder()); 
    boolean isStrait = this.isStrait(this.getHandInNumericalOrder()); 
    boolean isStrait2 = other.isStrait(other.getHandInNumericalOrder()); 
    boolean isFlush = this.isFlush(this.getHandInNumericalOrder()); 
    boolean isFlush2 = other.isFlush(other.getHandInNumericalOrder()); 
    boolean ThreeOfKind = this.isFlush(this.getHandInNumericalOrder()); 
    boolean ThreeOfKind2 = other.isFlush(other.getHandInNumericalOrder()); 

    ArrayList<Card> otherNumerical = other.getHandInNumericalOrder(); 
    ArrayList<Card> thisNumerical = this.getHandInNumericalOrder(); 


    System.out.println(thisNumerical); 
    System.out.println(otherNumerical); 


    if(isThisPair2 == true) 
    { 
     System.out.println("The first hand is a pair"); 
    } 
    if(isThisPair == true){ 
     System.out.println("The second hand has a pair"); 
    } 
    if (isStrait == true){ 
     System.out.println("The first hand has a straight"); 

    } 
    if (ThreeOfKind == true){ 
     System.out.println("The sfirst hand has a straight"); 

    } 
    if (ThreeOfKind2 == true){ 
     System.out.println("The second hand has a straight"); 

    } 


    System.out.println(isThisPair2); 
    System.out.println(isThisPair); 

    System.out.println(isStrait); 
    System.out.println(isFlush); 





    //call the card hierarchy class 
    // TODO Auto-generated method stub 
    return 0; 
} 



} 
+0

'Card'类中'isSameFace()'方法的逻辑是什么? – 2014-10-28 20:28:59

+0

我不确定你的isPair()和ThreeOfAKind()方法的含义。他们是否应该检查当前的手是否只是一对(一种三)或是否包含一对(但也可能是三种)。 – markus 2014-10-28 20:29:05

+0

一个建议:当以这种方式评估扑克牌手(即测试手牌类型而不是使用复杂的查找表技术)时,有三件事是必不可少的:(1)按照从高到低的顺序排列牌, 2)从上到下测试每种手型,即首先查找直线冲洗,然后四边形,然后满屋子等,最后不留下对,并且(3)不要忘记轮子的特殊情况A2345笔直)。 – 2014-10-28 21:28:58

回答

2

使用HashMap来计算给定卡牌等级在手中出现的次数。通过你的手迭代。如果卡片的等级不存在于地图中,则插入值为1.如果卡片的值已经存在于地图中,则将值增加1.然后,值为3的任何键值对都将跳闸。适应这个概念寻找成对,满船,四边形是非常容易的。

有一个概念,现在实现代码。

+1

+1不吸食吸血鬼 – tnw 2014-10-28 20:29:00

+2

@tnw谢谢,亲切。我们只是通过鼓励积极的思考来帮助自己,因为有一天,我们可能需要调试这个人的代码。 :) – MarsAtomic 2014-10-28 20:30:16

+0

如果您有五张按排名排序的卡片的阵列,则可以通过不超过三次比较来确定是否存在三种卡片。构建HashMap将会浪费时间。 – 2014-10-28 21:34:44

0

看着你的代码,我建议你使用for循环而不是增强for循环遍历它。实际上,如果你已经完成了一些工作,我会建议你使用一个迭代器,但是如果没有的话,你可以使用Collections频率方法来计算一个集合中出现的次数。您可以使用ArrayList中的值创建一个临时集合,该集合应该删除重复项,并且可以使用具有该集合的非重复项的For Each循环来查找原始ArrayList中重复项的数量。

0

忽视不同的变量。这是我在我的项目中做的。

boolean hasThreeOfAKind = false; 
     int[] value = new int[cards.length]; 
     for(int i =0; i<cards.length; i++){ 
      Card myCard = cards[i]; 
      value[i] = myCard.getValue(); //an array that stores the values of the cards passed in 
     } 
     int counter = 0; 
     for(int i =0; i<cards.length; i++){ 
      for(int j =i+1; j<cards.length; j++){ 
       if(value[i] == value[j]){ 
        counter++; //if a pair is found increment the counter 
       } 
       if(j == cards.length-1 && counter!=2){ //if the array of values has been looped 
                 //through and a second "pair" of the same 
                 //value hasn't been found. 
        counter = 0; //start again 
       } 
//if the array has been looped through and 3 cards of the same value has been found 
       else if(j== cards.length-1 && counter >= 2){ 
        hasThreeOfAKind = true; 
       } 
      } 
     } 

     return hasThreeOfAKind;