扑克牌游戏-交作业
package com.imooc.pukepai; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class PuKeTest { public Set<PuKe> si; public List<PuKe> puke; public List<Player> player; /** * 创建一副扑克牌 */ public void creatPuke(){ puke=new ArrayList<PuKe>(); String[] li={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; String[] lis={"黑桃","红桃","方块","梅花"}; //创建集合中的对象 PuKe pu; for(int i=0;i<lis.length;i++){ for(int j=0;j<li.length;j++){ pu=new PuKe(); pu.colour=lis[i]; pu.num=li[j]; puke.add(pu); } } System.out.println("-------创建扑克牌成功--------"); for (PuKe p : puke) { System.out.print(p.colour+p.num+" "); } } /** * 洗牌 * @return */ public void xiPai(){ System.out.println("-----开始洗牌------"); si=new HashSet<PuKe>(); PuKe pu; for(int i=0;i<puke.size();i++){ pu=new PuKe(); pu.colour=puke.get(i).colour; pu.num=puke.get(i).num; si.add(pu); } System.out.println(); System.out.println("-------洗牌成功-------"); for (PuKe p : si) { System.out.print(p.colour+p.num+" "); } } /** * 创建两个player */ public void creatPlayer(int a){ System.out.println("--------开始创建玩家--------"); player=new ArrayList<Player>(); Scanner sc=new Scanner(System.in); Player players; for(int i=1;i<=a;i++){ try { players=new Player(); System.out.println("请输入第"+i+"个人的ID:"); players.id=sc.nextInt(); System.out.println("请输入第"+i+"个人的名字:"); players.name=sc.next(); player.add(players); }catch(InputMismatchException e){ System.out.println("您输入的ID有误,请重新输入!"); this.creatPlayer(a); }catch (NumberFormatException e) { // TODO: handle exception System.out.println("程序有误,请重新开始!"); e.printStackTrace(); } } System.out.println("这个游戏参加的玩家有"+a+"个人,他们分别是:"); for (Player p : player) { System.out.println("玩家:"+p.id+"号"+p.name); } } /** * 玩家从洗完牌的集合中获取扑克牌 */ public void getPuke(int b){ System.out.println("---------获取"+b+"张手牌--------"); List<PuKe> puk=new ArrayList<PuKe>(); puk.addAll(si); // for (PuKe puKe : puk) { // System.out.print(puKe.colour+puKe.num+" "); // } int c=player.size(); List<PuKe> ps; for(int i=0;i<c;i++){ ps=new ArrayList<PuKe>(); player.get(i).pukes=new ArrayList<PuKe>(); System.out.println("玩家"+player.get(i).name+"选的牌为:"); for(int j=0;j<b;j++){ ps.add(puk.get(i+j*c)); System.out.print(puk.get(i+j*c).colour+puk.get(i+j*c).num+""); } System.out.println(); player.get(i).pukes.addAll(ps); // for (PuKe puKe : player.get(i).pukes) { // System.out.println(puKe.colour+puKe.num+""); // } } } /** * 将玩家的扑克牌进行比较 */ public void vsMax(){ System.out.println("-------开始进行比较------"); // player=new ArrayList<Player>(); Map<String,PuKe> map=new HashMap<String,PuKe>(); List<PuKe> lis=new ArrayList<PuKe>(); for(int i=0;i<player.size();i++){ int b=player.get(i).pukes.size(); Collections.sort(player.get(i).pukes,new CardComparator()); System.out.println(player.get(i).name+"的最大牌为:"+player.get(i).pukes.get(b-1)); lis.add(player.get(i).pukes.get(b-1)); map.put(player.get(i).name, player.get(i).pukes.get(b-1)); } //对各个人的最大值进行整合排序 Collections.sort(lis,new CardComparator()); int c=lis.size(); Set<Entry<String,PuKe>> entrySet=map.entrySet(); for(Entry<String,PuKe> entry:entrySet){ if(entry.getValue().equals(lis.get(c-1))){ System.out.println(entry.getKey()+"获得了胜利!其最大值为:"+entry.getValue()); } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PuKeTest pt=new PuKeTest(); pt.creatPuke(); pt.xiPai(); pt.creatPlayer(3); pt.getPuke(4); pt.vsMax(); } } package com.imooc.pukepai; public class PuKe { public String colour; public String num; public PuKe(){ } public PuKe(String colour,String num){ this.colour=colour; this.num=num; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((colour == null) ? 0 : colour.hashCode()); result = prime * result + ((num == null) ? 0 : num.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof PuKe)) return false; PuKe other = (PuKe) obj; if (colour == null) { if (other.colour != null) return false; } else if (!colour.equals(other.colour)) return false; if (num == null) { if (other.num != null) return false; } else if (!num.equals(other.num)) return false; return true; } @Override public String toString() { return colour+num; } } package com.imooc.pukepai; import java.util.List; public class Player { public String name; public int id; public List<PuKe> pukes; } package com.imooc.pukepai; import java.util.Comparator; public class CardComparator implements Comparator<PuKe> { @Override public int compare(PuKe o1, PuKe o2) { // TODO Auto-generated method stub if(o1.num.equals(o2.num)){ return o1.colour.compareTo(o2.colour); }else{ return o1.num.compareTo(o2.num); } } }
写的头蒙