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

自己参考网上做的扑克牌联系,欢迎大神指导!

public class Poke {
    public String num;
    public String kind;
    public Poke(String num, String kind) {
        this.num = num;
        this.kind = kind;
    }
    public Poke(){
    }
}
public class Player {
    public Integer id;
    public String name;
    public List<Poke> hold;
    public Player(Integer id, String name) {
        this.id = id;
        this.name = name;
        this.hold =new ArrayList<Poke>();
    }
}
public class MainTest {
    //1.创建衣服扑克牌,包括黑红梅方,2-10,jqka
    //2.创建两个玩家,id,姓名,手牌(扑克牌的集合)
    //3.洗牌,打乱创建好的扑克牌的顺序(所以要用list集合)
    //4.发牌,江西好的扑克牌集合,从第一个开始,发给两个人各一张,每人一共两张
    //5.比大小.两个人取手中最大的牌进行比较,若大小一样,则比对花色,黑红梅方
    //异常处理:创建用户id输入不为整书时,提示重新输入整数类型

    private List<Poke> pokeList=new ArrayList<Poke>();
    private List<Player> playersList=new ArrayList<Player>();

    //创建扑克牌
    public void PokeBuild(){
        System.out.println("————创建扑克牌————");
        String[] kind={"方块","梅花","红桃","黑桃"};
        for (int i=2;i<11;i++){


            for (int j=0;j<4;j++){
                String k=String.valueOf(i);
                Poke poke=new Poke(k,kind[j]);
                pokeList.add(poke);
            }}

        //创建jqka

            List<Poke> pokelistjqka=new ArrayList<Poke>();

            for (int j=0;j<4;j++){
                Poke poke1=new Poke("J",kind[j]);
                pokelistjqka.add(poke1);
            }
        for (int j=0;j<4;j++){
            Poke poke1=new Poke("Q",kind[j]);
            pokelistjqka.add(poke1);
        }
        for (int j=0;j<4;j++){
            Poke poke1=new Poke("K",kind[j]);
            pokelistjqka.add(poke1);
        }
        for (int j=0;j<4;j++){
            Poke poke1=new Poke("A",kind[j]);
            pokelistjqka.add(poke1);
        }
            pokeList.addAll(pokelistjqka);

        System.out.println("————扑克牌创建完毕————");
        System.out.println("全部扑克牌如下");
        for (Poke poke:pokeList.subList(0,13) ) {
            System.out.print(poke.kind+poke.num+" ");
        }
        System.out.println();
        for (Poke poke:pokeList.subList(13,26) ) {
            System.out.print(poke.kind+poke.num+" ");
        }
        System.out.println();
        for (Poke poke:pokeList.subList(26,39) ) {
            System.out.print(poke.kind+poke.num+" ");
        }
        System.out.println();
        for (Poke poke:pokeList.subList(39,52) ) {
            System.out.print(poke.kind+poke.num+" ");
        }
        System.out.println(
                       );

    }

    //洗牌
    public void PokeRandom(){
        System.out.println("————开始洗牌————");
        Collections.shuffle(pokeList);
        System.out.println("————洗牌完成————");
    }

    //创建玩家
    public void ManBuilde(){
        System.out.println("开始创建玩家");
        int i=1;
        while(true){
            while (i<3){
            try{
                    Player player;
                    System.out.println("请输入第" + i + "个玩家id");
                    Scanner console = new Scanner(System.in);
                    Integer id = console.nextInt();
                    System.out.println("请输入第" + i + "个玩家名字");
                    String name=console.next();
                    player=new Player(id,name);
                    playersList.add(player);
                    System.out.println("成功创建玩家"+playersList.get(i-1).name);
                   i++;
            }catch (Exception e){
                System.out.println("请输入整数");
                continue;
            }
        }
            System.out.println("——创建玩家成功——");
            break;
    }}

    //发牌给玩家
    public void GivePoke(){
        System.out.println("———开始发牌———");
        Poke poke1=new Poke(pokeList.get(0).num,pokeList.get(0).kind);
        Poke poke2=new Poke(pokeList.get(1).num,pokeList.get(1).kind);
        Poke poke3=new Poke(pokeList.get(2).num,pokeList.get(2).kind);
        Poke poke4=new Poke(pokeList.get(3).num,pokeList.get(3).kind);
            playersList.get(0).hold.add(poke1);
            playersList.get(1).hold.add(poke2);
            playersList.get(0).hold.add(poke3);
            playersList.get(1).hold.add(poke4);
        System.out.println("——两个玩家的手牌——");
        for (int i=0;i<2;i++){
            //妈的,hold也是一个list,也要选择读其中的第几个!!!!操
            System.out.println(playersList.get(i).name+"   "+playersList.get(i).hold.get(0).kind+playersList.get(i).hold.get(0).num
            +playersList.get(i).hold.get(1).kind+playersList.get(i).hold.get(1).num);
        }
    }

    //比大小,先玩家自己对比手牌中的大小,选大的和别人比
    public void PokeCompare(){
        System.out.println("————接下来比大小————");
        //通过手中牌的索引位置比较大小
        int oneMax,twoMax;
        Poke poke1=playersList.get(0).hold.get(0);
        Poke poke2=playersList.get(0).hold.get(1);
        Poke poke3=playersList.get(1).hold.get(0);
        Poke poke4=playersList.get(1).hold.get(1);
        int poke1Index=playersList.indexOf(poke1);
        int poke2Index=playersList.indexOf(poke2);
        int poke3Index=playersList.indexOf(poke3);
        int poke4Index=playersList.indexOf(poke4);
        if (poke1Index>poke2Index){
            oneMax=poke1Index;
            System.out.println("玩家"+playersList.get(0).name+"  最大的手牌为"+poke1.kind+poke1.num);
        }else {
            oneMax=poke2Index;
            System.out.println("玩家"+playersList.get(0).name+"  最大的手牌为"+poke2.kind+poke2.num);
        }
        if (poke3Index>poke4Index){
            twoMax=poke3Index;
            System.out.println("玩家"+playersList.get(0).name+"  最大的手牌为"+poke3.kind+poke3.num);
        }else {
            twoMax=poke4Index;
            System.out.println("玩家"+playersList.get(1).name+"  最大的手牌为"+poke4.kind+poke4.num);
        }
        System.out.println("——比赛结果为——");
        if (oneMax>twoMax){
            System.out.println("玩家"+playersList.get(0).name+"胜出");
        }
        else {
            System.out.println("玩家"+playersList.get(1).name+"胜出");
        }

    }

    public static void main(String[] args) {
        MainTest mt=new MainTest();
        mt.PokeBuild();
        mt.PokeRandom();
        mt.ManBuilde();
        mt.GivePoke();
        mt.PokeCompare();
    }
}


正在回答

2 回答

https://img1.sycdn.imooc.com//5b0635520001c31c06880042.jpg为什么我把你的代码运行的时候,通过hold集合调用不到poke类型的kind和num啊?


0 回复 有任何疑惑可以回复我~

咸鱼点赞

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409792    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

自己参考网上做的扑克牌联系,欢迎大神指导!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信