Card game services.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3133 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2004-10-13 02:03:26 +00:00
parent af6409e742
commit 82cf7b0a47
15 changed files with 1534 additions and 0 deletions
@@ -0,0 +1,258 @@
//
// $Id: Card.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.data;
import java.io.IOException;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.presents.dobj.DSet;
/**
* Instances of this class represent individual playing cards.
*/
public class Card implements CardCodes,
DSet.Entry
{
/**
* No-arg constructor for deserialization.
*/
public Card()
{}
/**
* Returns the value of the card, either from 2 to 11 or
* KING, QUEEN, JACK, ACE, or JOKER.
*
* @return the value of the card
*/
public int getNumber()
{
return number;
}
/**
* Returns the suit of the card: HEARTS, DIAMONDS, CLUBS, or
* SPADES. If the card is the joker, the suit is invalid (-1).
*
* @return the suit of the card
*/
public int getSuit()
{
return suit;
}
/**
* Checks whether the card is a number card (2 to 10).
*
* @return true if the card is a number card, false otherwise
*/
public boolean isNumber()
{
return number <= 11;
}
/**
* Checks whether the card is a face card (KING, QUEEN, or JACK).
*
* @return true if the card is a face card, false otherwise
*/
public boolean isFace()
{
return number == KING || number == QUEEN || number == JACK;
}
/**
* Checks whether the card is an ace.
*
* @return true if the card is an ace, false otherwise
*/
public boolean isAce()
{
return number == ACE;
}
/**
* Checks whether the card is a joker.
*
* @return true if the card is a joker, false otherwise
*/
public boolean isJoker()
{
return number == JOKER;
}
/**
* Writes this object to the specified output stream.
*
* @param oos the output stream
*/
public void writeObject(ObjectOutputStream oos)
throws IOException
{
oos.defaultWriteObject();
oos.writeInt(number);
oos.writeInt(suit);
}
/**
* Reads this object from the specified input stream.
*
* @param ois the input stream
*/
public void readObject(ObjectInputStream ois)
throws IOException,
ClassNotFoundException
{
ois.defaultReadObject();
number = ois.readInt();
suit = ois.readInt();
key = new Integer((number << 2) | suit);
}
// documentation inherited
public Comparable getKey()
{
return key;
}
/**
* Returns a hash code for this card.
*
* @return this card's hash code
*/
public int hashCode()
{
return key.intValue();
}
/**
* Checks this card for equality with another.
*
* @param other the other card to compare
* @return true if the cards are equal, false otherwise
*/
public boolean equals(Object other)
{
if(other instanceof Card)
{
return number == ((Card)other).number &&
suit == ((Card)other).suit;
}
else
{
return false;
}
}
/**
* Returns a string representation of a given number.
*
* @param number the number to represent
* @return the string representation
*/
public static String numberToString(int number)
{
switch(number)
{
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
case 10: return "Ten";
case JACK: return "Jack";
case QUEEN: return "Queen";
case KING: return "King";
case ACE: return "Ace";
case JOKER: return "Joker";
default: return "???";
}
}
/**
* Returns a string representation of a particular suit.
*
* @param suit the suit to represent
* @return the string representation
*/
public static String suitToString(int suit)
{
switch(suit)
{
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
case SPADES: return "Spades";
default: return "???";
}
}
/**
* Returns a string representation of this card.
*
* @return a description of this card
*/
public String toString()
{
if(number == JOKER)
{
return "Joker";
}
else
{
return numberToString(number) + " of " + suitToString(suit);
}
}
/**
* Package-only constructor for Deck.
*
* @param number the number of the card
* @param suit the suit of the card
*/
Card(int number, int suit)
{
this.number = number;
this.suit = suit;
key = new Integer((number << 2) | suit);
}
/** The number of the card. */
private int number;
/** The suit of the card. */
private int suit;
/** The comparison key. */
private Integer key;
}
@@ -0,0 +1,60 @@
//
// $Id: CardCodes.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.data;
import com.threerings.presents.data.InvocationCodes;
/**
* Constants relating to the card services.
*/
public interface CardCodes extends InvocationCodes
{
/** The suit of hearts. */
public static final int HEARTS = 0;
/** The suit of diamonds. */
public static final int DIAMONDS = 1;
/** The suit of clubs. */
public static final int CLUBS = 2;
/** The suit of spades. */
public static final int SPADES = 3;
/** The number of the jack. */
public static final int JACK = 11;
/** The number of the queen. */
public static final int QUEEN = 12;
/** The number of the king. */
public static final int KING = 13;
/** The number of the ace. */
public static final int ACE = 14;
/** The number of the joker. */
public static final int JOKER = 15;
/** A message that carries a Hand of cards to a player. */
public static final String TAKE_HAND = "take_hand";
}
@@ -0,0 +1,145 @@
//
// $Id: Deck.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.data;
import java.util.Collections;
import com.threerings.io.Streamable;
import com.threerings.util.StreamableArrayList;
/**
* Instances of this class represent decks of cards.
*/
public class Deck implements CardCodes,
Streamable
{
/** The cards in the deck. */
public StreamableArrayList cards;
/**
* Default constructor creates an unshuffled deck of cards without
* jokers.
*/
public Deck()
{
cards = new StreamableArrayList();
reset(false);
}
/**
* Constructor.
*
* @param includeJokers whether or not to include the two jokers
* in the deck
*/
public Deck(boolean includeJokers)
{
cards = new StreamableArrayList();
reset(includeJokers);
}
/**
* Resets the deck to its initial state: an unshuffled deck of
* 52 or 54 cards, depending on whether the jokers are included.
*
* @param includeJokers whether or not to include the two jokers
* in the deck
*/
public void reset(boolean includeJokers)
{
cards.clear();
for(int i=HEARTS;i<=SPADES;i++)
{
for(int j=2;j<=ACE;j++)
{
cards.add(new Card(j, i));
}
}
if(includeJokers)
{
cards.add(new Card(JOKER, -1));
cards.add(new Card(JOKER, -1));
}
}
/**
* Shuffles the deck.
*/
public void shuffle()
{
Collections.shuffle(cards);
}
/**
* Deals a hand of cards from the deck.
*
* @param size the size of the hand to deal
* @return the newly created and populated hand, or null
* if there are not enough cards in the deck to deal the hand
*/
public Hand dealHand(int size)
{
if(cards.size() < size)
{
return null;
}
else
{
Hand hand = new Hand();
for(int i=0;i<size;i++)
{
hand.cards.add(cards.get(cards.size()-1));
cards.remove(cards.size()-1);
}
return hand;
}
}
/**
* Returns a hand of cards to the deck.
*
* @param hand the hand of cards to return
*/
public void returnHand(Hand hand)
{
cards.addAll(hand.cards);
hand.cards.clear();
}
/**
* Returns a string representation of this deck.
*
* @return a description of this deck
*/
public String toString()
{
return "[cards=" + cards.toString() + "]";
}
}
@@ -0,0 +1,80 @@
//
// $Id: Hand.java,v 1.1 2004/10/13 02:03:26 andrzej Exp $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.parlor.card.data;
import java.util.*;
import com.threerings.io.Streamable;
import com.threerings.util.StreamableArrayList;
/**
* Instances of this class represent hands of cards.
*/
public class Hand implements CardCodes,
Streamable
{
/** The cards in the hand. */
public StreamableArrayList cards;
/**
* Default constructor creates an empty hand.
*/
public Hand()
{
cards = new StreamableArrayList();
}
/**
* Counts the members of a particular suit within this hand.
*
* @param suit the suit of interest
* @return the number of cards in the specified suit
*/
public int getSuitMemberCount(int suit)
{
int members = 0;
Iterator it = cards.iterator();
while(it.hasNext())
{
if(((Card)it.next()).getSuit() == suit)
{
members++;
}
}
return members;
}
/**
* Returns a string representation of this hand.
*
* @return a description of this hand
*/
public String toString()
{
return "[cards=" + cards.toString() + "]";
}
}