目录
1.大众麻将洗牌小程序
1.1实现效果
1.2源码展示
2.扑克牌洗牌小程序
2.1实现效果
2.2源码展示
大众麻将有3种牌色分别为万、条、筒。每种牌色有9张,如一万到九万。每种牌色有四张,因此万、条、筒*9*4一共108张。因此,设计洗牌流程如下。
(1)打印、洗牌
(2)四人牌面展示、剩余牌面展示
(1)Test.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//大众麻将通常情况下四人为一局游戏,每个人拿13张牌
public class Test {
public static void shows() {
//实例化一个Play类的对象
Play play = new Play();
//String类型的一维顺序表
List list = play.buyMaJiang(new ArrayList<>());
//String类型的二维顺序表
List> lists = new ArrayList<>();
//打印出牌面
System.out.println("牌面为万、条、筒:");
for (int i = 0; i < 4; i++) {
System.out.println(list);
}
System.out.println("共108张!");
System.out.println("========================================分割线=========================================");
//打印出洗牌后的牌面
System.out.println("洗牌:");
for (int i = 0; i < 4; i++) {
play.xiPai(list);
lists.add(new ArrayList<>(list));
System.out.println(list);
}
System.out.println("========================================分割线=========================================");
for (int i = 0; i < 4; i++) {
System.out.print("第"+(i+1)+"个人牌面为:");
for (int j = 0; j < 13; j++) {
System.out.print(lists.get(i).get(j)+" ");
lists.get(i).remove(j);
}
System.out.println();
}
System.out.println("========================================分割线=========================================");
System.out.println("剩余牌面:");
for (int i = 0; i < 4; i++) {
System.out.println(lists.get(i));
}
}
public static void main(String[] args) {
shows();
}
}
(2)MaJiang.java
public class MaJiang {
//初始化麻将,一个二维数组
public static final String[][] CardFace = {
{"\uD83C\uDC07","\uD83C\uDC08","\uD83C\uDC09","\uD83C\uDC0A",
"\uD83C\uDC0B","\uD83C\uDC0C","\uD83C\uDC0D","\uD83C\uDC0E","\uD83C\uDC0F"
},
{"\uD83C\uDC10","\uD83C\uDC11","\uD83C\uDC12","\uD83C\uDC13",
"\uD83C\uDC14","\uD83C\uDC15","\uD83C\uDC16","\uD83C\uDC17","\uD83C\uDC18"
},
{"\uD83C\uDC19","\uD83C\uDC1A","\uD83C\uDC1B","\uD83C\uDC1C",
"\uD83C\uDC1D","\uD83C\uDC1E","\uD83C\uDC1F","\uD83C\uDC20","\uD83C\uDC21"
}
};
}
(3)Play.java
import java.util.List;
import java.util.Random;
public class Play extends MaJiang {
//购买一副牌
public List buyMaJiang(List maJiangs) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
/*MaJiang maJiang = new MaJiang();*/
maJiangs.add(CardFace[i][j]);
}
}
return maJiangs;
}
//洗牌
public void xiPai(List maJiangs) {
for (int i = maJiangs.size()-1; i >0 ; i--) {
Random random = new Random();
int str = random.nextInt(i);
swap(maJiangs,i,str);
}
}
//交换
private void swap(List maJiangs,int i,int j) {
String tmp = maJiangs.get(j);
maJiangs.set(j,maJiangs.get(i));
maJiangs.set(i,tmp);
}
}
扑克牌一共有六种花色,红桃、黑桃、梅花、方块、大王、小王。在此程序中,我们不考虑大小王。每种花色从1-13(A-K),因此一共有4*13=52张牌。设计思想如下:
(1)Test.java
package demo;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
//实例化出一个Game类型的对象
Game game = new Game();
//实例化一个泛型类Poker
List pokers = game.disPlay(new ArrayList<>());
//打印出顺序表
System.out.println("牌面为:");
game.disPlay(pokers);
System.out.println(pokers);
//实例化一个二维顺序表
List> lists = game.play(pokers);
//打印三个人的牌面
for (int i = 0; i < 3; i++) {
System.out.print("第"+(i+1)+"个人为:"+lists.get(i));
System.out.println();
}
//打印剩余的牌
System.out.println("剩余的牌为:");
System.out.println(pokers);
}
}
(2)Poker.java
package demo;
public class Poker {
//花色
private String huaSe;
//数字
private int number;
//提供一个构造方法,方便我们设置扑克牌
public Poker(String huaSe, int number) {
this.huaSe = huaSe;
this.number = number;
}
//重写toString方法,使打印的并非地址值
@Override
public String toString() {
return huaSe+" "+number;
}
}
(3)Game.java
package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Game {
private static final String[] card= {"♠","♦","♥","♣"};
//打印扑克牌
public List disPlay(List pokers) {
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
Poker poker = new Poker(card[i],j);
pokers.add(poker);
}
}
return pokers;
}
//玩牌
public List> play(List pokers) {
//实例出一个二维顺序表
List> lists = new ArrayList<>();
//实例出三个顺序表,三个玩家
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
List list3 = new ArrayList<>();
lists.add(list1);
lists.add(list2);
lists.add(list3);
//随机生成某个下标
for (int i = pokers.size()-1; i >0 ; i--) {
Random random = new Random();
int num = random.nextInt(i);
//调用swap使随机生成的下标值与i值互换
swap(pokers,num,i);
}
//二维顺序表存储中一维顺序表存储数据
for (int i = 0; i <5 ; i++) {
for (int j = 0; j < 3; j++) {
Poker removePoker = pokers.remove(0);
lists.get(j).add(removePoker);
}
}
return lists;
}
//打乱扑克牌
public void swap(List pokers,int i,int j) {
Poker tmp = pokers.get(j);
pokers.set(j,pokers.get(i));
pokers.set(i,tmp);
}
}
本期博客,主要是为了针对ArrayList顺序表-进行设计的两个小练习,大家也可以设置出自己想要编写的程序,快去试试吧~