做了快一天了,id输入字母抛出异常那块没有做,比较个人手牌大小和二人手牌大小不知道怎么实现不了,求大佬们帮忙看看,提提建议,在此先谢过了
玩家类player:
import java.util.*; public class player { private String name; private int id; public player(int id,String name){ this.id=id; this.name=name; this.cardinhand=new ArrayList<card>() ; } public int getid(){ return id; } public String getname(){ return name; } public List<card> cardinhand; }
扑克类card:
public class card { private String color;//代表牌的花色 private String num;//代表牌的大小 public String getcolor(){ return color; } public String getnum(){ return num; } public void setcard(String color,String num){ this.color=color; this.num=num; } public card(String color,String num){ this.color=color; this.num=num; } }
主类cardplay:
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class cardplay { /* 创建一个List存放牌作为牌堆 */ public List cardList; public cardplay(){ this.cardList=new ArrayList(); } /* 添加一副除了大小王的牌,并且存到List当中 */ public void cardadd(){ for(Integer i=2;i<11;i++){ card cd=new card("黑桃",i.toString()); cardList.add(cd); } card cd1=new card("黑桃","A"); cardList.add(cd1); card cd2=new card("黑桃","J"); cardList.add(cd2); card cd3=new card("黑桃","Q"); cardList.add(cd3); card cd4=new card("黑桃","K"); cardList.add(cd4); for(Integer j=2;j<11;j++){ card cd=new card("红桃",j.toString()); cardList.add(cd); } card cd8=new card("红桃","A"); cardList.add(cd8); card cd5=new card("红桃","J"); cardList.add(cd5); card cd6=new card("红桃","Q"); cardList.add(cd6); card cd7=new card("红桃","K"); cardList.add(cd7); for(Integer k=2;k<11;k++){ card cd=new card("梅花",k.toString()); cardList.add(cd); } card cd9=new card("梅花","A"); cardList.add(cd9); card cd10=new card("梅花","J"); cardList.add(cd10); card cd11=new card("梅花","Q"); cardList.add(cd11); card cd12=new card("梅花","K"); cardList.add(cd12); for(Integer m=2;m<11;m++){ card cd=new card("方片",m.toString()); cardList.add(cd); } card cd13=new card("方片","A"); cardList.add(cd13); card cd14=new card("方片","J"); cardList.add(cd14); card cd15=new card("方片","Q"); cardList.add(cd15); card cd16=new card("方片","K"); cardList.add(cd16); System.out.print("添加了:"); for (Object obj:cardList) { card cd=(card) obj; System.out.print(cd.getcolor()+cd.getnum()+" "); } } /* 洗牌功能,使用collections的shuffle方法来打乱List中元素的顺序以达到洗牌的目的 */ public void cardshuffle(){ Collections.shuffle(cardList); } /* 发牌功能 */ public void carddeal(player py1,player py2){ card card1=(card)cardList.get(0); py1.cardinhand.add(card1); System.out.println("玩家:"+py1.getname()+"拿牌"); py2.cardinhand.add((card)cardList.get(1)); System.out.println("玩家:"+py2.getname()+"拿牌"); py1.cardinhand.add((card)cardList.get(2)); System.out.println("玩家:"+py1.getname()+"拿牌"); py2.cardinhand.add((card)cardList.get(3)); System.out.println("玩家:"+py2.getname()+"拿牌"); } public void cardcompare(player py){ card cd=new card(null,null); card cd1=py.cardinhand.get(0); card cd2=py.cardinhand.get(1); if(cd1.getnum().compareTo(cd2.getnum())>0){ cd.setcard(cd1.getcolor(),cd1.getnum()); }else if(cd1.getnum().compareTo(cd2.getnum())<0){ cd.setcard(cd2.getcolor(),cd2.getnum()); }else{ switch(cd1.getcolor()){ case("黑桃"): cd.setcard(cd1.getcolor(),cd1.getnum()); break; case("红桃"): if(cd2.getcolor()=="黑桃"){ cd.setcard(cd2.getcolor(),cd2.getnum()); }else{ cd.setcard(cd1.getcolor(),cd1.getnum()); }break; case("梅花"): if(cd2.getcolor()=="方片"){ cd.setcard(cd1.getcolor(),cd1.getnum()); }else{ cd.setcard(cd2.getcolor(),cd2.getnum()); } case("方片"): cd.setcard(cd2.getcolor(),cd2.getnum()); break; } } System.out.println("玩家"+py.getname()+"最大的手牌是"+cd.getcolor()+cd.getnum()); if(cd.getnum().equals(cd2.getnum())&&cd.getcolor().equals(cd2.getcolor())){ py.cardinhand.set(1,cd1); py.cardinhand.set(0,cd); } }