为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Java

//建立一副扑克牌
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));
}

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

点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消