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

【参考源码】Java入门第三季 7-1 简易扑克牌游戏

标签:
Java

先创建 PokerCard 扑克牌类,存放牌的花色、大小,还有一个关键值weight,用于定义52张牌的排位大小,这样就避免了后面比较双方牌大小的繁琐步骤

public class PokerCard implements Comparable<PokerCard> {

    String color;
    String number;
    int weight; // 存放扑克牌的比较大小

    public PokerCard(String newColor, String newNumber, int newWeight) {
        color = newColor;
        number = newNumber;
        weight = newWeight;
    }

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

    public int compareTo(PokerCard arg0) {
        // TODO Auto-generated method stub

        if (this.weight < arg0.weight) {
            return -1;
        } else {
            return 1;
        }
    }

}

再定义玩家类 PokerPlayer,存放玩家ID,姓名,所拿的牌

public class PokerPlayer {

    int id;
    String name;
    List<PokerCard> playerCardList;

    public PokerPlayer(int newId, String newName) {
        id = newId;
        name = newName;
        playerCardList = new ArrayList<PokerCard>();
    }

}

主函数

public class PokerGame {

    List<PokerCard> cardList = new ArrayList<PokerCard>();
    List<PokerPlayer> playerList = new ArrayList<PokerPlayer>();

    /*
     * 创建扑克牌
     */
    public void creatCar() {
        System.out.println("----------创建扑克牌----------");
        String[] numbers = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
                "Q", "K", "A" };
        String[] colors = { "方块", "梅花", "红桃", "黑桃" };
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                cardList.add(new PokerCard(colors[i], numbers[j], j + 1 + 13
                        * i));
            }
        }
        System.out.println("----------扑克牌创建成功!---------");
        System.out.println("为:" + cardList.toString());
    }

    /*
     * 洗牌
     */
    public void randomCard() {
        System.out.println("-------开始洗牌----------");
        List<PokerCard> cardListTemp = new ArrayList<PokerCard>();
        Random random = new Random();
        for (int i = 0; i < 52; i++) {
            int randomIndex = random.nextInt(52 - i);
            cardListTemp.add(cardList.get(randomIndex));
            cardList.remove(randomIndex);
        }
        System.out.println("-------洗牌结束----------");
        cardList = cardListTemp;
        // System.out.println("为:"+cardList.toString());

    }

    /*
     * 创建玩家
     */
    public void createPlayer() {
        System.out.println("------------创建玩家---------------");
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 2; i++) {
            System.out.println("输入第" + i + "位玩家的ID和姓名:");
            System.out.println("输入ID:");
            int id = sc.nextInt();
            System.out.println("输入姓名:");
            String name = sc.next();
            playerList.add(new PokerPlayer(id, name));
        }
        System.out.println("----欢迎玩家:" + playerList.get(0).name);
        System.out.println("----欢迎玩家:" + playerList.get(1).name);

    }

    /*
     * 开始发牌
     */
    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("--------发牌结束!-------------");
    }

    /*
     * 开始游戏
     */
    public void startGame() {
        PokerPlayer player01 = playerList.get(0);
        PokerPlayer player02 = playerList.get(1);

        Collections.sort(player01.playerCardList);
        System.out.println("玩家:" + player01.name + "的最大手牌为:"
                + player01.playerCardList.get(1).color
                + player01.playerCardList.get(1).number);
        Collections.sort(player02.playerCardList);
        System.out.println("玩家:" + player02.name + "的最大手牌为:"
                + player02.playerCardList.get(1).color
                + player02.playerCardList.get(1).number);

        if (player01.playerCardList.get(1).weight < player02.playerCardList
                .get(1).weight) {
            System.out.println("------------玩家" + player02.name
                    + "获胜!-------------");
        } else {
            System.out.println("------------玩家" + player01.name
                    + "获胜!-------------");
        }

        System.out.println("玩家各自的手牌为:");
        System.out.println(player01.name + ":" + player01.playerCardList);
        System.out.println(player02.name + ":" + player02.playerCardList);
    }

    public static void main(String[] args) {

        PokerGame pg = new PokerGame();
        pg.creatCar();
        pg.randomCard();
        pg.createPlayer();
        pg.assignCard();
        pg.startGame();

    }

}
点击查看更多内容
19人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消