参考后还是用了一天才做出来。。。细心细心。。
细心。 检查一个小错误 ,用了3个小时。。
package guoke.com; /** * @author guokewind * @date 创建时间:2015年5月4日 下午11:38:29 * @version * @parameter * @since * @return */ /** * 创建一个扑克牌类 * 花色黑红梅方片用400-100表示, * 点数用2-14表示 * */ public class Poker { private int points; //纯点数102-114,202-214,302-314,402-414 private String pointsString;//纯点数对应的牌点2-A private String color;//花色 private String card;//牌名 public Poker(){ } public void creatPoker(int points){ //花色用100-400表示,点数用2-14表示 int nm=0; int cl=0; nm=points%100; cl=points/100; this.points=points;//!!千万不要忘记初始化成员变量!! switch(nm){//将纯点数转为牌点 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){//将纯点数转为花色 case 4: this.color="黑桃"; break; case 3: this.color="红桃"; break; case 2: this.color="梅花"; break; case 1: this.color="方片"; } this.card=color+pointsString;//将纯点数转换成牌面字符串 } 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表示,点数用2-14表示 return this.card; } } package guoke.com; import java.util.ArrayList; import java.util.List; /** * @author guokewind * @date 创建时间: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 创建时间:2015年5月4日 下午11:38:14 * @version * @parameter * @since * @return */ /* * 游戏类 * 扑克牌游戏,2名玩家分别输入ID和名字后 * 可以分别得到两张牌, * 从自己的2张手牌中最大的1张牌用来比较。 * 点数最大者获胜,若点数相同,比较花色(黑>红>梅>方),花色大的胜利 */ public class Game { private static List<Poker> pokerList=new ArrayList<Poker>();//扑克牌集合 private static List<Player> playerList=new ArrayList<Player>(); /* * 方法:创建扑克牌 * 从方片梅花红桃黑桃2-k放进pokerList中 * */ public void creatPoker(){//创建52扑克牌,从方片梅花红桃黑桃2-k放进去 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()+",");//测试牌是否创建成功 } } System.out.println("]"); } /* *洗牌,打乱牌在pokerList中的顺序 */ public void shuffle(){ Collections.shuffle(pokerList); // for (Poker card : pokerList){ // System.out.print(card.getCard());//测试是否洗牌成功 // } } /* * 发牌 * 玩家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()+"拿牌"); } /* * 从玩家手牌选出最大牌 * 先比较点数,点数大则返回点数大的牌 * 如点数相等,则比较花色,返回花色大的牌 */ 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){//比较点数,返回点数大的牌 return poker; } else if(points==points2){//点数相同,比较花色 int color=poker.getPoints()/100;//将花色转换成可比较的整型值 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应分别对应玩家1,玩家2的最大牌 int points=poker.getPoints()%100; int points2=poker2.getPoints()%100; if(points>points2){//比较点数,返回点数大的牌 return playerList.get(0); } else if(points==points2){//点数相同,比较花色 int color=poker.getPoints()/100;//将花色转换成可比较的整型值 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("----------创建扑克牌--------"); System.out.println("----------扑克牌创建成功----"); game.creatPoker(); System.out.println("----------开始洗牌----------"); game.shuffle(); System.out.println("----------洗牌结束----------"); System.out.println("----------创建玩家----------"); 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("请输入整数哦~"); 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("----------开始发牌----------"); game.deal(playerList.get(0), playerList.get(1));//发牌 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()+"]"); } }