后续swing界面附上!!!敬请期待!
package com.yikuan.cn; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; /** * 实现模拟斗地主的功能: * 组合牌、洗牌、发牌、看牌 * @author Administrator * */ public class DouDiZhu { //1.组合牌,先排除大王小王2张牌; /*花色4个固定值,存到数组中(数组效率高)*/ /*点数13个固定值,存到数组中*/ /*遍历数组*/ public static void main(String[] args) { /*创建Map集合,键是编号,值是牌*/ HashMap<Integer, String> pooker = new HashMap<Integer, String>(); /*创建list集合,存储编号*/ ArrayList<Integer> pookerNumber = new ArrayList<Integer>(); /*定义出13个点数(从大到小)的数组*/ String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; /*定义4个花色数组(点击保存UTF-8)*/ String[] colors = {"♠","♥","♣","■"}; /*定义一个整数变量,作为键出现*/ int index = 2;/*先去掉两王0,1*/ /*遍历数组:花色+点数的组合,存到Map集合*/ for (String number : numbers) { for (String color : colors) { pooker.put(index, color + number); pookerNumber.add(index); index++; } } // System.out.println(pooker);/*到这里是个无序集合..*/ /*存储大王,小王*/ pooker.put(0, "大王"); pookerNumber.add(0); pooker.put(1, "小王"); pookerNumber.add(1); //洗牌,将牌的编号打乱 Collections.shuffle(pookerNumber); // System.out.println(pookerNumber); //发牌,将牌的编号发给玩家集合,底牌集合 ArrayList<Integer> player1 = new ArrayList<Integer>(); ArrayList<Integer> player2 = new ArrayList<Integer>(); ArrayList<Integer> player3 = new ArrayList<Integer>(); ArrayList<Integer> bottom = new ArrayList<Integer>(); /*发牌采用的是集合的索引%3*/ /*取模得0--->player1*/ /*取模得1--->player2*/ /*取模得2--->player3*/ for(int i=0;i<pookerNumber.size();i++){ /*i从0开始,如果对3取模的话,就得先做底牌,如果i<3,存到底牌去*/ if(i<3){ bottom.add(pookerNumber.get(i)); }else if(i%3 == 0){ player1.add(pookerNumber.get(i)); }else if(i%3 == 1){ player2.add(pookerNumber.get(i)); }else if(i%3 == 2){ player3.add(pookerNumber.get(i)); } } /*看牌前,先对玩家手中的编号进行排序*/ Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); //看牌,将玩家手中的编号,到Map集合中查找,根据键找到值 look("王宝强",player1,pooker); look("马蓉",player2,pooker); look("宋喆",player3,pooker); look("底牌",bottom,pooker); } private static void look(String name,ArrayList<Integer> player, HashMap<Integer, String> pooker) { System.out.print(name+" "); /*遍历ArrayList集合,获取元素,作为键,再到集合Map中找值*/ for (Integer key : player) { String value = pooker.get(key); System.out.print(value+" "); } System.out.println(); } }