IT兄弟连 Java语法教程 综合案例

  • 2019 年 10 月 5 日
  • 筆記

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/ITXDL123/article/details/98025836

1.案例需求

编写程序,模拟斗地主游戏洗牌和发牌的流程。

2.应用知识

● 数组的声明

● 数组的遍历

● for循环

● if-else分支结构

3.需求解析

模拟斗地主游戏洗牌和发牌,需要在程序中定义一个String类型的数组用来存储扑克牌,数组的大小为54,初始化这个数组,将不同花色不同点数以及大小王以字符串的形式存储到这个数组中共54张牌。开始洗牌,定义一个新的数组,数组长度也是54,用来存储洗后的扑克牌,使用随机数在未洗牌的扑克数组中进行抽取,被随机到的扑克牌进入新数组中,然后在原数组中将该牌标记未已经被洗出去了,如果再次随机到该牌,则重新随机获取下一张牌,最后新数组中存储了洗好的扑克牌。牌洗好后,需要为三位玩家每人发17张牌,然后留下3张做为底牌,三位玩家的手牌使用三个数组表示,每个数组长度为17,底牌使用一个长度为3的数组表示。通过for循环依次为三位玩家发牌,当发到第51张牌时停止发牌,剩下的三张扑克牌存入底牌数组中。

4.需求实现

import java.util.Random;

public class PokerGame {

public static void main(String[] args){

//创建数组,用于存储扑克牌,扑克牌共54张,所以设定数组大小为54

String[] pokers = new String[54];

//定义花色数组,共4中花色。

String[] colors = {"红桃","黑桃","梅花","方片"};

//定义牌面数组

String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q", "K"};

//定义大小王

String[] kings = {"大王","小王"};

//使用循环将牌存储到pokers数组中

int index = 0; //index用来表示扑克牌数组的下标

for(int i = 0;i<numbers.length;i++){

for(int j = 0;j<colors.length;j++){

pokers[index++] = colors[j] + numbers[i];

}

}

//将大小王添加进扑克牌数组

pokers[index++] = kings[0];

pokers[index] = kings[1];

System.out.println("现在有一副新扑克");

//输出扑克牌

for (String poker : pokers){

System.out.print(poker+",");

}

//开始洗牌

//定义数组,用来存储被洗出的牌

String[] newPokers = new String[pokers.length];

//定义数组,用以表示被随机取出的牌

boolean[] make = new boolean[pokers.length];

//洗牌

for (int i = 0;i<pokers.length;i++){

//创建随机数对象

Random rd = new Random();

//获取随机数,当作即将被洗出的牌的下标

int pokerIndex = rd.nextInt(pokers.length);

//判断被洗出的牌是否已经被洗出了

if (!make[pokerIndex]) {

//如果该牌没被洗出则将该牌添加进洗出数组

newPokers[i] = pokers[pokerIndex];

//将该牌设置为已经被洗出了

make[pokerIndex] = true;

} else i–;//如果该牌已经被洗出了,控制扑克数组下标减1重新选牌

}

//控制换行输出

System.out.println();

//输出洗牌后的牌

for (String poker : newPokers){

System.out.print(poker+",");

}

//定义3名玩家手牌数组以及底牌数组

String[] one = new String[17];

String[] two = new String[17];

String[] three = new String[17];

String[] underPoker = new String[3];

//定义下标在发牌时表示不同玩家需要发的第几张牌

int oneIndex = 0;

int twoIndex = 0;

int threeIndex = 0;

int underPokerIndex = 0;

//循环进行按顺序发牌

for(int i = 0; i<newPokers.length ;i++){

//因为每位玩家只需要发17张手牌,最后3张留给底牌,

//所以发到51张时就不应该给继续给玩家发牌了

if(i<=50 && i % 3 == 0){

one[oneIndex++] = newPokers[i];

}else if(i<=50 && i % 3 ==1){

two[twoIndex++] = newPokers[i];

}else if (i<=50 && i % 3 == 2){

three[threeIndex++] = newPokers[i];

}else{

underPoker[underPokerIndex++] = newPokers[i];

}

}

//循环输出每名玩家的手牌以及底牌

System.out.println();

System.out.print("玩家1得到的牌:");

for (String poker: one){

System.out.print(poker + ",");

}

System.out.println();

System.out.print("玩家2得到的牌:");

for (String poker: two){

System.out.print(poker + ",");

}

System.out.println();

System.out.print("玩家3得到的牌:");

for (String poker: three){

System.out.print(poker + ",");

}

System.out.println();

System.out.print("底牌:");

for (String poker: underPoker){

System.out.print(poker + ",");

}

}

}