Design the data structures for a generic deck of cards.
Explain how you would subclass it to implement particular card game.
2. Implementation
Q1: deck of card?
A1: 52 cards is composed of 4 type of card (Spades, Heart, Diamond, Clubs) and each type has 13 cards from letter A,2,3,4,5,6,7,8,9,10,J,Q,K(think it of number 1 to number 13)
A2:An 2D array such as card[4][13], first dimension is type and second dimension is letter
Q1: how to play those cards?
A1: shuffle, take one from deck for each player, ???
public class Card
{
public enum Suit
{
CLUBS (1), SPADES(2), HEARTS(3), DIAMONDS(4);
int value;
private Suit(int v){ value = v;}
}
private int card;
private Suit suit;
public Card(int r, Suit s)
{
card = r;
suit = s;
}
public int getValue() {return card;}
public Suit getSuit() {return suit;}
}
Public class BlackJackCard extends Card
{
public BlackJackCard(int r, Suit s) {super(r,s);}
public int value()
{
int r = super.value();
// card.value = A
if (r==1) return 11; // aces are 11
// card.vlaue = 2~9
if (r < 10) return r;
// card.value = 10,J,Q,K
return 10;
}
boolean isAce()
{
return super.value() ==1;
}
}
Public class Hand
{
// use BlackJackCard class to get a set of cards
}
3. Similar onesRock paper scissor
Tic tae toe
No comments:
Post a Comment