本当に助けが必要な、やらなければならない別のエクササイズがあります。私の isFlush メソッドが機能しているかどうかさえわかりません。なぜかというと、私のデッキがシャッフルされておらず、ハンドが処理されておらず、完全に行き詰っています。誰かが私を助けたり、正しい方向に向けたりできますか? 演習は次のとおりです。
演習 12.5 この演習の目標は、ランダムなポーカー ハンドを生成して分類するプログラムを作成し、さまざまなポーカー ハンドの確率を推定できるようにすることです。ポーカーをプレイしなくても心配はいりません。私はあなたが知る必要があるすべてを教えます。
を。ウォーミングアップとして、shuffleDeck と subdeck を使用して、それぞれ 5 枚のカードを持つ 4 つのランダムなポーカー ハンドを生成して出力するプログラムを作成します。いいことありましたか?可能性のあるポーカー ハンドは、値の昇順で次のとおりです。 ペア: 同じランクの 2 枚のカード 2 ペア: 同じランクの 2 枚のカードのペア スリー オブ ア カインド: 同じランクの 3 枚のカード ストレート: ランクが同じ 5 枚のカードシーケンス フラッシュ: 同じスーツの 5 枚のカード フルハウス: 1 つのランクの 3 枚のカード、別の 4 枚のカードの 2 枚: 同じランクの 4 枚のカード ストレート フラッシュ: 5 枚のカードが連続して同じスーツ
b. Deck をパラメーターとして取り、ハンドにフラッシュが含まれているかどうかを示すブール値を返す isFlush というメソッドを作成します。
c. ハンドを取り、そのハンドにスリーカードが含まれているかどうかを示すブール値を返す isThreeKind というメソッドを作成します。
d. 数千のハンドを生成し、それらにフラッシュまたはスリーカードが含まれているかどうかをチェックするループを作成します。それらのハンドの 1 つを取得する確率を推定します。
e. 他のポーカー ハンドをテストするメソッドを記述します。いくつかは他よりも簡単です。複数のテストに使用できる汎用ヘルパー メソッドを作成すると便利な場合があります。
f. 一部のポーカー ゲームでは、プレイヤーはそれぞれ 7 枚のカードを受け取り、7 枚のカードのうち上位 5 枚でハンドを形成します。セブンカード ハンドを生成し、確率を再計算するようにプログラムを変更します。
そして、ここに私のコードがあります:
public class Poker {
public static void main(String[] args) {
Deck deck = new Deck ();
Deck.dealDeck (deck);
}
}
class Card {
int suit, rank;
int index = 0;
//Card[] deck = new Card [52];
public Card () {
this.suit = 0; this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit; this.rank = rank;
}
public static void printCard (Card c) {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
}
}
class Deck {
Card[] cards;
public Deck (int n) {
cards = new Card[n];
}
public Deck () {
cards = new Card[52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
cards[index] = new Card (suit, rank);
index++;
}
}
}
public static Deck dealDeck(Deck deck){
shuffle(deck);
Deck hand1 = subDeck(deck, 0, 4);
Deck hand2 = subDeck(deck, 5, 9);
Deck hand3 = subDeck(deck, 10, 14);
Deck hand4 = subDeck(deck, 15, 19);
Deck pack = subDeck(deck, 20, 51);
return deck;
}
public static Deck shuffle(Deck deck){
for (int i = 0; i < 52; i++){
int rand = (int)(Math.random()*(i + 1));
**Deck[] temp = deck[i];
deck[i] = deck[rand];
deck[rand] = deck[temp];**
}
return deck;
}
public static Deck subDeck(Deck deck, int low, int high) {
Deck sub = new Deck (high-low+1);
for (int i = 0; i < sub.cards.length; i++) {
sub.cards[i] = deck.cards[low+i];
}
return sub;
}
public static void printDeck (Deck hand) {
for (int i = 0; i < hand.cards.length; i++) {
Card.printCard (hand.cards[i]);
}
}
public static boolean isFlush(Card[] x, int y) {
int count = 0;
for(int index = 0; index < y; index++){
boolean comp = compareIfFlush(x[index], x[index + 1]);
if (comp = true){
count++;
}
}
if (count >= 5){
System.out.println("Congratulations! You have a flush!");
return true;
}
else{
System.out.println("Sorry, you do not have a flush.");
return false;
}
}
public static boolean compareIfFlush(Card c1, Card c2){
if (c1.suit != c2.suit){
return false;
}
return true;
}
}
困っている部分を太字にしました。「配列が必要ですが、exercise125poker.Deck が見つかりました」というエラーが表示されます。このエラーを修正する方法がわからないため、このエラーの回避策が必要です。誰でも助けることができますか?