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

java入门第三季 7-1 简易扑克练习

慕粉1474703600
关注TA
已关注
手记 2
粉丝 1
获赞 3

比较方法

import java.util.Comparator;

public class CardComparator implements Comparator<Card>{

    @Override
    public int compare(Card o1, Card o2) {
        // TODO Auto-generated method stub
        int flag=0;
        String [] color={"黑桃","红桃","方片","梅花"};
        String [] value={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        int x1=0,x2=0;
        for(int i=0;i<value.length;i++){
            if(o1.value==value[i])
                x1=i;
            if(o2.value==value[i])
                x2=i;
        }
        if(x1>x2){
            flag=1;
        }
        else if (x1<x2) {
            flag=-1;
        }
        else if(x1==x2) {
            for(int i=0;i<color.length;i++){
                if(o1.value==color[i])
                    x1=i;
                if(o2.value==color[i])
                    x2=i;
            }
            if(x1>x2)
                flag=1;
            if(x1<x2)
                flag=-1;
            if(x1==x2)
                flag=0;
        }

        return flag;
    }

}

用户类

import java.util.ArrayList;

public class User {
    public int id;
    public String name;
    ArrayList<Card> handCard;
    public User(int id, String name) {
        super();
        this.id = id;
        this.name = name;
        handCard=new ArrayList<Card>();
    }
}

卡片类


public class Card {
    public String color;
    public String value;
    public Card(String color, String value) {
        super();
        this.color = color;
        this.value = value;
    }
    @Override
    public String toString() {
        return color+value ;
    }

}

测试类

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Test {
    public ArrayList<Card> card;
    public ArrayList<User> users;
    public ArrayList<Card> userA;
    public ArrayList<Card> userB;

    public void initiaCard() {
        //卡片初始化
        card=new ArrayList<Card>();
        String [] color={"黑桃","红桃","方片","梅花"};
        String [] value={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                card.add(new Card(color[i], value[j]));
            }
        }
        System.out.println("卡片创建成功");
        for (Card string : card) {
            System.out.print(string);
        }
        Collections.shuffle(card);
        System.out.println("洗牌完毕");

    }
    public void initiaUser() {
        //添加用户
        users=new ArrayList<User>();
        Scanner scanner=new Scanner(System.in);
        for(int i=1;i<3;i++){
            int id=0;
            System.out.println("请输入第"+i+"位玩家的ID");
            try {
                id=scanner.nextInt();
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("请输入整数类型的ID");
                String rubish=scanner.next();//防止无限循环输出第一条语句
                i--;
                continue;
            }

            System.out.println("请输入第"+i+"位玩家的姓名");
            String name=scanner.next();
            User user=new User(id, name);
            users.add(user);

        }

        System.out.println("用户创建成功");
    }
    public void faPai() {
        System.out.println("开始发牌");
        int j=0;
        for(int i=0;i<2;i++){
            System.out.println(users.get(0).name+"拿牌");
            users.get(0).handCard.add(card.get(j));
            j++;
            System.out.println(users.get(1).name+"拿牌");
            users.get(1).handCard.add(card.get(j));
            j++;
        }
        System.out.println("发牌结束");
        //遍历玩家拿到的手牌
        for (User user : users) {
            System.out.println(user.name+"手牌为---"+user.handCard);
        }
    }
    public void compare() {
        CardComparator cardComparator=new CardComparator();
         //分别用两个集合存储两个用户所得的卡牌
         userA=new ArrayList<Card>();
         userB=new ArrayList<Card>();
         userA.add(users.get(0).handCard.get(0));
         userA.add(users.get(0).handCard.get(1));
         userB.add(users.get(1).handCard.get(0));
         userB.add(users.get(1).handCard.get(1));
         //判断第一个用户最大的卡牌是哪张
         int x=cardComparator.compare(userA.get(0), userA.get(1));
         Card aCard=null;
         if(x==1){
             aCard=userA.get(0);
         }
         else {
             //包括相等的情况
             aCard=userA.get(1);
        }
         System.out.println(users.get(0).name+"的最大卡牌为"+aCard);

        //判断第二个用户最大的卡牌是哪张
         int y=cardComparator.compare(userB.get(0), userB.get(1));
         Card bCard=null;
         if(y==1){
             bCard=userB.get(0);
         }
         else {
             //包括相等的情况
             bCard=userB.get(1);
        }
         //把他们各自最大的卡牌拿出来比较
         System.out.println(users.get(1).name+"的最大卡牌为"+bCard);
         int result=cardComparator.compare(aCard, bCard);
         if(result==1){
             System.out.println(users.get(0).name+"获胜");
         }else if (result==0) {
            System.out.println(users.get(0).name+"与"+users.get(1).name+"平均");
        }else {
            System.out.println(users.get(1).name+"获胜");
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test test=new Test();
        test.initiaCard();//初始化卡牌
        test.initiaUser();//初始化用户
        test.faPai();//发牌
        test.compare();//比较大小并输出
    }

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

热门评论

代码和你一样,项目报错是怎么回事?

没空理你了可能 你梦里弄了


查看全部评论