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,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);
}
}
@@ -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)
{}
}
@@ -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;
}
@@ -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());
}
}
@@ -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);
}
@@ -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() + "]";
}
}
@@ -0,0 +1,75 @@
//
// $Id: CardGameManager.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.server;
import com.threerings.parlor.card.Log;
import com.threerings.parlor.card.data.CardCodes;
import com.threerings.parlor.card.data.Deck;
import com.threerings.parlor.card.data.Hand;
import com.threerings.parlor.game.GameManager;
import com.threerings.presents.dobj.MessageEvent;
/**
* A manager class for card games. Handles common functions like dealing hands
* of cards to all players.
*/
public class CardGameManager extends GameManager
implements CardCodes
{
/**
* Deals a hand of cards to each player from the specified
* Deck.
*
* @param deck the deck from which to deal
* @param size the size of the hands to deal
* @return the array of hands dealt to each player, or null if
* the deal was canceled because the deck did not contain enough
* cards
*/
public Hand[] dealHands(Deck deck, int size)
{
if(deck.cards.size() < size * _playerCount)
{
return null;
}
else
{
Hand[] hands = new Hand[_playerCount];
for(int i=0;i<_playerCount;i++)
{
_omgr.postEvent(
new MessageEvent(
_playerOids[i],
TAKE_HAND,
new Object[] { hands[i] = deck.dealHand(size) }
)
);
}
return hands;
}
}
}
@@ -0,0 +1,47 @@
//
// $Id: TrickCardGameController.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.trick;
import com.threerings.parlor.turn.TurnGameController;
/**
* A card game controller interface for trick-based card games, such as
* Spades and Hearts.
*/
public interface TrickCardGameController extends TurnGameController
{
/**
* Notifies the controller that the gameplay entered or left a hand.
*
* @param playingHand true if the gameplay entered a hand, false if
* it left one
*/
public void playingHandDidChange(boolean playingHand);
/**
* Notifies the controller that the gameplay entered or left a trick.
*
* @param playingTrick true if the gameplay entered a trick, false if
* it left one
*/
public void playingTrickDidChange(boolean playingTrick);
}
@@ -0,0 +1,66 @@
//
// $Id: TrickCardGameControllerDelegate.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.trick;
import com.threerings.parlor.turn.TurnGameControllerDelegate;
import com.threerings.presents.dobj.AttributeChangedEvent;
/**
* A card game controller delegate for trick-based card games, such as
* Spades and Hearts.
*/
public class TrickCardGameControllerDelegate extends TurnGameControllerDelegate
{
/** The trick card game controller. */
protected TrickCardGameController _tcgctrl;
/**
* Constructor.
*
* @param controller the game controller
*/
public TrickCardGameControllerDelegate(TrickCardGameController controller)
{
super(controller);
_tcgctrl = controller;
}
// Documentation inherited
public void attributeChanged(AttributeChangedEvent ace)
{
super.attributeChanged(ace);
TrickCardGameObject tcgObj = (TrickCardGameObject)_gameObj;
if(ace.getName().equals(tcgObj.getPlayingHandFieldName()))
{
_tcgctrl.playingHandDidChange(tcgObj.getPlayingHand());
}
else if(ace.getName().equals(tcgObj.getPlayingTrickFieldName()))
{
_tcgctrl.playingTrickDidChange(tcgObj.getPlayingTrick());
}
}
}
@@ -0,0 +1,61 @@
//
// $Id: TrickCardGameManager.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.trick;
import com.threerings.parlor.turn.TurnGameManager;
/**
* A card game manager interface for trick-based card games, such as
* Spades and Hearts.
*/
public interface TrickCardGameManager extends TurnGameManager
{
/**
* Notifies the manager that a hand is about to start.
*/
public void handWillStart();
/**
* Notifies the manager that a hand just started.
*/
public void handDidStart();
/**
* Notifies the manager that a hand has ended.
*/
public void handDidEnd();
/**
* Notifies the manager that a trick is about to start.
*/
public void trickWillStart();
/**
* Notifies the manager that a trick just started.
*/
public void trickDidStart();
/**
* Notifies the manager that a trick has ended.
*/
public void trickDidEnd();
}
@@ -0,0 +1,132 @@
//
// $Id: TrickCardGameManagerDelegate.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.trick;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.parlor.game.GameManager;
import com.threerings.parlor.turn.TurnGameManagerDelegate;
/**
* A card game manager delegate for trick-based card games, such as
* Spades and Hearts.
*/
public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate
{
/** The trick card game manager. */
protected TrickCardGameManager _tcgmgr;
/** The game object. */
protected TrickCardGameObject _trickCardGame;
/**
* Constructor.
*
* @param manager the game manager
*/
public TrickCardGameManagerDelegate(TrickCardGameManager manager)
{
super(manager);
_tcgmgr = manager;
}
// Documentation inherited.
public void setFirstTurnHolder()
{
if(_trickCardGame.getPlayingHand() && _trickCardGame.getPlayingTrick())
{
super.setFirstTurnHolder();
}
else
{
_turnIdx = -1;
}
}
// Documentation inherited.
public void setNextTurnHolder()
{
if(_trickCardGame.getPlayingHand() && _trickCardGame.getPlayingTrick())
{
super.setNextTurnHolder();
}
else
{
_turnIdx = -1;
}
}
// Documentation inherited.
public void didStartup(PlaceObject plobj)
{
super.didStartup(plobj);
_trickCardGame = (TrickCardGameObject)plobj;
}
/**
* Starts a hand of cards.
*/
public void startHand()
{
_tcgmgr.handWillStart();
_trickCardGame.setPlayingHand(true);
_tcgmgr.handDidStart();
}
/**
* Ends a hand of cards.
*/
public void endHand()
{
_trickCardGame.setPlayingHand(false);
_tcgmgr.handDidEnd();
}
/**
* Starts a trick.
*/
public void startTrick()
{
_tcgmgr.trickWillStart();
_trickCardGame.setPlayingTrick(true);
_tcgmgr.trickDidStart();
}
/**
* Ends a trick.
*/
public void endTrick()
{
_trickCardGame.setPlayingTrick(false);
_tcgmgr.trickDidEnd();
}
}
@@ -0,0 +1,74 @@
//
// $Id: TrickCardGameObject.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.trick;
import com.threerings.parlor.turn.TurnGameObject;
/**
* Game objects for trick-based card games must implement this interface.
*/
public interface TrickCardGameObject extends TurnGameObject
{
/**
* Returns the name of the field that signals whether or not the users
* are currently playing a hand.
*
* @return the name of the playingHand field
*/
public String getPlayingHandFieldName();
/**
* Checks whether the users are currently playing a hand.
*
* @return true if the users are playing a hand, false otherwise
*/
public boolean getPlayingHand();
/**
* Brings the gameplay in or out of a hand.
*
* @param playingHand true to enter a hand, false to leave one
*/
public void setPlayingHand(boolean playingHand);
/**
* Returns the name of the field that signals whether or not the users
* are currently playing a trick.
*
* @return the name of the playingTrick field
*/
public String getPlayingTrickFieldName();
/**
* Checks whether the users are currently playing a trick.
*
* @return true if the users are playing a trick, false otherwise
*/
public boolean getPlayingTrick();
/**
* Brings the gameplay in or out of a trick.
*
* @param playingTrick true to enter a trick, false to leave one
*/
public void setPlayingTrick(boolean playingTrick);
}