一个简单的红心大战游戏源码

源代码在线查看: cardhand.java

软件大小: 141 K
上传用户: lqlm521
关键词: 源码
下载地址: 免注册下载 普通下载 VIP

相关代码

				package com.skybright.pig;
				
				import java.util.Vector;
				
				import javax.microedition.lcdui.*;
				
				
				public class CardHand
				{
				    private Card[] cards = null;
				    private Card[] scores = null;
				    private String player = null;
				    private Image header = null;
				    private boolean isComputer;
				
				    public CardHand(String pname, String header, boolean b)
				    {
				        cards = new Card[13];
				        scores = new Card[14];
				        player = "\u60A8";
				        isComputer = false;
				
				        try
				        {
				            this.header = Image.createImage(header);
				        }catch (Exception e)
				        {
				            this.header = null;
				        }
				
				        if (pname != null)
				        {
				            player = pname;
				        }
				
				        isComputer = b;
				    }
				
				    public CardHand(String pname, boolean b)
				    {
				        cards = new Card[13];
				        scores = new Card[14];
				        player = "\u60A8";
				        isComputer = false;
				
				        if (pname != null)
				        {
				            player = pname;
				        }
				
				        isComputer = b;
				    }
				
				    public Image getHeader()
				    {
				        return header;
				    }
				
				    public String getName()
				    {
				        return player;
				    }
				
				    public boolean isComputer()
				    {
				        return isComputer;
				    }
				
				    public void add(Card c)
				    {
				        // insert card by card number ascend
				        for (int i = 0; i < cards.length; i++)
				        {
				            if (cards[i] == null)
				            {
				                cards[i] = c;
				
				                return;
				            }
				
				            if (c.getValue() 				            {
				                for (int j = cards.length - 2; j >= i; j--)
				                {
				                    if (cards[j] != null)
				                    {
				                        cards[j + 1] = cards[j];
				                    }
				                }
				
				                cards[i] = c;
				
				                return;
				            }
				        }
				    }
				
				    public Card show(int index, Vector v, int round)
				    {
				        int key = 0;
				        boolean isFirst = false;
				
				        if ((v.size() == 4) || (v.size() == 0))
				        {
				            isFirst = true;
				        }
				
				        if ((v.size() < 4) && (v.size() > 0))
				        {
				            key = ((CardRound) v.elementAt(0)).getCard().getSuitIndex();
				        }
				
				        try
				        {
				            Card c = null;
				            int n = 0;
				            int rec = -1;
				
				            for (int i = 0; i < cards.length; i++)
				            {
				                if ((cards[i] != null) && (n == index))
				                {
				                    c = cards[i];
				                    rec = i;
				                }
				
				                if (cards[i] != null)
				                {
				                    n++;
				                }
				            }
				
				            if ((round == 0) && (c != null) && isFirst &&
				                    (c.getValue() != 0))
				            { //player1 must hand first card ,which is not allowed 2
				
				                Alert alert = new Alert("\u8b66\u544a");
				
				                alert.setString(
				                        "\u7b2c\u4e00\u5f20\u724c\u5fc5\u987b\u51fa\u6885\u82b12");
				
				                alert.setType(AlertType.WARNING);
				                alert.setTimeout(-2);
				
				                PigMiDlet.display.setCurrent(alert);
				
				                Card card1 = null;
				
				                return card1;
				            }
				
				            if ((c != null) && !isFirst && haveSuit(key) &&
				                    (c.getSuitIndex() != key))
				            { //card which was hand must be as same as the first card's style
				
				                Alert alert = new Alert("\u8b66\u544a");
				
				                switch (key)
				                {
				                case 0:
				                    alert.setString(
				                            "\u5fc5\u987b\u8ddf\u724c,\u8bf7\u51fa\u6885\u82b1");
				
				                    break;
				
				                case 1:
				                    alert.setString(
				                            "\u5fc5\u987b\u8ddf\u724c,\u8bf7\u51fa\u65b9\u7247");
				
				                    break;
				
				                case 2:
				                    alert.setString(
				                            "\u5fc5\u987b\u8ddf\u724c,\u8bf7\u51fa\u9ed1\u6843");
				
				                    break;
				
				                case 3:
				                    alert.setString(
				                            "\u5fc5\u987b\u8ddf\u724c,\u8bf7\u51fa\u7ea2\u6843");
				
				                    break;
				                }
				
				                alert.setType(AlertType.WARNING);
				                alert.setTimeout(-2);
				                PigMiDlet.display.setCurrent(alert);
				
				                Card card2 = null;
				
				                return card2;
				            }
				
				            if ((round == 0) && (c != null) &&
				                    (c.getSuitIndex() == 3 || c.getValue() == 36))
				            {
				                Card card3 = null;
				
				                return card3;
				            }
				
				            if (c != null)
				            {
				                cards[rec] = null;
				            }
				
				            Card card4 = c;
				
				            return card4;
				        }catch (Exception e)
				        {
				            e.printStackTrace();
				        }
				
				        Card card = null;
				
				        return card;
				    }
				
				    private boolean haveSuit(int key)
				    {
				        for (int i = 0; i < cards.length; i++)
				        {
				            if ((cards[i] != null) && (cards[i].getSuitIndex() == key))
				            {
				                return true;
				            }
				        }
				
				        return false;
				    }
				
				    public Card show(Vector v, int round)
				    {
				        boolean isFirst = false;
				
				        if ((v.size() == 4) || (v.size() == 0))
				        {
				            isFirst = true;
				        }
				
				        if (isFirst)
				        { //hava  priority to hand card
				
				            if (round == 0)
				            { //first hand card
				
				                return getMinCards();
				            }
				
				            Card c = getBlackHeart();
				
				            if (c != null)
				            {
				                return c;
				            }else
				            {
				                return getMinCards();
				            }
				        }else
				        { //follow other player
				
				            return follow(v, round);
				        }
				    }
				
				    private Card getMinCards()
				    {
				        int min = -1;
				
				        for (int i = 0; i < cards.length; i++)
				        {
				            if ((cards[i] != null) && (cards[i].getCardIndex() > min))
				            {
				                Card c = cards[i];
				                cards[i] = null;
				
				                return c;
				            }
				        }
				
				        return null;
				    }
				
				    private Card getBlackHeart()
				    {
				        int val = 0;
				        int cardIndex = -1;
				
				        for (int i = 0; i < cards.length; i++)
				        {
				            if ((cards[i] == null) || (cards[i].getSuitIndex() != 2))
				            {
				                continue;
				            }
				
				            if ((cards[i] != null) && (cards[i].getSuitIndex() == 2) &&
				                    (cards[i].getCardIndex() == 10))
				            {
				                return null;
				            }
				
				            if (cards[i].getCardIndex() > 10)
				            {
				                val--;
				            }else
				            {
				                val++;
				                cardIndex = i;
				            }
				        }
				
				        if ((val >= 0) && (cardIndex >= 0))
				        {
				            Card c = cards[cardIndex];
				            cards[cardIndex] = null;
				
				            return c;
				        }else
				        {
				            return null;
				        }
				    }
				
				    private Card follow(Vector v, int round)
				    {
				        Card min = getRoundKey(v);
				        Card max = getRoundMax(v);
				        int n;
				
				        if ((min.getSuitIndex() == 2) && (hasPig() >= 0))
				        {
				            if (max.getCardIndex() > 10)
				            {
				                n = hasPig();
				
				                Card c = cards[n];
				                cards[n] = null;
				
				                return c;
				            }
				
				            n = hasSmall(max);
				
				            if (n >= 0)
				            {
				                Card c = cards[n];
				                cards[n] = null;
				
				                return c;
				            }
				
				            n = hasBig(max);
				
				            if (n >= 0)
				            {
				                Card c = cards[n];
				                cards[n] = null;
				
				                return c;
				            }else
				            {
				                n = hasPig();
				
				                Card c = cards[n];
				                cards[n] = null;
				
				                return c;
				            }
				        }
				
				        n = hasSmall(max);
				
				        if (n >= 0)
				        {
				            Card c = cards[n];
				            cards[n] = null;
				
				            return c;
				        }
				
				        n = hasBig(max);
				
				        if (n >= 0)
				        {
				            Card c = cards[n];
				            cards[n] = null;
				
				            return c;
				        }
				
				        n = hasPig();
				
				        if (n >= 0)
				        {
				            Card c = cards[n];
				            cards[n] = null;
				
				            return c;
				        }
				
				        for (int i = cards.length - 1; i >= 0; i--)
				        {
				            if ((cards[i] != null) && (cards[i].getSuitIndex() == 3) &&
				                    (round > 0))
				            {
				                Card c = cards[i];
				                cards[i] = null;
				
				                return c;
				            }
				        }
				
				        for (int i = cards.length - 1; i >= 0; i--)
				        {
				            if (cards[i] != null)
				            {
				                Card c = cards[i];
				                cards[i] = null;
				
				                return c;
				            }
				        }
				
				        return null;
				    }
				
				    private Card getRoundKey(Vector v)
				    {
				        if ((v.size() < 4) && (v.size() > 0))
				        {
				            return ((CardRound) v.elementAt(0)).getCard();
				        }else
				        {
				            return null;
				        }
				    }
				
				    private Card getRoundMax(Vector v)
				    {
				        Card f = getRoundKey(v);
				        int rtn = 0;
				
				        for (int i = 0; i < v.size(); i++)
				        {
				            Card c = ((CardRound) v.elementAt(0)).getCard();
				
				            if ((c.getSuitIndex() == f.getSuitIndex()) &&
				                    (c.getCardIndex() > f.getCardIndex()))
				            {
				                rtn = i;
				            }
				        }
				
				        if ((v.size() < 4) && (v.size() > 0))
				        {
				            return ((CardRound) v.elementAt(rtn)).getCard();
				        }else
				        {
				            return null;
				        }
				    }
				
				    private int hasPig()
				    {
				        for (int i = 0; i < cards.length; i++)
				        {
				            if ((cards[i] != null) && (cards[i].getSuitIndex() == 2) &&
				                    (cards[i].getCardIndex() == 10))
				            {
				                return i;
				            }
				        }
				
				        return -1;
				    }
				
				    private int hasSmall(Card c)
				    {
				        for (int i = cards.length - 1; i >= 0; i--)
				        {
				            if ((cards[i] != null) &&
				                    (cards[i].getSuitIndex() == c.getSuitIndex()) &&
				                    (cards[i].getCardIndex() < c.getCardIndex()))
				            {
				                return i;
				            }
				        }
				
				        return -1;
				    }
				
				    private int hasBig(Card c)
				    {
				        for (int i = 0; i < cards.length; i++)
				        {
				            if ((cards[i] != null) &&
				                    (cards[i].getSuitIndex() == c.getSuitIndex()) &&
				                    (cards[i].getCardIndex() > c.getCardIndex()))
				            {
				                return i;
				            }
				        }
				
				        return -1;
				    }
				
				    public int left()
				    {
				        int n = 0;
				
				        for (int i = 0; i < cards.length; i++)
				        {
				            if (cards[i] != null)
				            {
				                n++;
				            }
				        }
				
				        return n;
				    }
				
				    public void score(Card c)
				    {
				        if ((c.getSuitIndex() == 2) && (c.getCardIndex() == 10))
				        {
				            scores[13] = c;
				        }else
				        {
				            scores[c.getCardIndex()] = c;
				        }
				    }
				
				    public int getScore()
				    {
				        int rtn = 0;
				
				        for (int i = 0; i < scores.length; i++)
				        {
				            if (scores[i] != null)
				            {
				                rtn = (i >= 13) ? (rtn + 13) : rtn + 1;
				            }
				        }
				
				        return rtn;
				    }
				
				    public Card[] getCards()
				    {
				        return cards;
				    }
				
				    public Card[] getScoreCards()
				    {
				        return scores;
				    }
				
				    /**
				     * destroy resource
				     */
				    public void destory()
				    {
				        if (cards != null)
				        {
				            cards = null;
				        }
				
				        if (scores != null)
				        {
				            scores = null;
				        }
				
				        if (header != null)
				        {
				            header = null;
				        }
				
				        if (player != null)
				        {
				            player = null;
				        }
				
				        System.gc();
				    }
				}			

相关资源