From 82cf7b0a4764359d8427c056c3545cf3521b9f55 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 13 Oct 2004 02:03:26 +0000 Subject: [PATCH] Card game services. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3133 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/parlor/card/Log.java | 56 ++++ .../card/client/CardGameController.java | 70 +++++ .../parlor/card/client/CardPanel.java | 213 +++++++++++++++ .../parlor/card/client/CardSprite.java | 150 ++++++++++ .../card/client/CardSpriteObserver.java | 47 ++++ .../com/threerings/parlor/card/data/Card.java | 258 ++++++++++++++++++ .../parlor/card/data/CardCodes.java | 60 ++++ .../com/threerings/parlor/card/data/Deck.java | 145 ++++++++++ .../com/threerings/parlor/card/data/Hand.java | 80 ++++++ .../parlor/card/server/CardGameManager.java | 75 +++++ .../card/trick/TrickCardGameController.java | 47 ++++ .../TrickCardGameControllerDelegate.java | 66 +++++ .../card/trick/TrickCardGameManager.java | 61 +++++ .../trick/TrickCardGameManagerDelegate.java | 132 +++++++++ .../card/trick/TrickCardGameObject.java | 74 +++++ 15 files changed, 1534 insertions(+) create mode 100644 src/java/com/threerings/parlor/card/Log.java create mode 100644 src/java/com/threerings/parlor/card/client/CardGameController.java create mode 100644 src/java/com/threerings/parlor/card/client/CardPanel.java create mode 100644 src/java/com/threerings/parlor/card/client/CardSprite.java create mode 100644 src/java/com/threerings/parlor/card/client/CardSpriteObserver.java create mode 100644 src/java/com/threerings/parlor/card/data/Card.java create mode 100644 src/java/com/threerings/parlor/card/data/CardCodes.java create mode 100644 src/java/com/threerings/parlor/card/data/Deck.java create mode 100644 src/java/com/threerings/parlor/card/data/Hand.java create mode 100644 src/java/com/threerings/parlor/card/server/CardGameManager.java create mode 100644 src/java/com/threerings/parlor/card/trick/TrickCardGameController.java create mode 100644 src/java/com/threerings/parlor/card/trick/TrickCardGameControllerDelegate.java create mode 100644 src/java/com/threerings/parlor/card/trick/TrickCardGameManager.java create mode 100644 src/java/com/threerings/parlor/card/trick/TrickCardGameManagerDelegate.java create mode 100644 src/java/com/threerings/parlor/card/trick/TrickCardGameObject.java diff --git a/src/java/com/threerings/parlor/card/Log.java b/src/java/com/threerings/parlor/card/Log.java new file mode 100644 index 000000000..ebf203917 --- /dev/null +++ b/src/java/com/threerings/parlor/card/Log.java @@ -0,0 +1,56 @@ +// +// $Id: Log.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; + +/** + * A placeholder class that contains a reference to the log object used by + * the Card services. + */ +public class Log +{ + public static com.samskivert.util.Log log = + new com.samskivert.util.Log("card"); + + /** Convenience function. */ + public static void debug (String message) + { + log.debug(message); + } + + /** Convenience function. */ + public static void info (String message) + { + log.info(message); + } + + /** Convenience function. */ + public static void warning (String message) + { + log.warning(message); + } + + /** Convenience function. */ + public static void logStackTrace (Throwable t) + { + log.logStackTrace(com.samskivert.util.Log.WARNING, t); + } +} diff --git a/src/java/com/threerings/parlor/card/client/CardGameController.java b/src/java/com/threerings/parlor/card/client/CardGameController.java new file mode 100644 index 000000000..0e8e26a44 --- /dev/null +++ b/src/java/com/threerings/parlor/card/client/CardGameController.java @@ -0,0 +1,70 @@ +// +// $Id: CardGameController.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.client; + +import com.threerings.crowd.data.PlaceConfig; + +import com.threerings.crowd.util.CrowdContext; + +import com.threerings.parlor.card.Log; + +import com.threerings.parlor.card.data.CardCodes; +import com.threerings.parlor.card.data.Hand; + +import com.threerings.parlor.game.GameController; + +import com.threerings.presents.dobj.MessageEvent; +import com.threerings.presents.dobj.MessageListener; + +/** + * A controller class for card games. Handles common functions like + * accepting dealt hands. + */ +public abstract class CardGameController extends GameController + implements MessageListener, + CardCodes +{ + // Documentation inhertied + public void init(CrowdContext context, PlaceConfig config) + { + super.init(context, config); + + _ctx.getClient().getClientObject().addListener(this); + } + + // Documentation inherited + public void messageReceived(MessageEvent event) + { + if(event.getName().equals(TAKE_HAND)) + { + handDealt((Hand)event.getArgs()[0]); + } + } + + /** + * Called when the server deals the client a new hand of cards. + * + * @param hand the hand dealt to the user + */ + protected void handDealt(Hand hand) + {} +} diff --git a/src/java/com/threerings/parlor/card/client/CardPanel.java b/src/java/com/threerings/parlor/card/client/CardPanel.java new file mode 100644 index 000000000..52d967432 --- /dev/null +++ b/src/java/com/threerings/parlor/card/client/CardPanel.java @@ -0,0 +1,213 @@ +// +// $Id: CardPanel.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.client; + +import java.awt.*; +import java.awt.event.*; + +import java.util.*; + +import com.samskivert.util.ObserverList; + +import com.threerings.media.FrameManager; +import com.threerings.media.VirtualMediaPanel; + +import com.threerings.media.image.Mirage; + +import com.threerings.media.sprite.Sprite; + +import com.threerings.parlor.card.Log; + +import com.threerings.parlor.card.data.Card; +import com.threerings.parlor.card.data.CardCodes; +import com.threerings.parlor.card.data.Deck; +import com.threerings.parlor.card.data.Hand; + +/** + * Extends VirtualMediaPanel to provide services specific to rendering + * and manipulating playing cards. + */ +public abstract class CardPanel extends VirtualMediaPanel + implements CardCodes +{ + /** Calls CardSpriteObserver.cardSpriteClicked. */ + protected static class CardSpriteClickedOp implements ObserverList.ObserverOp + { + protected CardSprite sprite; + protected MouseEvent me; + + public CardSpriteClickedOp(CardSprite sprite, MouseEvent me) + { + this.sprite = sprite; + this.me = me; + } + + public boolean apply(Object observer) + { + ((CardSpriteObserver)observer).cardSpriteClicked(sprite, me); + return true; + } + } + + /** Calls CardSpriteObserver.cardSpriteDragged. */ + protected static class CardSpriteDraggedOp implements ObserverList.ObserverOp + { + protected CardSprite sprite; + protected MouseEvent me; + + public CardSpriteDraggedOp(CardSprite sprite, MouseEvent me) + { + this.sprite = sprite; + this.me = me; + } + + public boolean apply(Object observer) + { + ((CardSpriteObserver)observer).cardSpriteDragged(sprite, me); + return true; + } + } + + + /** + * Constructor. + * + * @param frameManager the frame manager + */ + public CardPanel(FrameManager frameManager) + { + super(frameManager); + + addMouseListener( + new MouseAdapter() + { + public void mousePressed(MouseEvent me) + { + ArrayList al = new ArrayList(); + + _spritemgr.getHitSprites(al, me.getX(), me.getY()); + + if(al.size() > 0) + { + Iterator it = al.iterator(); + int highestLayer = Integer.MIN_VALUE; + CardSprite highestSprite = null; + + while(it.hasNext()) + { + Sprite sprite = (Sprite)it.next(); + + if(sprite instanceof CardSprite) + { + CardSprite cs = (CardSprite)sprite; + + if(cs.getRenderOrder() > highestLayer) + { + highestLayer = cs.getRenderOrder(); + highestSprite = cs; + } + } + } + + activeCardSprite = highestSprite; + + if(activeCardSprite != null) + { + handleX = activeCardSprite.getX() - me.getX(); + handleY = activeCardSprite.getY() - me.getY(); + + hasBeenDragged = false; + } + } + else + { + activeCardSprite = null; + } + } + + public void mouseReleased(MouseEvent me) + { + if(activeCardSprite != null && hasBeenDragged) + { + activeCardSprite.queueNotification( + new CardSpriteDraggedOp(activeCardSprite, me) + ); + } + } + + public void mouseClicked(MouseEvent me) + { + if(activeCardSprite != null) + { + activeCardSprite.queueNotification( + new CardSpriteClickedOp(activeCardSprite, me) + ); + } + } + } + ); + + addMouseMotionListener( + new MouseMotionAdapter() + { + public void mouseDragged(MouseEvent me) + { + if(activeCardSprite != null && + activeCardSprite.isDraggable()) + { + activeCardSprite.setLocation( + me.getX() + handleX, + me.getY() + handleY + ); + + hasBeenDragged = true; + } + } + } + ); + } + + /** + * Returns the image for the back of a playing card. + * + * @return the card back image + */ + public abstract Mirage getCardBackImage(); + + /** + * Returns the image for the front of the specified card. + * + * @param card the desired card + * @return the card front image + */ + public abstract Mirage getCardImage(Card card); + + + /** The last card sprite pressed. */ + private CardSprite activeCardSprite; + + /** The location of the cursor in the active sprite. */ + private int handleX, handleY; + + /** Whether or not the active sprite has been dragged. */ + private boolean hasBeenDragged; +} diff --git a/src/java/com/threerings/parlor/card/client/CardSprite.java b/src/java/com/threerings/parlor/card/client/CardSprite.java new file mode 100644 index 000000000..9a14475a9 --- /dev/null +++ b/src/java/com/threerings/parlor/card/client/CardSprite.java @@ -0,0 +1,150 @@ +// +// $Id: CardSprite.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.client; + +import com.threerings.media.image.Mirage; + +import com.threerings.media.sprite.OrientableImageSprite; + +import com.threerings.parlor.card.data.Card; + +/** + * A sprite representing a playing card. + */ +public class CardSprite extends OrientableImageSprite +{ + /** The panel responsible for the sprite. */ + protected CardPanel _panel; + + /** The depicted card. */ + protected Card _card; + + /** Whether or not the card is facing up. */ + protected boolean _facingUp; + + /** Whether or not the user can drag the card around the board. */ + protected boolean _draggable; + + + /** + * Creates a new upward-facing card sprite. + * + * @param panel the panel responsible for the sprite + * @param card the card to depict + */ + public CardSprite(CardPanel panel, Card card) + { + _panel = panel; + _card = card; + _facingUp = true; + + updateMirage(); + } + + /** + * Creates a new card sprite. + * + * @param panel the panel responsible for the sprite + * @param card the card to depict + * @param facingUp whether or not the card should be facing up + */ + public CardSprite(CardPanel panel, Card card, boolean facingUp) + { + _panel = panel; + _card = card; + _facingUp = facingUp; + + updateMirage(); + } + + /** + * Sets the card to depict. + * + * @param card the new card + */ + public void setCard(Card card) + { + _card = card; + + updateMirage(); + } + + /** + * Returns the card being depicted. + * + * @return the current card + */ + public Card getCard() + { + return _card; + } + + /** + * Turns this card up or down. + * + * @param facingUp whether or not the card should be facing up + */ + public void setFacingUp(boolean facingUp) + { + _facingUp = facingUp; + + updateMirage(); + } + + /** + * Checks whether this card is facing up or down. + * + * @return true if the card is facing up, false if facing down + */ + public boolean isFacingUp() + { + return _facingUp; + } + + /** + * Sets whether or not the user can drag this card around the board. + * + * @param draggable whether or not the user can drag the card + */ + public void setDraggable(boolean draggable) + { + _draggable = draggable; + } + + /** + * Checks whether or not the user can drag this card. + * + * @return true if the user can drag the card, false if not + */ + public boolean isDraggable() + { + return _draggable; + } + + /** + * Updates the mirage according to the current state. + */ + private void updateMirage() + { + setMirage(_facingUp ? _panel.getCardImage(_card) : _panel.getCardBackImage()); + } +} diff --git a/src/java/com/threerings/parlor/card/client/CardSpriteObserver.java b/src/java/com/threerings/parlor/card/client/CardSpriteObserver.java new file mode 100644 index 000000000..96c113346 --- /dev/null +++ b/src/java/com/threerings/parlor/card/client/CardSpriteObserver.java @@ -0,0 +1,47 @@ +// +// $Id: CardSpriteObserver.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.client; + +import java.awt.event.*; + +/** + * Observer interface for (draggable) card sprites. + */ +public interface CardSpriteObserver +{ + /** + * Notifies the observer that the user clicked a card sprite. + * + * @param sprite the dragged sprite + * @param me the mouse event associated with the drag + */ + public void cardSpriteClicked(CardSprite sprite, MouseEvent me); + + /** + * Notifies the observer that the user dragged a card sprite to a new + * location. + * + * @param sprite the dragged sprite + * @param me the mouse event associated with the drag + */ + public void cardSpriteDragged(CardSprite sprite, MouseEvent me); +} diff --git a/src/java/com/threerings/parlor/card/data/Card.java b/src/java/com/threerings/parlor/card/data/Card.java new file mode 100644 index 000000000..6038a124a --- /dev/null +++ b/src/java/com/threerings/parlor/card/data/Card.java @@ -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; +} diff --git a/src/java/com/threerings/parlor/card/data/CardCodes.java b/src/java/com/threerings/parlor/card/data/CardCodes.java new file mode 100644 index 000000000..829941a8d --- /dev/null +++ b/src/java/com/threerings/parlor/card/data/CardCodes.java @@ -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"; +} diff --git a/src/java/com/threerings/parlor/card/data/Deck.java b/src/java/com/threerings/parlor/card/data/Deck.java new file mode 100644 index 000000000..ced89fa1e --- /dev/null +++ b/src/java/com/threerings/parlor/card/data/Deck.java @@ -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