扑克牌例题:
使用集合写一个具有发牌功能的扑克牌程序。
我们需要创建四个类,一个封装对象的属性,一个封装牌的花色和大小也就是牌的类型,一个实现发牌,排序,洗牌功能,也就是封装对象的行为,最后一个实现图形化界面。
代码示例:
对象属性封装类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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;
}
|
牌的类型封装类,使用一个枚举器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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;
}
|
对象方法实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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;
// 将最后处理好的三副牌返回出去
}
}
|
实现图形化界面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
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个数字来进行判断排序。
代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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);
}
}
|
本文转自 ZeroOne01 51CTO博客,原文链接:http://blog.51cto.com/zero01/1976557,如需转载请自行联系原作者