Java使用ArrayList实现扑克牌的示例代码
作者:摸鱼王胖嘟嘟 时间:2021-10-29 15:17:17
前言
学习了关于集合类的知识,我们可以做一个小项目来加深对集合类知识的学习!
一、项目要求
代码实现,一副扑克牌(不包括大小王)的购买、打乱、发牌。
二、具体实现
2.1 Card类
class Card {
private int rank;//数字
private String suit;//花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return "[ " + this.suit + ":"+this.rank+" ]";
}
}
2.2 生成扑克牌
private static final String[] suits = {"♥", "♠", "♣", "♦"};
//假设没有大小王:1 2 3............. 11 12 13
public static List<Card> buyCard() {
ArrayList<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
// String suit = suits[i];
// int rank = j;
// Card card = new Card(rank, suit);
// cards.add(card);
cards.add(new Card(j,suits[i]));
}
}
return cards;
}
2.3 打乱顺序
private static void swap(List<Card> cards, int i, int j) {
Card tmp = cards.get(i);
cards.set(i,cards.get(j));
cards.set(j,tmp);
}
//洗牌
public static void shuffle(List<Card> cards) {
int size = cards.size();
for (int i = size-1; i > 0 ; i--) {
Random random = new Random();
int rand = random.nextInt(i);
swap(cards, i, rand);
}
}
2.4 发牌
System.out.println("揭牌:3个人每个人轮流揭牌5张牌");
ArrayList<List<Card>> hand = new ArrayList<>();
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
//每个人,轮流揭牌
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card card = cards.remove(0);
hand.get(j).add(card);
}
}
三、Test.java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card {
private int rank;//数字
private String suit;//花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return "[ " + this.suit + ":"+this.rank+" ]";
}
}
public class Test1 {
private static final String[] suits = {"♥", "♠", "♣", "♦"};
//假设没有大小王:1 2 3............. 11 12 13
public static List<Card> buyCard() {
ArrayList<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
// String suit = suits[i];
// int rank = j;
// Card card = new Card(rank, suit);
// cards.add(card);
cards.add(new Card(j,suits[i]));
}
}
return cards;
}
private static void swap(List<Card> cards, int i, int j) {
Card tmp = cards.get(i);
cards.set(i,cards.get(j));
cards.set(j,tmp);
}
//洗牌
public static void shuffle(List<Card> cards) {
int size = cards.size();
for (int i = size-1; i > 0 ; i--) {
Random random = new Random();
int rand = random.nextInt(i);
swap(cards, i, rand);
}
}
public static void main(String[] args) {
List<Card> cards = buyCard();
System.out.println("买牌:" + cards);
shuffle(cards);
System.out.println("洗牌:" + cards);
System.out.println("揭牌:3个人每个人轮流揭牌5张牌");
ArrayList<List<Card>> hand = new ArrayList<>();
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
//每个人,轮流揭牌
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card card = cards.remove(0);
hand.get(j).add(card);
}
}
System.out.println("第一个人的牌:"+ hand1);
System.out.println("第二个人的牌:"+ hand2);
System.out.println("第三个人的牌:"+ hand3);
System.out.println("剩下的牌:"+cards);
}
public static void main1(String[] args) {
// 1. 构造一副扑克牌
// 2. 揭牌
Card card = new Card(3,"♥");
System.out.println(card);
}
}
来源:https://blog.csdn.net/weixin_61341342/article/details/127152129
标签:Java,ArrayList,扑克牌
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决RedisTemplate调用increment报错问题
2023-11-20 06:35:05
myBatis实现三 级嵌套复杂对象的赋值问题
2023-11-23 06:42:26
使用java springboot设计实现的图书管理系统(建议收藏)
2023-11-25 00:54:49
![](https://img.aspxhome.com/file/2023/8/59928_0s.jpg)
Java编程实现对十六进制字符串异或运算代码示例
2023-11-06 15:58:15
![](https://img.aspxhome.com/file/2023/2/58892_0s.png)
c#实现输出本月的月历
2023-07-18 19:41:33
Java截取字符串的几种方法示例
2023-11-29 12:36:32
![](https://img.aspxhome.com/file/2023/5/60585_0s.png)
Java后台批量生产echarts图表并保存图片
2023-11-25 03:24:39
JDK1.8中的ConcurrentHashMap源码分析
2023-11-27 06:02:32
![](https://img.aspxhome.com/file/2023/1/60481_0s.jpg)
深入了解Spring中最常用的11个扩展点
2023-07-05 17:46:40
![](https://img.aspxhome.com/file/2023/1/57531_0s.png)
零基础写Java知乎爬虫之获取知乎编辑推荐内容
2023-11-29 04:33:39
![](https://img.aspxhome.com/file/2023/3/60783_0s.png)
Java经典用法总结(二)
2023-11-24 20:39:10
springboot对接支付宝支付接口(详细开发步骤总结)
2023-11-10 23:07:35
![](https://img.aspxhome.com/file/2023/6/58746_0s.png)
Flutter之Timer实现短信验证码获取60s倒计时功能的代码
2023-07-20 20:00:30
![](https://img.aspxhome.com/file/2023/8/89528_0s.gif)
使用C#发送Http请求实现模拟登陆实例
2023-06-22 22:25:07
![](https://img.aspxhome.com/file/2023/7/67047_0s.png)
Mybatis-Plus进阶分页与乐观锁插件及通用枚举和多数据源详解
2023-11-23 11:00:58
![](https://img.aspxhome.com/file/2023/2/59382_0s.png)
Java基础教程之static五大应用场景
2023-11-11 05:10:43
Java中stream处理中map与flatMap的比较和使用案例
2023-11-21 02:27:53
idea启动springmvc项目时报找不到类的解决方法
2023-11-09 16:51:04
![](https://img.aspxhome.com/file/2023/6/59416_0s.png)
Java用 Rhino/Nashorn 代替第三方 JSON 转换库
2023-11-04 02:20:26
kill命令在Java应用中使用的注意事项小结
2023-11-11 13:01:55
![](https://img.aspxhome.com/file/2023/9/58889_0s.jpg)