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

📄 hand.java

📁 手机无线网络纸牌游戏源代码。非常适合学习使用
💻 JAVA
字号:
// Hand.java
//
// Copyright (c) 2000-2001 Symbian Ltd. All rights reserved

package com.symbian.devnet.whist.game;

import java.util.*;

/**
 * A class implementing a hand of cards.
 * @author Symbian Devnet
 */
public class Hand
{
	/** Represents the number of cards in a deck. */
	private static final int NO_OF_CARDS = 52;
	/** Represents the number of cards in a hand. */
	private static final int NO_OF_CARDS_PER_HAND = 13;
	/** Represents the number of players in the game. */
	private static final int NO_OF_PLAYERS = 4;
	/** Represents a vector of the players in the game. */
	private Vector players;
	/** 
	 * A double array with the dimensions [4][13] which hold the cards in each 
	 * hand as they are being dealt.
	 */
	private int[][] hands;
	/** A Vector containing the cards in the hand. */
	private Vector cardsInHand;
	/** The trump suit. */	
	private int trump;

	/** 
	 * Constructor which initialises the Vector cardsInHand to the correct size, 
	 * which is determined by the number of cards in the (NO_OF_CARDS_PER_HAND).
	 * This constructor is called  when a new hand of cards is required, for 
	 * example whem starting a new game.
	 * <P>
	 * A deck is created, shuffled, and dealt and one hand is stored for that 
	 * player to use.
	 */
	Hand()
	{
		cardsInHand = new Vector(NO_OF_CARDS_PER_HAND);
		createDeck();
		createHand();	
	}
	
	/** 
	 * Constructor which initialises the cardsInHand vector and fills it
	 * with Cards from dataReceived.
	 * @param the data received in the datagram
	 */
	Hand(char[] dataReceived)
	{
		cardsInHand = new Vector(NO_OF_CARDS_PER_HAND);
		createHand(dataReceived);			
	}
	
	/**
	 * Takes the first hand from that created by the constructor and
	 * places the cards in the vector cardsInHand.  It also sets the 
	 * trump suit.
	 */
	private void createHand()
	{
		for (int i = 0; i < NO_OF_CARDS_PER_HAND; i++)
		{
			Card c = new Card(hands[0][i]);
			cardsInHand.addElement(c); 
		}			
		trump = ((Card)(cardsInHand.elementAt(12))).getSuit();	
	}
	
	/**
	 * Extracts the cards received in the datagram and fills the
	 * cardsInHand vector with them. It also sets the trump suit for 
	 * the game.
	 * @param dataReceived the data received in the datagram
	 */
	private void createHand(char[] dataReceived) 
	{	
		String t = "";
		String temp;
		Integer i;
		int cards = 0;
		int count = 2;
		int j = 0;	
		while (cards < NO_OF_CARDS_PER_HAND)
		{				
			t = "";				
			while (dataReceived[count] != '?')			
				t+= String.valueOf(dataReceived[count++]);									
			count++;
			i = new Integer(t);	
			Card c = new Card(i.intValue());	
			cardsInHand.addElement(c);	
		cards++;
		}		

		int r = 0;
		while (dataReceived[r] != '#')
			r++;		
		r++;
		
		t = "";
		while (dataReceived[r] != '#')
		{
			t+= String.valueOf(dataReceived[r++]);
		}			
		count++;
		i = new Integer(t);		
		Card x = new Card(i.intValue());
		trump = x.getSuit();	
	}
	
	/**
	 * Creates a deck of cards and calls the shuffle method.  It also
	 * creates an instance of the Random class.
	 */ 
	private void createDeck()
	{
		Integer in;
		players = new Vector(4);
		for (int i = 0; i < NO_OF_PLAYERS; i++)
		{	in = new Integer(i);
			players.addElement(in);
		}
		hands = new int[NO_OF_PLAYERS][NO_OF_CARDS_PER_HAND];
		Random r = new Random(); 
		shuffle(r);
	}	
	
