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

Java入门第三季习题,简易扑克牌游戏

标签:
Java
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class PlayCard {
    public static void main(String[] args){
        Player player1=new Player();
        Player player2=new Player();
        String[] dot={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        String[] color={"","","",""};
        List<Poker> pokers = new ArrayList<Poker>();
        for(int i=0;i<4;i++)
            for(int j=0;j<13;j++){pokers.add(new Poker(dot[j],color[i]));}
        System.out.println("创建扑克牌成功!");
        System.out.println(pokers);
        Collections.shuffle(pokers);    //用shuffle方法达成洗牌的效果
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个玩家的姓名:");
        player1.setName(sc.nextLine());
        int i=0;
        while(true) {
            System.out.println("请输入第一个玩家的ID");
            try {
                i = sc.nextInt();
            }catch(java.util.InputMismatchException e)
            {
                System.out.println("请输入整数ID!");
                continue;
            }finally{
                sc.nextLine();
            }
            break;
        }
        player1.setId(i);
        System.out.println("第一个玩家创建成功:"+player1);
        System.out.println("请输入第二个玩家的姓名:");
        player2.setName(sc.nextLine());
        while(true) {
            System.out.println("请输入第二个玩家的ID");
            try {
                i = sc.nextInt();
            }catch(Exception e)
            {
                System.out.println("请输入整数ID!");
                continue;
            }finally{
                sc.nextLine();
            }
            break;
        }
        player2.setId(i);
        System.out.println("第二个玩家创建成功:"+player2);
        System.out.println("-----开始发牌-----");
        for(int j=0;j<3;j=j+2){
            player1.setMyPokers(pokers.get(j));
            System.out.println("玩家:"+player1.getName()+"-拿牌");
            player2.setMyPokers(pokers.get(j+1));
            System.out.println("玩家:"+player2.getName()+"-拿牌");
        }
        System.out.println("------发牌结束------");
        System.out.println("------开始游戏------");
        List<Poker> pokers1 = player1.getMyPokers();
        Collections.sort(pokers1);
        List<Poker> pokers2 = player2.getMyPokers();
        Collections.sort(pokers2);
        System.out.println("玩家:"+player1.getName()+"最大的手牌为:"+pokers1.get(1));
        System.out.println("玩家:"+player2.getName()+"最大的手牌为:"+pokers2.get(1));
        if(pokers1.get(1).compareTo(pokers2.get(1))>0)
            System.out.println("-------------玩家"+player1.getName()+"获胜-------------");
        else
            System.out.println("-------------玩家"+player2.getName()+"获胜-------------");
        System.out.println("玩家各自的手牌为:");
        System.out.println(player1.getName()+":"+pokers1);
        System.out.println(player2.getName()+":"+pokers2);
        sc.close();
    }
}

public class Poker implements Comparable<Poker>{
    private String dot;
    private String color;
    public Poker(String dot,String color){
        this.dot = dot;
        this.color = color;
    }
    //将扑克牌的点数变成权值方便比较大小
    private int dotWeihgts(){
        switch(this.dot){
            case "3":return 3;
            case "4":return 4;
            case "5":return 5;
            case "6":return 6;
            case "7":return 7;
            case "8":return 8;
            case "9":return 9;
            case "10":return 10;
            case "J":return 11;
            case "Q":return 12;
            case "K":return 13;
            case "A":return 14;
            case "2":return 15;
            default: return 0;
        }
    }

    //将扑克牌的花色转换成权值方便比较大小,我们这里是黑桃最大,方片最小。。。
    private int colorWeights(){
        switch(this.color){
            case "":return 1;
            case "":return 2;
            case "":return 3;
            case "":return 4;
            default:return 0;
        }
    }

    public int compareTo(Poker poker){
        if(this.dotWeihgts()>poker.dotWeihgts())
            return 1;
        else if(this.dotWeihgts()==poker.dotWeihgts()){
            if(this.colorWeights()>poker.colorWeights())
                return 1;
            else if(this.colorWeights()==poker.colorWeights())
                return 0;
            else
                return -1;
        }
        else
            return -1;
    }

    @Override
    public String toString() {
        return color+dot;
    }
}

public class Player {
    private int id;
    private String name;
    private List<Poker> myPokers;

    public Player(){
        myPokers = new ArrayList<Poker>();
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

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

    public void setMyPokers(Poker poker){
        this.myPokers.add(poker);
    }

    public List<Poker> getMyPokers() {
        return myPokers;
    }

    @Override
    public String toString() {
        return "Player{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
点击查看更多内容
2人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消