创建数组、集合,存放数据
复制 | public class FightAgainstLandlords { |
| |
| |
| |
| private ArrayList<String> poker; |
| |
| |
| |
| |
| private String[] colors; |
| |
| |
| |
| |
| private String[] numbers; |
| } |
构造方法FightAgainstLandlords
复制 | public class FightAgainstLandlords { |
| public FightAgainstLandlords(ArrayList<String> poker, String[] colors, String[] numbers) { |
| this.poker = poker; |
| this.colors = colors; |
| this.numbers = numbers; |
| } |
| } |
定义打乱牌牌序方法
复制 | public class FightAgainstLandlords { |
| |
| |
| |
| public ArrayList<String> fiftyFive() { |
| for (String color: colors) { |
| for (String number: numbers) { |
| poker.add(color + number); |
| } |
| } |
| poker.add("大王"); |
| poker.add("小王"); |
| |
| Collections.shuffle(poker); |
| return poker; |
| } |
| } |
发牌
复制 | public class FightAgainstLandlords { |
| |
| |
| |
| |
| |
| |
| public ArrayList<String> licensing(int j, ArrayList<String> pokers) { |
| |
| ArrayList<String> people1 = new ArrayList<>(); |
| ArrayList<String> people2 = new ArrayList<>(); |
| ArrayList<String> people3 = new ArrayList<>(); |
| |
| ArrayList<String> basePoker = new ArrayList<>(); |
| |
| for (int i = 0; i < pokers.size(); i++) { |
| String p = pokers.get(i); |
| if ( i < 51) { |
| if (i % 3 == 0) { |
| people1.add(p); |
| } else if (i % 3 == 1) { |
| people2.add(p); |
| } else { |
| people3.add(p); |
| } |
| } else { |
| basePoker.add(p); |
| } |
| } |
| |
| |
| if (j == 1) { |
| return people1; |
| } else if (j == 2) { |
| return people2; |
| } else if (j == 3) { |
| return people3; |
| } else { |
| return basePoker; |
| } |
| } |
| } |
测试FightAgainstLandlords类
复制 | import java.util.ArrayList; |
| |
| public class DemoFightAgainstLandlords { |
| public static void main(String[] args) { |
| |
| ArrayList<String> poker = new ArrayList<>(); |
| String[] colors = {"红桃", "黑桃", "梅花", "方块"}; |
| String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; |
| |
| |
| FightAgainstLandlords fightAgainstLandlords = new FightAgainstLandlords(poker, colors, numbers); |
| |
| ArrayList<String> pokers = fightAgainstLandlords.fiftyFive(); |
| |
| |
| ArrayList<String> people1 = fightAgainstLandlords.licensing(1, pokers); |
| ArrayList<String> people2 = fightAgainstLandlords.licensing(2, pokers); |
| ArrayList<String> people3 = fightAgainstLandlords.licensing(3, pokers); |
| ArrayList<String> basePoker = fightAgainstLandlords.licensing(4, pokers); |
| |
| |
| System.out.println("people1:" + people1); |
| System.out.println("people2:" + people2); |
| System.out.println("people3:" + people3); |
| System.out.println("basePoker:" + basePoker); |
| } |
| } |
复制 | 输出结果(每个人的牌,和底牌都是随机的): |
| people1:[红桃3, 梅花J, 梅花K, 方块J, 方块K, 梅花10, 红桃6, 梅花9, 黑桃Q, 红桃Q, 梅花4, 黑桃A, 方块2, 红桃8, 方块4, 黑桃8, 红桃K] |
| people2:[梅花A, 方块3, 小王, 黑桃J, 红桃7, 方块5, 方块9, 黑桃10, 方块8, 梅花Q, 方块6, 梅花6, 红桃10, 方块Q, 黑桃5, 黑桃2, 红桃A] |
| people3:[梅花5, 梅花8, 黑桃7, 黑桃4, 红桃9, 黑桃9, 黑桃K, 方块7, 黑桃6, 梅花3, 方块10, 红桃4, 黑桃3, 红桃5, 大王, 红桃J, 方块A] |
| basePoker:[红桃2, 梅花2, 梅花7] |
FightAgainstLandlords类的所有代码
复制 | import java.util.ArrayList; |
| import java.util.Collections; |
| |
| public class FightAgainstLandlords { |
| |
| |
| |
| |
| private ArrayList<String> poker; |
| |
| |
| |
| |
| private String[] colors; |
| |
| |
| |
| |
| private String[] numbers; |
| |
| public FightAgainstLandlords(ArrayList<String> poker, String[] colors, String[] numbers) { |
| this.poker = poker; |
| this.colors = colors; |
| this.numbers = numbers; |
| } |
| |
| |
| |
| |
| public ArrayList<String> fiftyFive() { |
| for (String color: colors) { |
| for (String number: numbers) { |
| poker.add(color + number); |
| } |
| } |
| poker.add("大王"); |
| poker.add("小王"); |
| |
| Collections.shuffle(poker); |
| return poker; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public ArrayList<String> licensing(int j, ArrayList<String> pokers) { |
| |
| ArrayList<String> people1 = new ArrayList<>(); |
| ArrayList<String> people2 = new ArrayList<>(); |
| ArrayList<String> people3 = new ArrayList<>(); |
| |
| ArrayList<String> basePoker = new ArrayList<>(); |
| |
| for (int i = 0; i < pokers.size(); i++) { |
| String p = pokers.get(i); |
| if ( i < 51) { |
| if (i % 3 == 0) { |
| people1.add(p); |
| } else if (i % 3 == 1) { |
| people2.add(p); |
| } else { |
| people3.add(p); |
| } |
| } else { |
| basePoker.add(p); |
| } |
| } |
| |
| |
| if (j == 1) { |
| return people1; |
| } else if (j == 2) { |
| return people2; |
| } else if (j == 3) { |
| return people3; |
| } else { |
| return basePoker; |
| } |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)