欢迎各位点赞👍 评论**✍收藏⭐**
目录
[1. 牌面的创建](#1. 牌面的创建)
[2. 牌面的设计](#2. 牌面的设计)
[2.1 创建一副牌](#2.1 创建一副牌)
[2.2 洗牌](#2.2 洗牌)
[2.3 发牌](#2.3 发牌)
[3. 全部码源](#3. 全部码源)
[3.1 MyCard.java](#3.1 MyCard.java)
[3.2 MyCardGame.java](#3.2 MyCardGame.java)
[3.3 PlayCard.java](#3.3 PlayCard.java)
[3.4 运行图片](#3.4 运行图片)
[4. 小结](#4. 小结)
1. 牌面的创建
可以先创建一个类来表示牌面的花色和面值
java
public class MyCard {
//扑克牌牌面数值
public int value;
//扑克牌花色
public String design;
@Override
public String toString() {
return "[ "+design+" "+value+" ]";
}
}
2. 牌面的设计
牌面创建好了之后,需要进行许多加工
2.1 创建一副牌
java
/*
* 创建一副牌
* */
public List<MyCard> createCard() {
List<MyCard> list = new ArrayList<>(51);
for (int i = 1; i <= 13; i++) {
for (int j = 0; j < SUIT.length; j++) {
MyCard myCard = new MyCard();
myCard.design = SUIT[j];
myCard.value = i;
list.add(myCard);
}
}
return list;
}
2.2 洗牌
java
/*
* 洗牌
* */
public void shuffleCard(List<MyCard> list) {
Random random = new Random();
for (int i = list.size() - 1; i >= 0; i--) {
int ranCount = random.nextInt(52);
swap(list,i,ranCount);
}
}
/*
* 交换
* */
private void swap(List<MyCard> list,int i,int ranCount) {
MyCard ret = list.get(i);
list.set(i,list.get(ranCount));
list.set(ranCount,ret);
}
2.3 发牌
java
/*
* 现有 5 个人,每人抽 6 张牌比大小
* */
List<List<MyCard>> peoples = new ArrayList<>();
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
for (int i = 0; i < 5; i++) { // 5人
for (int j = 0; j < 6; j++) { //抽 6 张牌
peoples.get(i).add(list.remove(0));
}
}
System.out.println("第一个人的牌:");
System.out.println(peoples.get(0));
System.out.println("第二个人的牌:");
System.out.println(peoples.get(1));
System.out.println("第三个人的牌:");
System.out.println(peoples.get(2));
System.out.println("第四个人的牌:");
System.out.println(peoples.get(3));
System.out.println("第五个人的牌:");
System.out.println(peoples.get(4));
3. 全部码源
3.1 MyCard.java
java
package deom1;
public class MyCard {
//扑克牌牌面数值
public int value;
//扑克牌花色
public String design;
@Override
public String toString() {
return "[ "+design+" "+value+" ]";
}
}
3.2 MyCardGame.java
java
package deom1;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MyCardGame {
/*
* 定义花色的种类
* */
public String[] SUIT = {"♥","♣","♦","♠"};
/*
* 创建一副牌
* */
public List<MyCard> createCard() {
List<MyCard> list = new ArrayList<>(51);
for (int i = 1; i <= 13; i++) {
for (int j = 0; j < SUIT.length; j++) {
MyCard myCard = new MyCard();
myCard.design = SUIT[j];
myCard.value = i;
list.add(myCard);
}
}
return list;
}
/*
* 洗牌
* */
public void shuffleCard(List<MyCard> list) {
Random random = new Random();
for (int i = list.size() - 1; i >= 0; i--) {
int ranCount = random.nextInt(52);
swap(list,i,ranCount);
}
}
/*
* 交换
* */
public void swap(List<MyCard> list,int i,int ranCount) {
MyCard ret = list.get(i);
list.set(i,list.get(ranCount));
list.set(ranCount,ret);
}
}
3.3 PlayCard.java
java
package deom1;
import java.util.ArrayList;
import java.util.List;
public class PlayCard {
public static void main(String[] args) {
MyCardGame myCardGame = new MyCardGame();
System.out.println("新的扑克牌:");
List<MyCard> list = myCardGame.createCard();
System.out.println(list);
System.out.println("洗牌:");
myCardGame.shuffleCard(list);
System.out.println(list);
/*
* 现有 5 个人,每人抽 6 张牌比大小
* */
List<List<MyCard>> peoples = new ArrayList<>();
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
peoples.add(new ArrayList<>());
for (int i = 0; i < 5; i++) { // 5人
for (int j = 0; j < 6; j++) { //抽 6 张牌
peoples.get(i).add(list.remove(0));
}
}
System.out.println("第一个人的牌:");
System.out.println(peoples.get(0));
System.out.println("第二个人的牌:");
System.out.println(peoples.get(1));
System.out.println("第三个人的牌:");
System.out.println(peoples.get(2));
System.out.println("第四个人的牌:");
System.out.println(peoples.get(3));
System.out.println("第五个人的牌:");
System.out.println(peoples.get(4));
}
}
3.4 运行图片
4. 小结
以上就是对 该算法 的了解,具体还需宝子们去实践,如果觉得该博客对你有用的话,希望一键三连,点个关注不迷路,谢谢支持