登录站点

用户名

密码

能在机顶盒上运行的纸牌游戏

3已有 136 次阅读  2009-12-16 16:30

package app.card;

import java.awt.*;
import java.awt.Image.*;
import java.text.*;
import java.io.*;
import java.util.Hashtable;
import org.avmsc.dtvm.event.InputEvent;

public class CardControl extends Frame {
    // private Hashtable hashIndex = new Hashtable();
    public static Card allCard[][]=null;
    public Card listHead[]=null;
    public static Image cardBackImageAd=null;
    public static int moveCardCount = 0;
    public int currentList = 0;
    public int time = 0;
    public int score = 0;
    public int currentListNumber = 0;
    public HelpMessage help=null;
    public Card currentCard = null;
    public final static Color backColor = new Color(20,80,20);
    public final static Color borderColor = new Color(200,20,20);
    public final static int FOCUS_WIDTH = 26;//18;
    public final static int FOCUS_HEIGHT = 32;//23;
    public boolean gameStart = false;
    public final static Image focusedIcon = Toolkit.getDefaultToolkit().createImage ("/workdir/MBT_resources/classes/app/card/cards/hand.gif");
    public CardControl()
    {
          setBounds(0, 0, 720, 540);
          initCard();
          setVisible(true);
    }

    public CardControl(int count)
    {
    }

    public void initCard()
    {
           int m=0;//listNumber
           int n=0;//cardNumber
           int k=0;
           int listMax=0;
           int array[] = new int[52];
           time = 0;
           gameStart = false;
           String name = null;
           moveCardCount = 0;
           currentCard = null;
           cardBackImageAd=null;
           allCard = new Card[13][];
           help = new HelpMessage();
           listHead = new Card[9];
           if(CardGame.CARD_DEBUG)
                     System.out.println("m ================="+m);
           String cardFileName = null;
           File file = null;
           for(int i=0;i<9;i++){
           if(i<7){
                m = i+1;
                listMax = m+13;
            }else{
               if(i==7){
               m = 24;
               listMax = 24;
           }
           if(i==8){
                  m=0;
                  listMax = 25;
                  allCard[8] = new Card[listMax];
                   for(n=0;n<24;n++){
                          allCard[8][n] = null;
                    }
                  allCard[8][0] = new Card(0, 0, m, 0, null);
                  allCard[8][0].lastNumber = 0;
                  allCard[8][0].openedCardCount = 0;
                   allCard[8][0].listAllCardCount = 24;
                    break;
           }
   }
   allCard[i] = new Card[listMax];
   for(int c=0;c<listMax;c++)
            allCard[i][c] = null;
   for(int j=0;j<m;j++){
    if(CardGame.CARD_DEBUG)
     System.out.println("j ===================================="+j);
    int tag = 1;
    int x=0;
    int y=0;
    int l=0;
    int number=0;
    while(tag==1){
     x =(int)(Math.random()*4);
                   y =(int)(Math.random()*13)+1;
     number = x*100+y;
                        for(l=0;l<k+1;l++){
                                if(array[l] == number) break;
                        }
                        if(l==k+1) tag=0;
    }
    if(CardGame.CARD_DEBUG) System.out.println("number = "+number+", k="+k);
    array[k] = number;
    k++;
    if(CardGame.CARD_DEBUG)
     System.out.println("m =================");
          if(x==0) name = "card_heart_" + y + ".JPG";
       if(x==1) name = "card_spade_" + y + ".JPG";
          if(x==2) name = "card_club_" + y + ".JPG";
          if(x==3) name = "card_diamond_" + y + ".JPG";
    if(CardGame.CARD_DEBUG) System.out.println("i ================="+i+"j = "+j);
        allCard[i][j] = new Card(x,y,name);
    allCard[i][j].listType = i;
    allCard[i][j].listNumber = j;
    allCard[i][j].lastNumber = m-1;
    allCard[i][j].listAllCardCount = listMax;
    allCard[i][j].state = false;
    cardFileName = Card.strFilePath + allCard[i][j].cardImageFile;
    file = new File(cardFileName);
    if(file.exists()){
       Toolkit tk = Toolkit.getDefaultToolkit();
       allCard[i][j].adImage = tk.createImage(cardFileName);
    }else{
       allCard[i][j].adImage = null;
    }
    if(CardGame.CARD_DEBUG){
     System.out.println("m =================111");
     System.out.println("allCard[" + i + "][" + j + "]--->listType=" + allCard[i][j].listType + "listNumber="
      + allCard[i][j].listNumber + "cardType=" + allCard[i][j].cardType + "cardNumber="
      + allCard[i][j].cardNumber + "filename=" + allCard[i][j].cardImageFile);   
    }
   }
   listHead[i] = new Card(0, 0, m, 0, "");
   listHead[i].listAllCardCount = 13;
}
for(m=9;m<13;m++){
   allCard[m] = new Card[14];
   for(n=0;n<14;n++){
    allCard[m][n] = null;
   }
   allCard[m][0] = new Card(0, 0, m, 0, "card_blank.JPG");
   allCard[m][0].lastNumber = 0;
   allCard[m][0].listAllCardCount = 14;
   cardFileName = Card.strFilePath + allCard[m][0].cardImageFile;
   file = new File(cardFileName);
   if(file.exists()){
      Toolkit tk = Toolkit.getDefaultToolkit();
      allCard[m][0].adImage = tk.createImage(cardFileName);
   }else{
      allCard[m][0].adImage = null;
   }
}
    }


