扑克游戏

扑克游戏

要求:在窗口中显示一幅扑克牌,在未洗牌前是按照花色排列显示,点击重来按钮后会洗牌,然后会按照打乱的顺序排列

package com.lovoinfo;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;

/** * 扑克游戏窗口类 * @author Administrator * */
@SuppressWarnings("serial")
public class PokerGameFrame extends JFrame {
    private boolean initialized = false;

    private int index = 0;

    private JButton shuffleButton, resetButton, nextButton;

    private List<Card> cardList = new ArrayList<Card>();

    private Poker poker = new Poker();
    private Card card = null;

    private BufferedImage offImage = new BufferedImage(1000, 600, 1);

    public PokerGameFrame() {
        this.setTitle("扑克游戏");
        this.setSize(1000, 600);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        shuffleButton = new JButton("洗牌");
        shuffleButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                poker.shuffle();
            }
        });

        resetButton = new JButton("重来");
        resetButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardList.clear();
                index = 0;
                poker.shuffle();
                repaint();
            }
        });

        nextButton = new JButton("下一张");
        nextButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(index < 54) {
                    card = poker.deal(index++);
                    cardList.add(card);
                    repaint();
                }
            }
        });

        this.setLayout(new FlowLayout());
        this.add(shuffleButton);
        this.add(resetButton);
        this.add(nextButton);
    }

    @Override
    public void paint(Graphics g) {
        Graphics g3 = offImage.getGraphics();
        super.paint(g3);

        // 窗口初次加载时在窗口不可见区域将所有图片绘制一次避免图片无法显示的问题
        if(!initialized) {
            for(int i = 1; i <= 54; i++) {
                g.drawImage(getToolkit().getImage(i + ".jpg"), -200, -200, null);
            }
            initialized = true;
        }

        // 循环绘制容器中的每张牌
        for(int i = 0; i < cardList.size(); i++) {
            Card temp = cardList.get(i);
            g3.drawImage(temp.getImage(), 20 + 16 * i, 150, null);
        }

        g.drawImage(offImage, 0, 0, null);
    }

    public static void main(String[] args) {
        new PokerGameFrame().setVisible(true);
    }
}
package com.lovoinfo;

import java.awt.Image;
import java.awt.Toolkit;

/** * 一张扑克 * * @author jackfrued * */
public class Card {
    private Suite suite;
    private int face;
    private Image image;

    /** * 构造器 * @param suite 花色 * @param face 点数 */
    public Card(Suite suite, int face) {
        this(suite, face, null);
    }

    /** * 构造器 * @param suite 花色 * @param face 点数 * @param filename 对应的图片 */
    public Card(Suite suite, int face, String filename) {
        this.suite = suite;
        this.face = face;
        this.image = Toolkit.getDefaultToolkit().getImage(filename);
    }

    /** * 获得花色 * @return 花色 */
    public Suite getSuite() {
        return suite;
    }

    /** * 获得点数 * @return 点数 */
    public int getFace() {
        return face;
    }

    /** * 获得图片 * @return 牌对应的图片 */
    public Image getImage() {
        return image;
    }

    public String toString() {
        String str = "";
        if(suite != Suite.Joker) {
            switch(suite) {
            case Spade:
                str += "黑桃"; break;
            case Heart:
                str += "红桃"; break;
            case Club:
                str += "草花"; break;
            case Diamond:
                str += "方块"; break;
            default:
            }

            switch(face) {
            case 1:
                str += "A"; break;
            case 11:
                str += "J"; break;
            case 12:
                str += "Q"; break;
            case 13:
                str += "K"; break;
            default:
                str += face; break;
            }
        }
        else {
            str = face == 15? "小王" : "大王";
        }

        return str;
    }

}
package com.lovoinfo;

public class Poker {
    private Card[] cards = new Card[54];

    /** * 构造器(初始化54张牌) */
    public Poker() {
        Suite[] suites = { Suite.Spade, Suite.Heart, Suite.Club, Suite.Diamond };
        int[] faces = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

        for(int i = 0; i < cards.length - 2; i++) {
            cards[i] = new Card(suites[i / 13], faces[i % 13], (i + 1) + ".jpg");
        }

        cards[52] = new Card(Suite.Joker, 15, "53.jpg");    // 小王
        cards[53] = new Card(Suite.Joker, 16, "54.jpg");    // 大王
    }

    /** * 洗牌(随机乱序) */
    public void shuffle() {
        for(int i = 0; i < 100; i++) {
            int index1 = (int) (Math.random() * cards.length);
            int index2 = (int) (Math.random() * cards.length);
            Card temp = cards[index1];
            cards[index1] = cards[index2];
            cards[index2] = temp;
        }
    }

    /** * 发牌 * @param index 位置 * @return 一张牌 */
    public Card deal(int index) {
        return cards[index];
    }
}
package com.lovoinfo;

/** * 枚举(花色类型) * @author Administrator * */
public enum Suite {
    Spade, Heart, Club, Diamond, Joker
}

你可能感兴趣的:(扑克游戏)