1.先直接上结果图,后边再附上代码逐步来解释
2.完整代码如下:
import java.util.*;public class Doudizhu {public static void main(String[] args) {//准备牌//添加牌索引List<Integer> index = new ArrayList<>();//Collections.addAll(index,1,2,3);for (int i = 0; i < 54; i++) {index.add(i);}//System.out.println(index);//定义数组存储花色和13张牌,添加到一个List集合List<String> array = new ArrayList<>();array.add("大王");array.add("小王");String[] colors = {"♠", "♥", "♣", "♦"};String[] nums = {"2","A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};for (String num : nums){for (String color : colors){array.add(color + num);}}// System.out.println(array);//存到Map集合Map<Integer, String> poker = new HashMap<>();int i = 0;for (Integer integer : index) {poker.put(integer, (i));i++;}//System.out.println(poker);//洗牌Collections.shuffle(index);//System.out.println(index);//发牌List<Integer> player0 = new ArrayList<>();List<Integer> player1 = new ArrayList<>();List<Integer> player2 = new ArrayList<>();List<Integer> dipai = new ArrayList<>();for (int j = 0; j < index.size(); j++) {Integer _index = (j); //获取牌if (j >= 51) {dipai.add(_index);} else if (j % 3 == 0) {player0.add(_index);} else if (j % 3 == 1) {player1.add(_index);} else {player2.add(_index);}}//给每个人的手牌排序Collections.sort(player0);Collections.sort(player1);Collections.sort(player2);Collections.sort(dipai);// 看牌,调用看牌方法String[] names = {"玩家0","玩家1","玩家2","底牌"};lookPoker(poker,player0,names[0]);lookPoker(poker,player1,names[1]);lookPoker(poker,player2,names[2]);lookPoker(poker,dipai,names[3]);}// 看牌,根据玩家手牌索引获取牌值public static void lookPoker(Map<Integer, String> poker,List<Integer> list,String name) {System.out.print("rn" + name + ":");for (Integer integer : list) {System.out.print(" [" + (integer)+"]");}}}
3.来整理一下怎么实现的具体思路:
1)用List1来存储54张牌的索引,方便待会用来洗牌(排序),集合起名为index
//准备牌//添加牌索引List<Integer> index = new ArrayList<>();//Collections.addAll(index,1,2,3); //这个方式语法是正确的,但是由于我的jdk版本过低并不支持,所以用下面的for循环方式for (int i = 0; i < 54; i++) {index.add(i);}//System.out.println(index);
2)定义数组存储花色和13张牌的组合,然后按照从大到小顺序添加到一个List2集合中(跟第一步中的List2对应上,即54张牌从大到小对应索引0-53)
//定义数组存储花色和13张牌,添加到一个List集合List<String> array = new ArrayList<>();array.add("大王");array.add("小王");String[] colors = {"♠", "♥", "♣", "♦"};String[] nums = {"2","A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};for (String num : nums){for (String color : colors){array.add(color + num);}}// System.out.println(array);
3)定义一个map集合(双列集合),键用来存储索引,对应值存储牌面(54张牌对应54个键值对)
//存到Map集合Map<Integer, String> poker = new HashMap<>();int i = 0;for (Integer integer : index) {poker.put(integer, (i));i++;}//System.out.println(poker);
4)用Collections.shuffle(index);来进行洗牌(打乱List1集合中的元素,即:打乱序号)
//洗牌Collections.shuffle(index);//System.out.println(index);
5)定义四个集合List,分别来接收三个人的牌和底牌(其实接收的是牌对应的索引)
//发牌List<Integer> player0 = new ArrayList<>();List<Integer> player1 = new ArrayList<>();List<Integer> player2 = new ArrayList<>();List<Integer> dipai = new ArrayList<>();for (int j = 0; j < index.size(); j++) {Integer _index = (j); //获取牌if (j >= 51) {dipai.add(_index);} else if (j % 3 == 0) {player0.add(_index);} else if (j % 3 == 1) {player1.add(_index);} else {player2.add(_index);}}
6)给每个人的手牌以及底牌排序
//给每个人的手牌排序Collections.sort(player0);Collections.sort(player1);Collections.sort(player2);Collections.sort(dipai);
7)看牌,每个人看牌,调用看牌方法
// 看牌,调用看牌方法String[] names = {"玩家0","玩家1","玩家2","底牌"};lookPoker(poker,player0,names[0]);lookPoker(poker,player1,names[1]);lookPoker(poker,player2,names[2]);lookPoker(poker,dipai,names[3]);
8)看牌方法代码
// 看牌,根据玩家手牌索引获取牌值public static void lookPoker(Map<Integer, String> poker,List<Integer> list,String name) {System.out.print("rn" + name + ":");for (Integer integer : list) {System.out.print(" [" + (integer)+"]");}}
4.心得体会
主要是对集合理解到位,要对List和Map集合的功能了解到位。
本文发布于:2024-01-30 23:45:51,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170662955223702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |