The First------JAVA完成简单斗地主程序
主要运用
JAVA基础复习
- 新建牌库 ,一共54张;
- 增加了 扑克牌中的花色与数字 ,
- 增加了支持洗牌 功能;
- 从总牌库中 增减三张底牌 ;
- 增加了 发牌,排序显示 等功能。
最后排序的主要思路
开始新建一个数组,建立牌库时就将这个数组加进去,用简单的**i++**模式,每当生成一个新牌面的时候就加一,并且添加之间的关联关系,最后排序的时候可以直接排序这个数组。
最终展示
链接: link.
图片:
代码展示与解析
新建54张牌,由于之后需要排序输出,所以多建一个Map数组
ArrayList<Integer> pokes = new ArrayList<>();//用于排序的牌面大小
Map<Integer, String> poker = new HashMap<>();//定义的牌面
对于牌面的新建我们选择分别新建两个数组再进行排列组合
List<String> numbers = new ArrayList<>();
List<String> colors = new ArrayList<>();
int index = 0;
//双重循环
for (String n : numbers) {
for (String c : colors) {
poker.put(index, c + n);//添加到牌面
pokes.add(index);//存到牌面大小里面用于排序
index++;
}
}
这里多定义了一个index通过加入循环的方式可以方便比较大小达到排序的目的
使用for循环进行发牌(需要新建三个玩家数组与一个底牌数组)
for (int i = 0; i < pokes.size(); i++) {
if (i >= 51) {
dipai.add(pokes.get(i));
} else if (i % 3 == 0) {
***.add(pokes.get(i));
} else if (i % 3 == 1) {
***.add(pokes.get(i));
} else if (i % 3 == 2) {
***.add(pokes.get(i));
}
}
排序使用sort方法
Collections.sort(***);//使用的是玩家1数组
Collections.sort(***);//使用的是玩家2数组
Collections.sort(***);//使用的是玩家3数组
Collections.sort(dipai);
最后输出,为了使结果美观可以使用println \n print交替使用的方式
System.out.println("***拿到的牌为:");
for (Integer poke : ***) {
System.out.print(poker.get(poke));
}
System.out.println("\n***拿到的牌为:");
for (Integer poke : ***) {
System.out.print(poker.get(poke));
}
System.out.println("\n***拿到的牌为:");
for (Integer poke : ***) {
System.out.print(poker.get(poke));
}
System.out.println("\n底牌为:");
for (Integer poke : dipai) {
System.out.print(poker.get(poke));