Remove trailing whitespace throughout, and then widen/vararg/foreachize some

card game stuff that we were looking at.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@804 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Dave Hoover
2009-03-17 17:46:50 +00:00
parent d05664f649
commit 20657dbaff
31 changed files with 271 additions and 301 deletions
@@ -34,8 +34,7 @@ import com.threerings.parlor.turn.client.TurnGameController;
import static com.threerings.parlor.card.Log.log; import static com.threerings.parlor.card.Log.log;
/** /**
* A controller class for card games. Handles common functions like * A controller class for card games. Handles common functions like accepting dealt hands.
* accepting dealt hands.
*/ */
public abstract class CardGameController extends GameController public abstract class CardGameController extends GameController
implements TurnGameController, CardCodes, CardGameReceiver implements TurnGameController, CardCodes, CardGameReceiver
@@ -50,8 +49,7 @@ public abstract class CardGameController extends GameController
Thread.dumpStack(); Thread.dumpStack();
} }
_ctx.getClient().getInvocationDirector().registerReceiver( _ctx.getClient().getInvocationDirector().registerReceiver(new CardGameDecoder(this));
new CardGameDecoder(this));
super.willEnterPlace(plobj); super.willEnterPlace(plobj);
} }
@@ -61,8 +59,7 @@ public abstract class CardGameController extends GameController
{ {
super.didLeavePlace(plobj); super.didLeavePlace(plobj);
_ctx.getClient().getInvocationDirector().unregisterReceiver( _ctx.getClient().getInvocationDirector().unregisterReceiver(CardGameDecoder.RECEIVER_CODE);
CardGameDecoder.RECEIVER_CODE);
} }
// Documentation inherited. // Documentation inherited.
@@ -80,8 +77,8 @@ public abstract class CardGameController extends GameController
} }
/** /**
* Called when the server deals the client a new hand of cards. Default * Called when the server deals the client a new hand of cards. Default implementation does
* implementation does nothing. * nothing.
* *
* @param hand the hand dealt to the user * @param hand the hand dealt to the user
*/ */
@@ -89,8 +86,8 @@ public abstract class CardGameController extends GameController
{} {}
/** /**
* Dispatched to the client when it has received a set of cards * Dispatched to the client when it has received a set of cards from another player. Default
* from another player. Default implementation does nothing. * implementation does nothing.
* *
* @param plidx the index of the player providing the cards * @param plidx the index of the player providing the cards
* @param cards the cards received * @param cards the cards received
@@ -99,9 +96,8 @@ public abstract class CardGameController extends GameController
{} {}
/** /**
* Dispatched to the client when the server has forced it to send * Dispatched to the client when the server has forced it to send a set of cards to another
* a set of cards to another player. Default implementation does * player. Default implementation does nothing.
* nothing.
* *
* @param plidx the index of the player to which the cards were sent * @param plidx the index of the player to which the cards were sent
* @param cards the cards sent * @param cards the cards sent
@@ -110,8 +106,8 @@ public abstract class CardGameController extends GameController
{} {}
/** /**
* Dispatched to the client when a set of cards is transferred between * Dispatched to the client when a set of cards is transferred between two other players in
* two other players in the game. Default implementation does nothing. * the game. Default implementation does nothing.
* *
* @param fromidx the index of the player sending the cards * @param fromidx the index of the player sending the cards
* @param toidx the index of the player receiving the cards * @param toidx the index of the player receiving the cards
@@ -48,8 +48,8 @@ public class Card implements DSet.Entry, Comparable<Card>, CardCodes
} }
/** /**
* Returns the value of the card, either from 2 to 11 or * Returns the value of the card, either from 2 to 11 or KING, QUEEN, JACK, ACE, RED_JOKER, or
* KING, QUEEN, JACK, ACE, RED_JOKER, or BLACK_JOKER. * BLACK_JOKER.
* *
* @return the value of the card * @return the value of the card
*/ */
@@ -59,8 +59,8 @@ public class Card implements DSet.Entry, Comparable<Card>, CardCodes
} }
/** /**
* Returns the suit of the card: SPADES, HEARTS, DIAMONDS, or * Returns the suit of the card: SPADES, HEARTS, DIAMONDS, or CLUBS. If the card is the joker,
* CLUBS. If the card is the joker, the suit is undefined. * the suit is undefined.
* *
* @return the suit of the card * @return the suit of the card
*/ */
@@ -116,8 +116,8 @@ public class Card implements DSet.Entry, Comparable<Card>, CardCodes
} }
/** /**
* Checks whether or not this card is valid. The no-arg public * Checks whether or not this card is valid. The no-arg public constructor for deserialization
* constructor for deserialization creates an invalid card. * creates an invalid card.
* *
* @return true if this card is valid, false if not * @return true if this card is valid, false if not
*/ */
@@ -158,14 +158,13 @@ public class Card implements DSet.Entry, Comparable<Card>, CardCodes
} }
/** /**
* Compares this card to another. The card order is the same as the * Compares this card to another. The card order is the same as the initial deck ordering: two
* initial deck ordering: two through ten, jack, queen, king, ace for * through ten, jack, queen, king, ace for spades, hearts, clubs, and diamonds, then the red
* spades, hearts, clubs, and diamonds, then the red joker and the * joker and the black joker.
* black joker.
* *
* @param other the other card to compare this to * @param other the other card to compare this to
* @return -1, 0, or +1, depending on whether this card is less than, * @return -1, 0, or +1, depending on whether this card is less than, equal to, or greater
* equal to, or greater than the other card * than the other card
*/ */
public int compareTo (Card other) public int compareTo (Card other)
{ {
@@ -36,8 +36,8 @@ import com.threerings.parlor.game.server.GameManager;
import com.threerings.parlor.turn.server.TurnGameManager; import com.threerings.parlor.turn.server.TurnGameManager;
/** /**
* A manager class for card games. Handles common functions like dealing * A manager class for card games. Handles common functions like dealing hands of cards to all
* hands of cards to all players. * players.
*/ */
public class CardGameManager extends GameManager public class CardGameManager extends GameManager
implements TurnGameManager, CardCodes implements TurnGameManager, CardCodes
@@ -72,9 +72,8 @@ public class CardGameManager extends GameManager
} }
/** /**
* This should be called to start a rematched game. It just starts the * This should be called to start a rematched game. It just starts the current game anew, but
* current game anew, but provides a mechanism for derived classes to * provides a mechanism for derived classes to do special things when there is a rematch.
* do special things when there is a rematch.
*/ */
public void rematchGame () public void rematchGame ()
{ {
@@ -86,10 +85,9 @@ public class CardGameManager extends GameManager
} }
/** /**
* Derived classes can override this method and take any action needed * Derived classes can override this method and take any action needed prior to a game
* prior to a game rematch. If the rematch needs to be vetoed for any * rematch. If the rematch needs to be vetoed for any reason, they can return false from this
* reason, they can return false from this method and the rematch will * method and the rematch will be aborted.
* be aborted.
*/ */
protected boolean gameWillRematch () protected boolean gameWillRematch ()
{ {
@@ -97,14 +95,13 @@ public class CardGameManager extends GameManager
} }
/** /**
* Deals a hand of cards to the player at the specified index from * Deals a hand of cards to the player at the specified index from the given Deck.
* the given Deck.
* *
* @param deck the deck from which to deal * @param deck the deck from which to deal
* @param size the size of the hand to deal * @param size the size of the hand to deal
* @param playerIndex the index of the target player * @param playerIndex the index of the target player
* @return the hand dealt to the player, or null if the deal * @return the hand dealt to the player, or null if the deal was canceled because the deck did
* was canceled because the deck did not contain enough cards * not contain enough cards
*/ */
public Hand dealHand (Deck deck, int size, int playerIndex) public Hand dealHand (Deck deck, int size, int playerIndex)
{ {
@@ -124,14 +121,12 @@ public class CardGameManager extends GameManager
} }
/** /**
* Deals a hand of cards to each player from the specified * Deals a hand of cards to each player from the specified Deck.
* Deck.
* *
* @param deck the deck from which to deal * @param deck the deck from which to deal
* @param size the size of the hands to deal * @param size the size of the hands to deal
* @return the array of hands dealt to each player, or null if * @return the array of hands dealt to each player, or null if the deal was canceled because
* the deal was canceled because the deck did not contain enough * the deck did not contain enough cards
* cards
*/ */
public Hand[] dealHands (Deck deck, int size) public Hand[] dealHands (Deck deck, int size)
{ {
@@ -150,10 +145,8 @@ public class CardGameManager extends GameManager
} }
/** /**
* Gets the player index of the specified client object, or -1 * Gets the player index of the specified client object, or -1 if the client object does not
* if the client object does not represent a player. If the game has ended, * represent a player. If the game has ended, looks through the players of the now-ended game.
* looks through the players of the now-ended game.
*
*/ */
public int getPlayerIndex (ClientObject client) public int getPlayerIndex (ClientObject client)
{ {
@@ -169,8 +162,8 @@ public class CardGameManager extends GameManager
} }
/** /**
* Returns the client object corresponding to the specified player index, * Returns the client object corresponding to the specified player index, or null if the
* or null if the position is not occupied by a player. * position is not occupied by a player.
*/ */
public ClientObject getClientObject (int pidx) public ClientObject getClientObject (int pidx)
{ {
@@ -211,34 +204,30 @@ public class CardGameManager extends GameManager
} }
/** /**
* Sends sets of cards between players simultaneously. Each player is * Sends sets of cards between players simultaneously. Each player is guaranteed to receive
* guaranteed to receive the notification of cards received after the * the notification of cards received after the notification of cards sent. The length of the
* notification of cards sent. The length of the arrays passed must * arrays passed must be equal to the player count.
* be equal to the player count.
* *
* @param toPlayerIndices for each player, the index of the player to * @param toPlayerIndices for each player, the index of the player to transfer cards to
* transfer cards to
* @param cards for each player, the cards to transfer * @param cards for each player, the cards to transfer
*/ */
public void transferCardsBetweenPlayers (int[] toPlayerIndices, public void transferCardsBetweenPlayers (int[] toPlayerIndices, Card[][] cards)
Card[][] cards)
{ {
// Send all removal notices // Send all removal notices
for (int i = 0; i < _playerCount; i++) { for (int ii = 0; ii < _playerCount; ii++) {
ClientObject fromClient = getClientObject(i); ClientObject fromClient = getClientObject(ii);
if (fromClient != null) { if (fromClient != null) {
CardGameSender.sentCardsToPlayer(fromClient, CardGameSender.sentCardsToPlayer(fromClient, toPlayerIndices[ii], cards[ii]);
toPlayerIndices[i], cards[i]);
} }
} }
// Send all addition notices and notify everyone else // Send all addition notices and notify everyone else
for (int i = 0; i < _playerCount; i++) { for (int ii = 0; ii < _playerCount; ii++) {
ClientObject toClient = getClientObject(toPlayerIndices[i]); ClientObject toClient = getClientObject(toPlayerIndices[ii]);
if (toClient != null) { if (toClient != null) {
CardGameSender.sendCardsFromPlayer(toClient, i, cards[i]); CardGameSender.sendCardsFromPlayer(toClient, ii, cards[ii]);
} }
notifyCardsTransferred(i, toPlayerIndices[i], cards[i].length); notifyCardsTransferred(ii, toPlayerIndices[ii], cards[ii].length);
} }
} }
@@ -250,8 +239,8 @@ public class CardGameManager extends GameManager
* @param toPlayerIdx the index of the player receiving the cards * @param toPlayerIdx the index of the player receiving the cards
* @param cards the number of cards sent * @param cards the number of cards sent
*/ */
protected void notifyCardsTransferred (final int fromPlayerIdx, protected void notifyCardsTransferred (
final int toPlayerIdx, final int cards) final int fromPlayerIdx, final int toPlayerIdx, final int cards)
{ {
final int senderOid = _playerOids[fromPlayerIdx], final int senderOid = _playerOids[fromPlayerIdx],
receiverOid = _playerOids[toPlayerIdx]; receiverOid = _playerOids[toPlayerIdx];
@@ -25,8 +25,7 @@ import com.threerings.parlor.card.client.CardGameController;
import com.threerings.parlor.turn.client.TurnGameControllerDelegate; import com.threerings.parlor.turn.client.TurnGameControllerDelegate;
/** /**
* A card game controller delegate for trick-based card games, such as * A card game controller delegate for trick-based card games, such as Spades and Hearts.
* Spades and Hearts.
*/ */
public class TrickCardGameControllerDelegate public class TrickCardGameControllerDelegate
extends TurnGameControllerDelegate extends TurnGameControllerDelegate
@@ -36,8 +35,7 @@ public class TrickCardGameControllerDelegate
* *
* @param controller the game controller * @param controller the game controller
*/ */
public TrickCardGameControllerDelegate (CardGameController public TrickCardGameControllerDelegate (CardGameController controller)
controller)
{ {
super(controller); super(controller);
_cgctrl = controller; _cgctrl = controller;
@@ -28,8 +28,6 @@ import java.util.List;
import com.samskivert.util.ArrayUtil; import com.samskivert.util.ArrayUtil;
import com.samskivert.util.Interval; import com.samskivert.util.Interval;
import com.samskivert.util.RandomUtil; import com.samskivert.util.RandomUtil;
import com.samskivert.util.StringUtil;
import com.threerings.util.Name; import com.threerings.util.Name;
import com.threerings.presents.data.ClientObject; import com.threerings.presents.data.ClientObject;
@@ -215,15 +213,15 @@ public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate
// make sure they're actually a player // make sure they're actually a player
int fromidx = _cgmgr.getPlayerIndex(client); int fromidx = _cgmgr.getPlayerIndex(client);
if (fromidx == -1) { if (fromidx == -1) {
log.warning("Send request from non-player [username=" + log.warning("Send request from non-player",
((BodyObject)client).who() + ", cards=" + StringUtil.toString(cards) + "]."); "username", ((BodyObject)client).who(), "cards", cards);
return; return;
} }
// make sure they have the cards // make sure they have the cards
if (!_hands[fromidx].containsAll(cards)) { if (!_hands[fromidx].containsAll(cards)) {
log.warning("Tried to send cards not held [username=" + log.warning("Tried to send cards not held",
((BodyObject)client).who() + ", cards=" + StringUtil.toString(cards) + "]."); "username", ((BodyObject)client).who(), "cards", cards);
return; return;
} }
@@ -253,15 +251,13 @@ public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate
// make sure their hand contains the specified card // make sure their hand contains the specified card
if (!_hands[pidx].contains(card)) { if (!_hands[pidx].contains(card)) {
log.warning("Tried to play card not held [username=" + username + log.warning("Tried to play card not held", "username", username, "card", card);
", card=" + card + "].");
return; return;
} }
// make sure the card is legal to play // make sure the card is legal to play
if (!_trickCardGame.isCardPlayable(_hands[pidx], card)) { if (!_trickCardGame.isCardPlayable(_hands[pidx], card)) {
log.warning("Tried to play illegal card [username=" + username + log.warning("Tried to play illegal card", "username", username, "card", card);
", card=" + card + "].");
return; return;
} }
@@ -281,14 +277,13 @@ public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate
// make sure the requester is one of the players // make sure the requester is one of the players
int pidx = _cgmgr.getPlayerIndex(client); int pidx = _cgmgr.getPlayerIndex(client);
if (pidx == -1) { if (pidx == -1) {
log.warning("Rematch request from non-player [username=" + log.warning("Rematch request from non-player", "username", ((BodyObject)client).who());
((BodyObject)client).who() + "].");
return; return;
} }
// make sure the player hasn't already requested // make sure the player hasn't already requested
if (_trickCardGame.getRematchRequests()[pidx] != TrickCardGameObject.NO_REQUEST) { if (_trickCardGame.getRematchRequests()[pidx] != TrickCardGameObject.NO_REQUEST) {
log.warning("Repeated rematch request [username=" + ((BodyObject)client).who() + "]."); log.warning("Repeated rematch request", "username", ((BodyObject)client).who());
return; return;
} }
@@ -361,8 +356,8 @@ public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate
{ {
int[] rematchRequests = _trickCardGame.getRematchRequests(); int[] rematchRequests = _trickCardGame.getRematchRequests();
int count = 0; int count = 0;
for (int i = 0; i < rematchRequests.length; i++) { for (int rematchRequest : rematchRequests) {
if (rematchRequests[i] != TrickCardGameObject.NO_REQUEST) { if (rematchRequest != TrickCardGameObject.NO_REQUEST) {
count++; count++;
} }
} }
@@ -562,8 +557,8 @@ public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate
*/ */
protected boolean anyHandsEmpty () protected boolean anyHandsEmpty ()
{ {
for (int i = 0; i < _hands.length; i++) { for (Hand _hand : _hands) {
if (_hands[i].isEmpty()) { if (_hand.isEmpty()) {
return true; return true;
} }
} }
@@ -33,8 +33,7 @@ public class TrickCardGameUtil
implements TrickCardCodes implements TrickCardCodes
{ {
/** /**
* For four-player games with fixed partnerships, this returns the index * For four-player games with fixed partnerships, this returns the index of the player's team.
* of the player's team.
* *
* @param pidx the player index * @param pidx the player index
*/ */
@@ -44,8 +43,7 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games with fixed partnerships, this returns the index * For four-player games with fixed partnerships, this returns the index of the other team.
* of the other team.
* *
* @param tidx the index of the team * @param tidx the index of the team
*/ */
@@ -55,8 +53,8 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games with fixed partnerships, this returns the index * For four-player games with fixed partnerships, this returns the index of the player's
* of the player's partner. * partner.
*/ */
public static int getPartnerIndex (int pidx) public static int getPartnerIndex (int pidx)
{ {
@@ -64,8 +62,8 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games with fixed partnerships, this returns the index * For four-player games with fixed partnerships, this returns the index of one of the members
* of one of the members of a team. * of a team.
* *
* @param tidx the index of the team * @param tidx the index of the team
* @param midx the index of the player within the team * @param midx the index of the player within the team
@@ -76,8 +74,8 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games, this returns the index of the player after the * For four-player games, this returns the index of the player after the specified player
* specified player going clockwise around the table. * going clockwise around the table.
*/ */
public static int getNextInClockwiseSequence (int pidx) public static int getNextInClockwiseSequence (int pidx)
{ {
@@ -88,8 +86,8 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games with fixed partnerships, this returns the * For four-player games with fixed partnerships, this returns the relative location of one
* relative location of one player from the point of view of another. * player from the point of view of another.
* *
* @param pidx1 the index of the player to whom the location is relative * @param pidx1 the index of the player to whom the location is relative
* @param pidx2 the index of the player whose location is desired * @param pidx2 the index of the player whose location is desired
@@ -101,8 +99,7 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games, returns the index of the player to the left of * For four-player games, returns the index of the player to the left of the specified player.
* the specified player.
*/ */
public static int getLeftIndex (int pidx) public static int getLeftIndex (int pidx)
{ {
@@ -110,8 +107,8 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games, returns the index of the player to the right of * For four-player games, returns the index of the player to the right of the specified
* the specified player. * player.
*/ */
public static int getRightIndex (int pidx) public static int getRightIndex (int pidx)
{ {
@@ -119,8 +116,7 @@ public class TrickCardGameUtil
} }
/** /**
* For four-player games, returns the index of the player across from the * For four-player games, returns the index of the player across from the specified player.
* specified player.
*/ */
public static int getOppositeIndex (int pidx) public static int getOppositeIndex (int pidx)
{ {
@@ -140,8 +136,8 @@ public class TrickCardGameUtil
*/ */
public static boolean containsCard (PlayerCard[] cards, Card card) public static boolean containsCard (PlayerCard[] cards, Card card)
{ {
for (int i = 0; i < cards.length; i++) { for (PlayerCard pc : cards) {
if (cards[i].card.equals(card)) { if (pc.card.equals(card)) {
return true; return true;
} }
} }
@@ -149,14 +145,13 @@ public class TrickCardGameUtil
} }
/** /**
* Determines the number of cards that belong to the specified suit within * Determines the number of cards that belong to the specified suit within the array given.
* the array given.
*/ */
public static int countSuitMembers (PlayerCard[] cards, int suit) public static int countSuitMembers (PlayerCard[] cards, int suit)
{ {
int count = 0; int count = 0;
for (int i = 0; i < cards.length; i++) { for (PlayerCard pc : cards) {
if (cards[i].card.getSuit() == suit) { if (pc.card.getSuit() == suit) {
count++; count++;
} }
} }
@@ -172,21 +167,19 @@ public class TrickCardGameUtil
} }
/** /**
* Returns the highest card (according to the standard A,K,...,2 ordering) * Returns the highest card (according to the standard A,K,...,2 ordering) in the suit lead,
* in the suit lead, with an optional trump suit. * with an optional trump suit.
* *
* @param trumpSuit the trump suit, or -1 for none * @param trumpSuit the trump suit, or -1 for none
*/ */
public static PlayerCard getHighestInLeadSuit (PlayerCard[] cardsPlayed, public static PlayerCard getHighestInLeadSuit (PlayerCard[] cardsPlayed, int trumpSuit)
int trumpSuit)
{ {
PlayerCard highest = cardsPlayed[0]; PlayerCard highest = cardsPlayed[0];
for (int i = 1; i < cardsPlayed.length; i++) { for (int i = 1; i < cardsPlayed.length; i++) {
PlayerCard other = cardsPlayed[i]; PlayerCard other = cardsPlayed[i];
if ((other.card.getSuit() == highest.card.getSuit() && if ((other.card.getSuit() == highest.card.getSuit() &&
other.card.compareTo(highest.card) > 0) || other.card.compareTo(highest.card) > 0) ||
(other.card.getSuit() == trumpSuit && (other.card.getSuit() == trumpSuit && highest.card.getSuit() != trumpSuit)) {
highest.card.getSuit() != trumpSuit)) {
highest = other; highest = other;
} }
} }