package?day42.TestDrive;import?java.util.*;public?class?PlayingCard?{ public?static?void?main(String[]?args){ //創(chuàng)建一副新?lián)淇伺? System.out.println("-------開始創(chuàng)建撲克牌!---------------"); Poker?poker?=?new?Poker(); List<Poker>?p?=?poker.CreatAPoker(); System.out.println("-------創(chuàng)建撲克牌成功!---------------"); System.out.println("------------開始洗牌------------"); Collections.shuffle(p);//洗牌 System.out.println("------------洗牌結(jié)束!-----------------"); System.out.println("------------創(chuàng)建玩家-------------------"); Player?player?=?new?Player(); List<Player>?players?=?player.creatPlayer(); //開始發(fā)牌 PlayingCard?pc?=?new?PlayingCard(); pc.deal(players,?p); } //發(fā)牌給玩家 public?void?deal(List<Player>?pl,?List<Poker>?po)?{ System.out.println("開始發(fā)牌"); List<Integer>?numList?=?new?ArrayList<Integer>();? List<Poker>?pokerList?=?new?ArrayList<Poker>(); List<String>?aStr?=?new?ArrayList<String>();//a玩家拿的牌 List<String>?bStr?=?new?ArrayList<String>();//b玩家拿的牌 List<Integer>?aNum?=?new?ArrayList<Integer>();//a玩家拿的牌轉(zhuǎn)為點數(shù)大小 List<Integer>?bNum?=?new?ArrayList<Integer>();//b玩家拿的牌轉(zhuǎn)為點數(shù)大小 //輪流各自發(fā)兩張牌 Random?random?=?new?Random(); for(int?i?=?0;?i?<?pl.size()?*?2;?i++)?{ int?num?=?random.nextInt(po.size()); do?{ pokerList.add(po.get(num)); }while(pokerList.size()?!=?new?HashSet<Poker>(pokerList).size()); } //牌的牌名List for(int?i?=?0;?i?<?pokerList.size();?i++)?{ if(i?%?2?==?0)?{ System.out.println("【?"+pl.get(0).getPlayerName()+"拿一張!?】"); aStr.add(pokerList.get(i).suit?+?pokerList.get(i).point); }else?{ System.out.println("【?"+pl.get(1).getPlayerName()+"拿一張!?】"); bStr.add(pokerList.get(i).suit?+?pokerList.get(i).point); } } //打印出玩家各自手中的牌 System.out.println("***>>>?"+pl.get(0).getPlayerName()+"?的牌是"+aStr); System.out.println("***>>>?"+pl.get(1).getPlayerName()+"?的牌是"+bStr); Poker?poker?=?new?Poker(); for(int?i?=?0;?i?<?pokerList.size();?i++)?{ int?num?=?poker.numTransformation(pokerList.get(i)); poker.pokerNum.add(num); } //將玩家手里的牌列出最大值 Collections.sort(poker.pokerNum); int?maxPokerNum?=?poker.pokerNum.get(poker.pokerNum.size()-1); //牌的點數(shù)List for(int?i?=?0;?i?<?pokerList.size();?i++)?{ if(i?%?2?==?0)?{ aNum.add(poker.numTransformation(pokerList.get(i))); }else?{ bNum.add(poker.numTransformation(pokerList.get(i))); } } //判斷兩個人誰手里的牌最大 if(aNum.contains(maxPokerNum))?{ System.out.println("***>>>?"+pl.get(0).getPlayerName()+"?贏了!"+"?<<<***"); }else?if(aNum.contains(maxPokerNum))?{ System.out.println("***>>>?"+pl.get(1).getPlayerName()+"?贏了!"+"?<<<***"); } }}//創(chuàng)建玩家類class?Player{ private?String?playerName;//玩家名字 private?int?playerId;//玩家ID public?List<String>?hand?=?new?ArrayList<String>();//手牌:撲克牌的集合 public?Player()?{ } //Player的含參構(gòu)造方法 public?Player(String?playerName,?int?playerId)?{ this.playerName?=?playerName; this.playerId?=?playerId; } public?String?getPlayerName()?{ return?playerName; } public?void?setPlayerName(String?playerName)?{ this.playerName?=?playerName; } public?int?getPlayerId()?{ return?playerId; } public?void?setPlayerId(int?playerId)?{ this.playerId?=?playerId; } @Override public?String?toString()?{ return?"Player?[playerName="?+?playerName?+?",?playerId="?+?playerId?+?"]"; } //創(chuàng)建玩家名片 public?List<Player>?creatPlayer()?{ //創(chuàng)建兩名玩家 Player?player1?=?new?Player(); Player?player2?=?new?Player(); List<Player>?playerList?=?new?ArrayList<Player>(); Player[]?players?=?{player1,?player2}; Scanner?input?=?new?Scanner(System.in); int?i?=?1; for(Player?pl:players?)?{ System.out.println("請輸入第"+i+"位玩家的姓名:>>>"); pl.setPlayerName(input.next()); for(int?j?=?0;?j?<?3;?j++)?{ try{ System.out.println("請輸入第"+i+"位玩家的ID:>>>"); pl.setPlayerId(input.nextInt()); break; }catch(Exception?ex)?{ input?=?new?Scanner(System.in);//阻止無限循環(huán) System.out.println("請輸入整數(shù)ID!"); } } i++; playerList.add(pl); } for(Player?pl1:players)?{ System.out.println("------歡迎玩家:"+pl1.getPlayerName()); } return?playerList; }}//創(chuàng)建撲克類class?Poker{ public?String?suit; public?String?point; public?String[]?suitArray?=?{"方塊",?"梅花",?"紅桃",?"黑桃"};//花色 public?String[]?pointArray?=?{"3",?"4",?"5",?"6",?"7",?"8",?"9",?"10",?"J",?"Q",?"K",?"A",?"2"};//點數(shù) public?List<String>?p?=?new?ArrayList<String>();//打印撲克 public?List<Poker>?poker?=?new?ArrayList<Poker>();//一副撲克 public?List<Integer>?pokerNum?=?new?ArrayList<Integer>();//每張牌對象的集合 //Poker的構(gòu)造方法 public?Poker()?{ } public?Poker(String?suit,?String?point)?{ this.suit?=?suit; this.point?=?point; } //創(chuàng)建一副撲克 public?List<Poker>?CreatAPoker()?{ for(int?j?=?0;?j?<?pointArray.length;?j++)?{ for(int?i?=?0;?i?<?suitArray.length;?i++)?{ Poker?pn?=?new?Poker(suitArray[i],?pointArray[j]); poker.add(pn); } } for(int?i?=?0;?i?<?poker.size();?i++)?{//String型的是通過Poker來的 String?sb?=?poker.get(i).suit+poker.get(i).point; p.add(sb); } for(int?i?=?0;?i?<?poker.size();?i++)?{//Integer型的也是通過Poker來的 pokerNum.add(numTransformation(poker.get(i))); } //打印出一副新建的撲克牌 System.out.println(p);//poker是String類型所以可以打印出來? return?poker; } //點數(shù)轉(zhuǎn)換數(shù)字比大小 public?int?numTransformation(Poker?c)?{ //花權(quán)值 int?ps?=?0; if(c.suit?==?"方塊")?{ ps?=?1; }else?if(c.suit?==?"梅花"){ ps?=?2; }else?if(c.suit?==?"紅桃")?{ ps?=?3; }else?if(c.suit?==?"黑桃")?{ ps?=?4; } //牌權(quán)值 int?pp?=?0; if(c.point?==?"J")?{ pp?=?11; }else?if(c.point?==?"Q")?{ pp?=?12; }else?if(c.point?==?"K")?{ pp?=?13; }else?if(c.point?==?"A")?{ pp?=?12; }else?if(c.point?==?"2")?{ pp?=?13; }else?{ pp?=?Integer.parseInt(c.point);//數(shù)字型字符串轉(zhuǎn)換成數(shù)字 } return?pp?*?10?+?ps; }}
2019-11-07
花里胡哨? ?看不懂啊