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,扑克牌
0
投稿
猜你喜欢
微信跳一跳辅助Java代码实现
2022-03-31 19:12:03
SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)
2022-11-05 23:12:26
SpringBoot Security密码加盐实例
2023-06-08 17:06:48
android使用handlerthread创建线程示例
2023-07-05 17:29:57
Android仿支付宝上芝麻信用分雷达图
2023-04-21 18:44:30
java后端进行跨域的几种方式小结
2021-09-03 14:53:31
浅析Java自定义注解的用法
2022-06-08 01:52:58
springboot bootstrap.yml nacos配置中心问题
2022-07-07 08:25:01
C#实现简单记事本程序
2022-07-20 23:52:37
Android App在线程中创建handler的方法讲解
2021-06-24 17:00:33
深入分析java并发编程中volatile的实现原理
2023-11-23 05:02:25
C#交错数组用法实例
2022-08-13 09:34:55
Winform 实现进度条弹窗和任务控制
2023-06-20 04:27:09
Java回调函数原理实例与代理模式的区别讲解
2023-04-16 21:21:01
SpringTask实现定时任务方法讲解
2022-06-11 11:43:42
Java8中的 Lambda表达式教程
2023-10-13 01:32:29
深入了解c#多线程编程
2022-08-14 15:13:58
Android开发之线程通信详解
2022-08-21 21:37:34
使用@Value值注入及配置文件组件扫描
2023-12-01 21:24:12
Android使用CountDownTimer模拟短信验证倒计时
2022-10-24 18:00:28