Java第三季 简易扑克牌游戏 练习完成(已自测通过)

/**
 * 扑克牌类
 * Created by yuyong on 2017/3/8.
 */
public class Card {

    public String color;    // 花色
    public String number;    // 牌数

    public Card(String color, String number) {
        this.color = color;
        this.number = number;
    }

    @Override
    public String toString() {
        return color + number;
    }
}
import java.util.ArrayList;
import java.util.List;

/**
 * 玩家类
 * Created by yuyong on 2017/3/8.
 */
public class Player {

    public String id;    // ID
    public String name;    // 姓名
    public List<Card> playerCardList;    // 玩家拿到的手牌

    public Player(String id, String name) {
        this.id = id;
        this.name = name;
        this.playerCardList = new ArrayList<>();
    }
}
import java.util.Comparator;

/**
 * 自定义比较规则类
 * Created by yuyong on 2017/3/8.
 */
// 继承Comparator<T>接口
public class CompareRule implements Comparator<Card> {

    @Override
    public int compare(Card o1, Card o2) {
        String colors = "方块 梅花 红桃 黑桃";
        String numbers = "A 2 3 4 5 6 7 8 9 10 J Q K";

        // 比较数字的大小,选出较大的手牌
        if (numbers.indexOf(o1.number) < numbers.indexOf(o2.number)) {
            return -1;
        } else if (numbers.indexOf(o1.number) > numbers.indexOf(o2.number)) {
            return 1;
        } else {
            // 数字相同时,再比较花色的大小,选出较大的手牌
            if (colors.indexOf(o1.color) < colors.indexOf(o2.color)) {
                return -1;
            } else if (colors.indexOf(o1.color) > colors.indexOf(o2.color)) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}
import java.util.*;

/**
 * 主函数
 * Created by yuyong on 2017/3/8.
 */
public class Main {

    // 定义cardList和playerList两个属性
    public List<Card> cardList = new ArrayList<>();
    public List<Player> playerList = new ArrayList<>();

    /**
     * 创建扑克牌
     */
    public void createCard() {
        System.out.println("========== 开始创建一副扑克牌(不含大小王) ==========");
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        String[] colors = {"方块", "梅花", "红桃", "黑桃"};
        for (String c : colors) {
            for (int i = 0; i < numbers.length; i++) {
                Card card = new Card(c, numbers[i]);
                cardList.add(card);
            }
        }
        StringBuilder builder = new StringBuilder();
        for (Card card : cardList) {
            builder.append(card.color + card.number + ",");
        }
        System.out.println("========== 扑克牌创建成功 ==========");
        System.out.println("扑克牌为:【 " + builder.substring(0, builder.lastIndexOf(",")) + " 】");
        System.out.println();
    }

    /**
     * 洗牌
     */
    public void randomCard() {
        System.out.println("========== 开始洗牌 ==========");
        Collections.shuffle(cardList);
        System.out.println("========== 洗牌结束 ==========");
        StringBuilder builder2 = new StringBuilder();
        for (Card card : cardList) {
            builder2.append(card.color + card.number + ",");
        }
        System.out.println("洗牌后为:【 " + builder2.substring(0, builder2.lastIndexOf(",")) + " 】");
        System.out.println();
    }

    /**
     * 创建两位玩家
     */
    public void createPlayer() {
        System.out.println("========== 开始创建玩家 ==========");
        Scanner scanner = new Scanner(System.in);
        for (int i = 1; i < 3; i++) {
            System.out.println("********** 请输入第" + i + "位玩家的ID和姓名 **********");
            System.out.println("请先输入ID:");
            String id = scanner.next();
            while (true) {
                if (!id.matches("\\d*")) {
                    System.out.println("错误的ID,请重新输入正整数!");
                    id = scanner.next();
                } else {
                    break;
                }
            }
            System.out.println("请再输入玩家姓名:");
            String name = scanner.next();
            playerList.add(new Player(id, name));
        }
        for (Player p : playerList) {
            System.out.println("----欢迎玩家:" + p.id + " " + p.name);
        }
        System.out.println();
    }

    /**
     * 发牌
     * 每位玩家发两张牌
     */
    public void assignCard() {
        System.out.println("========== 开始发牌 ==========");
        for (int i = 0; i < 2; i++) {
            System.out.println("****玩家:" + playerList.get(0).name + " --拿牌");
            playerList.get(0).playerCardList.add(cardList.get(2 * i));

            System.out.println("****玩家:" + playerList.get(1).name + " --拿牌");
            playerList.get(1).playerCardList.add(cardList.get(2 * i + 1));
        }
        System.out.println("========== 发牌结束 ==========");
        System.out.println();
    }

    /**
     * 游戏开始
     */
    public void startGame() {
        Player player1 = playerList.get(0);
        Player player2 = playerList.get(1);

        System.out.println("========== 开始游戏 ==========");
        // 选出玩家1的最大的手牌
        Collections.sort(player1.playerCardList, new CompareRule());
        // 选出玩家2的最大的手牌
        Collections.sort(player2.playerCardList, new CompareRule());

        List<Card> bigCards = new ArrayList<>();
        bigCards.add(player1.playerCardList.get(1));
        bigCards.add(player2.playerCardList.get(1));
        // 用两个玩家最大的手牌,进行比较
        Collections.sort(bigCards, new CompareRule());
        System.out.println("****玩家:" + player1.name + "最大的手牌为:" + player1.playerCardList.get(1).color + player1.playerCardList.get(1).number);
System.out.println("****玩家:" + player2.name + "最大的手牌为:" + player2.playerCardList.get(1).color + player2.playerCardList.get(1).number);
        // 比较谁获胜
        if (bigCards.get(1).equals(player1.playerCardList.get(1))) {
            System.out.println("========== 玩家:" + player1.name + "获胜 ==========");
        } else {
            System.out.println("========== 玩家:" + player2.name + "获胜 ==========");
        }
        System.out.println();
        // 两位玩家分别摊牌
        System.out.println("玩家各自的手牌为:");
        System.out.println(player1.playerCardList);
        System.out.println(player2.playerCardList);
    }

    public static void main(String[] args) {
        Main m = new Main();
        m.createCard();
        m.randomCard();
        m.createPlayer();
        m.assignCard();
        m.startGame();
    }
}

========== 开始创建一副扑克牌(不含大小王) ==========
========== 扑克牌创建成功 ==========
扑克牌为:【 方块A,方块2,方块3,方块4,方块5,方块6,方块7,方块8,方块9,方块10,方块J,方块Q,方块K,梅花A,梅花2,梅花3,梅花4,梅花5,梅花6,梅花7,梅花8,梅花9,梅花10,梅花J,梅花Q,梅花K,红桃A,红桃2,红桃3,红桃4,红桃5,红桃6,红桃7,红桃8,红桃9,红桃10,红桃J,红桃Q,红桃K,黑桃A,黑桃2,黑桃3,黑桃4,黑桃5,黑桃6,黑桃7,黑桃8,黑桃9,黑桃10,黑桃J,黑桃Q,黑桃K 】

========== 开始洗牌 ==========
========== 洗牌结束 ==========
洗牌后为:【 梅花A,红桃5,红桃7,红桃A,红桃2,梅花5,方块10,梅花Q,梅花8,梅花10,方块9,梅花9,梅花3,红桃8,方块6,梅花J,梅花7,方块4,黑桃A,黑桃K,方块2,红桃Q,梅花6,方块3,红桃9,方块A,方块8,梅花2,黑桃8,方块J,黑桃Q,梅花4,红桃10,方块7,黑桃6,黑桃4,黑桃7,黑桃2,黑桃3,梅花K,红桃3,方块K,黑桃5,红桃J,红桃6,黑桃9,红桃4,红桃K,黑桃J,方块Q,方块5,黑桃10 】

========== 开始创建玩家 ==========
** 请输入第1位玩家的ID和姓名 **
请先输入ID:
A
错误的ID,请重新输入正整数!
1
请再输入玩家姓名:
Billy
** 请输入第2位玩家的ID和姓名 **
请先输入ID:
2
请再输入玩家姓名:
Amanda
—-欢迎玩家:Billy
—-欢迎玩家:Amanda

========== 开始发牌 ==========
玩家:Billy –拿牌
玩家:Amanda –拿牌
玩家:Billy –拿牌
玩家:Amanda –拿牌
========== 发牌结束 ==========

========== 开始游戏 ==========
****玩家:Billy最大的手牌为:红桃7
========== 玩家:Billy获胜 ==========

玩家各自的手牌为:
[梅花A, 红桃7]
[红桃A, 红桃5]

点赞