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

as long as to go

package com.sun;
/**
 * 扑克牌类
 * @author T430u
 *
 */

public class Card  {
    private String name;//大小
    private String color;//花色
    //无参构造器
    public Card(){
        
    }
    //含参构造器
    public Card(String name,String color){
        this.name =name;
        this.color =color;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((color == null) ? 0 : color.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Card))
            return false;
        Card other = (Card) obj;
        if (color == null) {
            if (other.color != null)
                return false;
        } else if (!color.equals(other.color))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    
    public String toString() {
        return  color + name;
    }


}
package com.sun;
import java.util.Comparator;
/**
 * 比较类
 * @author T430u
 *
 */
public class CardComparator implements Comparator<Card> {
    public int compare(Card arg0, Card arg1) {
        String n = ((Card) arg0).getName();
        String n1 = ((Card) arg1).getName();
        int t = 0;
        if((t = n.compareTo(n1)) == 0){
            String c = ((Card) arg0).getColor();
            String c1 = ((Card) arg1).getColor();
            t = c.compareTo(c1);
        }
        return t;
    }

}
package com.sun;
import java.util.*;
/**
 * 玩家类
 * @author T430u
 *
 */
public class Player {
    private int id;//id
    private String name;//姓名
    List<Card> holdCard;//手牌
    //无参构造器
    Player(){
        
    }
    //含参构造器
    Player(int id,String name){
        this.id=id;
        this.name =name;
        this.holdCard=new ArrayList<Card>();
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}
package com.sun;
import java.util.*;
/**
 * 游戏类
 * @author T430u
 *
 */
public class Game {
    
    public ArrayList<Card> l,l2;
    Scanner input;
    
    public Game(){
        this.l2=new ArrayList<Card>();
        this.l=new ArrayList<Card>();
        this.input=new Scanner(System.in);
    }
    
    /**
     * 创建一副扑克牌
     * @param args
     */
    public void careatCard(){
        System.out.println("***************创建一副扑克牌******************");
        String b="黑桃";
        String m="梅花";
        String r="红桃";
        String f="方片";
        Card[] c ={
                new Card("2",f),new Card("3",f),new Card("4",f),new Card("5",f),
                new Card("6",f),new Card("7",f),new Card("8",f),new Card("9",f),
                new Card("10",f),new Card("J",f),new Card("Q",f),new Card("K",f),
                new Card("A",f),new Card("2",r),new Card("3",r),new Card("4",r),
                new Card("5",r),new Card("6",r),new Card("7",r),new Card("8",r),
                new Card("9",r),new Card("10",r),new Card("J",r),new Card("Q",r),
                new Card("K",r),new Card("A",r),new Card("2",m),new Card("3",m),
                new Card("4",m),new Card("5",m),new Card("6",m),new Card("7",m),
                new Card("8",m),new Card("9",m),new Card("10",m),new Card("J",m),
                new Card("Q",m),new Card("K",m),new Card("A",m),new Card("2",b),
                new Card("3",b),new Card("4",b),new Card("5",b),new Card("6",b),
                new Card("7",b),new Card("8",b),new Card("9",b),new Card("10",b),
                new Card("J",b),new Card("Q",b),new Card("K",b),new Card("A",b)};
        System.out.println("*****************扑克牌创建成功********************");
        System.out.println("为:");
        System.out.println(Arrays.toString(c));
        l.addAll(Arrays.asList(c));
    }
    /**
     * 洗牌
     * @param args
     */
    public void washCard(){
        System.out.println("********************开始洗牌***********************");
             ArrayList<Card> result=new ArrayList<Card>();
             List<Integer> o=new ArrayList<Integer>();
             for(int i=0;i<52;i++){
                Integer index;
                do{
                    Random r=new Random();
                   index=r.nextInt(52);
                }while(o.contains(index));
                o.add(index);
                //index就是捡出来的牌,加到新的数组中
                Card card=l.get(index);
                result.add(card);
                }
             System.out.println("******************洗牌结束**********************");
           
}

正在回答

1 回答

package com.sun;
import java.util.*;
/**
 * 游戏类
 * @author T430u
 *
 */
public class Game {
    
    public ArrayList<Card> l,l2;
    Scanner input;
    
    public Game(){
        this.l2=new ArrayList<Card>();
        this.l=new ArrayList<Card>();
        this.input=new Scanner(System.in);
    }
    
