所以我正在打扑克游戏,并且已经完成了我的直接和双手,但不知道从哪一种开始我的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;
}
}
'Card'类中'isSameFace()'方法的逻辑是什么? – 2014-10-28 20:28:59
我不确定你的isPair()和ThreeOfAKind()方法的含义。他们是否应该检查当前的手是否只是一对(一种三)或是否包含一对(但也可能是三种)。 – markus 2014-10-28 20:29:05
一个建议:当以这种方式评估扑克牌手(即测试手牌类型而不是使用复杂的查找表技术)时,有三件事是必不可少的:(1)按照从高到低的顺序排列牌, 2)从上到下测试每种手型,即首先查找直线冲洗,然后四边形,然后满屋子等,最后不留下对,并且(3)不要忘记轮子的特殊情况A2345笔直)。 – 2014-10-28 21:28:58