package com.imooc; import java.util.ArrayList; import java.util.List; /** * 创建玩家! * @author SANGONG * */ public class Player { public String id; public String name; public List<Puke> handList; public Player(String id, String name) { this.id = id; this.name = name; this.handList = new ArrayList<Puke>(); } public Player(){ } } package com.imooc; import java.util.Comparator; /** * 扑克牌类 * @author SANGONG * */ public class Puke { public String huase; public String value; public String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; public Puke(String huase,String value){ this.huase = huase; this.value = value; } } package com.imooc; import java.util.Comparator; public class PukeComparator implements Comparator <Puke>{ public String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; public int stringToInt(String[] str,String value){ int i = 0; while(!str[i].equals(value)){ i ++; } return i+2; } @Override public int compare(Puke o1, Puke o2) { Integer index1 = stringToInt(values,o1.value); Integer index2 = stringToInt(values,o2.value); int no = index1 - index2; if(no == 0) { return o1.huase.compareTo(o2.huase); }else if(no > 0){ return 1; //return index1.compareTo(index2); }else{ return -1; } } } package com.imooc; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Scanner; import java.util.Set; /** * 1.创建一副扑克牌,不考虑大小王 2.创建两名玩家,玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合 3.洗牌,将之前创建的扑克牌顺序打乱 4.发牌,将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张 5.开始游戏,比大小,大小相同比花色 * @author SANGONG * */ public class Game { public static List<Puke> handList; public static Map<String,Player> playersMap; public static List<Puke> pukeList; public static Scanner input; Puke[] pk = new Puke[52]; public String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; public Game(){ this.handList = new ArrayList<Puke>(); this.playersMap = new HashMap<String,Player>(); this.pukeList = new ArrayList<Puke>(); this.input = new Scanner(System.in); } //创建扑克牌,添加到puke序列中 public void addPuke(){ for(int i = 0;i < 13;i++){ pk[i] = new Puke("Diamonds",values[i]); //方块Diamonds } for(int i = 13;i < 26;i++){ pk[i] = new Puke("Hearts",values[i-13]); //红桃Hearts } for(int i = 26;i < 39;i++){ pk[i] = new Puke("Clubs",values[i-26]); //黑桃Clubs } for(int i = 39;i < 52;i++){ pk[i] = new Puke("Spades",values[i-39]); //梅花Spades } pukeList.addAll(Arrays.asList(pk)); } //输出puke序列 public void putOut(List<Puke> puke){ int i = 1; for (Object obj : puke) { Puke pks = (Puke)obj; System.out.print( pks.huase + pks.value + " "); if(i%8 == 0) System.out.println(); i++; } System.out.println(); } //洗牌,先将puke序列清空再将数组pk打乱顺序后添加到puke序列中 public void shuffle(){ pukeList.clear(); Random ran = new Random(); for(int i = 0;i < pk.length;i++){ int index = ran.nextInt(pk.length); Puke temp = pk[i]; pk[i] = pk[index]; pk[index] = temp; } pukeList.addAll(Arrays.asList(pk)); } //创建玩家添加到players序列 public void creatPlayer(int num){ Random ran = new Random(); for(int i = 1;i <= num;i++){ int m = ran.nextInt(100); if(m == 0){ m = ran.nextInt(100); } String id = m+""; do{ m = ran.nextInt(100); }while(playersMap.containsKey(id)); System.out.println("第" + i + "玩家的ID为" + id); System.out.print("输入该玩家的姓名:"); String name = input.next(); Player newPlayer = new Player(id,name); playersMap.put(id, newPlayer); } Set<String> keySet = playersMap.keySet(); for (String id : keySet) { Player pl = playersMap.get(id); System.out.println("欢迎玩家:" + pl.name + "进入游戏!"); } } //发牌方法;从puke序列中选取前几个元素加入到hand手牌序列中List<PuKe> hand,List<PuKe> puke public void licensing(){ int j = 0; for(int i = 0;i < 2; i++){ Set<String> keySet = playersMap.keySet(); for (String id : keySet) { Player pl = playersMap.get(id); Puke temp = (Puke)pukeList.get(j); j++; pl.handList.add(i, temp); System.out.println("----------玩家" + pl.name + "拿牌---------" ); } } } public static void main(String[] args){ Game gm = new Game(); System.out.println("--------------开始创建扑克牌----------------"); gm.addPuke(); System.out.println("--------------扑克牌创建成功!---------------"); System.out.println("扑克牌为:"); gm.putOut(pukeList); System.out.println("----------------开始洗牌-------------------"); gm.shuffle(); System.out.println("----------------洗牌完成!------------------"); //gm.putOut(pukeList); System.out.println("---------------开始创建玩家------------------"); System.out.print("请输入玩家人数:"); int num = input.nextInt(); gm.creatPlayer(num); System.out.println("----------------开始发牌-------------------"); gm.licensing(); System.out.println("----------------发牌结束!------------------"); System.out.println("----------------开始游戏-------------------"); Set<String> keySet = playersMap.keySet(); List<Puke> bigHandList = new ArrayList<Puke>(); for (String id: keySet) { Player pl = playersMap.get(id); Collections.sort(pl.handList,new PukeComparator()); System.out.println("玩家:" + pl.name + " 最大的手牌是:" + pl.handList.get(1).huase + pl.handList.get(1).value); bigHandList.add(pl.handList.get(1)); } for (String id: keySet) { Player pl = playersMap.get(id); Collections.sort(pl.handList,new PukeComparator()); boolean no1 = Collections.max(bigHandList,new PukeComparator() ) .huase.equals(pl.handList.get(1).huase ); boolean no2 = Collections.max(bigHandList,new PukeComparator() ) .value.equals(pl.handList.get(1).value ); if(no1 && no2){ System.out.println("--------------玩家:" + pl.name + "获胜!-------------"); } } for (String id: keySet) { Player pl = playersMap.get(id); System.out.print("玩家:" + pl.name + "的手牌为: " ); gm.putOut(pl.handList); } } }
--------------开始创建扑克牌----------------
--------------扑克牌创建成功!---------------
扑克牌为:
Diamonds2 Diamonds3 Diamonds4 Diamonds5 Diamonds6 Diamonds7 Diamonds8 Diamonds9
Diamonds10 DiamondsJ DiamondsQ DiamondsK DiamondsA Hearts2 Hearts3 Hearts4
Hearts5 Hearts6 Hearts7 Hearts8 Hearts9 Hearts10 HeartsJ HeartsQ
HeartsK HeartsA Clubs2 Clubs3 Clubs4 Clubs5 Clubs6 Clubs7
Clubs8 Clubs9 Clubs10 ClubsJ ClubsQ ClubsK ClubsA Spades2
Spades3 Spades4 Spades5 Spades6 Spades7 Spades8 Spades9 Spades10
SpadesJ SpadesQ SpadesK SpadesA
----------------开始洗牌-------------------
----------------洗牌完成!------------------
---------------开始创建玩家------------------
请输入玩家人数:4
第1玩家的ID为25
输入该玩家的姓名:jessy
第2玩家的ID为50
输入该玩家的姓名:luna
第3玩家的ID为91
输入该玩家的姓名:alx
第4玩家的ID为9
输入该玩家的姓名:skert
欢迎玩家:jessy进入游戏!
欢迎玩家:alx进入游戏!
欢迎玩家:skert进入游戏!
欢迎玩家:luna进入游戏!
----------------开始发牌-------------------
----------玩家jessy拿牌---------
----------玩家alx拿牌---------
----------玩家skert拿牌---------
----------玩家luna拿牌---------
----------玩家jessy拿牌---------
----------玩家alx拿牌---------
----------玩家skert拿牌---------
----------玩家luna拿牌---------
----------------发牌结束!------------------
----------------开始游戏-------------------
玩家:jessy 最大的手牌是:Spades6
玩家:alx 最大的手牌是:HeartsJ
玩家:skert 最大的手牌是:DiamondsA
玩家:luna 最大的手牌是:DiamondsQ
--------------玩家:skert获胜!-------------
玩家:jessy的手牌为: Spades5 Spades6
玩家:alx的手牌为: ClubsJ HeartsJ
玩家:skert的手牌为: ClubsK DiamondsA
玩家:luna的手牌为: Diamonds8 DiamondsQ
程序完整结果是这样。
不错。