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

java洗牌发牌第二次改写,参考了几位大神写的,修改了好久才运行成功

标签:
Java

参考了不少人写的,学java也有一段时间了,但是自己写东西还是很困难,在这个小程序也是,写写改改写了两天才搞好,虽然大多数是参考大神们的,但是运行出来那一刻还是挺开心的。
---------以此记录自己的学习


输入代码
package play.card;

public class Card implements Comparable<Card>{
    public String num;
    public String suit;
    public Card(String num,String suit) {
        this.num = num;
        this.suit = suit;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public Card() {
        super();
    }

    @Override
    public String toString() {
        String str = suit+" "+ num;
        return str;
    }

    @Override
    public int compareTo(Card o) {
        if(DeskOfCard.numList.indexOf(this.getNum())==DeskOfCard.numList.indexOf(this.getNum())){
            return Integer.valueOf(DeskOfCard.suitList.indexOf(this.getSuit())).
            compareTo(Integer.valueOf(DeskOfCard.suitList.indexOf(this.getSuit())));
        }
        else
            return Integer.valueOf(DeskOfCard.numList.indexOf(this.getNum())).
            compareTo(Integer.valueOf(DeskOfCard.numList.indexOf(this.getNum())));
    }
}

package play.card;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class DeskOfCard {
    static List<Card> cards;
    static String [] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    static String [] suits = {"方块","梅花","红桃","黑桃"};
    public static  final List<String> suitList = new ArrayList<String>(Arrays.asList(suits));
    public static final List<String> numList = new ArrayList<String>(Arrays.asList(nums));
    //创建一副手牌
    public void initCard(){
        cards = new ArrayList<Card>();
        //先创建四种花色的牌 
        for (int i = 0; i < 52; i++) {
            cards.add(new Card(nums[i%13],suits[i%3]));
        }
        System.out.println("全部扑克牌:");
        System.out.println(cards);
    }
    //发牌
    public void deal(List<Player> players,List<Card> cards){
        int count = 0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < players.size(); j++) {
                Player player = players.get(j);
                System.out.println("-------玩家:"+player.getName()+"拿牌");
                player.setCards(cards.get(count));
                count++;
            }
        }
    }
    //创建玩家
    public List<Player> createPlayers(int playersNum){
        List<Player> players = new ArrayList<Player>();
        Scanner scan = new Scanner(System.in);
        for (int i = 0; i < playersNum; i++) {
            System.out.println("请输入第"+(i+1)+"位玩家的姓名:");
            String name = scan.next();
            Player p = new Player(name);
            players.add(p);
        }
        if (scan!=null) {
            scan.close();
        }
        return players;
    }

    //比较大小
    public Card compare(Card c1,Card c2){
        if(numList.indexOf(c1.num)==numList.indexOf(c2.num)){
            return suitList.indexOf(c1.getSuit())>suitList.indexOf(c2.getSuit())?c1:c2;
        }
        else
            return numList.indexOf(c1.getNum())>numList.indexOf(c2.getNum())?c1:c2;
    }
    //测试程序
    public static void main(String[] args) {
        DeskOfCard test = new DeskOfCard();
        System.out.println("------------创建扑克牌-----------------");
        test.initCard();//初始化       
        System.out.println("-------------扑克牌创建成功-------------");

        System.out.println("-------------开始洗牌-------------");
        //洗牌
        Collections.shuffle(DeskOfCard.cards);
        System.out.println("-------------洗牌成功-------------");
        //创建玩家
        System.out.println("-------------创建玩家-------------");
        List<Player> players = test.createPlayers(2);//创建两名玩家
        for(Player player :players){
        System.out.println("------------欢迎玩家"+player.getName());    
        }

        //发牌
        System.out.println("-------------开始发牌-------------");
        test.deal(players, cards);
        //比牌
        List<Card> maxCard = new ArrayList<Card>();
        for (Player player :players) {
            Card tempCard = new Card("方片","2");
            for(Card card : player.getCards()){
                tempCard = test.compare(tempCard, card);
            }
            System.out.println("玩家:"+player.getName()+
                    "最大的牌为:"+player.getCards().get(0));
            maxCard.add(tempCard);
        }
        Card tempCard1 = new Card("方片", "2");

        for(Card card : maxCard){

                tempCard1 = test.compare(tempCard1, card) ;

        }
        String winner = players.get(maxCard.indexOf(tempCard1)).getName();
        System.out.println("--------玩家:"+winner +"获胜!");
        System.out.println("玩家各自手中的牌为:");
        for(Player player :players){
            System.out.println(player.toString());
        }
    }
}
package play.card;

import java.util.ArrayList;
import java.util.List;

public class Player {
    public String name;
    public List<Card> handCard;

    public Player(String name) {
        super();
        this.name = name;
        handCard = new ArrayList<Card>();
    }

    public List<Card> getCards() {
        return handCard;
    }

    public void setCards(Card cards) {
        this.handCard.add(cards);
    }

    public String getName() {
        return name;
    }

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

    public Player() {
        super();
    }

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

}

图片描述

点击查看更多内容
1人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消