java版斗地主,一副牌实现发牌过程

来源:互联网 发布:谷歌tts语音数据 编辑:程序博客网 时间:2024/05/01 18:22
package com.silei.linkedlist;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.TreeSet;public class abc {public static void main(String[] args) {HashMap<Integer,String> hm = new HashMap<Integer,String>(); //定义HashMap变量用于存储每张排的编号以及牌型ArrayList<Integer> array = new ArrayList<Integer>();//定义ArrayList变量存储排的编号String[] colors = {"♤","♥","♣","♢"}; //定义数组存储排的花色String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//定义数组存储牌值int index = 0;  //定义编号for(String number : numbers){  //遍历排值数组for(String color : colors){ //遍历花色hm.put(index, color.concat(number));//将花色与牌值拼接,并将编号与拼接后的结果存储到hm中array.add(index); //将编号存储到array中index++;}}/* * 将小王和大王存储到hm中 */hm.put(index, "小王");  array.add(index);index++;hm.put(index, "大王");array.add(index);Collections.shuffle(array);  //调用Collections集合的shuffle()方法,将array中存储的编号进行随机的置换,即打乱顺序/* * 定义四个TreeSet集合的变量用于存储底牌编号以及三个玩家的牌的编号 * 采用TreeSet集合是因为TreeSet集合可以实现自然排序 */TreeSet<Integer> playerOne = new TreeSet<Integer>();TreeSet<Integer> PlayerTwo = new TreeSet<Integer>();TreeSet<Integer> playerThree = new TreeSet<Integer>(); TreeSet<Integer> dipai = new TreeSet<Integer>();//遍历编号的集合,实现发牌for(int x = 0; x < array.size(); x++){if(x >= array.size() - 3){dipai.add(array.get(x));}else if( x % 3 == 0){playerOne.add(array.get(x));}else if(x % 3 == 1){PlayerTwo.add(array.get(x));}else if(x % 3 == 2){playerThree.add(array.get(x));}}lookPoker("底牌",dipai,hm);lookPoker("xiequn",PlayerTwo,hm);lookPoker("huangshen",playerOne,hm);lookPoker("diannao",playerThree,hm);}/** * 遍历每个玩家的牌以及底牌 * @param name * @param ts * @param hm */public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){System.out.print(name+":\t");  //打印玩家名称for(Integer key : ts){  //遍历玩家TreeSet集合,获得玩家的牌的编号String value = hm.get(key);//根据玩家牌编号获取具体的牌值System.out.print(value+"  ");//打印}System.out.println();}}

1 0
原创粉丝点击