	/**
	 * Simulates shuffling and dealing the deck of cards.  This method
	 * achieves this by randomly selecting a player and distributing the
	 * next card in the deck to them.  Once a player has 13 cards, they are
	 * removed from the vector of players and cannot be assigned more cards.
	 * <P>
	 * Note: it was thought to be easier to 'shuffle' 4 players than 52 cards.
	 * @param r an instance of the Random class
	 */
	private void shuffle(Random r)
	{
		int player;
		Integer iPlayer;
		Integer iV;
		int p = 0;	
		int rNo;
		int p1 = 0;
		int p2 = 0;
		int p3 = 0;
		int p4 = 0;
		for (int i = 0; i < (NO_OF_CARDS); i++)
		{	
			rNo = r.nextInt();
			if (rNo < 0)
				rNo= rNo * -1;
			player = rNo%players.capacity();							
			iPlayer = new Integer(player);	
					
			iV = new Integer(String.valueOf(players.elementAt(player)));

			switch (iV.intValue())
			{
				case 0: {hands[iV.intValue()][p1] = i; p1++; p = 1;} break;
				case 1: {hands[iV.intValue()][p2] = i; p2++; p = 2;} break;
				case 2: {hands[iV.intValue()][p3] = i; p3++; p = 3;} break;
				case 3: {hands[iV.intValue()][p4] = i; p4++; p = 4;} break;
				default: System.out.println("Unable to shuffle cards"); break;
			}
		
			if ((p == 1) && (p1 > 12))
			{					
				players.removeElement(players.elementAt(player));
				players.trimToSize();
			}
			else if ((p == 2) && (p2 > 12))
			{		
				players.removeElement(players.elementAt(player));
				players.trimToSize();
			}
			else if ((p == 3) && (p3 > 12))
			{		
				players.removeElement(players.elementAt(player));
				players.trimToSize();				
			}
			else if ((p == 4) && (p4 > 12))
			{		
				players.removeElement(players.elementAt(player));
				players.trimToSize();				
			}												
		}		
	}
	
	/** 
	 * Creates a deck of cards deals them.
	 */
	public void dealAnotherHand()
	{	
		createDeck();
		for (int i = 0; i < NO_OF_CARDS_PER_HAND; i++)
		{
			Card c = new Card(hands[0][i]);
			cardsInHand.addElement(c); 
		}
	}
	
	/** 
	 * Fills the cardsInHand vector with Cards from data received in the 
	 * datagram.
	 * @param the data received in the datagram
	 */
	public void receiveAnotherHand(char[] dataReceived)
	{
		String t = "";
		String temp;
		Integer i;
		int cards = 0;
		int count = 2;
		int j = 0;	
		while (cards < NO_OF_CARDS_PER_HAND)
		{				
			t = "";				
			while (dataReceived[count] != '?')			
				t+= String.valueOf(dataReceived[count++]);									
			count++;
			i = new Integer(t);	
			Card c = new Card(i.intValue());	
			cardsInHand.addElement(c);	
		cards++;
		}	
		
	}
	
	/**
	 * Returns the double array of dealt hands.
	 * @return a double array containing the 4 hands of cards
	 */
	public int[][] getHands()
	{
		return hands;
	}
	
	/** 
	 * Returns the trump card. 
	 * @return the trump card
	 */
	public int getTrump()
	{
		return trump;
	}
	
	/**
	 * Ensures that the card selected for play is 'legal' by ensuring that 
	 * the suit is the same as that of the trump card or the first card played
 	 * in the trick.  If the hand does not contain a card of either suit any
	 * card in the hand may be played.
	 * @param t			the card selected for play
	 * @param firstCard	the first card played in the trick
	 * @return true if the card can be played
	 */
	public boolean validateMove(int t, Card firstCard)
	{		
		Card taken = new Card(t);

		boolean move = false;

			
		if ((taken.getSuit() == firstCard.getSuit()) || (taken.getSuit() == trump)) 
		{							
			move = true;
		}
		else 
		{			
			boolean canGo = false;
			for (int i = 0; i < cardsInHand.size(); i++)
				if ((((Card)(cardsInHand.elementAt(i))).getSuit()) == firstCard.getSuit())
					canGo = true;				
			if (!canGo)	
			{			
				move = true;
			}
			else
				move = false;
		}			
		return move;
	}		

	/** 
	 * Finds the selected card in the vector and removes it.
	 * @param c the selected card
	 */
	private void makeTurn(Card c)
	{	
		int index = 14;		
		for (int i = 0; i < cardsInHand.size(); i++)
		{
			if ((((Card)(cardsInHand.elementAt(i))).getRank() == (c.getRank())) && (((Card)(cardsInHand.elementAt(i))).getSuit() == c.getSuit()))
				index = i;
		}	
		cardsInHand.removeElementAt(index);
	}	
	
	/**
	 * Removes the selected card from the vector.
	 * @param the selected card to be removed
	 */	
	public void removeCard(Card c) 
	{	
		makeTurn(c);
	}	
	
	/**
	 * Returns the current hand of cards.
	 * @return the current hand
	 */
	public Card[] getHand()
	{
		Card[] c = new Card[cardsInHand.size()];
		for (int i = 0; i < cardsInHand.size(); i++)
			c[i] = ((Card)(cardsInHand.elementAt(i)));
		return c;
	}		
}					

⌨️ 快捷键说明

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