    /**
     * 创建一副扑克牌
     * @param args
     */
    public void careatCard(){
        System.out.println("***************创建一副扑克牌******************");
        String b="黑桃";
        String m="梅花";
        String r="红桃";
        String f="方片";
        Card[] c ={
                new Card("2",f),new Card("3",f),new Card("4",f),new Card("5",f),
                new Card("6",f),new Card("7",f),new Card("8",f),new Card("9",f),
                new Card("10",f),new Card("J",f),new Card("Q",f),new Card("K",f),
                new Card("A",f),new Card("2",r),new Card("3",r),new Card("4",r),
                new Card("5",r),new Card("6",r),new Card("7",r),new Card("8",r),
                new Card("9",r),new Card("10",r),new Card("J",r),new Card("Q",r),
                new Card("K",r),new Card("A",r),new Card("2",m),new Card("3",m),
                new Card("4",m),new Card("5",m),new Card("6",m),new Card("7",m),
                new Card("8",m),new Card("9",m),new Card("10",m),new Card("J",m),
                new Card("Q",m),new Card("K",m),new Card("A",m),new Card("2",b),
                new Card("3",b),new Card("4",b),new Card("5",b),new Card("6",b),
                new Card("7",b),new Card("8",b),new Card("9",b),new Card("10",b),
                new Card("J",b),new Card("Q",b),new Card("K",b),new Card("A",b)};
        System.out.println("*****************扑克牌创建成功********************");
        System.out.println("为:");
        System.out.println(Arrays.toString(c));
        l.addAll(Arrays.asList(c));
    }
    /**
     * 洗牌
     * @param args
     */
    public void washCard(){
        System.out.println("********************开始洗牌***********************");
             ArrayList<Card> result=new ArrayList<Card>();
             List<Integer> o=new ArrayList<Integer>();
             for(int i=0;i<52;i++){
                Integer index;
                do{
                    Random r=new Random();
                   index=r.nextInt(52);
                }while(o.contains(index));
                o.add(index);
                //index就是捡出来的牌,加到新的数组中
                Card card=l.get(index);
                result.add(card);
                }
             System.out.println("******************洗牌结束**********************");
           
}
    /**
     * 创建玩家和发牌并游戏
     * @param args
     */
    public void careatPlayerAndGetCardAndPlay(){
        System.out.println("********************创建玩家****************************");
        System.out.println("请输入第一位玩家的id和姓名");
        int id=0;
        try{
            System.out.println("输入id:");
            id=input.nextInt();
        }catch(Exception e){
            System.out.println("请输入整数类型的id!");
            input.nextLine();
            careatPlayerAndGetCardAndPlay();
        }
        System.out.println("输入姓名:");
        String name=input.next();
        Player p=new Player(id,name);
        System.out.println("请输入第二位玩家的id和姓名:");
        int id2=0;
        try{
            System.out.println("输入id:");
            id2=input.nextInt();
        }catch(InputMismatchException e){
            System.out.println("请输入整数类型的id!");
            input.nextLine();
            careatPlayerAndGetCardAndPlay();
        }

        System.out.println("输入姓名:");
        String name2=input.next();
        Player p2=new Player(id2,name2);
        System.out.println("******欢迎玩家:"+name+"\n******欢迎玩家:"+name2);

        System.out.println("*****************开始发牌********************");
         List<Integer> o=new ArrayList<Integer>();
        for(int i=0;i<4;i++){
            Integer index;
            do{
                Random r=new Random();
               index=r.nextInt(52);
            }while(o.contains(index));
            o.add(index);
            //index就是捡出来的牌,加到新的数组中
            Card card=l.get(index);
            if(i%2==0){
                p.holdCard.add(card);
                System.out.println("******玩家:"+name+"拿牌");
            
            }else{
                p2.holdCard.add(card);
                System.out.println("******玩家:"+name2+"拿牌");
            }
        }
            System.out.println("*******************发牌结束!*******************");
        
            System.out.println("*******************开始游戏....*******************");
           
            System.out.println("玩家:"+p.getName()+"最大的手牌为:"+ Collections.max(p.holdCard,new CardComparator()));
            l2.add(Collections.max(p.holdCard,new CardComparator())); //添加各自的大牌到一个List
            System.out.println("玩家:"+p2.getName()+"最大的手牌为:"+ Collections.max(p2.holdCard,new CardComparator()));
            l2.add(Collections.max(p2.holdCard,new CardComparator())); //添加各自的大牌到一个List
            
            Card win = Collections.max(l2, new CardComparator());  //比较大牌的List集合 决出 最大的一张扑克
            //判断最大那张扑克属于谁的 谁就是胜利者
            if (p.holdCard.get(0).equals(win)||p.holdCard.get(1).equals(win)) {
                System.out.println("---------玩家:"+p.getName()+"获胜 !---------");
            }else{
                System.out.println("---------玩家:"+p2.getName()+"获胜 !---------");
            }
            
            
            System.out.println("各自玩家的手牌为:");
            System.out.println(p.getName()+":"+p.holdCard.toString());
            System.out.println(p2.getName()+":"+p2.holdCard.toString());
            
           
        }
    
    
    
    

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Game g=new Game();
        g.careatCard();
        g.washCard();
        g.careatPlayerAndGetCardAndPlay();
        
    }

}


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

举报

0/150
提交
取消

as long as to go

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