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

JAVA入门第三季-简易扑克牌程序-个人编写-源代码(含截图)

标签:
Java
图片描述
整体思路

创建Card类,定义单张扑克的属性(点数,花色),行为(比较大小),以及比较大小的规则;
创建Poker类,定义一副扑克牌,采用List集合框架;
创建Player类,定义玩家属性(id,名字,手牌);
创建Game类,程序入口,步骤如下:

  1. 创建一副牌的对象 ;
  2. 输入id,昵称 ;
  3. 抽牌(两人不会抽到同一张牌),洗牌(采用Card类中的排序方法);
  4. 比较两人的最大牌(还是采用Card类中的排序方法)

/**
 * Card类,定义扑克牌,以及扑克牌的比大小规则
 * Created by haivo on 2016/4/18.
 */
public class Card {
    public String color; //花色
    public String point; //点数
    public Card(String color,String point){
        this.color = color;
        this.point = point;
    }

    /**
     * 重写toString用于打印单张扑克的点数及花色
     */
    @Override
    public String toString() {
        return color+point;
    }

    /**
     * 扑克牌比较规则,采用Comparator实现,这个规则会在多个地方被用到
     */
    public static Comparator<Card> comparator = new Comparator<Card>() {
        @Override
        public int compare(Card o1, Card o2) {
            Poker comparePokerTemp = new Poker();
            if(o1==null  o2==null)
                return 0;
            int bigger = comparePokerTemp.point.indexOf(o1.point) - comparePokerTemp.point.indexOf(o2.point);
            if(bigger==0){
                bigger = comparePokerTemp.color.indexOf(o1.color) - comparePokerTemp.color.indexOf(o2.color);
            }
            return bigger;
        }
    };
}

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Card){
            if(color.equals(((Card) obj).color) && point.equals(((Card) obj).point)){
                return true;
            }
        }
        return false;
    }

/**
 * 扑克类:采用List泛型分别定义花色和点数
 * Created by haivo on 2016/4/18.
 */
public class Poker {
    public List<String> color = new ArrayList<>(); //花色共4种
    public List<String> point = new ArrayList<>(); //点数3.4.5····2共13种
    public List<Card> pokerList = new ArrayList<>(); //扑克中的扑克牌共52张,没有王牌

    public Poker() {
        color.add("");
        color.add("");
        color.add("");
        color.add("");
        point.add("3");
        point.add("4");
        point.add("5");
        point.add("6");
        point.add("7");
        point.add("8");
        point.add("9");
        point.add("10");
        point.add("J");
        point.add("Q");
        point.add("K");
        point.add("A");
        point.add("2");

        for (int color = 0; color < 4; color++) {
            for (int point = 0; point < 13; point++) {
                Card mCard = new Card(this.color.get(color),this.point.get(point));
                pokerList.add(mCard);
            }
        }
    }

    /**
     *打印手牌
     */
    public void printPokerCard(){
        for (int i = 0; i < this.pokerList.size(); i++) {
            System.out.print(this.pokerList.get(i).toString()+" ");
        }
        System.out.println("");
    }
}

/**
 * 玩家类
 * Created by haivo on 2016/4/18.
 */
public class Player {
    public int id;
    public String name;
    public List<Card> playerCard; //玩家手牌

    public Player(int id,String name) {
        this.id = id;
        this.name = name;
        this.playerCard = new ArrayList<>();
    }

    @Override
    public String toString() {
        return this.id + ":"+this.name;
    }
}

/**
 * Game类:两人抽牌选择手中的较大牌比大小
 * Created by haivo on 2016/4/18.
 */
public class Game {
    Poker mPoker = new Poker();
    Poker dealedPoker = new Poker();
    Scanner scanner = new Scanner(System.in);
    int surplusCards = 51;  //剩余牌数
    int dealCardCount; //将要抽取的手牌数量
    Player player1, player2;

    public static void main(String[] args) {
        Game g = new Game();
        System.out.println("创建了一副扑克牌: ");
        g.mPoker.printPokerCard();
        g.creatPlayer();
        System.out.println("请输入玩家要抽取多少张牌:(不超过25张)");
        g.dealCardCount = g.scanner.nextInt();
        g.pullAndShuffleCard(); //抽牌和洗牌
        g.compareMaxCard(); //两玩家的最大牌做比较,决出胜负
    }

    /**
     * 创建玩家
     */
    public void creatPlayer() {
        System.out.print("请输入第一名玩家ID: ");
        int inputId = scanner.nextInt();
        System.out.print("请输入第一名玩家昵称: ");
        String inputName = scanner.next();
        player1 = new Player(inputId, inputName);
        System.out.print("请输入第二名玩家ID: ");
        inputId = scanner.nextInt();
        System.out.print("请输入第二名玩家昵称: ");
        inputName = scanner.next();
        player2 = new Player(inputId, inputName);
    }

    /**
     * 抽取手牌和洗牌,采用comparator规则排序
     */
    public void pullAndShuffleCard(){
        player1.playerCard.addAll(dealCard(dealCardCount));
        System.out.println("玩家一抽取手牌是:");
        for (Card c: player1.playerCard) {
            System.out.print(c.toString()+" ");
        }
        Collections.sort(player1.playerCard,Card.comparator);
        System.out.println("\n玩家一洗牌后的手牌是:");
        for (Card c: player1.playerCard) {
            System.out.print(c.toString()+" ");
        }
        player2.playerCard.addAll(dealCard(dealCardCount));
        System.out.println("\n玩家二抽取的手牌是:");
        for (Card c: player2.playerCard) {
            System.out.print(c.toString()+" ");
        }
        Collections.sort(player2.playerCard,Card.comparator);
        System.out.println("\n玩家二洗牌后的手牌是:");
        for (Card c: player2.playerCard) {
            System.out.print(c.toString()+" ");
        }
    }

    /**
     * 玩家连续抽取dealCoun张牌,返回多张卡牌(用List盛装)
     * @param dealCount
     * @return
     */
    public List<Card> dealCard(int dealCount){
        Random random = new Random();
        List<Card> playCard = new ArrayList<>();
        do {
            Card card = dealedPoker.pokerList.get(random.nextInt(surplusCards--));
            playCard.add(card);
            dealCount--;
            dealedPoker.pokerList.remove(card);
        }while(dealCount>0);
        return playCard;
    }
    /**
     * 两玩家的最大牌做比较,决出胜负,采用comparator规则排序
     */
    public void compareMaxCard(){
        List<Card> biggerPlayerCard = new ArrayList<>(2);
        biggerPlayerCard.add(player1.playerCard.get(player1.playerCard.size()-1));
        biggerPlayerCard.add(player2.playerCard.get(player2.playerCard.size()-1));
        Collections.sort(biggerPlayerCard,Card.comparator);
        if(player1.playerCard.contains(biggerPlayerCard.get(1))){
            System.out.println("\n  所以:"+player1.toString()+" 获胜。");
        }else{
            System.out.println("\n  所以:"+player2.toString()+" 获胜。");
        }
    }
}
点击查看更多内容
165人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消