⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.java

📁 手机无线网络纸牌游戏源代码。非常适合学习使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			t = "";				
			while (dataReceived[count] != '%')					
				t += String.valueOf(dataReceived[count++]);													
			count++;			
			i = new Integer(t);	
			Card c = new Card(i.intValue());	
			cards[j++] = c;		
		}	
		return cards;
	}	
	
	/**
	 * Extracts the cards which have been played in the previous trick 
	 * from the data received and returns them as an array of Cards.
	 * @return an array of the cards played in the previous trick.
	 */
	private Card[] getLastRound()
	{		
		int count = 0;
		Card[] cards = new Card[4];
		while (dataReceived[count] != '@')
			count++;
		count++;			
		String t = "";
		String temp;
		Integer i;
		int j = 0;					   
		for (int p = 0; p < 4; p++)
		{								
			t = "";				
			while (dataReceived[count] != '@')			
				t += String.valueOf(dataReceived[count++]);									
			count++;
			i = new Integer(t);	
			Card c = new Card(i.intValue());	
			cards[j++] = c;					
		}		
		return cards;
	}	
	
	/**
	 * Extracts the first card played in the previous trick from the data
	 * received.
	 * @return the first card played in the previous trick
	 */
	private Card getFirstCard()
	{
		Card fc;
		int ctc = 0;
		while (dataReceived[ctc] != '~')
			ctc++;
		ctc++;		
		String tr = "";
		Integer in;
	
		while (dataReceived[ctc] != '~')		
			tr += String.valueOf(dataReceived[ctc++]);	
		in = new Integer(tr);
		fc = new Card(in.intValue());
		return fc;
	}	
	
	/**
	 * Retrieves the Trum suit and the cards played in the current round 
	 * and returns them as a String.  The cards are delimited by the "*" 
	 * symbol.
	 * @param 	the array of cards currently in play
	 * @return 	a String containing the trump suit and the cards currently 
	 * 			in play
	 */			
	private String displayCurrentCards(Card[] cards)
	{
		String temp = "";
		Card c = new Card(myHand.getTrump());
		temp = String.valueOf(c.getCard()) + "*";;		
		for (int i = 0; i < cards.length; i++)
			temp += String.valueOf(cards[i].getCard()) + ":";
		temp += "*";		
		currentFirstCard = cards[0];	
		return temp;
	}	
	
	/** 
	 * Takes the cards currently in play and inserts them into a String using
	 * the "%" to delimit the individual cards.
	 * @param 	the array of Cards currently in play
	 * @return 	a String containing those cards
	 */
	private String sendCurrentRound(Card[] cards)
	{
		String s = "%";
		for (int i = 0; i < cards.length; i++)
			s += String.valueOf(cards[i].getCard()) + "%";
		return s;
	}			
	
	/**
	 * Determines the trick winner.
	 * @param 	the array of cards played in the trick.
	 * @return 	the winner opf the trick (1, 2, 3, or 4)
	 */
	private int getWinner(Card[] played)
	{	
		winner = 4;
		for (int i = 0; i < 4; i++)
		{		
			if ((played[i].getSuit() == lastFirstCard.getSuit()) && (played[i].getRank() == lastFirstCard.getRank()))
				winner = i;							
		}	
		boolean setWinner = false;	
		Card trump = new Card(myHand.getTrump());		
		for (int i = 0; i < 4; i++)
		{					
			if ((played[i].getSuit() == trump.getSuit()) && (!setWinner))
			{								
				winner = i;				
				setWinner = true;			
			}
			else if ((played[i].getSuit() == trump.getSuit()) && (setWinner))
			{									
				if ((played[winner].getRank()) < (played[i].getRank()))
				{			
					winner = i;			
				}	
			}
		}
		if (!setWinner)
		{							
			for (int i = 0; i < 4; i++)
			{			
				if (played[i].getSuit() == lastFirstCard.getSuit())
					if (played[winner].getRank() < played[i].getRank())
						winner = i;					
			}
		}			
		return winner;
	}	

	/** 
	 * @param notification The event to send to all listeners
	 * Notify listeners of a new game event
	 */
	private void processGameEvent(GameEvent notification) 
	{	
		for (Enumeration en = listeners.elements(); en.hasMoreElements();)
		{
			GameEventListener listener = (GameEventListener)en.nextElement();
			listener.gameEventReceived(notification);
		}
	}
	
	/** 
	 * Allows owner classes to retrieve the trump suit of the game.
	 */
	public String getTrump()
	{
		int t = myHand.getTrump();
		String s;
		switch (t)
		{
			case HEART:		s = "Heart"; break;
			case CLUB:	 	s = "Club"; break;
			case DIAMOND: 	s = "Diamond"; break;
			default:		s = "Spade"; break;
		}
		return s;
	}
	
	/** 
	 * Retrieves the hand of cards from the Hand class and sorts them by suit 
	 * (Hearts, Clubs, Diamonds, Spades).  The number 9999 is used to delimit 
	 * the suits, for example the hand:
	 * H: 2 5 7 Queen
	 * C: 3 8
	 * D: 6 8 9 Ace
	 * S: 8 Jack
	 * would be returned as:
	 * 2 5 7 12 9999 3 8 9999 6 8 9 14 9999 8 11 9999 
	 * @return 	an array of the cards in the hand
	 */
	public int[] getHand()
	{
		Card[] c = myHand.getHand();
		int[] temp = new int[c.length + 4];
		int suit = 0;
		int count = 0;
		for (int i = 0; i < 4; i++) // cycle through suits
		{			
			for (int j = 0; j < c.length; j++) // cycle through cards
			{			
				if (c[j].getSuit() == suit)
					temp[count++] = c[j].getRank();
			}
			suit++;
			temp[count++] = 9999;		
		}		
		return temp;
	} 	
	
	/**
	 * Retrieves the previous round (using the 
	 * {@link com.symbian.devnet.whist.game.Game#getPreviousRound() getPreviousRound()} method).  The 
	 * card identification numbers which are returned are used to retrieve the
	 * suit and rank of the cards.  These are returned as Strings.
	 * @return 	a String array containing the four cards played in the previous
	 * 			round, for example H4 H9 H2 HK	 
	 */
	public String[] getPreviousRound()
	{
		Card[] c = getLastRound();
		String[] temp = new String[c.length];

		for (int i = 0; i < 4; i++) 
		{
			temp[i] = "";
			int s = c[i].getSuit();
			switch (s)
			{
				case 0:		temp[i] = "H"; break;
				case 1: 	temp[i] = "C"; break;
				case 2:	 	temp[i] = "D"; break;
				default:	temp[i] = "S"; break;		
			}
			s = c[i].getRank();
			switch (s)
			{
				case 2: 	temp[i] += "2"; 	break;
				case 3: 	temp[i] += "3"; 	break;
				case 4: 	temp[i] += "4"; 	break;
				case 5: 	temp[i] += "5"; 	break;
				case 6: 	temp[i] += "6"; 	break;
				case 7: 	temp[i] += "7"; 	break; 
				case 8: 	temp[i] += "8"; 	break;
				case 9: 	temp[i] += "9"; 	break;
				case 10: 	temp[i] += "10"; 	break;
				case 11:	temp[i] += "J"; 	break;
				case 12:	temp[i] += "Q"; 	break;
				case 13: 	temp[i] += "K"; 	break;
				default:	temp[i] += "A"; 	break;
			}
		}	
		return temp;
	}	
	
	/**
	 * Retrieves the cards in the current round (using the 
	 * {@link com.symbian.devnet.whist.game.Game#getPlayedCards() getPlayedCards()}
	 * method).  
	 * The card identification numbers which are returned are used to retrieve 
	 * the suit and rank of the cards.  These are returned as Strings.
	 * @return 	a String array containing the played cards played in the 
	 * 			previous round, for example H4 H9 where two players have taken 
	 *			a turn	 
	 */
	public String[] getCurrentCards()
	{
		Card[] c = getPlayedCards();
		String[] temp = new String[c.length];
		
		for (int i = 0; i < c.length; i++)
		{
			temp[i] = "";
			int s = c[i].getSuit();
			switch (s)
			{
				case 0:		temp[i] = "H"; break;
				case 1: 	temp[i] = "C"; break;
				case 2:	 	temp[i] = "D"; break;
				default:	temp[i] = "S"; break;		
			}
			s = c[i].getRank();
			switch (s)
			{
				case 2: 	temp[i] += "2"; 	break;
				case 3: 	temp[i] += "3"; 	break;
				case 4: 	temp[i] += "4"; 	break;
				case 5: 	temp[i] += "5"; 	break;
				case 6: 	temp[i] += "6"; 	break;
				case 7: 	temp[i] += "7"; 	break; 
				case 8: 	temp[i] += "8"; 	break;
				case 9: 	temp[i] += "9"; 	break;
				case 10: 	temp[i] += "10"; 	break;
				case 11:	temp[i] += "J"; 	break;
				case 12:	temp[i] += "Q"; 	break;
				case 13: 	temp[i] += "K";		break;
				default:	temp[i] += "A"; 	break;
			}
		}
		return temp;
	}
	
	/** 
	 * Determines if the chosen card is valid.  If this is the first card in the 
	 * trick, any card can be played and the function returns true.  If this is 
	 * not the first card in the trick, the suit is validated against that of 
	 * the first card taken and the trump card.
	 * @param suit	the suit of the selected card
	 * @param rank	the rank of the selected card
	 * @return true if the card can be played, or false if it can not
	 */
	public boolean checkCardIsValid(String suit, String rank)
	{
		int chosen = getChosenCard(suit, rank);
		boolean valid;
		if (!cardOne)		
			valid = myHand.validateMove(chosen, currentFirstCard);								
		else
			valid = true;
		if (valid)
		{		
			taken = chosen;
			Card c = new Card(chosen);
			myHand.removeCard(c);		
		}				
		return valid;	
	}	 	

	/** 
	 * Takes the buffer from the ReceivedTurnEvent and stores it in a locally 
	 * accessible array, using the {@link com.symbian.devnet.whist.tokenRing.ReceivedTurnEvent#getData() 
	 * ReceivedTurnEvent.getData} method. This data is kept in a synchronized array and 
	 * a waiting thread is then notified.
	 * @param rte a ReceivedTurnEvent
	 * @see com.symbian.devnet.whist.tokenRing.ReceivedTurnEvent ReceivedTurnEvent
	 */	
	public void TurnReceived(ReceivedTurnEvent rte)
	{			
		dataReceived = rte.getData();
		try
		{		
			Thread t = new Thread(){
				public void run()
				{
					processData();
					return;
				}	
			};
			t.start();				
		}
		catch(Exception e)
		{			
			System.exit(0);
		}	
	}

	/** 
	 * Register interest in game events
	 * @param listener The event listener
	 */	
	public void addGameEventListener(GameEventListener listener)
	{	
		listeners.addElement(listener);
	}
	
	/** 
	 * Deregister interest in game events
	 * @param listener The event listener
	 */	
	public void removeGameEventListener(GameEventListener listener)
	{
		listeners.removeElement(listener);
	}
	
	/**
	 * Gets the name of the winner of the previous trick.  
	 * <P>
	 * Note: the winner must have been calculated using 
	 * {@link com.symbian.devnet.whist.game.Game#getWinner(Card[] played) getWinner()}
	 * before this method is called.
	 * @return the name of the trick winner
	 */
	public String getTrickWinner()
	{		
		String win = tokenRing.getPlayerName(winner);				
		return win;
	}
	
	/**
	 * Gets the array of names of the players.
	 * @return an array of Strings containing the names of the players
	 */
	public String[] getNames()
	{
		String[] names = new String[MAX_NO_OF_PLAYERS];
		for (int i = 0; i < MAX_NO_OF_PLAYERS; i++)
			names[i] = tokenRing.getPlayerName(i);
		return names;	
	}	
	
	/** 
	 * Gets the current score.
	 * @return the current score
	 */
	public int[] getScore()
	{
		return sc.getScore();
	}
	
	/**
	 * Returns the scores for the players.
	 * @return the scores for the players
	 */
	public int[] getWinner() 
	{
		return sc.getWinner();
	}
	
	/** 
	 * Returns the position of the player in the game.
	 * @return the player number
	 */
	public int getPlayerNumber()
	{
		return tokenRing.getPlayerNo();
	}
	
	/** 
	 * Gets the number of the port the listener is listening on.
	 * @return the port the listener is listening on
	 */
	 
	public String getPort()
	{	
		return tokenRing.getPort();
	}
	
	/**
	 * Calls 
	 * {@link com.symbian.devnet.whist.tokenRing.TokenRing#sendMoveToAll(String s) sendMoveToAll()}
	 * to send a message to all other players that the game has been terminated and also calls
	 * {@link com.symbian.devnet.whist.tokenRing.TokenRing#resetNow() resetNos()} to reset 
	 * various values which will allow a new game to be started.
	 */
	public void quitGame()
	{
		gameStopped = false;
		firstGame = true;
		tokenRing.sendMoveToAll("Q");
		tokenRing.resetNos();
		
	}
	
	/**
	 * Calls 
	 * {@link com.symbian.devnet.whist.tokenRing.TokenRing#initiateSMS() initiateSMS()}
	 * to close down the UDP listener and start an SMS listener.
	 */
	public void setUpSMS()
	{
		tokenRing.initiateSMS();
	}
	
	/**
	 * Starts a new game of Whist.
	 * @param names 	the names of the four players involved in the game.
	 * @param addresses the addresses of the players
	 */
	public void startGame(String[] names, String[] addresses)
	{
		gameStopped = false;
		tokenRing.start(names, addresses);
	}
		
	/** Starts the eventLoop in the TokenRing class. */
	public void run()
	{
		try
		{
			tokenRing.eventLoop();
		}
		catch(Exception e)
		{}
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -