扑克
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; } }