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

简易扑克牌游戏原创代码

标签:
Java

主函数

package com.donnie;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        //创建牌组
        ArrayList<PokerCard> pokerCards = new ArrayList<>();
        String[] colors = new String[]{"黑桃", "红桃", "梅花", "方片"};
        for (int i = 1; i <= 14; i++) {
            for (String color : colors) {
                pokerCards.add(new PokerCard(color, i));
            }
        }
        Iterator iterator = pokerCards.iterator();
        while (iterator.hasNext()) {
            PokerCard pokerCard = (PokerCard) iterator.next();
            System.out.print(pokerCard.name() + " ");
        }
        System.out.println("------------------------------");

        //创建玩家
        Player player1 = new Player();
        player1.init();
        Player player2 = new Player();
        player2.init();

        System.out.println("------------------------------");

        //打乱牌组顺序
        Collections.shuffle(pokerCards);

        //发牌
        Map<Player, PokerCard[]> map = new HashMap<Player,PokerCard[]>();
        PokerCard[] pokerCards1 = new PokerCard[]{pokerCards.get(0), pokerCards.get(1)};
        PokerCard[] pokerCards2 = new PokerCard[]{pokerCards.get(2), pokerCards.get(3)};
        map.put(player1, pokerCards1);
        map.put(player2, pokerCards2);

        //判断输赢
        PokerCard player1Max = pokerCards1[0].compareTo(pokerCards1[1]);
        PokerCard player2Max = pokerCards2[0].compareTo(pokerCards2[1]);
        if (player1Max.compareTo(player2Max) == player1Max) {
            System.out.println(player1.name + "获胜");
        } else {
            System.out.println(player2.name + "获胜");
        }

        System.out.println("------------------------------");

        //输出各自的牌
        System.out.println("------------------------------");
        System.out.println(player1.name + "牌为");
        System.out.println(pokerCards1[0].name() + "\t" + pokerCards1[1].name());
        System.out.println(player2.name + "牌为");
        System.out.println(pokerCards2[0].name() + "\t" + pokerCards2[1].name());

    }
}

玩家类

package com.donnie;

import java.util.Scanner;

/**
 * Created by Donnie on 2017/1/16.
 */
public class Player {
    int id;
    String name;

    public Player(){}

    public Player(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public void init() {
        System.out.println("请输入玩家id(必须为整数)");
        Scanner scanner = new Scanner(System.in);
        int initID = scanner.nextInt();
        System.out.println("请输入玩家昵称");
        String initString = scanner.next();
        this.id = initID;
        this.name = initString;
    }
}

扑克牌类

package com.donnie;

/**
 * Created by Donnie on 2017/1/16.
 */
public class PokerCard {
    String color;
    int number;

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String name() {
        switch (number) {
            case (11):
                return color + "J";
            case (12):
                return color + "Q";
            case (13):
                return color + "K";
            case (14):
                return color + "A";
            default:
                return color + number;
        }
    }

    public int colorInt() {
        String[] colors = new String[]{"黑桃", "红桃", "梅花", "方片"};
        for (int i = 0; i < 4; i++) {
            if (this.color == colors[i]) {
                return i;
            }
        }
        return -1;
    }

    public PokerCard compareTo(PokerCard pokerCard) {
        if (this.number > pokerCard.number) {
            return this;
        } else if (this.number == pokerCard.number) {
            if (this.colorInt() > pokerCard.colorInt()) {
                return this;
            }
        } else {
            return pokerCard;
        }
        return this;
    }
}
点击查看更多内容
5人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消