2017-02-14 4 views
0

私は、ロイヤルフラッシュが処理されるまで、一度に5枚のカードを扱うことをシミュレートするプログラムを書く必要があります。これは10回行い、ロイヤルフラッシュを処理するのにかかる回数の平均をとっています。私はうまく動作するプログラムを書いたが、1つのデッキに2枚のカードがないという事実を考慮していないので、ロイヤルフラッシュを処理するのにかかった回数は20万回を超えていた。私はそれを修正したと思うが、今はプログラムが終了していないので、出力が得られない。無意識のうちに無限ループを作りましたか?このJavaプログラムはなぜ終了しませんか?

import java.util.Random; 

public class Problem1Version2 { 

public static void main(String[] args) { 

    // This program simulates the dealing of poker hands and returns the average 
    // over 10 rounds of how many hands are dealt until a royal straight flush is received. 

    // Initialize flush to false, which indicates that a royal straight flush has not 
    // yet been dealt. Set the count variable, i, to 1. 
    boolean flush = false; 
    int i = 1; 

    // Initialize the cards to 0 before any cards are dealt. 
    int card1 = 0, card2 = 0, card3 = 0, card4 = 0, card5 = 0; 

    // Initialize the total number of tries across all experiment rounds to 0. 
    int totalTries = 0; 

    // Repeat experiment for 10 seeds. 
    for (int j = 0; j < 10; j++) { 

     // The round number is the seed. 
     Random r = new Random(j); 

     // Deal hands of 5 until a royal straight flush is dealt. 
     while (flush == false) { 

      // To simulate the use of one deck, only one of each card can be dealt in a hand. 
      while (card1 == card2 || card1 == card3 || card1 == card4 || card1 == card5 || 
       card2 == card3 || card2 == card4 || card2 == card5 || card3 == card4 
       || card3 == card5 || card4 == card5) { 

       // Cards 1-13 are the hearts, 14-26 the spades, 27-39 the clubs, and 40-52 the diamonds. 
       card1 = r.nextInt(52) + 1; 
       card2 = r.nextInt(52) + 1; 
       card3 = r.nextInt(52) + 1; 
       card4 = r.nextInt(52) + 1; 
       card5 = r.nextInt(52) + 1; 
      } 

      // Calculate the max and min of the cards. 
      int max = Math.max(card1, Math.max(card2, Math.max(card3, Math.max(card4, card5)))); 
      int min = Math.min(card1, Math.min(card2, Math.min(card3, Math.min(card4, card5)))); 

      // Check if the hand dealt is a royal straight flush by confirming that 
      // they span 5 consecutive numbers (this is true if the difference between 
      // the max and min is 4 since they are all different), and that they all come 
      // from the same suit (1-13, 14-26, 27-39, or 40-52). 

      if (max - min == 4) { 
       if ((1 <= min) && (13 >= max)) { 
        flush = true; 
       } 
       else if ((14 <= min) && (26 >= max)) { 
        flush = true; 
       }  
       else if ((27 <= min) && (39 >= max)) { 
        flush = true; 
       }  
       else if ((40 <= min) && (52 >= max)) { 
        flush = true; 
       } 
       else { 
        i++;  
       } 
      } 
      else { 
       i++; 
      } 

     } 

     // Add count from last round to total number of tries over the 10 rounds. 
     totalTries += i; 

     // Reset flush to false for the while loop. 
     flush = false; 
    } 

    // Print the average number of required tries. 
    System.out.println("The average number of hands it took was " + totalTries/10.0); 
} 

} 
+8

デバッグ、デバッグおよび1より多くの時間をデバッグした:ロイヤルフラッシュのために、あなたの内側の論理がなければなりません! – Andremoniy

+0

私は行ごとに行っており、すべてが期待通りになります。私は決してそれが王室の潮流に達することはない。 – jillianjmb

+0

enumを使ってカードを表現し、 'List deck'を' Collection.shuffle(deck) 'とすることを考えてみましょう。これにより、コードが単純化され、デバッグが容易になります。 –

答えて

0

あなたは、次の場所で次の行

card1 = card2 = card3 = card4 = card5 = 0; 

を追加する必要があります。

