前提・実現したいこと
PH の #unfair と #classify を実装したいのですが、いい考えが浮かびません。ヒントを頂けないでしょうか。#unfair は、手札に同じ数字のカードがあったときに、 true を返します。#classify は、今の自分の手札が、どの役にあたるのかを分類します。
Card、CardObj、Joker、PokerHandは、前提です。
Card
Java
1enum Suit { 2 DIAMONDS, CLUBS, HEARTS, SPADES 3} 4 5enum Rank { 6 ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING 7} 8 9public interface Card { 10 public Rank getRank(); 11 public Suit getSuit(); 12 public boolean isJoker(); 13 // the following are not named in interfaces, typically, since they're assumed 14 // public boolean equals(Object o); 15 // public String toString(); 16} 17
CardObj
Java
1public class CardObj implements Card { 2 3 private Rank r; 4 private Suit s; 5 6 public CardObj(Rank r, Suit s) { 7 if (r == null || s == null) { 8 throw new IllegalArgumentException(); 9 } 10 this.r = r; 11 this.s = s; 12 } 13 14 public Rank getRank() { 15 return this.r; 16 } 17 18 public Suit getSuit() { 19 return this.s; 20 } 21 22 public boolean isJoker() { 23 return false; 24 } 25 26 public boolean equals(Object o) { 27 if (!(o instanceof Card)) 28 return false; 29 30 Card that = (Card)o; 31 32 return r.equals(that.getRank()) && s.equals(that.getSuit()); 33 } 34 35 public String toString() { 36 return r + "of" + s; 37 } 38 39} 40
Joker
Java
1public class Joker implements Card { 2 3 public Rank getRank() { 4 throw new UnsupportedOperationException("joker"); 5 } 6 7 public Suit getSuit() { 8 throw new UnsupportedOperationException("joker"); 9 } 10 11 public boolean isJoker() { 12 return true; 13 } 14 15 public boolean equals(Object o) { 16 return ((o instanceof Card) && ((Card)o).isJoker()); 17 } 18 19 public String toString() { 20 return "joker"; 21 } 22 23} 24
PokerHand
Java
1enum HandType { 2 NOTHING, PAIR, TWO_PAIR, THREE_OF_A_KIND, STRAIGHT, FLUSH, 3 FULL_HOUSE, FOUR_OF_A_KIND, STRAIGHT_FLUSH, ROYAL_FLUSH 4} 5 6public interface PokerHand { 7 public Card[] getCards(); 8 public boolean unfair(); // return true if hand contains duplicates 9 public HandType classify(); // classify the hand as its *best* hand type 10} 11
PH
Java
1public class PH implements PokerHand { 2 3 private Card[] cards; 4 5 public PH(Card[] cards) { 6 if (cards == null) 7 throw new IllegalArgumentException(); 8 9 for (Card c: cards) 10 if (c == null) 11 throw new IllegalArgumentException(); 12 13 if (cards.length != 5) 14 throw new IllegalArgumentException(); 15 16 this.cards = cards; 17 } 18 19 public Card[] getCards() { 20 return this.cards; 21 } 22 23 public boolean unfair() { 24 } 25 26 public HandType classify() { 27 } 28 29} 30
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/24 01:50