我制作了这个扑克计划。它评估手并返回结果。 一切都有效,除了我检查直线的方法。没有错误,但该方法永远不会返回true。 作为测试,我在main(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
答案 0 :(得分:1)
没有错误,但方法永远不会返回true
事实并非如此。您当前的hasStraight
方法几乎是正确的,实际上确实正确地返回了true / false。您遇到的主问题是您的主循环使用静态变量str
。有一个错误没有将str
正确重置为零。
简而言之,有两种方法可以解决您的问题:
1)使用str
if (!found) {
str = 0;
isStraight = false;
break;
}
2)主循环应使用hasStraight
的布尔结果而不是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;
}
如果我使用其中任何一个修正,主循环将循环,直到有一个直线。
注意: 我相信minFace检查应该是:
if (minFace > 8) {
return false;
}
Ace代码?
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;