while (flush == false) { 

    // To simulate the use of one deck, only one of each card can be 
    // dealt in a hand. 
    while (card1 == card2 || card1 == card3 || card1 == card4 || card1 == card5 
      || card2 == card3 || card2 == card4 || card2 == card5 || card3 == card4 
      || card3 == card5 || card4 == card5) { 

     // Cards 1-13 are the hearts, 14-26 the spades, 27-39 the 
     // clubs, and 40-52 the diamonds. 
     card1 = r.nextInt(52) + 1; 
     card2 = r.nextInt(52) + 1; 
     card3 = r.nextInt(52) + 1; 
     card4 = r.nextInt(52) + 1; 
     card5 = r.nextInt(52) + 1; 
    } 

    // Calculate the max and min of the cards. 
    int max = Math.max(card1, Math.max(card2, Math.max(card3, Math.max(card4, card5)))); 
    int min = Math.min(card1, Math.min(card2, Math.min(card3, Math.min(card4, card5)))); 

    // Check if the hand dealt is a royal straight flush by 
    // confirming that 
    // they span 5 consecutive numbers (this is true if the 
    // difference between 
    // the max and min is 4 since they are all different), and that 
    // they all come 
    // from the same suit (1-13, 14-26, 27-39, or 40-52). 

    if (max - min == 4) { 
     if ((1 <= min) && (13 >= max)) { 
      flush = true; 
     } else if ((14 <= min) && (26 >= max)) { 
      flush = true; 
     } else if ((27 <= min) && (39 >= max)) { 
      flush = true; 
     } else if ((40 <= min) && (52 >= max)) { 
      flush = true; 
     } else { 
      i++; 
     } 
    } else { 
     i++; 
    } 
    card1 = card2 = card3 = card4 = card5; 
} 

出力中:

かかった手の平均数は177968.4

ました

そして、あなたは現在、まっすぐなフラッシュのみをチェックします。ロイヤルフラッシュはチェックしません。

を出力

if (max - min == 4) { 
    if ((13 == max) || (26 == max) || (39 == max) || (52 == max)) { 
     flush = true; 
    } else { 
     i++; 
    } 
} else { 
    i++; 
} 

かかった手の平均数は4944953.0

+0

ありがとう!その行が1つ下の行であるときにforループでなぜ動作しないのか説明できますか? – jillianjmb

+0

@jillianjmbあなたは新しいカードをforループのすべての反復ではなく、whileループの反復を扱いたいので。正しくインデントされたコードを見て、再計算の値がどこでチェックされているかを考えてください。 – luk2302

+0

私は参照してください。私は当初、カードがフラッシュ=真であるときだけカードをリセットしなければならないと考えていたが、もちろん、それらがi ++が発生するたびにリセットされなければならない。ありがとうございました。 – jillianjmb

0

whileループを解除するには、flushの値を変更する必要があります。

+0

if文で私がしていることはなぜ機能しないのですか? – jillianjmb

+0

luk2302が彼の答えで言及したように、あなたのコードはカードを一度選んでいただけで、無限ループに入っていました。彼が書いたように何かを指定する必要があります。card1 = card2 = card3 = card4 = card5;新しいカードを毎回選択できるようにします。 – Maverick

1

外側whileループのcard1、card2、card3、card4、card5の値は変更していません。本質的に初めてのラウンドでは、(あなたが意図したように)すべての異なるものにそれらを設定しようとしていますが、その後の繰り返しでは、それらがすべて違うという条件を既に満たしているため、 !

+0

sooooありがとう!今度はなぜ他のプログラムも同じように働いているのが分かります。なぜなら、カードが同じかどうかをチェックしていなかったので、私はループしていませんでした! – jillianjmb

+0

ああ、いや、それはまだあなたが問題のD80が指摘修正しようとするために何をした:( – jillianjmb

+0

@jillianjmbを働いていないluk2302 @ – luk2302

-1

根本的な問題の1つは、乱数ジェネレータです。なぜあなたはいつもすべてのカードのすべての実行で同じ正確な値のために終わるのだろうか?それはRandomクラスが渡した最初のシードに基づいて「擬似乱数」であるからです。あなたのコードでは、実行ごとに1を渡すので、毎回同じ正確な値を持つようになります。よりランダムな値を取得するには、System.currentTimeMillis()を初期シードとして使用します。

+1

多分、彼は結果を再現できるように固定された種を持っています。とにかく、あなたの答えはOPが直面している問題を実際に解決することに近づかないでしょう。 – luk2302

関連する問題