package Day02
import java.util.Random;
//模拟斗地主洗牌,发牌过程
public class Demo03 {
public static void main(String[] args) {
//创建一幅扑克牌
String[] cards = new String[54];
String[] rank = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (int i = 0; i < rank.length; i++) {
cards[i] = "红桃" + rank[i];
cards[i + 13] = "黑桃" + rank[i];
cards[i + 13 + 13] = "方块" + rank[i];
cards[i + 13 + 13 + 13] = "梅花" + rank[i];
}
cards[52] = "King";
cards[53] = "smallKing";
for (int i = 0; i < cards.length; i++) {
System.out.print(cards[i] + " ");
if ((i + 1) % 13 == 0) {
System.out.println();
}
}
System.out.println();
System.out.println();
//洗牌
Random random = new Random();
for (int i = cards.length - 1; i >= 1; i--) {
int j = random.nextInt(i);
String t = cards[i];
cards[i] = cards[j];
cards[j] = t;
}
for (int i = 0; i < cards.length; i++) {
System.out.print(cards[i] + " ");
if ((i + 1) % 13 == 0) {
System.out.println();
}
}
System.out.println();
System.out.println();
//发牌
String[] lee=new String[17];
String[] wang=new String[17];
String[] zhao=new String[17];
for (int i=0;i<17;i++){
lee[i]=cards[i*3];
wang[i]=cards[i*3+1];
zhao[i]=cards[i*3+2];
}
for (int i=0;i<lee.length;i++){
System.out.print(lee[i]+" ");
} System.out.println();
for (int i=0;i<wang.length;i++){
System.out.print(wang[i]+" ");
} System.out.println();
for (int i=0;i<zhao.length;i++){
System.out.print(zhao[i]+" ");
}
System.out.println();
System.out.println(cards[51]+" "+cards[52]+" "+cards[53]);
}
}
利用for循环模拟斗地主洗牌及发牌过程
斗地主游戏发牌模拟
最新推荐文章于 2022-09-26 10:27:57 发布
本篇博客介绍了一个简单的斗地主游戏发牌过程模拟程序。通过Java代码实现了扑克牌的创建、洗牌和发牌流程,并展示了具体的实现细节。

682

被折叠的 条评论
为什么被折叠?



