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

📄 araneid.java

📁 有点类似于蜘蛛牌的一个扑克程序,比较好玩
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/*
 * 创建日期 2005-7-12 TODO 要更改此生成的文件的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */

/**
download by http://www.codefans.net
 * @author 叶少伟 TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class Araneid extends JFrame
{
    public static final int EASY          = 1;
    public static final int NATURAL       = 2;
    public static final int HARD          = 3;
    private int             grade         = Araneid.EASY;
    private Container       pane          = null;
    private PKCard          cards[]       = new PKCard[104];
    private JLabel          clickLabel    = null;
    private int             c             = 0;
    private int             n             = 0;
    private int             a             = 0;
    private int             finish        = 0;
    Hashtable               table         = null;
    private JLabel          groundLabel[] = null;

    public static void main(String[] args)
    {
        Araneid araneid = new Araneid();
    }

    /**
     * This is the default constructor
     */
    public Araneid()
    {
        super();
        this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        this.setSize(1024, 742);
        this.setJMenuBar(new AraneidMenuBar(this));
        this.pane = this.getContentPane();
        this.pane.setBackground(new Color(0, 112, 26));
        this.pane.setLayout(null);
        this.clickLabel = new JLabel();
        this.clickLabel.setBounds(883, 606, 121, 96);
        this.pane.add(this.clickLabel);
        this.clickLabel.addMouseListener(new MouseAdapter()
        {
            public void mouseReleased(MouseEvent me)
            {
                if (c < 60) Araneid.this.deal();
            }
        });
        this.setTitle("蜘蛛牌");
        this.initCards();
        this.randomCards();
        this.setCardsLocation();
        groundLabel = new JLabel[10];
        int x = 20;
        for (int i = 0; i < 10; i++)
        {
            groundLabel[i] = new JLabel();
            groundLabel[i]
                    .setBorder(javax.swing.BorderFactory
                            .createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
            groundLabel[i].setBounds(x, 25, 71, 96);
            x += 101;
            this.pane.add(groundLabel[i]);
        }
        this.setVisible(true);
        this.deal();
        this.addKeyListener(new KeyAdapter()
        {
            class Show extends Thread
            {
                public void run()
                {
                    Araneid.this.showEnableOperator();
                }
            }

            public void keyPressed(KeyEvent e)
            {
                if (finish != 8) if (e.getKeyCode() == KeyEvent.VK_D && c < 60)
                {
                    Araneid.this.deal();
                }
                else if (e.getKeyCode() == KeyEvent.VK_M)
                {
                    new Show().start();
                }
            }
        });
    }

    public void newGame()
    {
        this.randomCards();
        this.setCardsLocation();
        this.setGroundLabelZOrder();
        this.deal();
    }

    public int getC()
    {
        return c;
    }

    public void setGrade(int grade)
    {
        this.grade = grade;
    }

    public void initCards()
    {
        if (cards[0] != null)
        {
            for (int i = 0; i < 104; i++)
            {
                pane.remove(cards[i]);
            }
        }
        int n = 0;
        if (this.grade == Araneid.EASY)
        {
            n = 1;
        }
        else if (this.grade == Araneid.NATURAL)
        {
            n = 2;
        }
        else
        {
            n = 4;
        }
        for (int i = 1; i <= 8; i++)
        {
            for (int j = 1; j <= 13; j++)
            {
                cards[(i - 1) * 13 + j - 1] = new PKCard((i % n + 1) + "-" + j,
                        this);
            }
        }
        this.randomCards();
    }

    public void randomCards()
    {
        PKCard temp = null;
        for (int i = 0; i < 52; i++)
        {
            int a = (int) (Math.random() * 104);
            int b = (int) (Math.random() * 104);
            temp = cards[a];
            cards[a] = cards[b];
            cards[b] = temp;
        }
    }

    public void setNA()
    {
        a = 0;
        n = 0;
    }

    public void setCardsLocation()
    {
        table = new Hashtable();
        c = 0;
        finish = 0;
        n = 0;
        a = 0;
        int x = 883;
        int y = 580;
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                int n = i * 10 + j;
                pane.add(cards[n]);
                cards[n].turnRear();
                cards[n].moveto(new Point(x, y));
                table.put(new Point(x, y), cards[n]);
            }
            x += 10;
        }
        x = 20;
        y = 45;
        for (int i = 10; i > 5; i--)
        {
            for (int j = 0; j < 10; j++)
            {
                int n = i * 10 + j;
                if (n >= 104) continue;
                pane.add(cards[n]);
                cards[n].turnRear();
                cards[n].moveto(new Point(x, y));
                table.put(new Point(x, y), cards[n]);
                x += 101;
            }
            x = 20;
            y -= 5;
        }
    }

    public void showEnableOperator()
    {
        int x = 0;
        out: while (true)
        {
            Point point = null;
            PKCard card = null;
            do
            {
                if (point != null) n++;
                point = this.getLastCardLocation(n);
                while (point == null)
                {
                    point = this.getLastCardLocation(++n);
                    if (n == 10) n = 0;
                    x++;
                    if (x == 10) break out;
                }
                card = (PKCard) this.table.get(point);
            }
            while (!card.isCardCanMove());
            while (this.getPreviousCard(card) != null
                    && this.getPreviousCard(card).isCardCanMove())
            {
                card = this.getPreviousCard(card);
            }
            if (a == 10) a = 0;
            for (; a < 10; a++)
            {
                if (a != n)
                {
                    Point p = null;
                    PKCard c = null;
                    do
                    {
                        if (p != null) a++;
                        p = this.getLastCardLocation(a);
                        int z = 0;
                        while (p == null)
                        {
                            p = this.getLastCardLocation(++a);
                            if (a == 10) a = 0;
                            if (a == n) a++;
                            z++;
                            if (z == 10) break out;
                        }
                        c = (PKCard) this.table.get(p);
                    }
                    while (!c.isCardCanMove());
                    if (c.getCardValue() == card.getCardValue() + 1)
                    {
                        card.flashCard(card);
                        try
                        {
                            Thread.sleep(800);
                        }
                        catch (InterruptedException e)
                        {
                            // TODO 自动生成 catch 块
                            e.printStackTrace();
                        }
                        c.flashCard(c);
                        a++;
                        if (a == 10) n++;
                        break out;
                    }
                }
            }
            n++;
            if (n == 10) n = 0;
            x++;
            if (x == 10) break out;
        }
    }

    public void deal()
    {
        this.setNA();
        for (int i = 0; i < 10; i++)
        {
            if (this.getLastCardLocation(i) == null)
            {
                JOptionPane.showMessageDialog(this, "有空位不能发牌!", "提示",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
        }
        int x = 20;
        for (int i = 0; i < 10; i++)
        {
            Point lastPoint = this.getLastCardLocation(i);
            if (c == 0)
                lastPoint.y += 5;
            else
                lastPoint.y += 20;
            table.remove(cards[c + i].getLocation());
            cards[c + i].moveto(lastPoint);
            table.put(new Point(lastPoint), cards[c + i]);
            cards[c + i].turnFront();
            cards[c + i].setCanMove(true);
            this.pane.setComponentZOrder(cards[c + i], 1);
            Point point = new Point(lastPoint);
            if (cards[c + i].getCardValue() == 1)
            {
                int n = cards[c + i].whichColumnAvailable(point);
                point.y -= 240;
                PKCard card = (PKCard) this.table.get(point);
                if (card != null && card.isCardCanMove())
                {
                    this.haveFinish(n);
                }
            }
            x += 101;
        }
        c += 10;
    }

    public PKCard getPreviousCard(PKCard card)
    {
        Point point = new Point(card.getLocation());
        point.y -= 5;
        card = (PKCard) table.get(point);
        if (card != null) return card;
        point.y -= 15;
        card = (PKCard) table.get(point);
        return card;
    }

    public PKCard getNextCard(PKCard card)
    {
        Point point = new Point(card.getLocation());
        point.y += 5;
        card = (PKCard) table.get(point);
        if (card != null) return card;
        point.y += 15;
        card = (PKCard) table.get(point);
        return card;
    }

    public Point getLastCardLocation(int column)
    {
        Point point = new Point(20 + column * 101, 25);
        PKCard card = (PKCard) this.table.get(point);
        if (card == null) return null;
        while (card != null)
        {
            point = card.getLocation();
            card = this.getNextCard(card);
        }
        return point;
    }

    public Point getGroundLabelLocation(int column)
    {
        return new Point(groundLabel[column].getLocation());
    }

    public void setGroundLabelZOrder()
    {
        for (int i = 0; i < 10; i++)
        {
            pane.setComponentZOrder(groundLabel[i], 105 + i);
        }
    }

    public void haveFinish(int column)
    {
        Point point = this.getLastCardLocation(column);
        PKCard card = (PKCard) this.table.get(point);
        do
        {

            this.table.remove(point);
            card.moveto(new Point(20 + finish * 10, 580));
            pane.setComponentZOrder(card, 1);
            this.table.put(card.getLocation(), card);
            card.setCanMove(false);
            point = this.getLastCardLocation(column);
            if (point == null)
                card = null;
            else
                card = (PKCard) this.table.get(point);
        }
        while (card != null && card.isCardCanMove());
        finish++;
        if (finish == 8)
        {
            JOptionPane.showMessageDialog(this, "恭喜你,过关了!", "成功",
                    JOptionPane.PLAIN_MESSAGE);
        }
        if (card != null)
        {
            card.turnFront();
            card.setCanMove(true);
        }
    }
}

⌨️ 快捷键说明

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