本文主要是介绍Java 扑克牌发牌,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java 扑克牌发牌今天看到这个算法题,http://www.cnblogs.com/xishuai/p/3392981.html ,忍不住自己用Java做了一个。
初始化很重要,所有的52张牌按顺序放入到容器里边,标志位标记为false表示手里没这牌。
1 发牌
利用随机数,找到容器中的这张牌,将标志位标记为true,表示手里有了这张牌。
2 排序
因为放入的时候是按顺序的,于是每个花色各自,自然也是按照顺序,找出标志位为true的,输出即可。
3找出最大连续牌
思路是将连续的字符分隔出来,连续数目最大的,即为最大连续牌。每个花色的的最大牌找出来,再找出最大的。
做法:
每个花色里,生成一个字符串,其中,手里有的排,将索引加入字符串,再加分隔符。手里没有的,用分号加入字符串。
用分号分隔后,长度大于1的即为有连续牌的。从有连续牌的,找出最大的即可。
/*** desc* 程序描述:一副纸牌有52张,4种花色,每种花色13张。我们能用一个整数m就表示出所有的52种情况,规则是:m / 13: =0: 红心,=1: 方块,=2: 梅花,=3: 黑桃m % 13: =0:2,=1:3,=2:4 .... =8:10,=9:J,=10:Q,=11: K,=12:A比如:m = 15 就表示:方块4 m=38表示:梅花A我们希望用程序模拟1副扑克牌随机抽取13张,发给某人的过程。发牌后需要排序:规则是:先按花色,再按点数。花色的大小顺序是:梅花、方块、红心、黑桃。点数的顺序是:2、3、4、…. 10、J、Q、K、A。然后,挑选出最大的连续牌型。规则是:连续张数多的大。张数相等的则花色大的大(此时与点数无关)。我加了一条规则 最小连续是3张*/ package algorithm;import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.Vector;/*** @author new**/ public class Poker {private static final int CARDSNUM=13;private static final int MAX_NUM=52;private static final int MIN_CONSEC_NUM = 2;private Map<CardColor,Vector<Cards>> allCards=new HashMap<CardColor,Vector<Cards>>();public void start(){init();genarate();order();printMax();}private void init(){allCards.clear();for(CardColor color:CardColor.values()){Vector<Cards> v=new Vector<Cards>();for(CardDisplay cd:CardDisplay.values()){Cards c = new Cards();c.setCardColor(color);c.setCardDisplay(cd);c.setExist(false);c.setRealNum(CARDSNUM*color.colorType+cd.num);v.add(c);}allCards.put(color, v);}}/*** 随机抽取13张牌*/private void genarate(){System.out.println("my cards is :");int i=0;do{Random r = new Random();int tmp = r.nextInt(MAX_NUM);int answer = tmp / CARDSNUM ;for(Cards obj:allCards.get(getColor(answer))){if(obj.getRealNum()==tmp&&obj.isExist()==false){i++;obj.setExist(true);System.out.println(obj.getCardColor().colorDesc+":"+obj.getCardDisplay().displayStr);break;}}}while(i<CARDSNUM);}private CardColor getColor(int answer){for(CardColor cc:CardColor.values()){if(cc.colorType == answer){return cc;}}return null;}/*** 开始排序 其实不用排序 枚举初始化的时候已经排好序*/private void order(){System.out.println();System.out.println("ofter order :");for(CardColor key:allCards.keySet()){System.out.print(key.colorDesc +" : ");for(Cards obj:allCards.get(key)){if(obj.isExist()){System.out.print(obj.getCardDisplay().displayStr+" ");}}System.out.println();}}/*** 取出最大的连续牌型*/private void printMax(){CardColor maxColor=null;int maxCardIdx = -1;int maxCardLength =0;for(CardColor key:allCards.keySet()){Vector<Cards> v = allCards.get(key);StringBuffer tmpcards=new StringBuffer();for(int idx=0;idx<v.size();idx++){Cards obj = v.get(idx);if(obj.isExist()){tmpcards.append(idx).append("-");}else{tmpcards.append(";");}}String cardstr= tmpcards.toString().replaceAll("-;", ";");if(cardstr.endsWith("-"))cardstr=cardstr.substring(0,cardstr.length()-2);String[] tmpcardsarr=cardstr.split(";");int temp_maxCardIdx =-1;int tmp_maxCardLength =0;for(int i=0;i<tmpcardsarr.length;i++){String[] arr = tmpcardsarr[i].split("-");if(tmp_maxCardLength<=arr.length&&arr.length>=MIN_CONSEC_NUM){temp_maxCardIdx=Integer.parseInt(arr[0]);tmp_maxCardLength=arr.length;}}if(tmp_maxCardLength>=MIN_CONSEC_NUM){if(tmp_maxCardLength>maxCardLength||(maxColor!=null && tmp_maxCardLength==maxCardLength&&key.colorIndex>maxColor.colorIndex)){maxColor = key;maxCardIdx = temp_maxCardIdx;maxCardLength = tmp_maxCardLength;}}}System.out.println();System.out.println("max :");if(maxCardLength>=MIN_CONSEC_NUM){ System.out.print(maxColor.colorDesc+":");Vector<Cards> v = allCards.get(maxColor);for(int i=maxCardIdx;i<v.size();i++){Cards obj = v.get(i);if(obj.isExist()){System.out.print(obj.getCardDisplay().displayStr+" ");}else{return;}}}else{System.out.println("无连续牌");}}/*** @param args*/public static void main(String[] args) {Poker p = new Poker();p.start();}} class CardMax {CardColor maxColor;int maxCardIdx; } /*** * @author new**/ enum CardColor {CLUB(2,"梅花",1),//梅花DIAMOND(1,"方块",2),//方块HEART(0,"红桃",3),//红桃SPADE(3,"黑桃",4);//黑桃protected final int colorType;protected final String colorDesc;protected final int colorIndex;//代表花色大小 值越大越大private CardColor(int type,String desc,int oidx){this.colorType = type;this.colorDesc = desc;this.colorIndex = oidx;} } enum CardDisplay {CARD_2(0),CARD_3(1),CARD_4(2),CARD_5(3),CARD_6(4),CARD_7(5),CARD_8(6),CARD_9(7),CARD_10(8),CARD_J(9),CARD_Q(10),CARD_K(11),CARD_A(12);protected final String displayStr;protected final int num;private CardDisplay(int num){this.num = num;if(num<9){this.displayStr = String.valueOf(num+2);}else{String str="";switch(num){case 9:str = "J";break;case 10:str = "Q";break;case 11:str = "K";break;case 12:str="A";break;} this.displayStr = str;} } } /*** 每张牌对象* @author new**/ class Cards {Cards(){}Cards(CardColor cardColor,CardDisplay cardDisplay,int realNum,boolean exist){this.cardColor = cardColor;this.cardDisplay = cardDisplay;this.realNum = realNum;this.exist = exist;}private CardColor cardColor;//花色private CardDisplay cardDisplay;private int realNum;private boolean exist;public CardColor getCardColor() {return cardColor;}public CardDisplay getCardDisplay() {return cardDisplay;}public int getRealNum() {return realNum;}public boolean isExist() {return exist;}public void setCardColor(CardColor cardColor) {this.cardColor = cardColor;}public void setCardDisplay(CardDisplay cardDisplay) {this.cardDisplay = cardDisplay;}public void setRealNum(int realNum) {this.realNum = realNum;}public void setExist(boolean exist) {this.exist = exist;} }
测试结果:
my cards is : 方块:6 方块:8 方块:4 梅花:4 方块:A 红桃:9 黑桃:Q 梅花:9 梅花:5 黑桃:10 黑桃:5 方块:3 方块:9ofter order : 方块 : 3 4 6 8 9 A 黑桃 : 5 10 Q 红桃 : 9 梅花 : 4 5 9 max : 方块:8 9
这篇关于Java 扑克牌发牌的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!