目录
一、题目:
二、分析:
三、代码:
完整代码:
一、题目:
有三个人,甲乙丙三人打扑克牌,用ArrayList实现扑克牌(52张),揭牌(三个人轮流揭五张牌),洗牌。(其中,J、Q、K用11、12、13 代替)
二、分析:
要写出来一副扑克牌,并且需要实现三个人轮流揭五张牌以及让牌的顺序打乱。
三、代码:
1、要想实现一副牌,首先定义一张牌,新建一个Poker类,来实现。
其中定义了花色suit、数字rank。
private String suit; //花色
private int rank; //数字
提供一个构造方法来构造出一副牌。
public Poker(String suit, int rank){
this.suit = suit;
this.rank = rank;
}
重写toString方法来打印牌
//重写toString方法
@Override
public String toString() {
return suit + " " + rank;
}
}
2、再对整副牌进行实现,定义一个类Porkers
①先定义整副牌的花色,为一个数组。
public static final String[] SUITS = {"♥","♠","♣","♦"};
②当使用ArrayList时,使用List来定义这副牌(porkerList)。花色有四个花色,每个花色有13张牌,所以使用两个for循环的嵌套来实现牌。再引用Porker类来写出牌的格式(花色,数字)。实现一张牌就用add,写入pokerList中,最后返回整副牌。
public static List<Poker> buyPokers(){
List<Poker> pokerList = new ArrayList<>(); //这里放了所有的牌
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
String suit = SUITS[i]; //花色是哪个
int rank = j; //数字是哪个
Poker poker = new Poker(suit,rank);
pokerList.add(poker);
}
}
return pokerList;
}
③洗牌功能
首先定义一个swap交换牌的方法,其中的参数为这副牌中的某一张牌,以及另外两个参数。定义一个Poker类型的tmp变量,放入pokerList 的i下标的值,然后再更新i下标的值为j下标的值,最后把j下标的值更新为tmp的值,就完成了交换。
public static void swap(List<Poker> pokerList,int i,int j){
Poker tmp = pokerList.get(i);
pokerList.set(i,pokerList.get(j));
pokerList.set(j,tmp);
}
交换完成之后,还需要遍历整副牌。
使用Random来生成随机数来进行洗牌。for循环从整副牌的倒数第二张牌开始,每次遍历下一张牌,调用swap方法,对遍历的牌进行交换。
public static void shuffle(List<Poker> pokerList){
Random random = new Random();
for (int i = pokerList.size() - 1; i > 0 ; i--) {
int index = random.nextInt(i);
swap(pokerList,i,index);
}
}
④揭牌功能
三个人轮流揭五张牌,把三个人看为一个数组,再把五张牌也看作一个数组,就可以把某个人揭某张牌对应起来,是一个二维数组。确保每次揭牌都是从第一张揭牌。
//揭牌 3个人,每个人轮流揭五张牌
List<Poker> hand1 = new ArrayList<>();
List<Poker> hand2 = new ArrayList<>();
List<Poker> hand3 = new ArrayList<>();
List<List<Poker>> hand = new ArrayList<>(); //通过List实现的二维数组,
// 让三个人都能和每张牌联系起来
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
List<Poker> handTmp= hand.get(j); //j下标其实就是一个List
//来确定是谁的牌
handTmp.add(pokerList.remove(0)); //每次都从0下标揭牌,也就是第一张
}
}
最后输出整副牌,洗过的牌,每个人揭的牌以及剩下的牌。
完整代码:
//Poker类
//定义单张牌
public class Poker {
private String suit; //花色
private int rank; //数字
//提供构造方法,构造一张牌
public Poker(String suit, int rank){
this.suit = suit;
this.rank = rank;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
//重写toString方法
@Override
public String toString() {
return suit + " " + rank;
}
}
//Pokers类
import java.nio.channels.Pipe;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Pokers {
//定义花色
public static final String[] SUITS = {"♥","♠","♣","♦"};
//首先,买一副牌
public static List<Poker> buyPokers(){
List<Poker> pokerList = new ArrayList<>(); //这里放了所有的牌
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
String suit = SUITS[i]; //花色是哪个
int rank = j; //数字是哪个
Poker poker = new Poker(suit,rank);
pokerList.add(poker);
}
}
return pokerList;
}
//洗牌
//交换牌
public static void swap(List<Poker> pokerList,int i,int j){
Poker tmp = pokerList.get(i);
pokerList.set(i,pokerList.get(j));
pokerList.set(j,tmp);
}
public static void shuffle(List<Poker> pokerList){
Random random = new Random();
for (int i = pokerList.size() - 1; i > 0 ; i--) {
int index = random.nextInt(i);
swap(pokerList,i,index);
}
}
public static void main(String[] args) {
List<Poker> pokerList = buyPokers();
System.out.println("买牌:"+pokerList);
shuffle(pokerList);
System.out.println("洗牌:"+pokerList);
//揭牌 3个人,每个人轮流揭五张牌
List<Poker> hand1 = new ArrayList<>();
List<Poker> hand2 = new ArrayList<>();
List<Poker> hand3 = new ArrayList<>();
List<List<Poker>> hand = new ArrayList<>(); //通过List实现的二维数组,
// 让三个人都能和每张牌联系起来
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
List<Poker> handTmp= hand.get(j); //j下标其实就是一个List
//来确定是谁的牌
handTmp.add(pokerList.remove(0)); //每次都从0下标揭牌,也就是第一张
}
}
for (int i = 0; i < hand.size(); i++) {
System.out.println("第"+ i + "个人的牌是:"+ hand.get(i));
}
System.out.println("剩余的牌:"+pokerList);
}
}
以上就是本次扑克牌的基础功能实现。