參考后還是用了一天才做出來。。。細(xì)心細(xì)心。。
細(xì)心。 ?檢查一個小錯誤 ,用了3個小時。。
package?guoke.com; /**?*?@author??guokewind? ?*?@date?創(chuàng)建時間:2015年5月4日?下午11:38:29? ?*?@version? ?*?@parameter?? ?*?@since?? ?*?@return??*/ /** ?*?創(chuàng)建一個撲克牌類 ?*?花色黑紅梅方片用400-100表示, ?*?點數(shù)用2-14表示 ?*?*/ public?class?Poker?{ ????private?int?points;??//純點數(shù)102-114,202-214,302-314,402-414 ????private?String?pointsString;//純點數(shù)對應(yīng)的牌點2-A private?String?color;//花色 ????private?String?card;//牌名 ????public?Poker(){ ???? ????} ????public?void?creatPoker(int?points){ ???? ?//花色用100-400表示,點數(shù)用2-14表示 ?????? ??int?nm=0; ?????? ??int?cl=0; ?????? ??nm=points%100; ?????? ??cl=points/100; ?????? ??this.points=points;//?。∏f不要忘記初始化成員變量!!? ?????? ??switch(nm){//將純點數(shù)轉(zhuǎn)為牌點 ?????? ??case?11:?this.pointsString="J"; ?????? ?? break; ?????? ??case?12:?this.pointsString="Q"; ?????? ?? break; ?????? ??case?13:?this.pointsString="K"; ?????? ?? break; ?????? ??case?14:?this.pointsString="A"; ?????? ?? break; ?????? ??default:?this.pointsString=nm+""; ?????? ??} ?????? ??switch(cl){//將純點數(shù)轉(zhuǎn)為花色 ?????? ??case?4:??this.color="黑桃"; ?????? ??break; ?????? ??case?3:??this.color="紅桃"; ?????? ??break; ?????? ??case?2:??this.color="梅花"; ?????? ??break; ?????? ??case?1:??this.color="方片"; ?????? ??} ?????? ??this.card=color+pointsString;//將純點數(shù)轉(zhuǎn)換成牌面字符串 ??????} ??????public?void?setPoints(int?points){ ???? ??this.points=points; ??????} ??????public?int?getPoints(){ ???? ??return?this.points; ??????} ??????public?void?setColor(String?color){ ???? ??this.color=color; ??????} ??????public?String?getColor(){ ???? ??return?this.color; ??????} ??????public?String?getPointsString(){ ???? ??return?this.pointsString; ??????} ??????public?String?getCard(){//花色用100-400表示,點數(shù)用2-14表示 ???? ??return?this.card; ???? ?? ??????} } package?guoke.com; import?java.util.ArrayList; import?java.util.List; /**?*?@author??guokewind? ?*?@date?創(chuàng)建時間:2015年5月4日?下午11:38:43? ?*?@version? ?*?@parameter?? ?*?@since?? ?*?@return??*/ /* ?*?玩家類 ?*/ public?class?Player?{ private?int?ID;//玩家ID private?String?name;//玩家姓名 private?List<Poker>?handCard;//玩家手牌集合 public??Player(){ } public?Player(int?ID,String?name){ this.ID=ID; this.name=name; handCard=?new?ArrayList<Poker>(); } public?int?getID(){ return?ID; } public?String?getName(){ return?name; } public?void?setHandCard(Poker?poker){ handCard.add(poker); } public??List<Poker>??getHandCard(){//此為返回帶Poker泛型的List類型,記得加泛型! return?handCard; } } package?guoke.com; import?java.util.ArrayList; import?java.util.Collections; import?java.util.InputMismatchException; import?java.util.List; import?java.util.Scanner; /**?*?@author??guokewind? ?*?@date?創(chuàng)建時間:2015年5月4日?下午11:38:14? ?*?@version? ?*?@parameter?? ?*?@since?? ?*?@return??*/ /* ?*?游戲類 ?*?撲克牌游戲,2名玩家分別輸入ID和名字后 ?*?可以分別得到兩張牌, ?*?從自己的2張手牌中最大的1張牌用來比較。 ?*?點數(shù)最大者獲勝,若點數(shù)相同,比較花色(黑>紅>梅>方),花色大的勝利 ?*/ public?class?Game?{ private?static?List<Poker>?pokerList=new?ArrayList<Poker>();//撲克牌集合 private?static?List<Player>?playerList=new?ArrayList<Player>(); /* ?*?方法:創(chuàng)建撲克牌 ?*?從方片梅花紅桃黑桃2-k放進(jìn)pokerList中 ?*?*/ public?void?creatPoker(){//創(chuàng)建52撲克牌,從方片梅花紅桃黑桃2-k放進(jìn)去 System.out.print("撲克牌為:["); for?(int?i?=?1,m=0;?i?<=?4;?i++)?{ for?(int?j?=?2;?j?<=14;?j++)?{ ?Poker?pk=new?Poker(); pk.creatPoker(i*100+j); pokerList.add(pk); System.out.print(pokerList.get(m++).getCard()+",");//測試牌是否創(chuàng)建成功 } } System.out.println("]"); } /* ?*洗牌,打亂牌在pokerList中的順序 ?*/ public?void?shuffle(){ Collections.shuffle(pokerList); // for?(Poker?card?:?pokerList){ // System.out.print(card.getCard());//測試是否洗牌成功 // } } /* ?*?發(fā)牌 ?*?玩家a,b依次拿一張牌,并裝入handCard集合中 ?*?一共拿兩次 ?*/ public?void?deal(Player?a,Player?b){ a.setHandCard(pokerList.get(0)); System.out.println("----玩家:"+a.getName()+"拿牌"); b.setHandCard(pokerList.get(1)); System.out.println("----玩家:"+b.getName()+"拿牌"); a.setHandCard(pokerList.get(2)); System.out.println("----玩家:"+a.getName()+"拿牌"); b.setHandCard(pokerList.get(3)); System.out.println("----玩家:"+b.getName()+"拿牌"); } /* ?*?從玩家手牌選出最大牌 ?*?先比較點數(shù),點數(shù)大則返回點數(shù)大的牌 ?*?如點數(shù)相等,則比較花色,返回花色大的牌 ?*/ public?Poker?chooseMax(Player?a){ Poker?poker=a.getHandCard().get(0); Poker?poker2=a.getHandCard().get(1); int?points=poker.getPoints()%100; int?points2=poker2.getPoints()%100; if(points>points2){//比較點數(shù),返回點數(shù)大的牌 return?poker; } else?if(points==points2){//點數(shù)相同,比較花色 int?color=poker.getPoints()/100;//將花色轉(zhuǎn)換成可比較的整型值 int?color2=poker2.getPoints()/100; if(color>color2){//返回花色大的牌 return?poker; } ??else{ ??return?poker2; ??} } else{ return?poker2; } } /* ?*?看牌 ?*?比較兩者的最大牌大小,返回勝者 ?*/ public?Player?cheak(Poker?poker,Poker?poker2){//poker,poker2應(yīng)分別對應(yīng)玩家1,玩家2的最大牌 int?points=poker.getPoints()%100; int?points2=poker2.getPoints()%100; if(points>points2){//比較點數(shù),返回點數(shù)大的牌 return?playerList.get(0); } else?if(points==points2){//點數(shù)相同,比較花色 int?color=poker.getPoints()/100;//將花色轉(zhuǎn)換成可比較的整型值 int?color2=poker2.getPoints()/100; if(color>color2){//返回花色大的牌 return?playerList.get(0); } ??else{ ??return?playerList.get(1); ??} } else{ return?playerList.get(1); } } public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub Game?game=new?Game(); System.out.println("----------創(chuàng)建撲克牌--------"); System.out.println("----------撲克牌創(chuàng)建成功----"); game.creatPoker(); System.out.println("----------開始洗牌----------"); game.shuffle(); System.out.println("----------洗牌結(jié)束----------"); System.out.println("----------創(chuàng)建玩家----------"); Scanner?scan=new?Scanner(System.in); int?number=1;//用來顯示是?第幾位玩家 while(playerList.size()!=2){ int?i=0; int?ID=0; String?name=null; while(i==0){ try{System.out.println("請輸入第"+number+"位玩家的ID和姓名"); System.out.println("請輸入ID,按回車鍵確定"); ID=scan.nextInt(); i++; number++; }catch(InputMismatchException?e){ System.out.println("請輸入整數(shù)哦~"); scan.next();//此處用來消除之前的輸入,否則會一直報錯 } } System.out.println("請輸入名字,按回車鍵確定"); ?name=scan.next(); playerList.add(new?Player(ID,name)); } System.out.println("----歡迎玩家:"+playerList.get(0).getName()); System.out.println("----歡迎玩家:"+playerList.get(1).getName()); System.out.println("----------開始發(fā)牌----------"); game.deal(playerList.get(0),?playerList.get(1));//發(fā)牌 Poker?poker=game.chooseMax(playerList.get(0));//玩家1的最大牌 Poker?poker2=game.chooseMax(playerList.get(1));//玩家2的最大牌 Player?winner=game.cheak(poker,poker2); System.out.println("--------玩家:"+winner.getName()+"獲勝--------"); System.out.println("玩家各自的手牌為:"); Poker?handPoker=playerList.get(0).getHandCard().get(0); Poker?handPoker2=playerList.get(0).getHandCard().get(1); Poker?handPoker3=playerList.get(1).getHandCard().get(0); Poker?handPoker4=playerList.get(1).getHandCard().get(1); System.out.println("玩家"+playerList.get(0).getName()+":["+handPoker.getCard()+","+handPoker2.getCard()+"]"); System.out.println("玩家"+playerList.get(1).getName()+":["+handPoker3.getCard()+","+handPoker4.getCard()+"]"); } }
2015-06-09
666666
2015-05-26
不錯 ?想法很好,比我那笨笨的代碼簡單明了
2015-05-21
大神,求幫助。