今天的程序

package com.accp;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

/**
 * 卡片(一张扑克)
 * @author 骆昊
 *
 */
public class Card {
	private String suite;		// 花色
	private int face;			// 点数
	
	public Card(String suite, int face) {
		this.suite = suite;
		this.face = face;
	}

	public String getSuite() {
		return suite;
	}

	public int getFace() {
		return face;
	}
	
	/**
	 * 绘制卡片
	 * @param g 画笔
	 * @param x 横坐标
	 * @param y 纵坐标
	 */
	public void draw(Graphics g, int x, int y) {
		// 从上下文环境中取出边框图片进行绘制
		g.drawImage(GameContext.getImage("边框"), x, y, 100, 150, null);
		// 从上下文环境中取出花色对应的图片进行绘制
		g.drawImage(GameContext.getImage(suite), x + 10, y + 10, null);
		String str = "";
		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;
		}
		// 设置画笔颜色
		if(suite.equals("黑桃") || suite.equals("草花")) {
			g.setColor(Color.BLACK);
		}
		else {
			g.setColor(Color.RED);
		}
		// 设置画笔字体
		g.setFont(new Font("Times New Roman", Font.BOLD, 14));
		// 绘制点数
		g.drawString(str, x + 30, y + 30);
	}

}
一张牌
package com.accp;

import java.awt.Image;
import java.util.HashMap;
import java.util.Map;

/**
 * 游戏上下文环境(保持游戏要使用的图片)
 * @author 骆昊
 *
 */
public class GameContext {
	private static Map<String, Image> map = new HashMap<String, Image>();
	
	private GameContext() {
	}
	
	/**
	 * 添加一张图
	 * @param name 名字
	 * @param image 图片对象
	 */
	public static void addImage(String name, Image image){
		map.put(name, image);
	}
	
	/**
	 * 检索一张图
	 * @param name 名字
	 * @return 图片对象
	 */
	public static Image getImage(String name) {
		return map.get(name);
	}
}
游戏
package com.accp;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class GameFrame extends JFrame {
	private boolean initialized = false;	// 是否完成预加载图片
	private String[] names = {"边框", "草花", "方块", "红心", "黑桃"};
	private String[] filenames = {"border.jpg", "club.jpg", "diamond.jpg", "heart.jpg", "spade.jpg"};
	private Poker p = new Poker();		// 创建一副扑克
	private int index = 0;				// 发牌的位置
	private Card currentCard = null;	// 当前发的那张扑克
	
	private List<Card> list = new ArrayList<Card>();	// 保存三张扑克的容器
	
	private JButton nextButton, resetButton, nextThreeButton;

	public GameFrame() {
		for(int i = 0; i < filenames.length; i++) {
			GameContext.addImage(names[i], getToolkit().getImage(filenames[i]));
		}
		p.shuffle();
		
		this.setTitle("扑克游戏");
		this.setSize(800, 600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		// 给窗口设置一个流式布局管理器
		this.setLayout(new FlowLayout());
		
		nextButton = new JButton("下一张");
		nextButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(index < 52) {
					currentCard = p.deal(index++);
					if(index == 52) {
						nextButton.setEnabled(false);
						nextThreeButton.setEnabled(false);
						resetButton.setEnabled(true);
					}
					repaint();	// 重绘窗口
				}
			}
		});
		this.add(nextButton);
		
		resetButton = new JButton("重置");
		resetButton.setEnabled(false);
		resetButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				list.clear();
				p.shuffle();
				index = 0;
				currentCard = null;
				resetButton.setEnabled(false);
				nextButton.setEnabled(true);
				nextThreeButton.setEnabled(true);
				repaint();
			}
		});
		this.add(resetButton);
		
		nextThreeButton = new JButton("发三张");
		nextThreeButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				currentCard = null;
				list.clear();
				for(int i = 1; i <= 3 && index < 52; i++) {
					list.add(p.deal(index++));
				}
				if(index >= 52) {
					nextThreeButton.setEnabled(false);
					nextButton.setEnabled(false);
					resetButton.setEnabled(true);
				}
				repaint();
			}
		});
		this.add(nextThreeButton);
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		if(!initialized) {	// 先在窗口不可见区域预先加载图片
			for(int i = 0; i < names.length; i++) {
				g.drawImage(GameContext.getImage(names[i]), -1000, -1000, null);
			}
			initialized = true;	// 预加载只执行一次
		}
		if(currentCard != null) {	// 画一张扑克
			currentCard.draw(g, 300, 200);
		}
		else {	// 画三张扑克
			for(int i = 0; i < list.size(); i++) {
				list.get(i).draw(g, 150 + i * 125, 200);
			}
		}
	}
	
	public static void main(String[] args) {
		new GameFrame().setVisible(true);
	}
}
  


package com.accp;

/**
 * 扑克
 * @author 骆昊
 *
 */
public class Poker {
	private Card[] cards = new Card[52];
	
	public Poker() {
		String[] suites = {"黑桃", "红心", "草花", "方块"};
		int[] faces = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
		for(int i = 0; i < cards.length; i++) {
			cards[i] = new Card(suites[i / 13], faces[i % 13]);
		}
	}
	
	/**
	 * 洗牌(随机乱序)
	 */
	public void shuffle() {
		for(int i = 0; i < cards.length; i++) {
			int pos = (int) (Math.random() * cards.length);
			Card temp = cards[i];
			cards[i] = cards[pos];
			cards[pos] = temp;
		}
	}
	
	/**
	 * 发牌
	 * @param index 第几张牌的位置索引
	 * @return 指定位置的那张牌
	 */
	public Card deal(int index) {
		return cards[index];
	}
}



package com.accp;

public class Test01 {

	public static void main(String[] args) {
		Poker p = new Poker();
		
		p.shuffle();
		
		for(int i = 0; i < 52; i++) {
			System.out.println(p.deal(i));
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值