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

📄 game.java

📁 手机无线网络纸牌游戏源代码。非常适合学习使用
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				sc.trickWon(winner);
			}	
			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() + "@");
				noOfTricks++;				
				moved = "B%" +  String.valueOf(taken) + "%*@" + s + "~" + String.valueOf(getFirstCard().getCard()) + "~;" + scoreId + ";/";
					
				String st = moved.trim();
				
				tokenRing.sendMove(st);	
			}	
			else
			{
				gameStopped = false;
				synchronized(lock)
				{
					lock.notify();				
				}	
			}
		}
		else if (dataReceived[0] == 'B')
		{				
			Card[] cs = getLastRound();	
			cardOne = false;
			if (scoreId != getScoreId())
			{
				scoreId = getScoreId();
				lastFirstCard = getFirstCard();	
				winner = getWinner(cs);	
				sc.trickWon(winner);
			}
			else
				winner = getWinner(cs);				
			play = getPlayedCards();	
			currentFirstCard = play[0];							
			gameEventData = "WSOCHT";
			GameEvent ge = new GameEvent(this, gameEventData);								
			processGameEvent(ge);
			synchronized(lock)
			{
				try
				{
					lock.wait();
				}
				catch (InterruptedException ie)
				{}
			}
	
			if (!gameStopped)
			{	
				String s = new String("");
				for (int i = 0; i < 4; i++)	
					s += String.valueOf(cs[i].getCard() + "@");
		
				noOfTricks++;	
				if (play.length == 3)
				{
					take = new Card(taken);
					processEndOfTrick();							
				}
				else 	
				{
					moved = "B" + sendCurrentRound(play) + String.valueOf(taken) + "%*@" + s + "~" + String.valueOf(getFirstCard().getCard()) + "~;" + scoreId + ";/";			
					String st = moved.trim();
					tokenRing.sendMove(st);	
				}
			}			
			else
			{
				gameStopped = false;
				synchronized(lock)
				{
					lock.notify();				
				}	
			}
		}
		else if (dataReceived[0] == 'E')
		{
			String a = new String(dataReceived);
			Card[] cs = getLastRound();	
			lastFirstCard = getFirstCard();	
			winner = getWinner(cs);	
			sc.trickWon(winner);
			sc.hasGameEnded();
			gameEventData = "OWSE";
			GameEvent ge = new GameEvent(this, gameEventData);								
			processGameEvent(ge);
			synchronized(lock)
			{
				try
				{
					lock.wait();
				}
				catch (InterruptedException ie)
				{}
			}
			if (!gameStopped)
			{
				String s = "E";
				
				if (dataReceived[1] == '1')
					s += "2@";
				else if (dataReceived[1] == '2')
					s += "3@";
				else // dataReceived[1] == '3'
					System.exit(1);
								
				for (int i = 0; i < 4; i++)	
					s += ((String.valueOf(cs[i].getCard()) + "@"));	
				s += ("~" + String.valueOf(getFirstCard().getCard()) + "~;" + scoreId + ";/");

				tokenRing.sendMove(s);	
			}
			else
			{
				gameStopped = false;
				synchronized(lock)
				{
					lock.notify();				
				}	
			}
		}	
		else if (dataReceived[0] == 'Q')
		{	
			tokenRing.resetNos();	
			firstGame = true;
			gameEventData = "Q";
			GameEvent ge = new GameEvent(this, gameEventData);								
			processGameEvent(ge);
		}							
	}
	
	/** 
	 * Traverses the dataReceived array until the ';' character is found.  The 
	 * proceeding int is then extracted and returned.
	 * @return the idenfitication number for the score
	 */
	private int getScoreId()
	{
		int count = 0;
		int id;
		Integer temp;
		String s = "";
		while (dataReceived[count] != ';')
			count ++;
		count++;
		while (dataReceived[count] != ';')
		s += dataReceived[count++];
		temp = new Integer(s);	
		id = temp.intValue();
		return id;
	}	
	
	/** 
	 * Called when a trick has finished (i.e. four cards have been played).  It 
	 * determines if a new hand must be dealt (13 tricks have been played), 
	 * gets the winner of the trick, determines if the game has been won, and 
	 * sends the relevant information on to the next player (usually the winner
	 * of the trick).
	 */
	private void processEndOfTrick()
	{			
		int pNo = tokenRing.getPlayerNo();		
		played = orderCards(pNo);	
		lastFirstCard = currentFirstCard;	
		winner = getWinner(played);	
		sc.trickWon(winner);
			
		boolean endGame = sc.hasGameEnded();
		if 	(endGame)
		{		
			gameEventData = "WF";
			GameEvent ge = new GameEvent(this, gameEventData);								
			processGameEvent(ge);	
			synchronized(lock)
			{
				try
				{
					lock.wait();
				}
				catch (InterruptedException ie)
				{}	
			}
			if (!gameStopped)	
				nextGame();
			else
			{
				gameStopped = false;
				synchronized(lock)
				{
					lock.notify();				
				}	
			}	
		}								
		else if (noOfTricks == (MAX_NO_OF_TRICKS + 1))
		{	
			isNewGame = false;			
			myHand.dealAnotherHand();
			moved = deal(myHand.getHands());
			noOfTricks = 1;
			String s = "@";
			for (int i = 0; i < 4; i++)	
				s += String.valueOf(played[i].getCard() + "@");	
			s += "~" + currentFirstCard.getCard() + "~;" + ++scoreId + ";/F";
			String st = moved.trim() + s.trim();	
			if (!gameStopped)
				tokenRing.sendMove(st);
			else
			{
				gameStopped = false;
				synchronized(lock)
				{
					lock.notify();				
				}	
			}		
		}
		else
		{	
			String s = new String("@");
			for (int i = 0; i < 4; i++)	
				s += String.valueOf(played[i].getCard() + "@");		
			if ((winner + 1) == tokenRing.getPlayerNo())
			{		
				dataReceived = s.toCharArray();
				cardOne = true;								
				
				gameEventData = "WSOHT";
				GameEvent ge = new GameEvent(this, gameEventData);								
				processGameEvent(ge);
				synchronized(lock)
				{
					try
					{
						lock.wait();
					}
					catch (InterruptedException ie)
					{}
				}		
				if (!gameStopped)
				{			
					if (!firstGame)	
						moved = "B%" + String.valueOf(taken) + "%*"+ s + "~" + String.valueOf(currentFirstCard.getCard()) + "~;" + ++scoreId + ";/F";
					else
					{
						moved = "B%" + String.valueOf(taken) + "%*"+ s + "~" + String.valueOf(played[1].getCard()) + "~;" + ++scoreId + ";/F";
						firstGame = false;
					}
					noOfTricks++;
					String st = moved.trim();
					tokenRing.sendMove(st);	
				}	
				else
				{
					gameStopped = false;
					synchronized(lock)
					{
						lock.notify();				
					}
				}
			}				
					
			else	
			{										
				moved =  "A" + s + "~" + String.valueOf(currentFirstCard.getCard()) + "~;" + ++scoreId + ";/F";					
				String st = moved.trim();		
					tokenRing.sendMove(st, winner);
			}
		}
	}

	/**
	 * Called at the end of a game once the user has decided whether or not to 
	 * play another game.  If another game is to be played the relevant 
	 * information is sent to the next dealer.  If another game has not been 
	 * requested the scoring is sent to the other players and the application
	 * exits.
	 * @param g "Y" or "y" to start a new game, "N" or "n" to exit.
	 */
	private void nextGame() 
	{		
		if (startNewGame)
		{
			isNewGame = true;
			String s = "X@";
			for (int i = 0; i < 4; i++)	
				s += String.valueOf(played[i].getCard() + "@");	
			s += "~" + currentFirstCard.getCard() + "~;" + ++scoreId + ";/T";				
			if (((sc.getGameNo()) % 4) == tokenRing.getPlayerNo())
			{			
				dataReceived = s.toCharArray();
				processData();
			}
			else	
			{
				if (tokenRing.getPlayerNo() < 4)
					tokenRing.sendMove(s,((sc.getGameNo()-1) % 4));				
				else 
					tokenRing.sendMove(s, 0);	
			}	
		}
		else 
		{
			String s = "E1@";
			for (int i = 0; i < 4; i++)	
				s += String.valueOf(played[i].getCard() + "@");	
			s += "~" + currentFirstCard.getCard() + "~/";
				tokenRing.sendMove(s);
				System.exit(1);	
		}	
	}
	
	/** 
	 * Takes the four cards played in the trick and re-orders them according
	 * to the current player number.  The cards are originally in the order 
	 * played (for example player3, player4, player1, player2) and are 
	 * returned in the order player1, player2, player3, player4.
	 * @param the player number
	 * @return an array of sorted cards
	 */
	private Card[] orderCards(int pNo) 
	{			
		played = new Card[4];		
		switch (pNo)
		{
			case 1:	{							
				played[0] = take;
				for (int i = 0; i < 3; i++)
					played[i+1] = play[i];	
			} break;
			case 2:	{									
				played[0] = play[2];
				played[1] = take;
				played[2] = play[0];
				played[3] = play[1];
			} break;
			case 3:	{								
				played[0] = play[1];
				played[1] = play[2];
				played[2] = take;
				played[3] = play[0];
			} break;
			default: {								
				for (int i = 0; i < 3; i++)
					played[i] = play[i];
				played[3] = take;
			} break;
		}		
		return played;
	}
	
	/** 
	 * Takes the double array of cards 
	 * (as dealt in the {@link com.symbian.devnet.whist.game.Hand#createHand() createHand()}
	 * method) and 
	 * extracts them into a string which can them be sent onto the other
	 * players.  The cards are delimited by the "?" symbol.  The trump suit
	 * is added to the end of the String and is delimited by "#" and "#%".
	 * @param  	a double array containing the hands of cards for the remaining
	 * 			players.
	 * @return 	a String containing the hands of the other players and the
	 *			trump card.
	 */
	private String deal(int[][] hands)
	{
		int[][] h = hands; 	
		String s = "YN";
		for (int i = 1; i < 4; i++)
			for (int j = 0; j < 13; j++)
				s += String.valueOf(h[i][j]) + "?";
		s += "#" + h[0][12] + "#%";
		return s;
	}
	
	/**
	 * Extracts the hands for the remaining players from the data received in
	 * a datagram and copies them into a String which can be sent to the next 
	 * player.
	 * @return A String containing the hands for the rest of the players
	 */
	private String getRestOfCards()
	{	
		String temp = "YB";
		int count = 1;
		for (int i = 0; i < 13; i++)
		{	
			while(dataReceived[count] != '?')
				count++;
			count++;
		}			
		while (dataReceived[count] != '%')
		{
			temp += dataReceived[count++];
		}						
			
		return temp.trim();
	}	

	/**
	 * Takes the chosen card in the form of a suit and value (for example h 9)
	 * and converts them into the unique identification number for that card.
	 * @param s	the suit of the chosen card
	 * @param v the value of the chosen card
	 * @return 	the unique identification number for that card
	 */
	private int getChosenCard(String s, String v) 
	{ 
		int su, vu;
		if (s.equals("H"))
			su = HEART;
		else if (s.equals("C"))
			su = CLUB;
		else if (s.equals("S"))
			su = SPADE;	
		else
			su = DIAMOND;	
			
		if (v.equals("J"))
			vu = JACK;
		else if (v.equals("Q"))
			vu = QUEEN;
		else if (v.equals("K"))
			vu = KING;
		else if (v.equals("A"))
			vu = ACE;
		else
		{
			Integer vle = new Integer(v);			
			vu = vle.intValue();
		}	
		int taken = Card.getCard(su, vu);	
		return taken;
	}	
	
	/**
	 * Extracts the cards which have been played in the current trick 
	 * from the data received and returns them as an array of Cards.
	 * @return an array of the cards played in the current trick.
	 */
	private Card[] getPlayedCards() 
	{
		int ct = 0;
		for (int i = 0; i < dataReceived.length; i++)
		{
			if (dataReceived[i] == '%')
				ct++;
		}	
		
		int count = 0;
		Card[] cards = new Card[(ct-1)];
		while (dataReceived[count] != '%')
			count++;
		count++;

		String t = "";
		String temp;
		Integer i;
		int j = 0;		
		while (dataReceived[count] != '*')
		{				

⌨️ 快捷键说明

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