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

📄 card.java

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

package com.symbian.devnet.whist.game;

/**
 * A class representing a playing card.  This class allows the mapping 
 * of a card represnted as a number between 0 and 51 to a suit and a rank.
 * @author Symbian Devnet
 */

public class Card 
{
	/** Number of suits available to play. */
	private static final int NO_OF_SUITS = 4;
	/** 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 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;
	/** A variable representing the suit of the Card object. */
	private int suit;
	/** 
	 * A varible representing the rank of the Card object, 
	 * for example 4 or KING.
	 */
	private int rank;
	
	/**
	 * Constructor for the Card class.  This takes an integer between 0 
	 * and 51, which uniquely represent a card, and maps that number to 
	 * a suit and rank.
	 * @param num	the number (0-51) of the card.
	 */
	Card(int num)
	{
		createCard(num);
	}
		
	/**
	 * This takes the card number (0-51) and maps it to a card rank and suit.
	 * For example 30 would map to Diamond 9.
	 * @param num	the number (0-51) of the card.
	 */
	private void createCard(int num)
	{
		int t = num % NO_OF_SUITS;
		switch (t)
		{
			case 0: 	suit = HEART; 	break;
			case 1: 	suit = CLUB; 	break;
			case 2: 	suit = DIAMOND; break;
			default: 	suit = SPADE; 	break;
		}
		t = num / NO_OF_SUITS;
		switch (t)
		{
			case 0: 	rank = 2; 		break;
			case 1: 	rank = 3;	 	break;
			case 2: 	rank = 4;	 	break;
			case 3: 	rank = 5;	 	break;
			case 4: 	rank = 6; 		break;
			case 5: 	rank = 7; 		break;
			case 6: 	rank = 8; 		break;
			case 7: 	rank = 9; 		break;
			case 8: 	rank = 10; 		break;
			case 9: 	rank = JACK; 	break;
			case 10: 	rank = QUEEN; 	break;
			case 11: 	rank = KING;	break;
			default: 	rank = ACE;		break;
		}
	}
	
	/**
	 * Returns the unique identiy number for the Card object it is called on.
	 * @return the unique card identy number (0 - 51)
	 */
	public int getCard()
	{
		return (rank - 2) * 4 + suit;
	}
	
	/**
	 * Calculates the identity number when given the rank and suit 
	 * of a card.
	 * @param suit	the suit of a card, for example HEART
	 * @param rank	the rank of the card, for example 7 or KING
	 * @return 		the unique card number representing the card with the given 
	 * 				parameters.
	 */
	public static int getCard(int suit, int rank)
	{
		return (rank - 2) * 4 + suit;
	}
		
	/**
	 * Returns the suit of the card.
	 * @return the card suit, for example CLUB
	 */
	public int getSuit()
	{
		return suit;
	}
	
	/**
	 * Returns the suit of the Card object, for example DIAMOND.
	 * @param c	a Card object
	 * @return one of four strings representing the suit of the Card.
	 */
	public static String getSuit(Card c)
	{
		String s;
		switch (c.suit)
		{
			case 0: 	s = "HEART"; 	break;
			case 1: 	s = "CLUB"; 	break;
			case 2: 	s = "DIAMOND"; 	break;
			default: 	s = "SPADE"; 	break;
		}

		return s;
	}
	
	/**
	 * Allows a Card object to be printed as shown below (where c is 26):
	 * Diamond: 8
	 * @param c	the Card object to be printed
	 */
	public static void print(Card c)
	{
		String s, r;
		switch (c.suit)
		{
			case 0: s = "Heart"; break;
			case 1: s = "Club"; break;
			case 2: s = "Diamond"; break;
			default: s = "Spade"; break;
		}
		switch (c.rank)
		{
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			case 10: r = String.valueOf(c.rank); break;
			case 11: r = "Jack"; break;
			case 12: r = "Queen"; break;
			case 13: r = "King"; break;
			default: r = "Ace"; break;
		}
	}	
	
	/**
	 * Returns the rank of the Card (an int between 2 and 14).
	 * @return	the card rank
	 */
	public int getRank()
	{
		return rank;
	}
}

		
	

⌨️ 快捷键说明

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