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

📄 duel.java

📁 小手机屏幕的红心大战.rar(JAVA源码-J2EE源码)
💻 JAVA
字号:
package com.skybright.pig;

import java.util.Vector;

import javax.microedition.lcdui.Canvas;


public class Duel implements Runnable
{
    private PigMiDlet midlet;
    private CardCanvas canvas;
    private ScoreCanvas scoreCanvas;
    private Score scores;
    private Deck deck;
    private CardHand[] players;
    private Vector cardsRound;
    private int roundKey;
    private int roundNum;
    private int whoseTurn;
    private int status;
    private int selectIndex;
    private long t;

    public Duel(PigMiDlet m)
    {
        players = new CardHand[4];
        cardsRound = new Vector();
        roundKey = 0;
        roundNum = 0;
        whoseTurn = -1;
        //change from logo to new
        status = Config.STATUS_LOGO;
        selectIndex = -1;
        midlet = m;
        scores = new Score();
        canvas = new CardCanvas(this);
        scoreCanvas = new ScoreCanvas(this);
        canvas.show();
       //logo show start time
        t = System.currentTimeMillis();
    }

    public void quit()
    {
        midlet.quit();
    }


    public void restart()
    {

        scores = new Score();
        canvas.duelStart();
        canvas.show();
    }


    public void nextMatch()
    {
        canvas.duelStart();
        canvas.show();
    }

    public void start()
    {
       /* if (deck != null)
        {
            deck.destroy();
        }
        */
        deck = new Deck();
        //     players[0] = new CardHand(null, false);
        //    players[1] = new CardHand("aa", true);
        //   players[2] = new CardHand("bb", true);
        //   players[3] = new CardHand("cc", true);
   /*     if (players[0] != null)
        {
            players[0].destory();
        }

        if (players[1] != null)
        {
            players[1].destory();
        }

        if (players[2] != null)
        {
            players[2].destory();
        }

        if (players[3] != null)
        {
            players[3].destory();
        }
        */

    //    players[0] = new CardHand("aa", "/s.png", false);
    //    players[1] = new CardHand("bb", "/e.png", true);
     //   players[2] = new CardHand("cc", "/n.png", true);
    //    players[3] = new CardHand("dd", "/w.png", true);



            players[0] = new CardHand(null, false);
            players[1] = new CardHand("\u83ca", true);
           players[2] = new CardHand("\u5170", true);
           players[3] = new CardHand("\u6885", true);

        roundNum = 0;
        status = Config.STATUS_RUN;
        cardsRound = new Vector();
        selectIndex = -1;
        whoseTurn = -1;

        if (scores.whoOver() != null)
        {
            scores.clear();
        }

        try
        {

        int n = 0;

        for (int i = 0; i < 52; i++)
        {
            Card c = deck.getCard();
            players[n].add(c);

            if (c.getValue() == 0)
            {
                whoseTurn = n;
            }

            n = nextPlayer(n);
        }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public CardHand getPlayer1()
    {
        return players[0];
    }

    public CardHand getPlayer2()
    {
        return players[1];
    }

    public CardHand getPlayer3()
    {
        return players[2];
    }

    public CardHand getPlayer4()
    {
        return players[3];
    }

    public Vector getCardsCound()
    {
        return cardsRound;
    }

    public void deal()
    {
        if (selectIndex < 0)
        {
            return;
        }

        Card c = players[0].show(selectIndex, cardsRound, roundNum);

        if (c == null)
        {
            return;
        }else
        {
            put(c);
            selectIndex = -1;

            return;
        }
    }

    public int getSelectIndex()
    {
        return selectIndex;
    }

    public void setSelectIndex(int i)
    {
        selectIndex = i;
    }

    public void run()
    {
        try
        {
            while (true)
            {
                //show logo for some time
                while (status == Config.STATUS_LOGO)
                {
                    Thread.sleep(200L);

                    if ((System.currentTimeMillis() - t) > 1000L)
                    {
                        //logo show time is over
                        status = Config.STATUS_NEW;
                        //show pig picture
                        canvas.repaint();
                        //  canvas.destoryLogo();
                        break;
                    }
                }

               while (status == Config.STATUS_NEW);
                {
                    Thread.sleep( Config.STATUS_LOGO);
                }

                if (status == Config.STATUS_GAMEOVER)
                {
                    scoreCanvas.show();

                    //  break;
                    continue;
                }

                if (players[whoseTurn].isComputer())
                {
                    Thread.sleep(Config.GAME_INTERVAL);

                    Card c = players[whoseTurn].show(cardsRound, roundNum);
                    put(c);
                }
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void put(Card c)
    {
        if (cardsRound.size() == 0)
        {
            roundKey = c.getSuitIndex();
        }

        if (cardsRound.size() == 4)
        {
            cardsRound.removeAllElements();
        }

        cardsRound.addElement(new CardRound(whoseTurn, c));
        canvas.repaint();
        whoseTurn = nextPlayer(whoseTurn);
        check();

        if ((cardsRound.size() == 4) && (whoseTurn == 0))
        {
            try
            {
                Thread.sleep(Config.GAME_INTERVAL);
            }catch (Exception exception)
            {
            }

            cardsRound.removeAllElements();
            canvas.repaint();
        }
    }

    private void check()
    {
        if (cardsRound.size() < 4)
        {
            return;
        }

        int n = 0;
        Card max = null;

        for (int i = 0; i < cardsRound.size(); i++)
        {
            CardRound cr = (CardRound) cardsRound.elementAt(i);
            int p = cr.getRoundIndex();
            Card c = cr.getCard();

            if (n == 0)
            {
                max = c;
                n = 1;

                continue;
            }

            if ((c.getSuitIndex() == max.getSuitIndex()) &&
                    (c.getCardIndex() > max.getCardIndex()))
            {
                max = c;
                whoseTurn = p;
            }
        }

        for (int i = 0; i < cardsRound.size(); i++)
        {
            CardRound cr = (CardRound) cardsRound.elementAt(i);
            Card c = cr.getCard();

            if ((c.getSuitIndex() == 3) ||
                    ((c.getSuitIndex() == 2) && (c.getCardIndex() == 10)))
            {
                players[whoseTurn].score(c);
            }
        }

        roundNum++;

        if (roundNum == 13)
        {
            gameover();
        }
    }

    private void gameover()
    {
        scores.set(this);
        status = Config.STATUS_GAMEOVER;
    }

    public int getScore(String name)
    {
        return scores.get(name);
    }

    public Score getScore()
    {
        return scores;
    }

    public int getStatus()
    {
        return status;
    }

    public int whoseTurn()
    {
        return whoseTurn;
    }

    private int nextPlayer(int i)
    {
        return (i >= 3) ? 0 : i + 1;
    }
}

⌨️ 快捷键说明

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