大家都完玩过斗地主,一副牌有什么组成呢?
答案是:一个牌盒和54张牌;54张牌有2到A各四种花色张加两张大小王
代码如下
String[] colors={"黑色","红桃","梅花","方块"};
String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] puke = new String[54];
现在我们需要把数字和花色拼接构成除开大小王,这里我们将运用到遍历
遍历方法
int k = 0; for (int i = 0; i < nums.length; i++) { for (int a = 0; a < colors.length; a++) { String card = colors[a] + nums[i]; puke[k] = card; k++; puke[52] = "小王"; puke[53] = "大王"; } }
现在我们的牌已经做好了,打牌你得洗牌
//洗牌
Random random = new Random();
for (int i = puke.length - 1; i > 1; i--) {
int j = random.nextInt(i);
String t = puke[i];
puke[i] = puke[j];
puke[j] = t;
最后一步就是分牌,这里不单独表示,全部代码
mport java.util.Arrays;
import java.util.Random;
public class Demo03 {
public static void main(String[] args) {
String[] nums = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "K", "Q", "J"};
String[] colors = {"黑色", "红桃", "梅花", "方块"};
String[] puke = new String[54];
int k = 0;
for (int i = 0; i < nums.length; i++) {
for (int a = 0; a < colors.length; a++) {
String card = colors[a] + nums[i];
puke[k] = card;
k++;
puke[52] = "小王";
puke[53] = "大王";
}
}
//洗牌
Random random = new Random();
for (int i = puke.length - 1; i > 1; i--) {
int j = random.nextInt(i);
String t = puke[i];
puke[i] = puke[j];
puke[j] = t;
}
String[] player1 = new String[17];
String[] player2 = new String[17];
String[] player3 = new String[17];
String[] dipai = new String[3];
dipai[0] = puke[53];
dipai[1] = puke[52];
dipai[2] = puke[51];
for (int i = 0; i < 17; i++) {
player1[i] = puke[i * 3];
player2[i] = puke[(i * 3) + 1];
player3[i] = puke[(i * 3) + 2];
}
for (String er1 : player1) {
System.out.print(er1 + " ");
}
System.out.println();
for (String er2 : player2) {
System.out.print(er2 + " ");
}
System.out.println();
for (String er3 : player3) {
System.out.print(er3 + " ");
}
System.out.println();
for (String c : dipai) {
System.out.print(c + " ");
}
}
}
随机抽取一张作为地主牌host
import java.util.Arrays;
import java.util.Random;
public class Demo03 {
public static void main(String[] args) {
String[] nums = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "K", "Q", "J"};
String[] colors = {"黑色", "红桃", "梅花", "方块"};
String[] puke = new String[54];
//成牌
int k = 0;
for (int i = 0; i < nums.length; i++) {
for (int a = 0; a < colors.length; a++) {
String card = colors[a] + nums[i];
puke[k] = card;
k++;
puke[52] = "小王";
puke[53] = "大王";
}
}
//洗牌
Random random = new Random();
for (int i = puke.length - 1; i > 1; i--) {
int j = random.nextInt(i);
String t = puke[i];
puke[i] = puke[j];
puke[j] = t;
}
//随机抽取一张牌作为地主牌
String[] host = new String[1];
for (int d = puke.length - 1; d > 1; d--) {
int j = random.nextInt(d);
host[0] = puke[j];
}
//设置3位玩家以及底牌
String[] player1 = new String[17];
String[] player2 = new String[17];
String[] player3 = new String[17];
String[] dipai = new String[3];
dipai[0] = puke[53];
dipai[1] = puke[52];
dipai[2] = puke[51];
//这里无法理解,建议画图,
//puke数组的0号索引给1号玩家 得出规律1号玩家的牌是3 的倍数
//puke数组的1号索引给2号玩家 得出规律1号玩家的牌是3 的倍数加1
//puke数组的2号索引给3号玩家 得出规律1号玩家的牌是3 的倍数加2
for (int i = 0; i < 17; i++) {
player1[i] = puke[i * 3];
player2[i] = puke[(i * 3) + 1];
player3[i] = puke[(i * 3) + 2];
}
//后面遍历出来
for (String er1 : player1) {
System.out.print(er1 + " ");
}
System.out.println();
for (String er2 : player2) {
System.out.print(er2 + " ");
}
System.out.println();
for (String er3 : player3) {
System.out.print(er3 + " ");
}
System.out.println();
for (String c : dipai) {
System.out.print(c + " ");
}
System.out.println();
for (String hosts : host) {
System.out.println(hosts + " ");
}
}
}
如果你后面想实现知道谁是地主的操作,那么你可以利用equals来对字符串进行比较,来知道谁是地主,这里我不做演示了
版权声明:本文为qq_42847719原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。