目录
一、升级综合案列需求分析
1.1 准备牌
- 准备108张共计两副牌,存储到一个集合当中
- 特殊牌:两张大王,两张小王
- 其他牌:
- 定义一个数组/集合,存储四种花色:♠,♥,♣,♦
- 定义一个数组/集合,存储13个序号:2,A,K,Q,J,10,9,8,7,6,5,4,3
- 循环嵌套遍历两个数组/集合,组装108张牌
1.2 洗牌
- 使用集合工具类Collections的方法
- static void shuffle(List<?> list) 使用指定的随机源对指定列表进行置换
- 随机打乱集合中的元素顺序
1.3 发牌
- 要求
- 一共四个玩家,每个玩家25张牌,共计108张,剩余8张作为底牌,一人一张轮流发牌,集合的索引(0-108)% 4
- 定义5个集合,存储4个玩家的牌和底牌
- 索引>=100,给底牌发牌
- 索引%4等于0给第一个玩家发牌
- 索引%4等于1给第二个玩家发牌
- 索引%4等于2给第三个玩家发牌
- 索引%4等于3给第四个玩家发牌
1.4 看牌
- 直接打印集合或遍历存储玩家和底牌的集合
二、代码实现
2.1 准备牌
- 定义一个存储108张牌的ArrayList集合,泛型使用String
package DemoDdz;
import java.util.ArrayList;
public class Upgrade {
public static void main(String[] args) {
// 1、准备牌
// 定义一个存储108张牌的ArrayList集合,泛型使用String
ArrayList<String> poker = new ArrayList<>();
// 定义两个数组,一个数组存储牌的花色,一个数组存储牌的序号
String[] colors = {"♥", "♠", "♣", "♦","♥", "♠", "♣", "♦"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
// 把两副牌的大小王都存储到poker集合中
poker.add("♚大王");
poker.add("♔小王");
poker.add("♚大王");
poker.add("♔小王");
// 循环嵌套遍历两个素组,组装108张牌
for (String number : numbers) {
for (String color : colors) {
// 把组装好的牌存储到poker集合中
poker.add(color + number);
}
}
}
}
2.2 洗牌
- 使用集合工具类Collections中的方法
Collections.shuffle(poker);
2.3 发牌
- 定义5个集合,存储4个玩家的牌和底牌
package DemoDdz;
import java.util.ArrayList;
import java.util.Collections;
public class Upgrade {
public static void main(String[] args) {
// 1、准备牌
// 定义一个存储108张牌的ArrayList集合,泛型使用String
ArrayList<String> poker = new ArrayList<>();
// 定义两个数组,一个数组存储牌的花色,一个数组存储牌的序号
String[] colors = {"♥", "♠", "♣", "♦", "♥", "♠", "♣", "♦"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
// 把两副牌的大小王都存储到poker集合中
poker.add("♚大王");
poker.add("♔小王");
poker.add("♚大王");
poker.add("♔小王");
// 循环嵌套遍历两个素组,组装108张牌
for (String number : numbers) {
for (String color : colors) {
// 把组装好的牌存储到poker集合中
poker.add(color + number);
}
}
/*
2、洗牌
使用集合工具类Collections中的方法
static void shuffle(list<?> list) 使用默认随机源对指定列表进行置换;
*/
Collections.shuffle(poker);
/*
3、发牌
定义5个集合,存储玩家的牌和底牌
*/
ArrayList<String> playe01 = new ArrayList<>();
ArrayList<String> playe02 = new ArrayList<>();
ArrayList<String> playe03 = new ArrayList<>();
ArrayList<String> playe04 = new ArrayList<>();
ArrayList<String> card = new ArrayList<>();
/*
循环遍历poker集合,获取每一张牌
使用poker集合的索引%4给4个玩家轮流发牌
剩余八张牌给底牌,需要先判断(i>=100)
*/
for (int i = 0; i < poker.size(); i++) {
// 获取每一张牌
String p = poker.get(i);
// 轮流发牌
if (i >= 100) {
// 给底牌发牌
card.add(p);
} else if (i % 4 == 0) {
playe01.add(p);
} else if (i % 4 == 1) {
playe02.add(p);
} else if (i % 4 == 2) {
playe03.add(p);
} else if (i % 4 == 3) {
playe04.add(p);
}
}
}
}
2.4 看牌-完整代码实现
package DemoDdz;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class Upgrade {
public static void main(String[] args) {
// 1、准备牌
// 定义一个存储108张牌的ArrayList集合,泛型使用String
ArrayList<String> poker = new ArrayList<>();
// 定义两个数组,一个数组存储牌的花色,一个数组存储牌的序号
String[] colors = {"♥", "♠", "♣", "♦", "♥", "♠", "♣", "♦"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
// 把两副牌的大小王都存储到poker集合中
poker.add("♚大王");
poker.add("♔小王");
poker.add("♚大王");
poker.add("♔小王");
// 循环嵌套遍历两个素组,组装108张牌
for (String number : numbers) {
for (String color : colors) {
// 把组装好的牌存储到poker集合中
poker.add(color + number);
}
}
/*
2、洗牌
使用集合工具类Collections中的方法
static void shuffle(list<?> list) 使用默认随机源对指定列表进行置换;
*/
Collections.shuffle(poker);
/*
3、发牌
定义5个集合,存储玩家的牌和底牌
*/
ArrayList<String> playe01 = new ArrayList<>();
ArrayList<String> playe02 = new ArrayList<>();
ArrayList<String> playe03 = new ArrayList<>();
ArrayList<String> playe04 = new ArrayList<>();
ArrayList<String> card = new ArrayList<>();
/*
循环遍历poker集合,获取每一张牌
使用poker集合的索引%4给4个玩家轮流发牌
剩余八张牌给底牌,需要先判断(i>=100)
*/
for (int i = 0; i < poker.size(); i++) {
// 获取每一张牌
String p = poker.get(i);
// 轮流发牌
if (i >= 100) {
// 给底牌发牌
card.add(p);
} else if (i % 4 == 0) {
playe01.add(p);
} else if (i % 4 == 1) {
playe02.add(p);
} else if (i % 4 == 2) {
playe03.add(p);
} else if (i % 4 == 3) {
playe04.add(p);
}
}
// 4、看牌
// 直接打印输出
System.out.println("星爷:" + playe01);
System.out.println("发哥:" + playe02);
System.out.println("华仔:" + playe03);
System.out.println("赌神:" + playe04);
System.out.println("底牌:" + card);
// 遍历数组中的所有元素
Iterator<String> it = poker.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
2.5 实现效果
- 打印输出后,每次刷新所发的牌均不一样
本文含有隐藏内容,请 开通VIP 后查看