📄 game.java
字号:
// Game.java
//
// Copyright (c) 2000-2001 Symbian Ltd. All rights reserved
package com.symbian.devnet.whist.game;
import java.io.*;
import java.util.*;
import com.symbian.devnet.whist.tokenRing.*;
/**
* A class implementing a card game.
* Implements ReceivedTurnListener.
* @see com.symbian.devnet.whist.tokenRing.ReceivedTurnListener
* @author Symbian Devnet
*/
public class Game implements ReceivedTurnListener, Runnable
{
/** Describes if a new game is to be started. */
public boolean startNewGame = false;
/** Describes if the game has been stopped by a player. */
public boolean gameStopped = false;
/** An object used as a lock for synchronization. */
public Object lock = new Object();
/** The instance of the Engine class. */
private static TokenRing tokenRing;
/** A byte array which will contain data received from datagrams. */
private char[] dataReceived;
private String moved;
/** The pile of dealt cards for that player. */
private Hand myHand;
/** The variable used to define the Heart suit. */
private static final int HEART = 0;
/** The variable used to define the Club suit. */
private static final int CLUB = 1;
/** The variable used to define the Diamond suit. */
private static final int DIAMOND = 2;
/** The variable used to define the Spade suit. */
private static final int SPADE = 3;
/** The variable used to define a Jack. */
private static final int JACK = 11;
/** The variable used to define a Queen. */
private static final int QUEEN = 12;
/** The variable used to define a King. */
private static final int KING = 13;
/** The variable used to define an Ace. */
private static final int ACE = 14;
/** The variable used to define the maximum number of players in a game. */
private static final int MAX_NO_OF_PLAYERS = 4;
/**
* The variable used to define the maximum number of tricks possible with
* one hand of cards.
*/
private static final int MAX_NO_OF_TRICKS = 13;
/** The card which has been selected for play for the current trick. */
private int selectedCard;
/** The first card played in the previous trick. */
private Card lastFirstCard;
/** The first card played in the current trick.*/
private Card currentFirstCard;
/** Describes whether this is the first card of a trick. */
private boolean cardOne = true;
/** An instance of a ScoreCard. */
private ScoreCard sc;
/** Tracks the progress of the game. */
private boolean endGame;
/** The number of the current trick. */
private int noOfTricks = 0;
/** The unique identification number of the card played in the current trick. */
private int taken;
/** The data to be passed to the GameEventListener. */
private String gameEventData;
/** A vector of listeners for ReceivedTurnEvents. */
private Vector listeners = new Vector();
/**
* The unique identification number of the card chosen by the autoplayer.
* This is only used for testing.
*/
private int autoPicked;
/** The winner of the previous trick. */
private int winner;
/** Determines if this is the first game. */
private boolean firstGame = true;
/** The Card played during the current trick. */
private Card take;
/** An array of the Cards played during the current trick. */
private Card[] play;
/** An array of the Cards played in the previous trick. */
private Card[] played;
/**
* The identification number assigned to the scoring for each trick.
* This ensures that the score for the same trick cannot be calculated
* twice.
*/
private int scoreId = 0;
/** Describes if a new game is being started. */
private boolean isNewGame = true;
/**
* Constructor which initialises the ScoreCard and the TokenRing and adds
* a ReceivedTurnListener to the TokenRing.
*/
public Game()
{
sc = new ScoreCard();
tokenRing = new TokenRing();
tokenRing.addReceivedTurnListener(this);
}
/**
* Called when the TurnReceivedListener receives an Event. The first
* character in the array of received data determines the action taken.
* These actions include creating a new Hand, updating the score, extracting
* cards and other information from the received data, and creating GameEvents.
*/
private void processData()
{
// In all cases excluding X or Q, further data is stored in the
// message and this must be extracted.
if (dataReceived[0] != 'X' && dataReceived[0] != 'Q')
{
int count1 = 0;
while (dataReceived[count1] != '/')
count1++;
count1++;
if (dataReceived[count1] == 'F')
isNewGame = false;
else
isNewGame = true;
}
// Start of new game - the receiver of this must deal the cards.
if (dataReceived[0] == 'X')
{
isNewGame = true;
noOfTricks = 1;
String st;
cardOne = true;
// No other games have been played.
if (firstGame == true)
{
// Just displays the cards on screen. The user is not able to take a turn.
myHand = new Hand();
moved = deal(myHand.getHands());
gameEventData = "H";
st = moved.trim() + "/";
}
else // One or more games have been successfully completed.
{
int count = 0;
Card[] cs = getLastRound();
// Check the scoreId and update the score if necessary.
if (getScoreId() != scoreId)
{
scoreId = getScoreId();
lastFirstCard = getFirstCard();
winner = getWinner(cs);
sc.trickWon(winner);
}
sc.hasGameEnded();
myHand = new Hand();
moved = deal(myHand.getHands());
gameEventData = "WSH";
String s = "";
for (int i = 0; i < 4; i++)
s += String.valueOf(cs[i].getCard() + "@");
st = (moved.trim() + "@" + s + "~" + String.valueOf(getFirstCard().getCard()) + "~;" + scoreId + ";/").trim();
if (!isNewGame)
st += "F";
}
if (!gameStopped)
tokenRing.sendMove(st);
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
else if (dataReceived[0] == 'Y')
{
// Player should take a turn.
noOfTricks = 1;
if (dataReceived[1] == 'N')
{
// New trick, no current cards to be displayed.
if (firstGame == true)
{
// If it is the first game, there will not be any previous
// round to display.
myHand = new Hand(dataReceived);
gameEventData = "HT";
GameEvent ge = new GameEvent(this, gameEventData);
processGameEvent(ge);
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException ie)
{}
}
if (!gameStopped)
{
Card temp = new Card(taken);
lastFirstCard = temp;
moved = getRestOfCards() + "%" + String.valueOf(taken) + "%*;" + scoreId + ";/";
}
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
else
{
// There is a previous round to display.
Card[] cs = getLastRound();
if (scoreId != getScoreId())
{
scoreId = getScoreId();
lastFirstCard = getFirstCard();
winner = getWinner(cs);
sc.trickWon(winner);
}
sc.hasGameEnded();
if (isNewGame)
myHand = new Hand(dataReceived);
else
myHand.receiveAnotherHand(dataReceived);
gameEventData = "WSOHT";
GameEvent ge = new GameEvent(this, gameEventData);
processGameEvent(ge);
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException ie)
{}
}
if (!gameStopped)
{
String s = "";
for (int i = 0; i < 4; i++)
s += String.valueOf(cs[i].getCard() + "@");
moved = getRestOfCards() + "%" + String.valueOf(taken) + "%*@" + s + "~" + String.valueOf(getFirstCard().getCard()) + "~;" + scoreId + ";/";
}
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
}
else
{
// Not a new trick and so the cards already played should be displayed.
cardOne = false;
if (firstGame == true)
{
myHand = new Hand(dataReceived);
play = getPlayedCards();
currentFirstCard = play[0];
gameEventData = "HCT";
GameEvent ge = new GameEvent(this, gameEventData);
processGameEvent(ge);
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException ie)
{}
}
if (!gameStopped)
{
lastFirstCard = currentFirstCard;
if (play.length < 2)
moved = (getRestOfCards().trim() + sendCurrentRound(play) + String.valueOf(taken) + "%*;" + scoreId + ";/");
else
moved = "Z" + sendCurrentRound(play) + String.valueOf(taken) + "%*;" + scoreId + ";/";
}
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
else
{
Card[] cs = getLastRound();
if (scoreId != getScoreId())
{
scoreId = getScoreId();
lastFirstCard = getFirstCard();
winner = getWinner(cs);
sc.trickWon(winner);
}
sc.hasGameEnded();
if (isNewGame)
myHand = new Hand(dataReceived);
else
myHand.receiveAnotherHand(dataReceived);
play = getPlayedCards();
currentFirstCard = play[0];
gameEventData = "WSOHCT";
GameEvent ge = new GameEvent(this, gameEventData);
processGameEvent(ge);
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException ie)
{}
}
if (!gameStopped)
{
if (play.length < 2)
{
String s = "";
for (int i = 0; i < 4; i++)
s += String.valueOf(cs[i].getCard() + "@");
moved = getRestOfCards().trim() + sendCurrentRound(play) + String.valueOf(taken) + "%*@" + s + "~" + String.valueOf(getFirstCard().getCard()) + "~;" + scoreId + ";/";
}
else
{
String s = "";
for (int i = 0; i < 4; i++)
s += String.valueOf(cs[i].getCard() + "@");
moved = "Z" + sendCurrentRound(play) + String.valueOf(taken) + "%*@" + s + "~" + String.valueOf(getFirstCard().getCard()) + "~;" + scoreId + ";/";
}
}
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
}
if (!gameStopped)
{
noOfTricks++;
String st = moved.trim();
firstGame = false;
if (!isNewGame)
st += "F";
tokenRing.sendMove(st);
}
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
else if (dataReceived[0] == 'Z')
{
if (firstGame == true)
{
cardOne = false;
play = getPlayedCards();
currentFirstCard = play[0];
gameEventData = "HCT";
GameEvent ge = new GameEvent(this, gameEventData);
firstGame = false;
lastFirstCard = currentFirstCard;
processGameEvent(ge);
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException ie)
{}
}
}
else
{
Card[] cs = getLastRound();
if (scoreId != getScoreId())
{
scoreId = getScoreId();
lastFirstCard = getFirstCard();
winner = getWinner(cs);
sc.trickWon(winner);
sc.hasGameEnded();
}
play = getPlayedCards();
currentFirstCard = play[0];
gameEventData = "WSOHCT";
GameEvent ge = new GameEvent(this, gameEventData);
processGameEvent(ge);
synchronized(lock)
{
try
{
lock.wait();
}
catch (InterruptedException ie)
{}
}
if (!gameStopped)
firstGame = false;
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
if (!gameStopped)
{
noOfTricks++;
take = new Card(taken);
processEndOfTrick();
}
else
{
gameStopped = false;
synchronized(lock)
{
lock.notify();
}
}
}
else if (dataReceived[0] == 'A')
{
cardOne = true;
Card[] cs = getLastRound();
if (scoreId != getScoreId())
{
scoreId = getScoreId();
lastFirstCard = getFirstCard();
winner = getWinner(cs);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -