From 469e15e58ec11a95eb96157e85a4df5df9bd1e88 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 6 Oct 2006 00:22:41 +0000 Subject: [PATCH] ActionScript versions of the Card stuff and Zone stuff. The Card stuff is non-trivial and shouldn't entirely be trusted until it's actually used for something. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@101 c613c5cb-e716-0410-b11b-feb51c14d237 --- .../com/threerings/parlor/card/data/Card.as | 251 ++++++++++++++++++ .../threerings/parlor/card/data/CardCodes.as | 59 ++++ .../com/threerings/parlor/card/data/Deck.as | 108 ++++++++ .../com/threerings/parlor/card/data/Hand.as | 94 +++++++ .../threerings/parlor/card/data/PlayerCard.as | 66 +++++ .../whirled/zone/data/SceneSummary.as | 82 ++++++ .../whirled/zone/data/ZoneSummary.as | 78 ++++++ 7 files changed, 738 insertions(+) create mode 100644 src/as/com/threerings/parlor/card/data/Card.as create mode 100644 src/as/com/threerings/parlor/card/data/CardCodes.as create mode 100644 src/as/com/threerings/parlor/card/data/Deck.as create mode 100644 src/as/com/threerings/parlor/card/data/Hand.as create mode 100644 src/as/com/threerings/parlor/card/data/PlayerCard.as create mode 100644 src/as/com/threerings/whirled/zone/data/SceneSummary.as create mode 100644 src/as/com/threerings/whirled/zone/data/ZoneSummary.as diff --git a/src/as/com/threerings/parlor/card/data/Card.as b/src/as/com/threerings/parlor/card/data/Card.as new file mode 100644 index 00000000..d3c28f2a --- /dev/null +++ b/src/as/com/threerings/parlor/card/data/Card.as @@ -0,0 +1,251 @@ +// +// $Id: Card.java 4191 2006-06-13 22:42:20Z ray $ +// +// 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.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; + +import com.threerings.util.Byte; +import com.threerings.util.Comparable; +import com.threerings.util.Hashable; +import com.threerings.util.Integer; +import com.threerings.util.StringBuilder; + +import com.threerings.presents.dobj.DSet; +import com.threerings.presents.dobj.DSet_Entry; + +/** + * Instances of this class represent individual playing cards. + */ +public class Card + implements DSet_Entry, Comparable, Hashable +{ + /** + * Creates a new card. + * + * @param number the number of the card + * @param suit the suit of the card + */ + public function Card (number :int = 0, suit :int = 0) + { + _value = ((suit << 5) | number); + } + + /** + * 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 function getSuit () :int + { + return (_value >> 5); + } + + /** + * Checks whether the card is a number card (2 to 10). + * + * @return true if the card is a number card, false otherwise + */ + public function isNumber () :Boolean + { + var number :int = getNumber(); + return number >= 2 && number <= 10; + } + + /** + * Checks whether the card is a face card (KING, QUEEN, or JACK). + * + * @return true if the card is a face card, false otherwise + */ + public function isFace () :Boolean + { + var number :int = getNumber(); + return number == CardCodes.KING || number == CardCodes.QUEEN || + number == CardCodes.JACK; + } + + /** + * Checks whether the card is an ace. + * + * @return true if the card is an ace, false otherwise + */ + public function isAce () :Boolean + { + return getNumber() == CardCodes.ACE; + } + + /** + * Checks whether the card is a joker. + * + * @return true if the card is a joker, false otherwise + */ + public function isJoker () :Boolean + { + var number :int = getNumber(); + return number == CardCodes.RED_JOKER || number == CardCodes.BLACK_JOKER; + } + + /** + * Returns a hash code for this card. + * + * @return this card's hash code + */ + public function hashCode () :int + { + return _value; + } + + /** + * 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 function compareTo (other :Object) :int + { + var otherValue :int = (other as Card)._value; + if (_value > otherValue) { + return +1; + } else if (_value < otherValue) { + return -1; + } else { + return 0; + } + } + + /** + * Checks this card for equality with another. + * + * @param other the other card to compare + * @return true if the cards are equal, false otherwise + */ + public function equals (other :Object) :Boolean + { + if (other is Card) { + return _value == (other as Card)._value; + } else { + return false; + } + } + + /** + * Returns a string representation of this card. + * + * @return a description of this card + */ + public function toString () :String + { + var number :int = getNumber(); + + if (number == CardCodes.RED_JOKER) { + return "RJ"; + + } else if (number == CardCodes.BLACK_JOKER) { + return "BJ"; + + } else { + var sb : StringBuilder = new StringBuilder(); + if (number >= 2 && number <= 9) { + sb.append(number); + + } else { + switch (number) { + case 10: sb.append('T'); break; + case CardCodes.JACK: sb.append('J'); break; + case CardCodes.QUEEN: sb.append('Q'); break; + case CardCodes.KING: sb.append('K'); break; + case CardCodes.ACE: sb.append('A'); break; + default: sb.append('?'); break; + } + } + + switch (getSuit()) { + case CardCodes.SPADES: sb.append('s'); break; + case CardCodes.HEARTS: sb.append('h'); break; + case CardCodes.CLUBS: sb.append('c'); break; + case CardCodes.DIAMONDS: sb.append('d'); break; + default: sb.append('?'); break; + } + + return sb.toString(); + } + } + + // from interface DSet_Entry + public function getKey () :Object + { + if (_key == null) { + _key = Byte.valueOf(_value); + } + return _key; + } + + /** + * Returns the value of the card, either from 2 to 11 or + * KING, QUEEN, JACK, ACE, RED_JOKER, or BLACK_JOKER. + * + * @return the value of the card + */ + public function getNumber () :int + { + return (_value & 0x1F); + } + + /** + * Checks whether or not this card is valid. The no-arg public + * constructor for deserialization creates an invalid card. + * + * @return true if this card is valid, false if not + */ + public function isValid () :Boolean + { + var number :int = getNumber(), suit :int = getSuit(); + return number == CardCodes.RED_JOKER || + number == CardCodes.BLACK_JOKER || + (number >= 2 && number <= CardCodes.ACE && + suit >= CardCodes.SPADES && suit <= CardCodes.DIAMONDS); + } + + // from interface Streamable + public function readObject (ins :ObjectInputStream) :void + { + _value = ins.readByte(); + } + + // from interface Streamable + public function writeObject (out :ObjectOutputStream) :void + { + out.writeByte(_value); + } + + /** The number of the card. */ + protected var _value :int; + + /** The comparison key. */ + protected var _key :Byte; +} +} diff --git a/src/as/com/threerings/parlor/card/data/CardCodes.as b/src/as/com/threerings/parlor/card/data/CardCodes.as new file mode 100644 index 00000000..c56cb360 --- /dev/null +++ b/src/as/com/threerings/parlor/card/data/CardCodes.as @@ -0,0 +1,59 @@ +// +// $Id: CardCodes.java 3224 2004-11-19 19:04:56Z andrzej $ +// +// 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 { + +/** + * Constants relating to the card services. + */ +public class CardCodes +{ + /** The suit of spades. */ + public static const SPADES :int = 0; + + /** The suit of hearts. */ + public static const HEARTS :int = 1; + + /** The suit of clubs. */ + public static const CLUBS :int = 2; + + /** The suit of diamonds. */ + public static const DIAMONDS :int = 3; + + /** The number of the jack. */ + public static const JACK :int = 11; + + /** The number of the queen. */ + public static const QUEEN :int = 12; + + /** The number of the king. */ + public static const KING :int = 13; + + /** The number of the ace. */ + public static const ACE :int = 14; + + /** The number of the red joker. */ + public static const RED_JOKER :int = 15; + + /** The number of the black joker. */ + public static const BLACK_JOKER :int = 16; +} +} diff --git a/src/as/com/threerings/parlor/card/data/Deck.as b/src/as/com/threerings/parlor/card/data/Deck.as new file mode 100644 index 00000000..556d2835 --- /dev/null +++ b/src/as/com/threerings/parlor/card/data/Deck.as @@ -0,0 +1,108 @@ +// +// $Id: Deck.java 3713 2005-09-27 21:58:10Z andrzej $ +// +// 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 mx.collections.ArrayCollection; + +import com.threerings.util.StreamableArrayList; + +/** + * Instances of this class represent decks of cards. + */ +public class Deck extends StreamableArrayList +{ + /** + * Constructor. + * + * @param includeJokers whether or not to include the two jokers + * in the deck + */ + public function Deck (includeJokers :Boolean = false) + { + reset(includeJokers); + } + + /** + * 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 function dealHand (size :int) :Hand + { + if (length < size) { + return null; + + } else { + var hand :Hand = new Hand(); + var offset :int = length - size; + for (var ii :int = 0; ii < size; ii++) { + hand.addItem(removeItemAt(offset)); + } + return hand; + } + } + + /** + * Returns a hand of cards to the deck. + * + * @param hand the hand of cards to return + */ + public function returnHand (hand :Hand) :void + { + addAll(hand); + hand.removeAll(); + } + + /** + * 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 function reset (includeJokers :Boolean) :void + { + removeAll(); + + for (var i :int = CardCodes.SPADES; i <= CardCodes.DIAMONDS; i++) { + for (var j :int = 2; j <= CardCodes.ACE; j++) { + addItem(new Card(j, i)); + } + } + + if (includeJokers) { + addItem(new Card(CardCodes.RED_JOKER, 3)); + addItem(new Card(CardCodes.BLACK_JOKER, 3)); + } + } + + /** + * Shuffles the deck. + */ + public function shuffle () :void + { + // TODO: Collections.shuffle(this); + } +} +} diff --git a/src/as/com/threerings/parlor/card/data/Hand.as b/src/as/com/threerings/parlor/card/data/Hand.as new file mode 100644 index 00000000..106864ed --- /dev/null +++ b/src/as/com/threerings/parlor/card/data/Hand.as @@ -0,0 +1,94 @@ +// +// $Id: Hand.java 3813 2006-01-19 21:50:53Z ray $ +// +// 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.util.StreamableArrayList; + +/** + * Instances of this class represent hands of cards. + */ +public class Hand extends StreamableArrayList +{ + public function Hand () + { + // nothing needed + } + + /** + * 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 function getSuitMemberCount (suit :int) :int + { + var members :int = 0; + for (var i :int = 0; i < length; i++) { + if ((getItemAt(i) as Card).getSuit() == suit) { + members++; + } + } + return members; + } + + /** + * Get an array of the cards in this hand. + */ + public function getCards () :Array + { + return toArray(); + } + + /** + * Adds all of the specified cards to this hand. + */ + public function addAllCards (cards :Array) :void + { + for (var i :int = 0; i < cards.length; i++) { + addItem(cards[i]); + } + } + + /** + * Checks whether this hand contains all of the specified cards. + */ + public function containsAllCards (cards :Array) :Boolean + { + for (var i :int = 0; i < cards.length; i++) { + if (indexOf(cards[i]) == -1) { + return false; + } + } + return true; + } + + /** + * Removes all of the specified cards from this hand. + */ + public function removeAllCards (cards :Array) :void + { + for (var i :int = 0; i < cards.length; i++) { + remove(cards[i]); + } + } +} +} diff --git a/src/as/com/threerings/parlor/card/data/PlayerCard.as b/src/as/com/threerings/parlor/card/data/PlayerCard.as new file mode 100644 index 00000000..5322f8c5 --- /dev/null +++ b/src/as/com/threerings/parlor/card/data/PlayerCard.as @@ -0,0 +1,66 @@ +// +// $Id: TrickCardGameObject.java 3382 2005-03-03 19:55:35Z mdb $ +// +// 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.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; +import com.threerings.io.Streamable; + +/** + * Pairs a player index with the card that the player played in the trick. + */ +public class PlayerCard + implements Streamable +{ + /** The index of the player. */ + public var pidx :int; + + /** The card that the player played. */ + public var card :Card; + + /** + * Creates a new player card. + * + * @param pidx the index of the player + * @param card the card played + */ + public function PlayerCard (pidx :int = 0, card :Card = null) + { + this.pidx = pidx; + this.card = card; + } + + // from interface Streamable + public function readObject (ins :ObjectInputStream) :void + { + pidx = ins.readInt(); + card = (ins.readObject() as Card); + } + + // from interface Streamable + public function writeObject (out :ObjectOutputStream) :void + { + out.writeInt(pidx); + out.writeObject(card); + } +} +} diff --git a/src/as/com/threerings/whirled/zone/data/SceneSummary.as b/src/as/com/threerings/whirled/zone/data/SceneSummary.as new file mode 100644 index 00000000..f6e8095c --- /dev/null +++ b/src/as/com/threerings/whirled/zone/data/SceneSummary.as @@ -0,0 +1,82 @@ +// +// $Id: SceneSummary.java 3310 2005-01-24 23:08:21Z mdb $ +// +// 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.whirled.zone.data { + +import com.threerings.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; +import com.threerings.io.Streamable; +import com.threerings.io.TypedArray; + +/** + * The scene summary class is used to provide info about the connected + * group of scenes that make up a zone. The group of scenes that make up a + * zone is a self-contained set of scenes, connected with one another (by + * portals) but not to any scenes outside the group. + */ +public class SceneSummary + implements Streamable +{ + /** The id of this scene. */ + public var sceneId :int; + + /** The name of this scene. */ + public var name :String; + + /** The ids of the scenes to which this scene is connected via + * portals. */ + public var neighbors :TypedArray; + + /** The directions in which each of the neighbors lay. */ + public var neighborDirs :TypedArray; + + public function SceneSummary () + { + // nothing needed + } + + /** + * Generates a string representation of this instance. + */ + public function toString () :String + { + return "[sceneId=" + sceneId + ", name=" + name + "]"; + } + + // from interface Streamable + public function readObject (ins :ObjectInputStream) :void + { + sceneId = ins.readInt(); + name = (ins.readField(String) as String); + neighbors = (ins.readObject() as TypedArray); + neighborDirs = (ins.readObject() as TypedArray); + } + + // from interface Streamable + public function writeObject (out :ObjectOutputStream) :void + { + out.writeInt(sceneId); + out.writeField(name); + out.writeObject(neighbors); + out.writeObject(neighborDirs); + } +} +} diff --git a/src/as/com/threerings/whirled/zone/data/ZoneSummary.as b/src/as/com/threerings/whirled/zone/data/ZoneSummary.as new file mode 100644 index 00000000..c8f503f4 --- /dev/null +++ b/src/as/com/threerings/whirled/zone/data/ZoneSummary.as @@ -0,0 +1,78 @@ +// +// $Id: ZoneSummary.java 3726 2005-10-11 19:17:43Z ray $ +// +// 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.whirled.zone.data { + +import com.threerings.util.Name; + +import com.threerings.io.ObjectInputStream; +import com.threerings.io.ObjectOutputStream; +import com.threerings.io.SimpleStreamableObject; +import com.threerings.io.TypedArray; + +/** + * The zone summary contains information on a zone, including its name and + * summary info on all of the scenes in this zone (which can be used to + * generate a map of the zone on the client). + */ +public class ZoneSummary extends SimpleStreamableObject +{ + /** The zone's fully qualified unique identifier. */ + public var zoneId :int; + + /** The name of the zone. */ + public var name :Name; + + /** The summary information for all of the scenes in the zone. */ + public var scenes :TypedArray; + + public function ZoneSummary () + { + // nothing needed + } + + /** + * Generates a string representation of this instance. + */ + override public function toString () :String + { + return "[zoneId=" + zoneId + ", name=" + name + "]"; + } + + // from interface Streamable + override public function readObject (ins :ObjectInputStream) :void + { + super.readObject(ins); + zoneId = ins.readInt(); + name = (ins.readObject() as Name); + scenes = (ins.readObject() as TypedArray); + } + + // from interface Streamable + override public function writeObject (out :ObjectOutputStream) :void + { + super.writeObject(out); + out.writeInt(zoneId); + out.writeObject(name); + out.writeObject(scenes); + } +} +}