Java使用ArrayList实现扑克牌的示例代码
短信预约 -IT技能 免费直播动态提醒
前言
学习了关于集合类的知识,我们可以做一个小项目来加深对集合类知识的学习!
一、项目要求
代码实现,一副扑克牌(不包括大小王)的购买、打乱、发牌。
二、具体实现
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);
}
}
到此这篇关于Java使用ArrayList实现扑克牌的示例代码的文章就介绍到这了,更多相关Java ArrayList扑克牌内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341