继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

有种打怪升级的感觉。。。

qq_弑神S无尘
关注TA
已关注
手记 1
粉丝 1
获赞 16

//建立一副扑克牌
public class Pai {
// 有个集合,就是牌
private ArrayList<String> pai;

public Pai() {
    pai = new ArrayList<String>();
    // TODO Auto-generated constructor stub
    String[] s = {"黑桃A","红桃A","梅花A","方片A",
                "黑桃2","红桃2","梅花2","方片2",
                "黑桃3","红桃3","梅花3","方片3",
                "黑桃4","红桃4","梅花4","方片4",
                "黑桃5","红桃5","梅花5","方片5",
                "黑桃6","红桃6","梅花6","方片6",
                "黑桃7","红桃7","梅花7","方片7",
                "黑桃8","红桃8","梅花8","方片8",
                "黑桃9","红桃9","梅花9","方片9",
                "黑桃10","红桃10","梅花10","方片10",
                "黑桃J","红桃J","梅花J","方片J",
                "黑桃Q","红桃Q","梅花Q","方片Q",
                "黑桃K","红桃K","梅花K","方片K",};

    // 生成一副牌。
    for (int i = 0; i < s.length; i++) {
        pai.add(s[i]);
    }

}

public ArrayList<String> getPai() {
    return pai;
}

}
public class Player {
public ArrayList<String> shouPai = new ArrayList<String>();
private int id;
private String name;
public Player(int id,String name) {
// TODO Auto-generated constructor stub
this.id=id;
this.name=name;
}
public void setId(int id) {
this.id = id;
}public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}public String getName() {
return name;
}

//找大牌
public String getMax(){
    Pai p = new Pai();
    int a1=p.getPai().indexOf(shouPai.get(0));
    int a2=p.getPai().indexOf(shouPai.get(1));
    return a1>a2? shouPai.get(0):shouPai.get(1);

}

}
/出了点问题,在我的牌中,黑桃《红桃《梅花《方片,因为存的时候就是这样存的,我也懒得改/
public class Play {
public static void main(String[] args) {
//拿一副牌
Pai pai = new Pai();
ArrayList<String> l=pai.getPai();
//俩玩家
//键盘录入的懒得写,异常处理懒得写,凑合用吧
Player p1 = new Player(1,"a");
Player p2 = new Player(2,"b");
//洗牌
xiPai(l);
for (String string : l) {
System.out.print(string);
}

    //发牌
    faPai(p1, l);
    faPai(p2, l);
    faPai(p1, l);
    faPai(p2, l);
    System.out.println();
    System.out.println(p1.shouPai);
    System.out.println(p2.shouPai);
    //找大小
    String s1=p1.getMax();
    String s2=p2.getMax();
    //这副牌是为了取到角标,依据角标来比较大小,存的时候的顺序就是大小顺序
    Pai p = new Pai();
    int player1=p.getPai().indexOf(s1) ;
    int player2=p.getPai().indexOf(s2) ;
    System.out.println(player1>player2? s1:s2);

}
//洗牌
private static void xiPai(List<String> l){
    Collections.shuffle(l);
}
//发牌,一人两张
public static void faPai(Player p,ArrayList<String> l){
    p.shouPai.add(l.remove(0));
}

}
弄了个大概,懒得修饰,凑合看吧

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP