练习:斗地主综合案例:有序版本
步骤:
- 准备牌
- 洗牌
- 发牌
- 排序
- 看牌
实现代码(一)
package com.Map;
import java.util.*;
/*
练习:斗地主综合案例:有序版本
步骤:
1.准备牌
2.洗牌
3.发牌
4.排序
5.看牌
*/
public class Demo09DouDiZhu {
public static void main(String[] args) {
//1.准备牌
//创建一个Map集合,存储牌的索引和组装好的牌
HashMap<Integer,String> pokerMap = new HashMap<>();
//创建一个List集合,存储牌的索引
ArrayList<Integer> pokerIndex = new ArrayList<>();
//创建一个List集合,存储组装好的牌
ArrayList<String> pokerList = new ArrayList<>();
String[] colors = new String[] {"♠", "♥", "♣", "♦"};
String[] numbers = new String[]{"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//往pokerList添加大王小王
pokerList.add("大王");
pokerList.add("小王");
//循环遍历,往里面添加其余52张牌
for (String c:colors){
for (String n:numbers){
pokerList.add(c+n);
}
}
System.out.println(pokerList);
//往Map集合中存储牌的索引和组装好的牌
for (int i = 0; i <pokerList.size() ; i++) {
pokerMap.put(i,pokerList.get(i));
pokerIndex.add(i);
}
//2.洗牌,将牌的索引打乱
Collections.shuffle(pokerIndex);
//3.发牌,创建四个List集合,3个玩家与底牌
ArrayList<Integer> player01 = new ArrayList<>();
ArrayList<Integer> player02 = new ArrayList<>();
ArrayList<Integer> player03 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
//发牌
for (int i =0;i<pokerIndex.size();i++){
Integer in= pokerIndex.get(i);
if (i>=51){
diPai.add(in);
}else if (i%3==0){
player01.add(in);
}
else if (i%3==1){
player02.add(in);
}
else if (i%3==2){
player03.add(in);
}
}
//4.排序:将玩家的牌的索引与底牌的索引进行排序,保证牌的有序
Collections.sort(player01);
Collections.sort(player02);
Collections.sort(player03);
Collections.sort(diPai);
//5.看牌
System.out.print("刘德华:");
for (Integer integer : player01) {
String value = pokerMap.get(integer);
System.out.print(value+" ");
}
System.out.println();
System.out.print("周润发:");
for (Integer integer : player02) {
String value = pokerMap.get(integer);
System.out.print(value+" ");
}
System.out.println();
System.out.print("周星驰:");
for (Integer integer : player03) {
String value = pokerMap.get(integer);
System.out.print(value+" ");
}
System.out.println();
System.out.print("底牌:");
for (Integer integer : diPai) {
String value = pokerMap.get(integer);
System.out.print(value+" ");
}
}
}
实现代码(二)优化版
package com.Map;
import java.util.*;
/*
练习:斗地主综合案例:有序版本
步骤:
1.准备牌
2.洗牌
3.发牌
4.排序
5.看牌
*/
public class Demo10DouDiZhu {
public static void main(String[] args) {
//1.准备牌
//创建一个Map集合,存储牌的索引和组装好的牌
HashMap<Integer,String> poker = new HashMap<>();
//创建一个List集合,存储牌的索引
ArrayList<Integer> pokerIndex = new ArrayList<>();
//定义两个集合,存储花色和牌的序号
List<String> colors = List.of("♠", "♥", "♣", "♦");
List<String> numbers = List.of("2","A","K","Q","J","10","9","8","7","6","5","4","3");
int index =0;
poker.put(index,"大王");
pokerIndex.add(index);
index++;
poker.put(index,"小王");
pokerIndex.add(index);
index++;
//循环嵌套遍历两个集合,组装52张牌,存储到集合中
for (String number : numbers) {
for (String color : colors) {
poker.put(index,color+number);
pokerIndex.add(index);
index++;
}
}
/*
2.洗牌
使用CollectionS中的方法shuffle(List)
*/
Collections.shuffle(pokerIndex);
/*
3.发牌
*/
//定义四个集合,存储玩家牌的索引和底牌的索引
ArrayList<Integer> player01 = new ArrayList<>();
ArrayList<Integer> player02 = new ArrayList<>();
ArrayList<Integer> player03 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
//遍历存储牌的索引的List集合,获取每一个牌的索引
for (int i =0;i<pokerIndex.size();i++){
Integer in= pokerIndex.get(i);
//先判断底牌
if (i>=51){
//给底牌发牌
diPai.add(in);
}else if (i%3==0){
//给玩家1发牌
player01.add(in);
}
else if (i%3==1){
//给玩家2发牌
player02.add(in);
}
else if (i%3==2){
//给玩家3发牌
player03.add(in);
}
}
/*
4.排序
使用Collections中的方法sort(list)
默认是升序
*/
Collections.sort(player01);
Collections.sort(player02);
Collections.sort(player03);
Collections.sort(diPai);
/*
5.看牌
调用看牌的方法
*/
looPoker("刘德华",poker,player01);
looPoker("周润发",poker,player02);
looPoker("周星驰",poker,player03);
looPoker("底牌",poker,diPai);
}
/*
定义一个看牌方法,提高代码的复用性
参数:
String name:玩家名称
HashMap<Integer,String> poker:存储牌的集合
ArrayList<Integer> list :存储玩家和底牌的List集合
查表法:
遍历玩家或者底牌集合,获取牌的索引
使用牌的索引,去Map集合中,找到对应的牌
*/
private static void looPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
//玩家,不换行
System.out.print(name+":");
//遍历玩家和底牌集合,获取牌的索引
for (Integer key:list){
//使用牌的索引,去Map集合中,找到对应的牌
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println(); //打印完每一个玩家的牌,换行
}
}