Reordered suits for aesthetic purposes, made cards Comparable for sorting.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3194 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2004-11-01 22:10:21 +00:00
parent 09d41faa37
commit c4eb1457dd
3 changed files with 47 additions and 27 deletions
@@ -1,5 +1,5 @@
//
// $Id: Card.java,v 1.6 2004/10/22 23:19:07 andrzej Exp $
// $Id: Card.java,v 1.7 2004/11/01 22:10:21 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -31,10 +31,8 @@ import com.threerings.presents.dobj.DSet;
/**
* Instances of this class represent individual playing cards.
*/
public class Card implements CardCodes,
DSet.Entry
public class Card implements DSet.Entry, Comparable, CardCodes
{
/**
* No-arg constructor for deserialization.
*/
@@ -49,18 +47,18 @@ public class Card implements CardCodes,
*/
public int getNumber ()
{
return (_value >> 2);
return (_value & 0x1F);
}
/**
* Returns the suit of the card: HEARTS, DIAMONDS, CLUBS, or
* SPADES. If the card is the joker, the suit is undefined.
* Returns the suit of the card: SPADES, HEARTS, DIAMONDS, or
* CLUBS. If the card is the joker, the suit is undefined.
*
* @return the suit of the card
*/
public int getSuit ()
{
return (_value & 0x03);
return (_value >> 5);
}
/**
@@ -121,7 +119,7 @@ public class Card implements CardCodes,
return number == RED_JOKER || number == BLACK_JOKER ||
(number >= 2 && number <= ACE &&
suit >= HEARTS && suit <= SPADES);
suit >= SPADES && suit <= DIAMONDS);
}
// Documentation inherited.
@@ -160,6 +158,29 @@ public class Card implements CardCodes,
}
}
/**
* Compares this card to another. The card order is the same as the
* initial deck ordering: two through ten, jack, queen, king, ace for
* spades, hearts, clubs, and diamonds, then the red joker and the
* black joker.
*
* @param other the other card to compare this to
* @return -1, 0, or +1, depending on whether this card is less than,
* equal to, or greater than the other card
*/
public int compareTo (Object other)
{
int otherValue = ((Card)other)._value;
if (_value > otherValue) {
return +1;
} else if(_value < otherValue) {
return -1;
} else {
return 0;
}
}
/**
* Returns a string representation of this card.
*
@@ -193,10 +214,10 @@ public class Card implements CardCodes,
}
switch (getSuit()) {
case HEARTS: sb.append('h'); break;
case DIAMONDS: sb.append('d'); break;
case CLUBS: sb.append('c'); break;
case SPADES: sb.append('s'); break;
case HEARTS: sb.append('h'); break;
case CLUBS: sb.append('c'); break;
case DIAMONDS: sb.append('d'); break;
default: sb.append('?'); break;
}
@@ -205,17 +226,16 @@ public class Card implements CardCodes,
}
/**
* Package-only constructor for Deck.
* Protected constructor for Deck.
*
* @param number the number of the card
* @param suit the suit of the card
*/
Card (int number, int suit)
protected Card (int number, int suit)
{
_value = (byte)((number << 2) | suit);
_value = (byte)((suit << 5) | number);
}
/** The number of the card. */
protected byte _value;
@@ -1,5 +1,5 @@
//
// $Id: CardCodes.java,v 1.2 2004/10/22 23:19:07 andrzej Exp $
// $Id: CardCodes.java,v 1.3 2004/11/01 22:10:21 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -28,17 +28,17 @@ import com.threerings.presents.data.InvocationCodes;
*/
public interface CardCodes extends InvocationCodes
{
/** The suit of hearts. */
public static final int HEARTS = 0;
/** The suit of spades. */
public static final int SPADES = 0;
/** The suit of diamonds. */
public static final int DIAMONDS = 1;
/** The suit of hearts. */
public static final int HEARTS = 1;
/** The suit of clubs. */
public static final int CLUBS = 2;
/** The suit of spades. */
public static final int SPADES = 3;
/** The suit of diamonds. */
public static final int DIAMONDS = 3;
/** The number of the jack. */
public static final int JACK = 11;
@@ -1,5 +1,5 @@
//
// $Id: Deck.java,v 1.5 2004/10/22 23:19:07 andrzej Exp $
// $Id: Deck.java,v 1.6 2004/11/01 22:10:21 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -72,15 +72,15 @@ public class Deck implements CardCodes,
{
cards.clear();
for (int i=HEARTS;i<=SPADES;i++) {
for (int i=SPADES;i<=DIAMONDS;i++) {
for (int j=2;j<=ACE;j++) {
cards.add(new Card(j, i));
}
}
if (includeJokers) {
cards.add(new Card(RED_JOKER, 0));
cards.add(new Card(BLACK_JOKER, 0));
cards.add(new Card(RED_JOKER, 3));
cards.add(new Card(BLACK_JOKER, 3));
}
}