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

做了快一天了,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);

        }
    }


正在回答

1 回答

修改成功!重写了compareTo方法来比较扑克牌的大小,修改了原来的代码,仍然需要大佬给出宝贵建议,谢谢!

玩家类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 implements Comparable<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;
    }

    @Override
    public int compareTo(card o) {
        if(this.getnum().equals(o.getnum())){
            switch(this.getcolor()){
                case("黑桃"):
                    return 1;

                case("红桃"):
                    if(o.getcolor()=="黑桃"){
                        return -1;
                    }else{
                        return 1;
                    }
                case("梅花"):
                    if(o.getcolor()=="方片"){
                        return 1;
                    }else{
                        return -1;
                    }
                case("方片"):
                    return -1;
            }

        }else{
            try {
                if (Integer.parseInt(this.getnum()) > Integer.parseInt(o.getnum())) {
                    return 1;
                } else {
                    return -1;
                }
            }catch(NumberFormatException e){
                if(this.getnum().equals("A")) return 1;
                else if(o.getnum().equals("A")) return -1;
                else{
                    char c1[]=this.getnum().toCharArray();
                    char c2[]=o.getnum().toCharArray();
                    return c1[0]-c2[0];
                }
            }
        }
    return 0;
    }
}

主类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);
        if(py.cardinhand.get(0).compareTo(py.cardinhand.get(1))>0){
            cd.setcard(py.cardinhand.get(0).getcolor(),py.cardinhand.get(0).getnum());
        }else if (py.cardinhand.get(0).compareTo(py.cardinhand.get(1))<0){
            cd.setcard(py.cardinhand.get(1).getcolor(),py.cardinhand.get(1).getnum());
        }else{
            System.out.println("傻屌,你又搞错了!");
        }
        System.out.println("玩家"+py.getname()+"最大的手牌是"+cd.getcolor()+cd.getnum());
        if(cd.getnum().equals(py.cardinhand.get(1).getnum())&&cd.getcolor().equals(py.cardinhand.get(1).getcolor())){
            py.cardinhand.set(1,py.cardinhand.get(0));
            py.cardinhand.set(0,cd);

        }
    }
public void cardcompare(player py1,player py2){
    if(py1.cardinhand.get(0).compareTo(py2.cardinhand.get(0))>0){
        System.out.println("最后的赢家是"+py1.getname());
    }else{
        System.out.println("最后的赢家是"+py2.getname());
    }
}
public static void main(String[] args){
        System.out.println("----------简易扑克牌游戏----------");
        cardplay cdp=new cardplay();
        cdp.cardadd();
        System.out.println("----------扑克牌创建成功----------");
        System.out.println("----------开始洗牌----------");
        cdp.cardshuffle();
        System.out.println("----------洗牌成功----------");
        System.out.println("----------创建玩家----------");
        Scanner scanner=new Scanner(System.in);
            System.out.println("请输入玩家一的id:");
            int id1=scanner.nextInt();
            System.out.println("请输入玩家一的姓名:");
            String name1=scanner.next();
            player py1=new player(id1,name1);
            System.out.println("请输入玩家二的id:");
            int id2=scanner.nextInt();
            System.out.println("请输入玩家二的姓名:");
            String name2=scanner.next();
            player py2=new player(id2,name2);
        System.out.println("欢迎玩家:"+py1.getname());
        System.out.println("欢迎玩家:"+py2.getname());
        System.out.println("----------开始发牌----------");
        cdp.carddeal(py1,py2);
        System.out.println("----------发牌成功----------");
        cdp.cardcompare(py1);
        cdp.cardcompare(py2);//各自比较玩家中手牌的大小并打印出最大手牌
        cdp.cardcompare(py1,py2);
        System.out.print("玩家"+py1.getname()+"的手牌为:");
        for(card cd1:py1.cardinhand){
            System.out.print(cd1.getcolor()+cd1.getnum());
        }
        System.out.print("玩家"+py2.getname()+"的手牌为:");
        for(card cd2:py2.cardinhand){
            System.out.print(cd2.getcolor()+cd2.getnum());
        }
        }
}


3 回复 有任何疑惑可以回复我~
#1

从小的梦想

其实可以简化一下。。 过于机械了,比如花色大小那里,可以把大小数据化,这样机器就知道哪个大了
2019-04-15 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

做了快一天了,id输入字母抛出异常那块没有做,比较个人手牌大小和二人手牌大小不知道怎么实现不了,求大佬们帮忙看看,提提建议,在此先谢过了

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