gpt4 book ai didi

java - 一副牌 - 检查顺子

转载 作者:行者123 更新时间:2023-11-30 06:13:41 26 4
gpt4 key购买 nike

我制作了这个扑克程序。它评估手牌并返回结果。除了我检查顺子的方法之外,一切都有效。没有错误,但该方法永远不会返回 true。作为测试,我在主程序 (DeckOfCardsTest) 上有一个 while 循环来重新运行程序,直到找到顺子。这是代码:

public class DeckOfCardsTest {
public static Card[] hand = new Card[5];




public static void main(String[] args) {

while(DeckOfCards.str == 0){
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle(); // place Cards in random order

// print the hand dealt
for (int i = 0; i < 5; i++) {
// deal and display a Card
//System.out.printf("%-19s%n", myDeckOfCards.dealCard());
hand[i]= myDeckOfCards.dealCard();

if (i % 5 == 0) { // output a newline after every fifth card, incase dealing 2 hands in the future
System.out.println();
}
}//end of for loop
for(int i = 0; i < hand.length; i++){
System.out.println(""+ hand[i]);
}
DeckOfCards.Pair();//check hand for pair, 2 pair, 3 of a kind, four of a kind, or a full house
DeckOfCards.hasFlush(hand);//check hand for flush

System.out.println("\n\n\t" + DeckOfCards.Results() + "\n\n " + DeckOfCards.str);
//System.out.println(""+DeckOfCards.hasStraight(hand));
DeckOfCards.flushn=0;
}
} //end of main

}//end of class

卡片类别

public class Card {
private final String face; // face of card ("Ace", "Deuce", ...)
private final String suit; // suit of card ("Hearts", "Diamonds", ...)

// two-argument constructor initializes card's face and suit
public Card(String cardFace, String cardSuit) {
this.face = cardFace; // initialize face of card
this.suit = cardSuit; // initialize suit of card
} //end of Card inilization

// return String representation of Card
public String toString() {
return face + " of " + suit;
}// endof toString method

public String getFace() {
return face;
}//end of getFace method

public String getSuit() {
return suit;
}//end of getSuit method
}//end of Class

甲板等级

import java.security.SecureRandom;

public class DeckOfCards {
// random number generator
private static final SecureRandom randomNumbers = new SecureRandom();
private static final int NUMBER_OF_CARDS = 52; // constant # of Cards
public static int flushn = 0;
public static int str=0;

//private Card[] hand;
private Card[] deck = new Card[NUMBER_OF_CARDS]; // Card references
private int currentCard = 0; // index of next Card to be dealt (0-51)

// constructor fills deck of Cards
public DeckOfCards() {
String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};

// populate deck with Card objects
for (int count = 0; count < deck.length; count++) {
deck[count] =
new Card(faces[count % 13], suits[count / 13]);
}
} //end of DeckOfCards constructor

public String getCard(){
String cCard = ("");

return cCard;
}

