前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >扑克牌例题与Collections工具类

扑克牌例题与Collections工具类

作者头像
端碗吹水
发布2020-09-23 10:29:50
2840
发布2020-09-23 10:29:50
举报

扑克牌例题:

使用集合写一个具有发牌功能的扑克牌程序。

我们需要创建四个类,一个封装对象的属性,一个封装牌的花色和大小也就是牌的类型,一个实现发牌,排序,洗牌功能,也就是封装对象的行为,最后一个实现图形化界面。

代码示例:

对象属性封装类:

代码语言:javascript
复制
package poker;
 
public class Poker {
//封装对象的属性
public Poker(String title, String image, Type type, int daxiao) {
this.title = title;
this.image = image;
this.type = type;
this.daxiao = daxiao;
}
 
private String title; // 牌面标题
private String image; // 照片文件
private int daxiao; // 牌的大小
private Type type; //牌的花色
 
public String getTitle() {
return title;
public void setTitle(String title) {
this.title = title;
public String getImage() {
return image;
public void setImage(String image) {
this.image = image;
}
public int getDaxiao() {
return daxiao;
}
public void setDaxiao(int daxiao) {
this.daxiao = daxiao;
}
public Type getTypu() {
return type;
}
public void setTypu(Type typu) {
this.type = typu;
}

牌的类型封装类,使用一个枚举器:

代码语言:javascript
复制
package poker;
public enum Type {
HONGTAO("hong", 3), HEITAO("hei", 4), MEIHUA("mei", 2), FANGKUAI("fang", 1);
// 两个变量一个储存花色,一个存储牌的大小
private String name;
private int num;
 
private Type(String name, int num) {
this.name = name;
this.num = num;

// 只提供get方法
public String getName() {
return name;

public int getNum() {
return num;
}

对象方法实现:

代码语言:javascript
复制
package poker;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
 
public class Method {
 
public static ArrayList[] getPoker() {
ArrayList<Poker> array = new ArrayList<>();
 
String[] titles = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };// 牌面的数字
String[] imagename = { "03", "04", "05", "06", "07", "08", "09", "10", "_j","_q", "_k", "01", "02" };// 照片文件的后缀
 
// 把所有花色的牌加入集合中
for (Type type : Type.values()) {
int daxiao = 10 + type.getNum(); // 牌的大小
int imageIndex = 0; // 记录照片文件的后缀数组的下标
 
for (String title : titles) { // 遍历牌面的数字
Poker p = new Poker(title, type.getName() + imagename[imageIndex++] + ".jpg", type, daxiao += 10); // 储存每张牌的牌面数字、照片地址、牌的花色、牌的大小
array.add(p); // 把每一张牌作为一个对象存储进集合中
}
}
 
// 单独处理大小王的数据注册
Poker dw = new Poker("大王", "dagui.jpg", Type.FANGKUAI, 300);
array.add(dw);
Poker xw = new Poker("小王", "xiaogui.jpg", Type.FANGKUAI, 200);
array.add(xw);
 
Collections.shuffle(array); // 使用Collections操作类中的混排方法来实现洗牌功能
 
// 发出三副牌
ArrayList[] trees = new ArrayList[] { new ArrayList<Poker>(), new ArrayList<Poker>(), new ArrayList<Poker>() };
 
// 使用迭达器拿值
Iterator it = array.iterator();
 
// 均匀的发出17张牌
for (ArrayList arrayList : trees) {
for (int i = 0; i < 54 / 3; i++) {
if(it.hasNext()){
arrayList.add(it.next());
}
}
}
 
// 将三副牌拿出来,然后使用冒泡排序法排序
for (ArrayList<Poker> arrayList : trees) {
for (int i = 0; i < arrayList.size(); i++) {
for (int j = arrayList.size()-1; j > i; j--) {
if (arrayList.get(j).getDaxiao() > arrayList.get(j - 1).getDaxiao()) {
Poker p2 = arrayList.get(j);

arrayList.set(j, arrayList.get(j-1));
arrayList.set(j - 1, p2);
}
}
}
}
return trees; // 将最后处理好的三副牌返回出去
}
}

实现图形化界面:

代码语言:javascript
复制
package poker;
 
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
 
public class JFramePoker extends JFrame {
 
private JPanel contentPane;
 
/**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFramePoker frame = new JFramePoker();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
 
/**
 * Create the frame.
 */
public JFramePoker() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 100, 1121, 871);
setTitle("发牌");
setResizable(false);
contentPane = new JPanel();
contentPane.setLayout(null);
setContentPane(contentPane);
 
ArrayList[] trees = Method.getPoker(); //将存储了三副牌的集合拿出来
int row = 0; //记录图片y坐标的位置
for (ArrayList arrayList : trees) { //将牌一副副拿出来
for (int i = arrayList.size() - 1; i >= 0; i--) { //将牌一张张的拿出来,并且将原本牌的顺序反过来
Poker p = (Poker) arrayList.get(i); //将每一张牌转换成对象
//添加照片
final JLabel label = new JLabel(new ImageIcon("image/" + p.getImage())); 
label.setBounds(i * 40, row, 170, 259);
getContentPane().add(label);
}
row += 270; //每发一张牌就改变一下坐标位置
}
}
}

Collections集合工具类:

此类的操作都是针对List系列的集合,能对集合实现排序等操作,但是如果需要排序自己写的类的实例化对象的话,需要在需要排序的类里重写compareTo();方法。

compareTo();方法:

此方法返回的是3个数字:1 0 -1,1代表大于,0代表等于,-1则代表小于,就是利用这3个数字来进行判断排序。

代码示例:

代码语言:javascript
复制
public class Student implements Comparable<Student> {
 
public Student(String name, String address, int age) {
 
this.name = name;
this.address = address;
this.age = age;
}
 
private String name;
private int age;
private String address;
 
public String getAddress() {
return address;
}
 
public void setAddress(String address) {
this.address = address;
}
 
public int getAge() {
return age;
}
 
public void setAge(int age) {
this.age = age;
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
//重写compareTo方法,按照name属性来排序
public int compareTo(Student o) {
 
return this.name.compareTo(o.name);
}
 
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档