完成了大体,如下
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; import java.util.Set; public class Test { List<Player> playerList; List<Poker> tempPoker;//只为在洗牌前顺序显示一次,看起来多余 Set<Poker> pokerToDeal; Scanner input; public Test(){ this.playerList=new ArrayList<Player>(); this.tempPoker=new ArrayList<Poker>(); this.pokerToDeal=new HashSet<Poker>(); input=new Scanner(System.in); } //创建一副扑克牌(不含大小王),洗牌 public void addPokerToDeal(){ System.out.println("---------创建扑克牌--------"); Poker[] poker={ new Poker("黑桃","2"),new Poker("黑桃","3"),new Poker("黑桃","4"),new Poker("黑桃","5"), new Poker("黑桃","6"),new Poker("黑桃","7"),new Poker("黑桃","8"),new Poker("黑桃","9"), new Poker("黑桃","10"),new Poker("黑桃","J"),new Poker("黑桃","Q"),new Poker("黑桃","K"),new Poker("黑桃","A"), new Poker("红桃","2"),new Poker("红桃","3"),new Poker("红桃","4"),new Poker("红桃","5"), new Poker("红桃","6"),new Poker("红桃","7"),new Poker("红桃","8"),new Poker("红桃","9"), new Poker("红桃","10"),new Poker("红桃","J"),new Poker("红桃","Q"),new Poker("红桃","K"),new Poker("红桃","A"), new Poker("梅花","2"),new Poker("梅花","3"),new Poker("梅花","4"),new Poker("梅花","5"), new Poker("梅花","6"),new Poker("梅花","7"),new Poker("梅花","8"),new Poker("梅花","9"), new Poker("梅花","10"),new Poker("梅花","J"),new Poker("梅花","Q"),new Poker("梅花","K"),new Poker("梅花","A"), new Poker("方块","2"),new Poker("方块","3"),new Poker("方块","4"),new Poker("方块","5"), new Poker("方块","6"),new Poker("方块","7"),new Poker("方块","8"),new Poker("方块","9"), new Poker("方块","10"),new Poker("方块","J"),new Poker("方块","Q"),new Poker("方块","K"),new Poker("方块","A"),}; tempPoker.addAll(Arrays.asList(poker)); System.out.println("---------扑克牌创建成功!----------\n"+"分别为:"); for (Poker pk : tempPoker) { System.out.print(pk.prefix+pk.ch+","); } System.out.println("--------开始洗牌。。。----------"); pokerToDeal.addAll(Arrays.asList(poker)); System.out.println("--------洗牌结束 !-----------"); /*for (Poker pk : pokerToDeal) { System.out.println(pk.prefix+pk.ch); }*/ } //创建两名玩家 public void playerAndDeal(){ try { for(int i=1;i<3;i++){ System.out.println("请输入第"+i+"位玩家的ID和姓名:"); System.out.println("输入ID:"); int id=input.nextInt(); System.out.println("输入姓名:"); String name=input.next(); Player player=new Player(id,name); playerList.add(player); } } catch (InputMismatchException e) { // TODO Auto-generated catch block System.out.println("ID必须为数字!"); input.next(); playerAndDeal(); } catch (Exception e) { e.printStackTrace(); } Player p1=playerList.get(0); Player p2=playerList.get(1); System.out.println("欢迎玩家:"+p1.name); System.out.println("欢迎玩家:"+p2.name); System.out.println("---------开始发牌...---------------"); List<Poker> pokerList=new ArrayList<Poker>(pokerToDeal); System.out.println("玩家:"+p1.name+"-拿牌"); p1.ownPoker.add((Poker) pokerList.get(0)); System.out.println("玩家:"+p2.name+"-拿牌"); p2.ownPoker.add((Poker) pokerList.get(1)); System.out.println("玩家:"+p1.name+"-拿牌"); p1.ownPoker.add((Poker) pokerList.get(2)); System.out.println("玩家:"+p2.name+"-拿牌"); p2.ownPoker.add((Poker) pokerList.get(3)); System.out.println("---------发牌结束!--------"); System.out.println("-----------开始游戏。。。--------------"); List<Poker> ownPokerList1=new ArrayList<Poker>(p1.ownPoker); Poker p1max; if(ownPokerList1.get(0).ch.equals(ownPokerList1.get(1).ch)){ Collections.sort(ownPokerList1, new PokerComparator()); p1max=ownPokerList1.get(1); }else{ Collections.sort(ownPokerList1); p1max=ownPokerList1.get(1); } System.out.println("玩家:"+p1.name+"最大的手牌为:"+p1max.prefix+p1max.ch); List<Poker> ownPokerList2=new ArrayList<Poker>(p2.ownPoker); Poker p2max; if(ownPokerList2.get(0).ch.equals(ownPokerList2.get(1).ch)){ Collections.sort(ownPokerList2, new PokerComparator()); p2max=ownPokerList2.get(1); }else{ Collections.sort(ownPokerList2); p2max=ownPokerList2.get(1); } System.out.println("玩家:"+p2.name+"最大的手牌为:"+p2max.prefix+p2max.ch); List<Poker> newList=new ArrayList<Poker>(); newList.add(p1max); newList.add(p2max); Poker maxPoker; if(newList.get(0).ch.equals(newList.get(1).ch)){ Collections.sort(newList, new PokerComparator()); maxPoker=newList.get(1); }else{ Collections.sort(newList); maxPoker=newList.get(1); } if(maxPoker.equals(p1max)) System.out.println("获胜玩家:"+p1.name); if(maxPoker.equals(p2max)) System.out.println("获胜玩家:"+p2.name); System.out.println("玩家各自的手牌为:"); System.out.println(p1.name+":"+ownPokerList1.get(0).prefix+ownPokerList1.get(0).ch +","+ownPokerList1.get(1).prefix+ownPokerList1.get(1).ch); System.out.println(p2.name+":"+ownPokerList2.get(0).prefix+ownPokerList2.get(0).ch +","+ownPokerList2.get(1).prefix+ownPokerList2.get(1).ch); } /* public void go(Player p1,Player p2){ System.out.println("-----------开始游戏。。。--------------"); List<Poker> ownPokerList1=new ArrayList<Poker>(p1.ownPoker); Poker p1max; if(ownPokerList1.get(0).ch.equals(ownPokerList1.get(1).ch)){ Collections.sort(ownPokerList1, new PokerComparator()); p1max=ownPokerList1.get(1); }else{ Collections.sort(ownPokerList1); p1max=ownPokerList1.get(1); } System.out.println("玩家:"+p1.name+"最大的手牌为:"+p1max.prefix+p1max.ch); List<Poker> ownPokerList2=new ArrayList<Poker>(p2.ownPoker); Poker p2max; if(ownPokerList2.get(0).ch.equals(ownPokerList2.get(1).ch)){ Collections.sort(ownPokerList2, new PokerComparator()); p2max=ownPokerList2.get(1); }else{ Collections.sort(ownPokerList2); p2max=ownPokerList2.get(1); } System.out.println("玩家:"+p2.name+"最大的手牌为:"+p2max.prefix+p2max.ch); List<Poker> newList=new ArrayList<Poker>(); newList.add(p1max); newList.add(p2max); Poker maxPoker; if(newList.get(0).ch.equals(newList.get(1).ch)){ Collections.sort(newList, new PokerComparator()); maxPoker=newList.get(1); }else{ Collections.sort(newList); maxPoker=newList.get(1); } if(maxPoker.equals(p1max)) System.out.println("获胜玩家:"+p1.name); if(maxPoker.equals(p2max)) System.out.println("获胜玩家:"+p2.name); System.out.println("玩家各自的手牌为:"); System.out.println(p1.name+":"+ownPokerList1.get(0).prefix+ownPokerList1.get(0).ch +","+ownPokerList1.get(1).prefix+ownPokerList1.get(1).ch); System.out.println(p2.name+":"+ownPokerList2.get(0).prefix+ownPokerList2.get(0).ch +","+ownPokerList2.get(1).prefix+ownPokerList2.get(1).ch); }*/ public static void main(String[] args) { // TODO Auto-generated method stub Test t=new Test(); t.addPokerToDeal(); t.playerAndDeal(); } } ======================================================================================================= import java.util.HashSet; import java.util.Set; public class Player { public int id; public String name; public Set<Poker> ownPoker; public Player(int id,String name){ this.id=id; this.name=name; this.ownPoker=new HashSet<Poker>(); } } ======================================================================================================== public class Poker implements Comparable<Poker>{ public String prefix; public String ch; public Poker(String prefix, String ch){ this.prefix=prefix; this.ch=ch; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((ch == null) ? 0 : ch.hashCode()); result = prime * result + ((prefix == null) ? 0 : prefix.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Poker)) return false; Poker other = (Poker) obj; if (ch == null) { if (other.ch != null) return false; } else if (!ch.equals(other.ch)) return false; if (prefix == null) { if (other.prefix != null) return false; } else if (!prefix.equals(other.prefix)) return false; return true; } @Override public int compareTo(Poker o) { // TODO Auto-generated method stub return this.ch.compareTo(o.ch); } }
按照我的思路,需要创建一个类PokerComparator用以比较黑、红、梅、方
我暂时如上写免于报错
如何更改或者补全呢?
另外,addPokerToDeal()中的一部分内容,我曾试图移到go()中去,然后调用
结果失败了,只好注释掉,见注释部分
public static void main(String[] args) { // TODO Auto-generated method stub Test t=new Test(); t.addPokerToDeal(); t.playerAndDeal(); t.go(p1, p2); 这句不行 }
因为调用时参数有问题,t.go()怎么写都不对,怎样才能完成调用?
你最好按照代码语言(选择JAVA),分开来发上来,这样看的太乱了,你也没说你有什么错误啊,或者运行报了什么错?如果不能运行的话,那就是代码还没完成,继续修改,可以参照下评论里的,我就是参照了下评论的,然后代码优化了很多