// shuffle deck of Cards with one-pass algorithm
public void shuffle() {
// next call to method dealCard should start at deck[0] again
currentCard = 0;

// for each Card, pick another random Card (0-51) and swap them
for (int first = 0; first < deck.length; first++) {
// select a random number between 0 and 51
int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

// swap current Card with randomly selected Card
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
} //end of shuffle method

// deal one Card
public Card dealCard() {

// determine whether Cards remain to be dealt
if (currentCard < deck.length) {
return deck[currentCard++]; // return current Card in array
}
else {
return null; // return null to indicate that all Cards were dealt
}
}//end of dealCard method


public static void hasFlush(Card[] hand) {

boolean isFlush = true;
String suit = hand[0].getSuit();
for(int i = 1; i < hand.length; i++) {
if(!(hand[i].getSuit().equals(suit))) {
isFlush = false;
}
}
if(isFlush){
flushn = 5;
}
}//end of flush evaluation

public static boolean hasStraight(Card[] hand) {
String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int minFace = 13;
for(int i = 0; i < hand.length; i++) {
int face = 13;
for(int j = 0; j < faces.length; j++) {
if(hand[i].getFace().equals(faces[j])) {
face = j;
}
}
if(face < minFace) {
minFace = face;
}
}

if(minFace > 7) {
return false;
}

boolean isStraight = true;
for(int i = minFace+1; i < minFace+hand.length; i++) {
boolean found = false;
for(int j = 0; j < hand.length; j++) {
if(hand[j].getFace().equals(faces[i])) {
found = true;
str++;
}
}
if(!found) {
str=0;
isStraight = false;
}
}
return isStraight;
}





public static int Pair(){
int pair = 0, done = 0, handcount = 1;
int pairCheck=0;

while (done <1){
for(int i = 0; i < 4; i++){
String tempCard = DeckOfCardsTest.hand[i].getFace();

for(int j=handcount; j < DeckOfCardsTest.hand.length; j++){
if(tempCard.equals(DeckOfCardsTest.hand[j].getFace()))
pair++;
}
handcount++;
}
pairCheck = pair;
done++;
}
return pairCheck;
}//endof Pair method

public static String Results(){
String results=("High Card");
int checkHand = 0;
if (Pair()>0 ){
switch (Pair()){

case 1:
results = ("Pair");
break;
case 2:
results = ("Two Pair!");
break;
case 3:
results = ("Three of a kind!");
break;
case 4:
results = ("Full House!");
break;
case 6:
results = ("FOUR of a kind!");
break;
default:
results = ("Somthing went terribly wrong, sorry");
break;
}
}//end of pairing results

if (flushn > 0){
results = ("Flush!");
}

if (str > 0){
results = ("Straight!");
}


return results;
}


}//end of Class

最佳答案

There is no error, but the method never returns true

这不是真的。您当前的 hasStraight 方法几乎是正确的,并且实际上正确返回 true/false。您遇到的主要问题是您的主循环使用静态变量str。存在一个错误,无法将 str 正确重置为零。

简而言之,有两种方法可以解决您的问题:

1) 修复 str 的问题

if (!found) {
str = 0;
isStraight = false;
break;
}

2) 主循环应使用 hasStraight 的 boolean 结果,而不是 str

boolean b = false;
while (!b) {
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle(); // place Cards in random order

// print the hand dealt
for (int i = 0; i < 5; i++) {
// deal and display a Card
// System.out.printf("%-19s%n", myDeckOfCards.dealCard());
hand[i] = myDeckOfCards.dealCard();

if (i % 5 == 0) { // output a newline after every fifth card,
// incase dealing 2 hands in the future
System.out.println();
}
} // end of for loop
for (int i = 0; i < hand.length; i++) {
System.out.println("" + hand[i]);
}
DeckOfCards.Pair();// check hand for pair, 2 pair, 3 of a kind, four
// of a kind, or a full house
DeckOfCards.hasFlush(hand);// check hand for flush

System.out.println("\n\n\t" + DeckOfCards.Results() + "\n\n " + DeckOfCards.str);
b = DeckOfCards.hasStraight(hand);
System.out.println("Straight= " + b);
DeckOfCards.flushn = 0;
}

如果我使用这些修复中的任何一个,主循环循环直到出现直线。

注释:1.我认为minFace检查应该是:

if (minFace > 8) {
return false;
}
  • A 可以很高吗?
  • 王牌代码?

    boolean hasAce = false;
    for (int i = 0; i < hand.length; i++) {
    if (hand[i].getFace().equals("Ace"))
    hasAce = true;
    }

    // ... Current code ....

    if (!isStraight && hasAce) {

    isStraight = true;
    for (int i = 9; i < 13; i++) {
    boolean found = false;
    for (int j = 0; j < hand.length; j++) {
    if (hand[j].getFace().equals(faces[i])) {
    found = true;
    str++;
    }
    }
    if (!found) {
    str = 0;
    isStraight = false;
    break;
    }
    }
    }
    return isStraight;

    关于java - 一副牌 - 检查顺子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49697095/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com