斗地主逻辑分析
1、准备牌:
准备牌54张,存储到一个集合中【特殊牌大王、小王】,
定义一个数组List colors = List.of( “♠”, “♥”, “♣”, “♦” );
定义一个数组存储所有的牌13张List colors = List.of( “2”,“A”…“3” );
2、洗牌:
随机打乱顺序再发牌
使用集合工具类 Collections 的方法
循环嵌套遍历所有的牌
shuffle(List<?> list, Random rnd):使用指定的随机源对指定列表进行置换。
会随机打乱集合中元素的顺序
3、发牌:
一人17张牌,剩余三张作为底牌,一人一张轮流发牌
定义四个集合用于存储【每人获得的牌(前三个集合),存储多余的三张牌(第四个集合)】
集合的索引(0-53) % 3
索引 % 2 有两个值(0、1)
索引 % 3 有三个值(0、1、2)
索引 >=51 改底牌发牌
4、排序
使用Collections中能够的sort(list)
5、看牌:
遍历一个集合,获取到另外一个集合的key,通过key查找到value
遍历玩家和底牌的List集合,获取到Map集合的key,通过key找到value
斗地主代码实现
package test.day20Map.poker;
import java.util.*;
/*
1、准备牌:
准备牌54张,存储到一个集合中【特殊牌大王、小王】,
定义一个数组List<String> colors = List.of( "♠", "♥", "♣", "♦" );
定义一个数组存储所有的牌13张List<String> colors = List.of( "2","A"..."3" );
2、洗牌:
随机打乱顺序再发牌
使用集合工具类 Collections 的方法
循环嵌套遍历所有的牌
shuffle(List<?> list, Random rnd):使用指定的随机源对指定列表进行置换。
会随机打乱集合中元素的顺序
3、发牌:
一人17张牌,剩余三张作为底牌,一人一张轮流发牌
定义四个集合用于存储【每人获得的牌(前三个集合),存储多余的三张牌(第四个集合)】
集合的索引(0-53) % 3
索引 % 2 有两个值(0、1)
索引 % 3 有三个值(0、1、2)
索引 >=51 改底牌发牌
4、排序
使用Collections中能够的sort(list)
5、看牌:
遍历一个集合,获取到另外一个集合的key,通过key查找到value
遍历玩家和底牌的List集合,获取到Map集合的key,通过key找到value
*/
public class Main {
public static void main(String[] args) {
//1、准备牌
//定义一个集合54张牌的HashMap集合
HashMap<Integer, String> poker = new HashMap<>();
//创建一个List集合,存储牌的索引
ArrayList<Integer> pokerIndex = new ArrayList<>();
//定义两个数组【存储牌的花色,序号】
List<String> colors = List.of("♠", "♥", "♣", "♦");
List<String> numbers = List.of("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K");
int index = 0;
//先把大王小王存储到poker中
poker.put(index,"大王");
pokerIndex.add(index);
index++;
poker.put(index,"小王");
pokerIndex.add(index);
index++;
//循环嵌套遍历两个数组,组装52张牌
for (String number: numbers) {
for (String color : colors) {
//System.out.println(color + number);
//将组装好的牌存储到poker集合中
poker.put(index,color + number);
pokerIndex.add(index);
index++;
}
}
//System.out.println(poker);
//System.out.println(pokerIndex);
//2、洗牌
Collections.shuffle(pokerIndex);
//System.out.println(poker);
//3、发牌
//定义4个集合,存储玩家的牌和底牌
ArrayList<Integer> player0 = new ArrayList<>();
ArrayList<Integer> player1 = new ArrayList<>();
ArrayList<Integer> player2 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
//遍历poker集合,获取每一张牌
//使用poker集合的索引%3给3个玩家轮流发牌
//剩余3张牌给底牌
for (int i = 0; i < pokerIndex.size(); i++) {
//获取每一张牌
Integer p = pokerIndex.get(i);
if (i >= 51) {
//将剩余的牌底牌
diPai.add(p);
} else if (i % 3 == 0) {
//发牌
player0.add(p);
} else if (i % 3 == 1) {
player1.add(p);
} else if (i % 3 == 2) {
player2.add(p);
}
}
//将底牌随机分给三个用户
Random r = new Random();
int index1 = r.nextInt(3);
for (int i = 0; i < diPai.size(); i++) {
Integer p1 = diPai.get(i);
if (index1 == 0) {
player0.add(p1);
} else if (index1 == 1) {
player1.add(p1);
} else if (index1 == 2) {
player2.add(p1);
}
}
//排序,默认是升序
Collections.sort(player0);
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(diPai);
getPoker("仓央嘉措:",poker,player0);
getPoker("纳兰性德:",poker,player1);
getPoker("长孙无忌:",poker,player2);
getPoker("底牌:",poker,diPai);
}
public static void getPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) {
System.out.print(name);
for (Integer key: list) {
System.out.print(poker.get(key) + " ");
}
System.out.println();
}
}
实现效果