【线上实习Day01】斗地主
- 题目
需求:斗地主发牌
一副扑克一共54张牌,花色四种,分别是"红桃",“黑桃”,“方块”,“草花”,序号分别是"A",“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”以及大小王
三个角色玩牌。
1:请输出洗牌后的随机扑克展示
2:三个人每人随机获得17张牌展示
3:留下的3张地主扑克底牌展示 - 思路
1.将牌号已牌号-花色数字对应的组合放入map中(对应的是integer-String)
2.打乱牌的顺序
3.根据发牌的规则给每个人固定的牌数
4.按大小排序
5.根据下标打印底牌和每个人牌号 - 代码
package com.company.game;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DouDiZhu {
//因为有54张牌所以必须放在一个数组里面
public static Integer[] index = new Integer[54];
//置入一个map中,卡牌号和对应的卡牌
private static Map<Integer, String> allCard = new HashMap<Integer, String>();
public static void main(String[] args) {
//初始化卡牌
initMap(allCard);
List<Integer> list = Arrays.asList(initIndex(index));
//洗牌
Collections.shuffle(list);
//发牌
//就像游戏规则一样一组一组牌发,每组发给你固定的牌数
List<Integer> player1 = list.subList(0, 17);
List<Integer> player2 = list.subList(17, 34);
List<Integer> player3 = list.subList(34, 51);
List<Integer> dipai = list.subList(51, 54);
//按大小排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(dipai);
System.out.print("玩家1:");
for (Integer i :player1){
System.out.print(allCard.get(i));
}
System.out.println();
System.out.print("玩家2:");
for (Integer i :player2){
System.out.print(allCard.get(i));
}
System.out.println();
System.out.print("玩家3:");
for (Integer i :player3){
System.out.print(allCard.get(i));
}
System.out.println();
System.out.print("底牌 :");
for (Integer i :dipai){
System.out.print(allCard.get(i));
}
}
// 初始化卡牌数量
public static Integer[] initIndex(Integer[] index) {
for (int i = 1; i < 55; i++) {
index[i - 1] = i;
}
return index;
}
//将卡牌按大小以及花色(黑红梅方)添加到map
//初始化卡牌的排序
public static void initMap(Map<Integer, String> map) {
String[] num = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6",
"5", "4", "3" };
String[] color = { "♠","♥","♦","♣"};
map.put(1, "RedJoker");
map.put(2, "BlackJoker");
int k = 3;
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < color.length; j++) {
map.put(k, color[j] + num[i]);
k++;
}
}
}
}
- 总结
第一天耍的挺开心的