English 中文(简体)
对比 j木手
原标题:Comparing poker hand in java

我正在开展一场波民游戏。 到目前为止,我一直坐在比较掩体手上。 我对如何做到这一点有某种想法,但我并不相信我的法律有什么错误。 有些人可以指出什么错误?

因此,请允许我简单介绍一下我的法典正在做些什么。

My other part of the program will go through the cards in my hand and list the frequency of a card value in an array.

h = Hearts
c = Clubs
d = Diamonds
s = Spades
d4 = 4 of Diamond

So let s say in my hand I have c3, h4, d4, s4, 2d I will call the other part of my program to read the cards in my hand and return a tally array. The tally array for my hand above will be [0,0,1,1,3,0,0,0,0,0,0,0,0,0] So pretty much what this array is saying is that my hand has one 2, one 3 and three 4.

现在,这是我试图找到三个幼儿园的方法,它应当归还给全部住房的假象(这三类加一平)。

public boolean hasThreeOfAKind() {
    int [] temp;
    temp = this.getCounts();
    for (int i = 0; i< temp.length; i++){
        if (temp [i] == 3 ){
            for (int j = 0; j < temp.length ; j++)
                if ( temp [j] != 2){
                    return true;
                }
        }
    }return false;

因此,我试图做到的首先是,我穿过阵列,如果有的话,还有3个。 如果是3个,我再次穿过阵列,看看是否有2个。 如果没有2,我知道这是三种。 如果是2个住房,那是完整的住房,我就应该回去。 我认为,我的逻辑是正确的,但与我的法典有些错误。

我遇到麻烦的第二个方面是,如何确定我的手是否有一个奶粉。 只有一对一对一对,才能返回。 如果存在两张奶制品,就不实返回。

public boolean hasOnePair() {
    int [] temp;
    temp = this.getCounts();

    for (int i = 0; i< temp.length; i++){
        if (temp [i] == 2 ){
            return true;
        }
    }return false;

就这一点而言,我想到的是,阵列的价值是按 des或按 as令排列的。 如果我选择了降排令,我先读一下阵列,看看是否有2,那么我就可以对下一个命令进行扫描,看看头2之后的下一个数值是否也是2。 如果是另外2个,那就会退回不实。

有些人可以看一下我的法典,指出什么错误? 非常感谢。

最佳回答

在您的<条码>中,有以下错误:

for (int j = 0; j < temp.length ; j++) 
  if ( temp [j] != 2){ 
    return true; 
  } 
} 

This will return true the first time it finds a non-2 j (which will be statisfied with the 3 you just checked above it -- thus returns true for a full house. You need instead:

boolean foundTwo = false;
for (int j = 0; j < temp.length ; j++) 
  if ( temp [j] == 2){ 
    foundTwo = true; 
  }
} 
if (!foundTwo) {
  return false;
}

同样,如果你发现另外2个与你已经发现的2个不同的话,你需要检查:

for (int i = 0; i< temp.length; i++) {  
  if (temp [i] == 2 ){
    boolean foundAnother = false;
    for (int j = 0; j< temp.length; j++) { 
      if (i != j && temp [j] == 2 ){
        foundAnother = true;  
      }  
    }
    if (!foundAnother) {
      return true;
    }
  }  
}
return false;  

你们可以做的另一件事是,每个被承认的手上都有过滤器:乳房过滤器、三过滤器、全家过滤器等,并通过手操作所有这些过滤器。 如果有更好的(高价值)配对,那么就会发现哪些过滤器能够真正返回(根据它们所寻求的模式),并选择最高点值。

问题回答

为什么你与这种低水平的原始公司合作? 是否有理由重新使用完整的卡片级? 你的法典将更加简单。

class Card
  enum Value {
    TWO,
    ...
    ACE
  };

  enum Suit {
    SPADES,
    ...
    CLUBS

  };
  private Suit suit;
  private Value value;

  public Card(Suit suit, Value value) {
    this.suit = suit;
    this.value = value;
  }
}

public class Hand {
  private final List<Card> cards;
  public Hand(Card first, Card second, Card third, Card fourth, Card fifth) {
     // add to cards list.

     // sort ascending by value

  }

  public boolean hasThreeOfAKind() {
    for (int i = 0; i < 3; i++) {
      Value firstValue = cards.get(i).getValue();
      Value secondValue = cards.get(i+1).getValue();
      Value thirdValue = cards.get(i+2).getValue();
      if (firstValue == secondValue && secondValue == thirdValue) {
        return true;
      }
    }
    return false;
  }
}

这不能直接回答你的问题,但我认为,这种法典更可读、更可维持,而且比仅涉及煽动者的法律更容易被削弱。 Java isn t C, 你并没有像C那样对待它。

In your threeOfAKind method, the second for loop will only get executed once unless the first number stored in your array is a 2, it should look more like this:

public boolean hasThreeOfAKind() {
    int [] temp;
    temp = this.getCounts();
    for (int i = 0; i< temp.length; i++){
        if (temp [i] == 3 ){
            for (int j = 0; j < temp.length ; j++)
                if ( temp [j] == 2){
                    return false;
                }
        }
    }
    return true;
}

在上述法典中,它第一次打上了一座楼梯,因为它认识到,手是一家完整的房屋,而不是一个三套住房,并且是假的。

并且就你的其他方法而言,它应当更像:

public boolean hasOnePair() {
    int [] temp;
    temp = this.getCounts();
    int count = 0;

    for (int i = 0; i< temp.length; i++){
        if (temp [i] == 2  ){
            count++;
        }
    }
    return count == 1;
}

检查每张类似卡的数量和阵列。 SOka{1,1,2,2,2,5,7} (letsooks over therips for now) willmap to (3,2,1,1) i,e a full home You caneck the second protocol

我帮助我的儿子在大学的 Java课程(我所教授的课程)上遇到同样的问题,这是我所建议。

First sort your cards from lowest to highest 2 to Ace regardless of suit. Then do comparing. If card[0]==card[3] or card[1]==card[4], you have 4 of a kind, ignore next 2 lines. If card[0]==card[2] or card[1]==card[3] or card[2]==card[4], you have 3 of a kind, ignore the next line. If card[0]==card[1] or card[1]==card[2] or card[2]==card[3] or card[3]==card[4], you have a pair.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签