    /**
     * Handle action accoridng to the following ke
     * when key = cancel exit
     * @param key indicate the action to take
     */
    public int handleAction(int key)
    {
int ret = 0;
int i=0;
int lastCard=currentListNumber;
// repaint();
if(gameStart == false) gameStart = true;
Graphics g = getGraphics();
        if(g == null) return 0;
if(CardGame.CARD_DEBUG)
     System.out.println("handleAction("+key+")-------->>> currentList = "+currentList+", currentListNumber = "+currentListNumber);
if(moveCardCount == -1){
     moveCardCount = 0;
     help.showNumberMessage(g, Card.cardStartXPosition+560,Card.cardStartXPosition, 1);
}
switch(key){
     case InputEvent.VK_0:
     case InputEvent.VK_1:
     case InputEvent.VK_2:
     case InputEvent.VK_3:
     case InputEvent.VK_4:
     case InputEvent.VK_5:
     case InputEvent.VK_6:
     case InputEvent.VK_7:
     case InputEvent.VK_8:
     case InputEvent.VK_9:
          if(currentList > 6) break;
          int keyValue = key - 0x30;
          if(moveCardCount > 0){
           moveCardCount = moveCardCount*10+keyValue;
    }else
                  moveCardCount = keyValue;
    if(CardGame.CARD_DEBUG)
       System.out.println("------------VK_0 :: moveCardCount="+moveCardCount);
    help.showNumberMessage(g, Card.cardStartXPosition+560,Card.cardStartXPosition, moveCardCount);
    if(moveCardCount > 20){
       help.showNumberMessage(g, Card.cardStartXPosition+560,Card.cardStartXPosition, -1);
       moveCardCount = -1;
       break;
    }
    if(moveCardCount <= 1) break;
    if(currentCard == null){
       if(moveCardCount <= allCard[currentList][0].openedCardCount && (currentListNumber >= (moveCardCount-1))){
     if(CardGame.CARD_DEBUG) System.out.println("VK_OK------>>> set move "+moveCardCount+" page cards !!! ");
     for(int a=moveCardCount-1; a>0; a--){
        currentListNumber--;       
     }
     currentCard = allCard[currentList][currentListNumber];
     allCard[currentList][0].lastNumber -= moveCardCount;
     allCard[currentList][0].openedCardCount -= moveCardCount;
     currentListNumber = allCard[currentList][0].lastNumber;
     if(currentListNumber < 0) currentListNumber = 0;
     currentCard.state = false;
     if(CardGame.CARD_DEBUG)
        System.out.println("cleanMoveCard----->>> currentCard.cardXPosition="+currentCard.cardXPosition
         +", currentCard.cardYPosition="+currentCard.cardYPosition
         +", currentCard.cardImageHeightn="+currentCard.cardImageHeight);
     moveCard(g, null);
       }else{
     if(CardGame.CARD_DEBUG)
        System.out.println("VK_OK------>>> set move card's page error !!! ");
     help.showNumberMessage(g, Card.cardStartXPosition+560,Card.cardStartXPosition, -1);
     moveCardCount = -1;
     break;
       }
    }
    break;
     case InputEvent.VK_COLORED_KEY_0:
          if(CardGame.CARD_DEBUG) System.out.println("-----------KEY_RED ::");
    help.messageBox(g, "ÓÎÏ·½«ÖØÐ·¢ÅÆ,ÕýÔÚ³õʼ»¯,ÇëÉÔµÈ......");   
    allCard = null;
          initCard();
    paint(g);
    break;

            case InputEvent.VK_CHANNEL_UP:
            case InputEvent.VK_UP:
     if(CardGame.CARD_DEBUG) System.out.println("--------VK_UP");
    if(currentCard != null){
           if(moveCardCount > 1) break;
     cleanMoveCard(g, currentCard);
     if(currentCard.state == false){
      currentCard.state = true;
      if(moveCardCount > 1){
         delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
      }else{
         if(lastCard > 0){
       delCursor(g, currentList, lastCard-1, allCard[currentList][lastCard-1]);
         }else{
       delCursor(g, currentList, 0, null);
         }
      }
     }else{
      delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
     }
     if(currentList > 8) currentList -= 6;
                        else if(currentList < 7 && currentList > 2) currentList += 6;
                        else if(currentList < 3) currentList = 9;
     if(currentList == 8) currentList = 1;
    }else{
       if(moveCardCount > 1){
     currentListNumber -= moveCardCount;
     allCard[currentList][0].lastNumber -= moveCardCount;
     allCard[currentList][0].openedCardCount -= moveCardCount;
     for(i=moveCardCount-1;i>=0;i--){
        currentListNumber ++;
        allCard[currentList][0].lastNumber++;
        allCard[currentList][0].openedCardCount++;
        lastCard = currentListNumber;
        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
     }
     moveCardCount = 0;
     help.showNumberMessage(g, Card.cardStartXPosition+560,Card.cardStartXPosition, moveCardCount);
       }else{
     if(currentList == 7 && allCard[7][0].lastNumber < 0 ){
        sendCardEnd(g,false);
     }else{
        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
     }
       }
       if(currentList > 8) currentList -= 6;
       else if(currentList > 6) currentList -= 7;
       else if(currentList > 2) currentList += 6;
       else if(currentList == 2) currentList = 8;
       else currentList += 7;
       // if(currentList == 8) currentList = 1;
       if(allCard[currentList][0] == null) currentList ++;
    }
    if(allCard[currentList][0] != null)
       lastCard = allCard[currentList][0].lastNumber;
    else
       lastCard = 0;
    //lastCard = allCard[currentList][0].lastNumber;
    if(lastCard < 0) lastCard = 0;
    currentListNumber = lastCard;
    if(currentCard == null){
     drawCursor(g, currentList, lastCard);
    }else{
                                moveCard(g, allCard[currentList][lastCard]);
                        }
            break;
            case InputEvent.VK_CHANNEL_DOWN:
            case InputEvent.VK_DOWN:
                        if(CardGame.CARD_DEBUG) System.out.println("--------VK_DOWN");      
    if(currentCard != null){
           if(moveCardCount > 1) break;
                                cleanMoveCard(g, currentCard);
     if(currentCard.state == false){
                                        currentCard.state = true;
      if(moveCardCount > 1){
         delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
      }else{
         if(lastCard > 0){
       delCursor(g, currentList, lastCard-1, allCard[currentList][lastCard-1]);
         }else delCursor(g, currentList, 0, null);
      }
                                }else{
                                        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
                                }
                                if(currentList > 8) currentList -= 6;
                                else if(currentList < 7 && currentList > 2) currentList += 6;
                                else if(currentList < 3) currentList = 9;
     if(currentList == 8) currentList = 1;
    }else{
       if(moveCardCount > 1){
     currentListNumber -= moveCardCount;
     allCard[currentList][0].lastNumber -= moveCardCount;
     allCard[currentList][0].openedCardCount -= moveCardCount;
     for(i=moveCardCount-1;i>=0;i--){
        currentListNumber ++;
        allCard[currentList][0].lastNumber++;
        allCard[currentList][0].openedCardCount++;
        lastCard = currentListNumber;
        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
     }
     moveCardCount = 0;
     help.showNumberMessage(g, Card.cardStartXPosition+560,Card.cardStartXPosition, moveCardCount);
       }else{
     if(currentList == 7 && allCard[7][0].lastNumber < 0){
        sendCardEnd(g,false);
     }else{
        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
     }
       }
       if(currentList > 8) currentList -= 6;
       else if(currentList > 6) currentList -= 7;
       else if(currentList > 2) currentList += 6;
       else if(currentList == 2) currentList = 8;
       else currentList += 7;
       if(allCard[currentList][0] == null) currentList ++;
    }
    if(allCard[currentList][0] != null)
       lastCard = allCard[currentList][0].lastNumber;
    else
       lastCard = 0;
    //lastCard = allCard[currentList][0].lastNumber;
    if(lastCard < 0) lastCard = 0;
    currentListNumber = lastCard;
    if(currentCard == null){
                        drawCursor(g, currentList, lastCard);
    }else{
                         moveCard(g, allCard[currentList][lastCard]);
                        }
    break;
            case InputEvent.VK_VOLUME_DOWN:
            case InputEvent.VK_LEFT:
                if(CardGame.CARD_DEBUG) System.out.println("--------VK_LEFT");
    if(currentCard != null){
                                cleanMoveCard(g, currentCard);
     if(currentCard.state == false){
                                        currentCard.state = true;
      if(moveCardCount > 1){
         delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
      }else{
         if(lastCard > 0){
             delCursor(g, currentList, lastCard-1, allCard[currentList][lastCard-1]);
         }else delCursor(g, currentList, 0, null);
      }
                                }else{
                                        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
                                }
                         if(currentList == 0)    currentList = 6;
                         else if(currentList == 8)      currentList = 12;
                         else currentList --;
    }else{
       if(moveCardCount > 1){
     currentListNumber -= moveCardCount;
     allCard[currentList][0].lastNumber -= moveCardCount;
     allCard[currentList][0].openedCardCount -= moveCardCount;
     for(i=0; i<moveCardCount; i++){
        currentListNumber ++;
        allCard[currentList][0].lastNumber++;
        allCard[currentList][0].openedCardCount++;
        lastCard = currentListNumber;
        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
     }
     moveCardCount = 0;
     help.showNumberMessage(g, Card.cardStartXPosition+560,Card.cardStartXPosition, moveCardCount);
       }else{
     if(currentList == 7 && allCard[7][0].lastNumber < 0){
        sendCardEnd(g,false);
     }else{
        delCursor(g, currentList, lastCard, allCard[currentList][lastCard]);
     }
       }
       if(currentList == 0) currentList = 6;
       else if(currentList == 7) currentList = 12;
       else{
     currentList --;
     if(allCard[currentList][0] == null) currentList --;
       }      
    }
    if(allCard[currentList][0] != null)
       lastCard = allCard[currentList][0].lastNumber;
    else
       lastCard = 0;
                 //lastCard = allCard[currentList][0].lastNumber;
    if(lastCard < 0) lastCard = 0;
    currentListNumber = lastCard;
    if(currentCard == null){
                        drawCursor(g, currentList, lastCard);
    }else{
                                moveCard(g, allCard[currentList][lastCard]);
                        }
                break;
            case InputEvent.VK_VOLUME_UP:
            case InputEvent.VK_RIGHT:
                 if(CardGame.CARD_DEBUG) System.out.println("--------VK_RIGHT");
    ...........

分享 举报

发表评论 评论 (1 个评论)

涂鸦板