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

📄 carddeck.java

📁 24分扑克牌游戏
💻 JAVA
字号:
/*
 * @(#)CardDeck.java Version 1.0 98/03/12
 * 
 * Copyright (c) 1998 by Huahai Yang
 * 
 * Use at your own risk. I do not guarantee the fitness of this 
 * software for any purpose, and I do not accept responsibility for 
 * any damage you do to yourself or others by using this software.
 * This file may be distributed freely, provided its contents 
 * are not tampered with in any way.
 *
 */

import java.awt.*;
import java.util.Vector;
import java.lang.Math;

/**
 * A card deck, holds 52 cards, deal them and provides their solutions
 * Runs as a thread to caculate solution.
 */
public class CardDeck extends DraggingImage implements Runnable
{
   Thread solutionThread;
   SynchronizedVector [] solutions;
      
   Vector deck;
   
   int cardPointer;
   
   boolean clickable;
   DraggingArea container;
   
   public CardDeck(int x, int y, Image image, DraggingArea container) 
   {
      super(x, y, image, container);
      draggable = false;
      clickable = true;
      
      deck = new Vector();
      for(int i = 0; i < 52; i++)
      {
         deck.addElement(new Card(i, container.cardImages[i], container));
      } // for
      shuffle();
      
      solutionThread = new Thread(this, "Solution");
      solutionThread.setPriority(Thread.MIN_PRIORITY); 
      solutionThread.start();
	} // 4 param constructor
	
	public void setThreadPriority(int priority)
	{
	   if(solutionThread != null && solutionThread.isAlive())
	   {
	      solutionThread.setPriority(priority);
	   } //if   
	} //setThreadPriority
	
	/**
	 * Stop solution thread
	 */
	public void stop()
	{
      if (solutionThread != null)
      {
         solutionThread.stop();
         solutionThread = null;
      } //if   
	} //stop
	
   /**
    * find solutions for all of the 13 deals
    */
   public void run()
   {
      int pointer = 0;
      solutions = new SynchronizedVector [13];
      
      // initiate solutions
      for(int i = 0; i < 13; i++)
      {
         solutions[i] = new SynchronizedVector(); 
      } // for
      
      for(int i = 0; i < 13; i++)
      {
         // search solution
         Solution sol = new Solution(
            ( (Card)deck.elementAt(pointer++) ).getCardValue(),
            ( (Card)deck.elementAt(pointer++) ).getCardValue(),
            ( (Card)deck.elementAt(pointer++) ).getCardValue(),
            ( (Card)deck.elementAt(pointer++) ).getCardValue() );
         
         // put solution sting in to a synchronized place, if the 
         // solution is not yet available, its consumers
         // have to wait
         solutions[i].put( sol.getSolution() );
      } // for      
   } // run   
	
   public void enableClick()
   {
      clickable = true;
   } // enableClick
   
   public void disableClick()
   {
      clickable = false;
   } // disableClick
   
   public boolean isClickable()
   {
      return clickable;
   } // isClickable
   
   /**
    * return a deal of four cards, 
    * these four card are selected from head of the card deck
    */
   public Card [] deal()
   {
      Card [] cards = new Card [4];
      
      if(cardPointer == 52) return null;
      
      for(int i = 0; i < 4; i++)
      {
         cards[i] = (Card)deck.elementAt(cardPointer);
         cardPointer++;
         
      } // for
      return cards ;
   } // deal 
   
   /**
    * get number of current deal
    */
   public int currentDealNumber()
   {
      int deal = cardPointer / 4 - 1;
      if(deal < 0) deal = 0;
      return deal;
   } // currentDealNumber   
   
   /**
    * get current deal's solution
    */
   public Vector currentSolution()
   {
      // if solution not yet available, will wait here
      return ( solutions[currentDealNumber()].get() );
   } // currentSolution  
   
   /**
    * rearrange order of cards and reset cardPointer to zero
    */
   public void shuffle()
   {
      Vector tmpDeck = new Vector();
      int cardNumber = deck.size();
      int pick;
      
      for(int i = cardNumber - 1; i >= 0; i--)
      {
         pick = (int)( Math.random() * i );
         tmpDeck.addElement( deck.elementAt(pick) );
         deck.removeElementAt(pick);
      } // for
      
      deck = tmpDeck;
      cardPointer = 0;
   } // shuffle  
   
}// CardDeck

⌨️ 快捷键说明

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