Java实现的一副扑克
## 简介 在很久很久以前,自己没事写的一个小练习。 封装了一个普扑克实体和一副牌,然后封装了一些方
渲染中...
## 简介
在很久很久以前,自己没事写的一个小练习。
封装了一个普扑克实体和一副牌,然后封装了一些方法,如:洗牌、抽牌等等。
使用的Java原生代码,没有任何第三方库,如果你想玩扑克,可以自行拷贝代码测试。
<!-- more -->
## 封装单张Poke的属性
```java
/**
* @class_name Poke
* @effect 封装单张扑克牌
* @author Mr hu
* @date 2018年11月10日 下午7:52:07
*/
public class Poke {
// 用于花色排序
private int color;
private String colorname;
// 用于点数排序
private int point;
private String pointname;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public String getColorname() {
return colorname;
}
public void setColorname(String colorname) {
this.colorname = colorname;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
public String getPointname() {
return pointname;
}
public void setPointname(String pointname) {
this.pointname = pointname;
}
// 编写带参构造方法
public Poke(int color, String colorname, int point, String pointname) {
this.color = color;
this.colorname = colorname;
this.point = point;
this.pointname = pointname;
}
@Override
public String toString() {
return "["+colorname+":"+pointname+"]";
}
}
```
## 封装一副扑克的行为
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @class_name PokeBehavior
* @effect 封装一副扑克牌的行为
* @author Mr hu
* @date 2018年11月10日 下午10:29:16
*/
public class PokeBehavior {
/*
* 编写生成一副扑克的方法
*/
public static List<Poke> generatePoke() {
// 创建Poke的List集合,用于存放一副扑克
List<Poke> pokelist = new ArrayList<Poke>();
// 创建四种花色从A—K各13张,共52张扑克,并添加到Poke集合中
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 13; j++) {
String colorname = null;
String pointname = null;
// 1 ♡ | 2 ♢ | 3 ♤ | 4 ♣
switch (i) {
case 1: colorname = "♥";break;
case 2: colorname = "♢";break;
case 3: colorname = "♤";break;
case 4: colorname = "♣";break;
}
// 1 A | …… | 11 J | 12 Q | 13 K
switch (j) {
case 1: pointname = "A"; break;
case 2: pointname = "2"; break;
case 3: pointname = "3"; break;
case 4: pointname = "4"; break;
case 5: pointname = "5"; break;
case 6: pointname = "6"; break;
case 7: pointname = "7"; break;
case 8: pointname = "8"; break;
case 9: pointname = "9"; break;
case 10: pointname = "10"; break;
case 11: pointname = "J"; break;
case 12: pointname = "Q"; break;
case 13: pointname = "K"; break;
}
Poke poke = new Poke(i, colorname, j, pointname);
pokelist.add(poke);
}
}
// 创建大小王扑克并添加到Poke集合中
Poke pokemin = new Poke(5, "小", 14, "小王");
Poke pokemax = new Poke(6, "大", 15, "大王");
pokelist.add(pokemin);
pokelist.add(pokemax);
return pokelist;
}
/*
* 编写打乱list集合(洗牌)的方法
*/
public static List<Poke> disruptPoke(List<Poke> pokelist) {
Collections.shuffle(pokelist);
return pokelist;
}
/*
* 编写按点数大小排序的方法
*/
public static List<Poke> sortSize(List<Poke> pokelist) {
//按照Poke的点数进行升序排列
Collections.sort(pokelist, new Comparator<Poke>() {
public int compare(Poke p1, Poke p2) {
if(p1.getPoint() > p2.getPoint()){
return 1;
}
if(p1.getPoint() == p2.getPoint()){
return 0;
}
return -1;
}
});
return pokelist;
}
/*
* 编写按花色排序的方法
*/
public static List<Poke> sortColor(List<Poke> pokelist) {
//按照Poke的花色进行升序排列(1 ♡ | 2 ♢ | 3 ♤ | 4 ♣ )
Collections.sort(pokelist, new Comparator<Poke>() {
public int compare(Poke p1, Poke p2) {
if(p1.getColor() > p2.getColor()){
return 1;
}
if(p1.getColor() == p2.getColor()){
return 0;
}
return -1;
}
});
return pokelist;
}
/*
* 编写既按照花色又按照大小排序(A2345……A2345……)的方法
* 分析后可知,先大小再花色即可
* 若先花色,再大小,就会变成(AAAA 2222 3333 ……)的形式
*/
public static List<Poke> sortColorAndSize(List<Poke> pokelist) {
//按照Poke的花色进行升序排列
Collections.sort(pokelist, new Comparator<Poke>() {
public int compare(Poke p1, Poke p2) {
if(p1.getColor() > p2.getColor()){
return 1;
}
if(p1.getColor() == p2.getColor()){
return 0;
}
return -1;
}
});
// 调用编写好的花色排序方法
PokeBehavior.sortColor(pokelist);
return pokelist;
}
/*
* 编写抽取随机抽取一张扑克的方法
*/
public static Poke getOnePoke(List<Poke> pokelist) {
Poke poke=pokelist.get((int) (Math.random()*53));
return poke;
}
}
```
## 测试
```java
import java.util.List;
/**
* @class_name PokeMain
* @effect 测试
* @author Mr hu
* @date 2018年11月10日 下午10:28:45
*/
public class PokeMain {
// 生成一副扑克
public static List<Poke> pokelist = PokeBehavior.generatePoke();
public static void main(String[] args) {
// 输出初始状态poke
System.out.println("原生poke:");
for (Poke poke : pokelist) {
System.out.print(poke.toString());
int index = pokelist.indexOf(poke);
if (index == 12 || index == 25 || index == 38 || index == 51) {
System.out.println();
}
}
System.out.println();
// 测试打乱顺序
pokelist = PokeBehavior.disruptPoke(pokelist);
System.out.println("打乱顺序poke:");
for (Poke poke : pokelist) {
System.out.print(poke.toString());
int index = pokelist.indexOf(poke);
if (index == 12 || index == 25 || index == 38 || index == 51) {
System.out.println();
}
}
System.out.println();
// 测试按花色排序
pokelist = PokeBehavior.sortColor(pokelist);
System.out.println("按花色排序poke:");
for (Poke poke : pokelist) {
System.out.print(poke.toString());
int index = pokelist.indexOf(poke);
if (index == 12 || index == 25 || index == 38 || index == 51) {
System.out.println();
}
}
System.out.println();
// 测试按大小排序(先打乱上一步的花色排序)
pokelist = PokeBehavior.disruptPoke(pokelist);
pokelist = PokeBehavior.sortSize(pokelist);
System.out.println("按大小排序poke:");
for (Poke poke : pokelist) {
System.out.print(poke.toString());
int index = pokelist.indexOf(poke);
if (index == 3 || index == 7 || index == 11 || index == 15 || index == 19 ||
index == 23 || index == 27|| index == 31 || index == 35 || index == 39 ||
index == 43 || index == 47 || index == 51) {
System.out.println();
}
}
System.out.println();
// 测试既按大小又按花色排序
pokelist = PokeBehavior.sortColorAndSize(pokelist);
System.out.println("既按大小又按花色排序poke:");
for (Poke poke : pokelist) {
System.out.print(poke.toString());
int index = pokelist.indexOf(poke);
if (index == 12 || index == 25 || index == 38 || index == 51) {
System.out.println();
}
}
System.out.println();
System.out.println("随机抽取一个poke:");
System.out.println(PokeBehavior.getOnePoke(pokelist).toString());
}
}
```
## 输出结果
```
原生poke:
[♥:A][♥:2][♥:3][♥:4][♥:5][♥:6][♥:7][♥:8][♥:9][♥:10][♥:J][♥:Q][♥:K]
[♢:A][♢:2][♢:3][♢:4][♢:5][♢:6][♢:7][♢:8][♢:9][♢:10][♢:J][♢:Q][♢:K]
[♤:A][♤:2][♤:3][♤:4][♤:5][♤:6][♤:7][♤:8][♤:9][♤:10][♤:J][♤:Q][♤:K]
[♣:A][♣:2][♣:3][♣:4][♣:5][♣:6][♣:7][♣:8][♣:9][♣:10][♣:J][♣:Q][♣:K]
[小:小王][大:大王]
打乱顺序poke:
[♤:9][♤:A][♣:A][♢:10][♣:8][♢:7][♢:6][♥:7][♢:9][♣:K][♤:6][♤:3][♢:8]
[♥:3][♤:8][♣:J][♢:4][♣:2][♢:A][♢:5][♢:K][♣:9][♥:10][♤:7][♢:Q][♥:K]
[♤:5][♥:9][♣:6][♣:3][♥:6][小:小王][♥:8][♣:7][♥:2][♢:2][大:大王][♤:K][♣:10]
[♤:J][♥:J][♥:4][♣:5][♢:J][♤:2][♥:Q][♥:A][♤:4][♢:3][♣:4][♥:5][♤:Q]
[♣:Q][♤:10]
按花色排序poke:
[♥:7][♥:3][♥:10][♥:K][♥:9][♥:6][♥:8][♥:2][♥:J][♥:4][♥:Q][♥:A][♥:5]
[♢:10][♢:7][♢:6][♢:9][♢:8][♢:4][♢:A][♢:5][♢:K][♢:Q][♢:2][♢:J][♢:3]
[♤:9][♤:A][♤:6][♤:3][♤:8][♤:7][♤:5][♤:K][♤:J][♤:2][♤:4][♤:Q][♤:10]
[♣:A][♣:8][♣:K][♣:J][♣:2][♣:9][♣:6][♣:3][♣:7][♣:10][♣:5][♣:4][♣:Q]
[小:小王][大:大王]
按大小排序poke:
[♥:A][♣:A][♤:A][♢:A]
[♤:2][♥:2][♣:2][♢:2]
[♣:3][♤:3][♢:3][♥:3]
[♤:4][♣:4][♢:4][♥:4]
[♣:5][♤:5][♥:5][♢:5]
[♣:6][♥:6][♤:6][♢:6]
[♤:7][♣:7][♢:7][♥:7]
[♥:8][♤:8][♢:8][♣:8]
[♤:9][♢:9][♥:9][♣:9]
[♥:10][♢:10][♣:10][♤:10]
[♤:J][♥:J][♣:J][♢:J]
[♤:Q][♣:Q][♥:Q][♢:Q]
[♤:K][♣:K][♥:K][♢:K]
[小:小王][大:大王]
既按大小又按花色排序poke:
[♥:A][♥:2][♥:3][♥:4][♥:5][♥:6][♥:7][♥:8][♥:9][♥:10][♥:J][♥:Q][♥:K]
[♢:A][♢:2][♢:3][♢:4][♢:5][♢:6][♢:7][♢:8][♢:9][♢:10][♢:J][♢:Q][♢:K]
[♤:A][♤:2][♤:3][♤:4][♤:5][♤:6][♤:7][♤:8][♤:9][♤:10][♤:J][♤:Q][♤:K]
[♣:A][♣:2][♣:3][♣:4][♣:5][♣:6][♣:7][♣:8][♣:9][♣:10][♣:J][♣:Q][♣:K]
[小:小王][大:大王]
随机抽取一个poke:
[♥:4]
```
## 赞助请求V3
**建站因为热爱,生活需要Money,请屏幕前的大佬动动您发财的小手,点击一次以示鼓励,祝您生活愉快!**
<!-- 文章内嵌广告位 -->
<div class="article-ads"></div>
> PS:就目前的访问量,即便每个访客都点一次广告,收入也不足以支付运营成本。`如果看不到广告,可能是网络原因或被拦截了,那就算了吧。再次祝您生活愉快~~`END
评论
登录后查看和发表评论
前往登录