ผลต่างระหว่างรุ่นของ "01219343/unit/poker"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 13 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 1: แถว 1:
 +
: ''This exercise is part of [[01219343-55]].''
 +
 +
Follow the TDD steps to write these classes.
 +
 
== Card ==
 
== Card ==
  
Implements:
+
A card has a rank (A,1,...,9,10,J,Q,K) and a suit (S,H,D,C) (see [https://en.wikipedia.org/wiki/Suit_%28cards%29 suit]). 
  
* hasSameRank
+
Implement class <code>Card</code> that supports the following methods:
* hasSameSuit
+
 
* compareTo
+
* Construction with rank and suit as strings. (This might not be an efficient way to represent ranks and suits, but it simplifies the exercise.)  For example:
 +
 
 +
Card card = new Card("10","S");
 +
 
 +
* Getters: <code>getRank</code> and <code>getSuit</code>.
 +
* <code>hasSameRank(Card c)</code> that returns true when <code>c</code> has the same rank.
 +
* <code>hasSameSuit(Card c)</code> that returns true when <code>c</code> has the same suit.
 +
* The class should implement interface <code>Comparable&lt;Card&gt;</code> so that we can easily sort the cards.  Add that interface to the class and implement a public method <code>int compareTo(Card c)</code>.
 +
 
 +
Make sure you test these method thoroughly.  When fixing a broken tast, write the simplest possible code that make the test passes.
 +
 
 +
Given a list of <code>Card</code>s, we want to use <code>Collections.sort</code> to sort the list.  Make sure you have a test case that checks this requirement.
 +
 
 +
'''Notes:''' To create a list of Cards, you can use method <code>Arrays.asList</code>, e.g.,
 +
 
 +
List<Card> cards = Arrays.asList(new Card("10","S"), new Card("9", "D"), new Card("A","C"));
 +
 
 +
=== @Before ===
 +
 
 +
You may see yourself creating the same objects many times inside various test methods.  You can put all these initialization into one method by annotating the method with <code>@Before</code>.
 +
 
 +
public class CardTest {
 +
 +
Card S10, SA;
 +
 +
@Before
 +
public void initCards() {
 +
S10 = new Card("10","S");
 +
SA = new Card("A","S");
 +
}
 +
}
  
 
== Hand ==
 
== Hand ==
  
Use [https://en.wikipedia.org/wiki/List_of_poker_hands|this reference of poker hands] from wikipedia.
+
Use [https://en.wikipedia.org/wiki/List_of_poker_hands this reference of poker hands] from wikipedia.
 +
 
 +
Write class <code>PokerHand</code> that provides the following interface.
 +
 
 +
* Construction: you can construct a <code>PokerHand</code> by a list of 5 <code>Card</code>s.  If the number of cards in the list is not 5, the constructor should raise an exception.
 +
 
 +
* <code>getHighestCard</code>
 +
 
 +
* <code>isStraightFlush</code>
 +
* <code>isFourOfAKind</code>
 +
* <code>isFullHouse</code>
 +
* <code>isFlush</code>
 +
* <code>isStraight</code>
 +
* <code>isThreeOfAKind</code>
 +
* <code>isTwoPair</code>
 +
* <code>isOnePair</code>
 +
* <code>isNoPair</code>
 +
 
 +
Note that while a straight flush hand contains cards with the same suit, it is not considered a flush.  Any poker hand can belong to only one category.

รุ่นแก้ไขปัจจุบันเมื่อ 16:57, 24 มกราคม 2556

This exercise is part of 01219343-55.

Follow the TDD steps to write these classes.

Card

A card has a rank (A,1,...,9,10,J,Q,K) and a suit (S,H,D,C) (see suit).

Implement class Card that supports the following methods:

  • Construction with rank and suit as strings. (This might not be an efficient way to represent ranks and suits, but it simplifies the exercise.) For example:
Card card = new Card("10","S");
  • Getters: getRank and getSuit.
  • hasSameRank(Card c) that returns true when c has the same rank.
  • hasSameSuit(Card c) that returns true when c has the same suit.
  • The class should implement interface Comparable<Card> so that we can easily sort the cards. Add that interface to the class and implement a public method int compareTo(Card c).

Make sure you test these method thoroughly. When fixing a broken tast, write the simplest possible code that make the test passes.

Given a list of Cards, we want to use Collections.sort to sort the list. Make sure you have a test case that checks this requirement.

Notes: To create a list of Cards, you can use method Arrays.asList, e.g.,

List<Card> cards = Arrays.asList(new Card("10","S"), new Card("9", "D"), new Card("A","C"));

@Before

You may see yourself creating the same objects many times inside various test methods. You can put all these initialization into one method by annotating the method with @Before.

public class CardTest {

	Card S10, SA;
	
	@Before
	public void initCards() {
		S10 = new Card("10","S");
		SA = new Card("A","S");
	}
}

Hand

Use this reference of poker hands from wikipedia.

Write class PokerHand that provides the following interface.

  • Construction: you can construct a PokerHand by a list of 5 Cards. If the number of cards in the list is not 5, the constructor should raise an exception.
  • getHighestCard
  • isStraightFlush
  • isFourOfAKind
  • isFullHouse
  • isFlush
  • isStraight
  • isThreeOfAKind
  • isTwoPair
  • isOnePair
  • isNoPair

Note that while a straight flush hand contains cards with the same suit, it is not considered a flush. Any poker hand can belong to only one category.